How to extract builder setup function and make it async? #7596
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
minus the async part you basically got it right already. It's
The setup function itself just can't be async, simple as that. But as you figured out yourself, you can use tauri::async_runtime to spawn async tasks, or block on them ( Now the almost-solution you posted was almost there. The reason why your command worked is because it uses an AppHandle instead of fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
start_server()?;
// This one
let handle = app.handle();
tauri::async_runtime::spawn(async move { // also added move here
let verify_result = verify_local_server().await;
match verify_result {
Ok(_) => {
println!("Local Server is running");
}
Err(err) => {
handle.emit_all("local-server-down", ()); // changed this to handle.
println!("Local Server is not running");
println!("{}", err);
}
}
});
Ok(())
} |
Beta Was this translation helpful? Give feedback.
minus the async part you basically got it right already. It's
fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box<dyn std::error::Error>>
-> https://docs.rs/tauri/latest/tauri/struct.Builder.html#method.setupThe setup function itself just can't be async, simple as that. But as you figured out yourself, you can use tauri::async_runtime to spawn async tasks, or block on them (
block_on
to execute async functions as if they'd be sync).Now the almost-solution you posted was almost there. The reason why your command w…