feat(core): run app cleanup code before updater restart, closes #3605 (#3616)

This commit is contained in:
Lucas Fernandes Nogueira 2022-03-04 21:19:01 -03:00 committed by GitHub
parent 58070c1eb4
commit fce7d3bbae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Run `AppHandle` cleanup code before restarting the application when a new update is installed.

View File

@ -289,12 +289,18 @@ impl<R: Runtime> AppHandle<R> {
Ok(())
}
/// Exits the app
/// Exits the app. This is the same as [`std::process::exit`], but it performs cleanup on this application.
pub fn exit(&self, exit_code: i32) {
self.cleanup_before_exit();
std::process::exit(exit_code);
}
/// Restarts the app. This is the same as [`crate::api::process::restart`], but it performs cleanup on this application.
pub fn restart(&self) {
self.cleanup_before_exit();
crate::api::process::restart(&self.env());
}
/// Runs necessary cleanup tasks before exiting the process
fn cleanup_before_exit(&self) {
#[cfg(shell_execute)]

View File

@ -399,6 +399,11 @@ impl<A: Assets> Context<A> {
// TODO: expand these docs
/// Manages a running application.
pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// The application handle associated with this manager.
fn app_handle(&self) -> AppHandle<R> {
sealed::ManagerBase::app_handle(self)
}
/// The [`Config`] the manager was created with.
fn config(&self) -> Arc<Config> {
self.manager().config()

View File

@ -333,10 +333,7 @@ mod error;
pub use self::error::Error;
use crate::{
api::{dialog::blocking::ask, process::restart},
runtime::Runtime,
utils::config::UpdaterConfig,
Env, Manager, Window,
api::dialog::blocking::ask, runtime::Runtime, utils::config::UpdaterConfig, Env, Manager, Window,
};
/// Check for new updates
@ -560,14 +557,13 @@ Release Notes:
updater.download_and_install(pubkey.clone()).await?;
// Ask user if we need to restart the application
let env = window.state::<Env>().inner().clone();
let should_exit = ask(
Some(&window),
"Ready to Restart",
"The installation was successful, do you want to restart the application now?",
);
if should_exit {
restart(&env);
window.app_handle().restart();
}
}