Skip to content

Commit

Permalink
feat: use multi-threading instead of async
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Jun 28, 2024
1 parent 4643f50 commit a7f0552
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/pages/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use relm4::{ComponentParts, ComponentSender, SimpleComponent};
#[derive(Debug, Default)]
pub struct InstallationPage {
progress_bar: gtk::ProgressBar,
thread: Option<std::thread::JoinHandle<color_eyre::Result<()>>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -49,8 +50,7 @@ impl SimpleComponent for InstallationPage {
},

#[local_ref]
progress_bar -> gtk::ProgressBar {
}
progress_bar -> gtk::ProgressBar {}
}
}
}
Expand All @@ -72,21 +72,27 @@ impl SimpleComponent for InstallationPage {

#[tracing::instrument]
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
self.progress_bar.pulse();
if let Some(th) = &self.thread {
self.progress_bar.pulse();
if th.is_finished() {
let th = self.thread.take().unwrap();
let res = th.join().expect("Cannot join thread");
if let Err(e) = res {
tracing::error!("Installation failed: {e:?}");
}
self.progress_bar.set_fraction(1.0);
}
}
// handle channel logics here
match message {
InstallationPageMsg::StartInstallation => sender.command(|_out, shutdown| {
shutdown
.register(async move {
let state = INSTALLATION_STATE.read();
tracing::debug!("Starting installation...");
// FIXME: all errors are ignored due to shutdown.register() not handling results
state.installation_type.as_ref().unwrap().install(&state)?;

color_eyre::Result::<_>::Ok(())
})
.drop_on_shutdown()
}),
InstallationPageMsg::StartInstallation => {
self.thread = Some(std::thread::spawn(|| {
let state = INSTALLATION_STATE.read();
tracing::debug!(?state, "Starting installation...");
state.installation_type.as_ref().unwrap().install(&state)?;
Ok(())
}));
}
InstallationPageMsg::Navigate(action) => sender
.output(InstallationPageOutput::Navigate(action))
.unwrap(),
Expand Down

0 comments on commit a7f0552

Please sign in to comment.