瀏覽代碼

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

Lucas Fernandes Nogueira 3 年之前
父節點
當前提交
fce7d3bbae
共有 4 個文件被更改,包括 19 次插入7 次删除
  1. 5 0
      .changes/updater-restart-cleanup.md
  2. 7 1
      core/tauri/src/app.rs
  3. 5 0
      core/tauri/src/lib.rs
  4. 2 6
      core/tauri/src/updater/mod.rs

+ 5 - 0
.changes/updater-restart-cleanup.md

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

+ 7 - 1
core/tauri/src/app.rs

@@ -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)]

+ 5 - 0
core/tauri/src/lib.rs

@@ -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()

+ 2 - 6
core/tauri/src/updater/mod.rs

@@ -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();
     }
   }