-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change app handle setup to use a condition variable
- Loading branch information
Showing
5 changed files
with
34 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use std::sync::{Condvar, Mutex}; | ||
|
||
use tauri::{App, AppHandle}; | ||
|
||
static APP_HANDLE_MUTEX: Mutex<Option<AppHandle>> = Mutex::new(None); | ||
|
||
static APP_HANDLE_CONDVAR: Condvar = Condvar::new(); | ||
|
||
/// Setup the static app handle and notify waiting threads. | ||
pub fn setup(app: &mut App) { | ||
let mut app_handle = APP_HANDLE_MUTEX.lock().unwrap(); | ||
*app_handle = Some(app.handle()); | ||
APP_HANDLE_CONDVAR.notify_all(); | ||
} | ||
|
||
/// Get the app handle but block the current thread until it is available. | ||
/// | ||
/// The app handle is made available via [`setup`]. | ||
pub fn get_app_handle() -> AppHandle { | ||
let mut app_handle = APP_HANDLE_MUTEX.lock().unwrap(); | ||
while app_handle.is_none() { | ||
app_handle = APP_HANDLE_CONDVAR.wait(app_handle).unwrap(); | ||
} | ||
app_handle.as_ref().unwrap().clone() | ||
} |
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
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