Emitting Event During Loop #3971
-
Is it possible to emit a message to the frontend (JS) during a long loop running in the backend (Rust)? If so, may I see an example? Essentially I would like to do some long running processes on the backend thread with Rust and I would like to send progress messages back to the frontend during them. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
Never mind, I was able to piece it together using std::thread. |
Beta Was this translation helpful? Give feedback.
-
@FabianLars how about if we wanted an infinite loop that does async tasks? How would we go about using app_handle then that doesn't surmount to https://github.com/rfdonnelly/tauri-async-example/blob/main/src-tauri/src/main.rs This is what I got so far. But the emit all in the function is not seen by the frontend. I can verify the listen is set up correctly, since I did another test, but in that test of the infinite loop, it doesn't work past the first time. What I want .setup(|app| {
let app_handle = app.handle();
tauri::async_runtime::spawn(async move {
status_stream_process(&app_handle).await;
});
}
pub async fn status_stream_process<R: tauri::Runtime>(manager: impl Manager<R>) { ... } The test that doesn't work .setup(|app| {
let app_handle = app.handle();
tauri::async_runtime::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
app_handle.emit_all("daemonStatusUpdate", "testUpdate").unwrap();
}
});
} |
Beta Was this translation helpful? Give feedback.
Never mind, I was able to piece it together using std::thread.