tauri_plugin_dev_invoke/
lib.rs

1mod server;
2
3use std::collections::HashMap;
4use std::sync::Arc;
5use tauri::{plugin::Builder, Runtime};
6
7// Re-export the macro crate and paste
8pub use paste;
9pub use tauri_plugin_dev_invoke_macros::command;
10
11pub type BoxHandler =
12    Arc<dyn Fn(serde_json::Value) -> Result<serde_json::Value, String> + Send + Sync>;
13
14pub struct DevInvokeState {
15    pub handlers: HashMap<String, BoxHandler>,
16}
17
18pub fn init<R: Runtime>(state: DevInvokeState) -> tauri::plugin::TauriPlugin<R> {
19    let state = Arc::new(state);
20
21    Builder::new("dev-invoke")
22        .setup(move |app, _| {
23            #[cfg(debug_assertions)]
24            {
25                let app_handle = app.clone();
26                let state_clone = state.clone();
27                std::thread::spawn(move || {
28                    server::start(app_handle, state_clone, 3030);
29                });
30            }
31            let _ = app;
32            let _ = state;
33            Ok(())
34        })
35        .build()
36}
37
38#[macro_export]
39macro_rules! dev_invoke_handler {
40    ($($cmd:ident),* $(,)?) => {{
41        let mut handlers: std::collections::HashMap<String, $crate::BoxHandler> =
42            std::collections::HashMap::new();
43        $(
44            handlers.insert(
45                stringify!($cmd).to_string(),
46                std::sync::Arc::new(|args| {
47                    $crate::paste::paste! { [<__dev_invoke_wrapper_ $cmd>](args) }
48                })
49            );
50        )*
51        $crate::DevInvokeState { handlers }
52    }};
53}