diff --git a/src/stremio_app/app.rs b/src/stremio_app/app.rs index 414830d..b34ae32 100644 --- a/src/stremio_app/app.rs +++ b/src/stremio_app/app.rs @@ -10,7 +10,8 @@ use std::{ process::{self, Command}, str, sync::{Arc, Mutex}, - thread, time, + thread, + time::{self, Duration}, }; use url::Url; use winapi::um::{winbase::CREATE_BREAKAWAY_FROM_JOB, winuser::WS_EX_TOPMOST}; @@ -133,6 +134,10 @@ impl MainWindow { let web_tx_arg = web_tx.clone(); let web_tx_upd = web_tx.clone(); let web_rx = web_rx.clone(); + + let (updater_tx, updater_rx) = flume::unbounded::(); + let updater_tx_web = updater_tx.clone(); + let command_clone = self.command.clone(); // Single application IPC @@ -146,35 +151,53 @@ impl MainWindow { let force_update = self.force_update; let release_candidate = self.release_candidate; let autoupdater_setup_file = self.autoupdater_setup_file.clone(); + let mut last_update_check = time::Instant::now(); + thread::spawn(move || loop { - let current_version = env!("CARGO_PKG_VERSION") - .parse() - .expect("Should always be valid"); - let updater_endpoint = if let Some(ref endpoint) = autoupdater_endpoint { - endpoint.clone() - } else { - let mut rng = rand::thread_rng(); - let index = rng.gen_range(0..UPDATE_ENDPOINT.len()); - let mut url = Url::parse(UPDATE_ENDPOINT[index]).unwrap(); - if release_candidate { - url.query_pairs_mut().append_pair("rc", "true"); + let check_for_update = || { + let current_version = env!("CARGO_PKG_VERSION") + .parse() + .expect("Should always be valid"); + + let updater_endpoint = if let Some(ref endpoint) = autoupdater_endpoint { + endpoint.clone() + } else { + let mut rng = rand::thread_rng(); + let index = rng.gen_range(0..UPDATE_ENDPOINT.len()); + let mut url = Url::parse(UPDATE_ENDPOINT[index]).unwrap(); + if release_candidate { + url.query_pairs_mut().append_pair("rc", "true"); + } + url + }; + + let updater = + updater::Updater::new(current_version, &updater_endpoint, force_update); + match updater.autoupdate() { + Ok(Some(update)) => { + println!("New version ready to install v{}", update.version); + let mut autoupdater_setup_file = autoupdater_setup_file.lock().unwrap(); + *autoupdater_setup_file = Some(update.file.clone()); + web_tx_upd.send(RPCResponse::update_available()).ok(); + } + Ok(None) => println!("No new updates found"), + Err(e) => eprintln!("Failed to fetch updates: {e}"), } - url }; - let updater = updater::Updater::new(current_version, &updater_endpoint, force_update); - match updater.autoupdate() { - Ok(Some(update)) => { - println!("New version ready to install v{}", update.version); - let mut autoupdater_setup_file = autoupdater_setup_file.lock().unwrap(); - *autoupdater_setup_file = Some(update.file.clone()); - web_tx_upd.send(RPCResponse::update_available()).ok(); + updater_rx.try_iter().for_each(|message| { + if message.as_str() == "check_for_update" { + check_for_update(); } - Ok(None) => println!("No new updates found"), - Err(e) => eprintln!("Failed to fetch updates: {e}"), + }); + + let now = time::Instant::now(); + if now > (last_update_check + time::Duration::from_secs(UPDATE_INTERVAL)) { + last_update_check = now; + check_for_update(); } - thread::sleep(time::Duration::from_secs(UPDATE_INTERVAL)); + thread::sleep(Duration::from_millis(10)); }); // thread if let Ok(mut listener) = PipeServer::bind(socket_path) { @@ -224,6 +247,10 @@ impl MainWindow { web_tx_web .send(RPCResponse::visibility_change(true, 1, false)) .ok(); + updater_tx_web + .send("check_for_update".to_owned()) + .expect("Failed to send value to updater channel"); + let command_ref = command_clone.clone(); if !command_ref.is_empty() { web_tx_web.send(RPCResponse::open_media(command_ref)).ok();