1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#[cfg(any(feature = "embedded-server", feature = "no-server"))]
pub mod assets;
pub mod config;
pub mod event;
#[cfg(feature = "embedded-server")]
pub mod server;
mod app;
mod endpoints;
#[allow(dead_code)]
mod salt;
use std::process::Stdio;
pub use anyhow::Result;
use threadpool::ThreadPool;
pub use web_view::Handle;
use web_view::WebView;
pub use app::*;
pub use tauri_api as api;
thread_local!(static POOL: ThreadPool = ThreadPool::new(4));
pub fn spawn<F: FnOnce() -> () + Send + 'static>(task: F) {
POOL.with(|thread| {
thread.execute(move || {
task();
});
});
}
pub fn execute_promise_sync<T: 'static, F: FnOnce() -> crate::Result<String> + Send + 'static>(
webview: &mut WebView<'_, T>,
task: F,
callback: String,
error: String,
) {
let handle = webview.handle();
let callback_string =
api::rpc::format_callback_result(task().map_err(|err| err.to_string()), callback, error);
handle
.dispatch(move |_webview| _webview.eval(callback_string.as_str()))
.expect("Failed to dispatch promise callback");
}
pub fn execute_promise<T: 'static, F: FnOnce() -> crate::Result<String> + Send + 'static>(
webview: &mut WebView<'_, T>,
task: F,
callback: String,
error: String,
) {
let handle = webview.handle();
POOL.with(|thread| {
thread.execute(move || {
let callback_string =
api::rpc::format_callback_result(task().map_err(|err| err.to_string()), callback, error);
handle
.dispatch(move |_webview| _webview.eval(callback_string.as_str()))
.expect("Failed to dispatch promise callback")
});
});
}
pub fn call<T: 'static>(
webview: &mut WebView<'_, T>,
command: String,
args: Vec<String>,
callback: String,
error: String,
) {
execute_promise(
webview,
|| {
api::command::get_output(command, args, Stdio::piped())
.map_err(|err| err)
.map(|output| format!(r#""{}""#, output))
},
callback,
error,
);
}
pub fn close_splashscreen<T: 'static>(webview_handle: &Handle<T>) -> crate::Result<()> {
webview_handle.dispatch(|webview| {
webview.eval(r#"window.external.invoke(JSON.stringify({ cmd: "closeSplashscreen" }))"#)
})?;
Ok(())
}
#[cfg(test)]
mod test {
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig::with_cases(10000))]
#[test]
fn check_spawn_task(task in "[a-z]+") {
let dummy_task = move || {
format!("{}-run-dummy-task", task);
assert!(true);
};
crate::spawn(dummy_task);
}
}
}