Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust から JS へのイベント通知を実装 in
tauri/tauri-app-event
.
- Loading branch information
1 parent
1030a77
commit 763da70
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::sync::mpsc::{self, TryRecvError}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use tauri; | ||
use tauri::Manager; | ||
|
||
#[derive(Clone, serde::Serialize, serde::Deserialize)] | ||
pub struct BackendHelloEventPayload { | ||
pub message: String, | ||
} | ||
|
||
#[tauri::command] | ||
pub fn post_setup_process_for_backend(app: tauri::AppHandle) { | ||
|
||
let (tx, rx) = mpsc::channel(); | ||
|
||
// 新しいスレッドを作成して、 1 秒ごとにイベントを emit する処理を実装 | ||
tauri::async_runtime::spawn( async move { | ||
loop { | ||
println!("send backendHello event."); | ||
thread::sleep(Duration::from_millis(1000)); | ||
app.emit_all("backendHello", BackendHelloEventPayload { message: "Hello, World from backend!!!".into() }).unwrap(); | ||
|
||
// emit 終了通知用チャンネルに通知が来たらループをストップ | ||
match rx.try_recv() { | ||
Ok(_) | Err(TryRecvError::Disconnected) => { | ||
println!("stop backendHello event."); | ||
break; | ||
} | ||
Err(TryRecvError::Empty) => {} | ||
} | ||
} | ||
}); | ||
|
||
// 10 秒後に emit を終了(emit 終了通知用チャンネル経由でストップを通知) | ||
tauri::async_runtime::spawn( async move { | ||
thread::sleep(Duration::from_millis(10000)); | ||
let _ = tx.send(()); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters