From 27cae8ec417dfa08c2fc393baed9e494a4f58efb Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Wed, 2 Apr 2025 20:04:23 -0400 Subject: [PATCH] synchronized front end and backend to avoid race condition --- src-tauri/src/commands.rs | 3 +-- src-tauri/src/lib.rs | 22 +++++++++------------- src/App.tsx | 4 ++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 9e11ac8..7423940 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,5 +1,4 @@ use std::{fs::OpenOptions, io::Write}; - use dmp_core::{ music_controller::{ connections::LastFMAuth, @@ -92,4 +91,4 @@ pub async fn last_fm_init_auth(ctrl_handle: State<'_, ControllerHandle>) -> Resu LastFMAuth::Session(None), ); Ok(()) -} +} \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index be0bba5..572b87e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -19,7 +19,7 @@ use dmp_core::{ music_storage::library::{MusicLibrary, Song}, }; use parking_lot::RwLock; -use tauri::{http::Response, AppHandle, Emitter, Manager}; +use tauri::{http::Response, AppHandle, Emitter, Listener, Manager}; use uuid::Uuid; use wrappers::{_Song, stop}; @@ -41,9 +41,7 @@ const LAST_FM_API_SECRET: &str = env!("LAST_FM_API_SECRET", "None"); #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { - let (app_rx, app_tx) = bounded::(1); - - + let (sync_rx, sync_tx) = bounded::<()>(1); let app = tauri::Builder::default() .plugin(tauri_plugin_shell::init()) @@ -70,12 +68,10 @@ pub fn run() { get_config, save_config, close_window, + start_controller, ]) .manage(tempfile::TempDir::new().unwrap()) - .setup(move |app| { - app_rx.send(app.handle().clone()).unwrap(); - Ok(()) - }) + .manage(sync_rx) .register_asynchronous_uri_scheme_protocol("asset", move |ctx, req, res| { let query = req .clone() @@ -117,8 +113,6 @@ pub fn run() { .build(tauri::generate_context!()) .expect("error while building tauri application"); - let _controller_thread = start_controller(app.handle().clone()); - app.run(|_app_handle, event| match event { tauri::RunEvent::ExitRequested { .. } => { // api.prevent_exit(); @@ -128,7 +122,8 @@ pub fn run() { }); } -fn start_controller(app: AppHandle) -> JoinHandle<()> { +#[tauri::command] +fn start_controller(app: AppHandle) -> Result<(), String> { spawn(move || { let mut config = init_get_config().unwrap(); @@ -163,7 +158,7 @@ fn start_controller(app: AppHandle) -> JoinHandle<()> { } else { config.write_file().unwrap(); } - library.save(lib_path).unwrap(); + library.save(lib_path.to_path_buf()).unwrap(); app.emit("library_loaded", ()).unwrap(); @@ -226,7 +221,8 @@ fn start_controller(app: AppHandle) -> JoinHandle<()> { .unwrap(); let _controller = futures::executor::block_on(Controller::start(input)).unwrap(); - }) + }); + Ok(()) } fn init_get_config() -> Result { diff --git a/src/App.tsx b/src/App.tsx index 7005407..deb8369 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -71,6 +71,10 @@ function App() { return () => { unlisten.then((f) => f()) } }, []); + useEffect(() => { + const invoke_: any = invoke("start_controller").then(() => {}); + }, []) + return (