Skip to content

Commit

Permalink
refactor: change app handle setup to use a condition variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Toromyx committed Sep 27, 2023
1 parent 1fdc62a commit 7655d4a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
25 changes: 25 additions & 0 deletions src-tauri/src/app_handle.rs
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()
}
3 changes: 2 additions & 1 deletion src-tauri/src/command/ocr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::path::PathBuf;
use tesseract::Tesseract;

use crate::{
command::error::CommandError, entity_crud, entity_crud::EntityCrudTrait, get_app_handle,
app_handle::get_app_handle, command::error::CommandError, entity_crud,
entity_crud::EntityCrudTrait,
};

/// Get the optically recognized characters from the specified recipe file.
Expand Down
26 changes: 3 additions & 23 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
windows_subsystem = "windows"
)]

use tauri::{AppHandle, Wry};
use tauri::Wry;

use crate::command::{
entity::{
Expand Down Expand Up @@ -44,6 +44,7 @@ use crate::command::{
unit_list::unit_list_get,
};

mod app_handle;
mod command;
mod database;
mod dom_content_loaded;
Expand All @@ -60,25 +61,6 @@ mod scraper;
mod unit_conversion;
mod window;

/// This static variable holds the app handle once the tauri app has started.
static mut APP_HANDLE: Option<AppHandle> = None;

/// Get and unwrap the static app handle [`APP_HANDLE`].
///
/// See [`try_get_app_handle`] for a non-panicing version.
///
/// # Panics
///
/// This function panics when [`APP_HANDLE`] is [`None`]. this is the case when the tauri app is un-initialized.
pub fn get_app_handle() -> &'static AppHandle {
try_get_app_handle().expect("Could not get the app handle.")
}

/// Get the static app handle [`APP_HANDLE`].
pub fn try_get_app_handle() -> Option<&'static AppHandle> {
unsafe { APP_HANDLE.as_ref() }
}

#[tokio::main]
async fn main() {
setup()
Expand All @@ -89,9 +71,7 @@ async fn main() {
fn setup() -> tauri::Builder<Wry> {
tauri::Builder::default()
.setup(|app| {
unsafe {
APP_HANDLE = Some(app.handle());
}
app_handle::setup(app);
log::init();
dom_content_loaded::setup(app);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{fs::create_dir_all, path::PathBuf};

use crate::get_app_handle;
use crate::app_handle::get_app_handle;

macro_rules! mutable_or_immutable {
($var_name:ident, $var_value:expr) => {
Expand Down
9 changes: 3 additions & 6 deletions src-tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
use tauri::{Manager, Window};

use crate::try_get_app_handle;
use crate::app_handle::get_app_handle;

/// Try to get the main window.
///
/// Returns [`None`] when the [`try_get_app_handle`] returns [`None`].
/// Returns [`None`] when the window with label `main` does not exist.
pub fn try_get_window() -> Option<Window> {
match try_get_app_handle() {
Some(app_handle) => app_handle.get_window("main"),
_ => None,
}
get_app_handle().get_window("main")
}

/// Get the main window.
Expand Down

0 comments on commit 7655d4a

Please sign in to comment.