Skip to content

Commit

Permalink
refactor(app): wait for app-ready before checking for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Jan 30, 2025
1 parent 7148d7f commit 520971b
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions src/stremio_app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<String>();
let updater_tx_web = updater_tx.clone();

let command_clone = self.command.clone();

// Single application IPC
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 520971b

Please sign in to comment.