Skip to content

Commit b8c0d7e

Browse files
fix: run_return not responding to restart (#13040)
* fix: `run_return` not responding to `restart` * Document run_return will handle restart requests * Add change file
1 parent e4982df commit b8c0d7e

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

.changes/run-return-restart.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri: patch:bug
3+
---
4+
5+
Fix `run_return` not responding to `restart` and `request_restart`

crates/tauri/src/app.rs

+42-45
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub struct AppHandle<R: Runtime> {
349349
event_loop: Arc<Mutex<EventLoop>>,
350350
}
351351

352+
/// Not the real event loop, only contains the main thread id of the event loop
352353
#[derive(Debug)]
353354
struct EventLoop {
354355
main_thread_id: ThreadId,
@@ -1180,37 +1181,21 @@ impl<R: Runtime> App<R> {
11801181
/// _ => {}
11811182
/// });
11821183
/// ```
1183-
pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
1184-
let app_handle = self.handle().clone();
1185-
let manager = self.manager.clone();
1186-
1187-
app_handle.event_loop.lock().unwrap().main_thread_id = std::thread::current().id();
1184+
pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, callback: F) {
1185+
self.handle.event_loop.lock().unwrap().main_thread_id = std::thread::current().id();
11881186

1189-
self.runtime.take().unwrap().run(move |event| match event {
1190-
RuntimeRunEvent::Ready => {
1191-
if let Err(e) = setup(&mut self) {
1192-
panic!("Failed to setup app: {e}");
1193-
}
1194-
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Ready, &manager);
1195-
callback(&app_handle, event);
1196-
}
1197-
RuntimeRunEvent::Exit => {
1198-
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager);
1199-
callback(&app_handle, event);
1200-
app_handle.cleanup_before_exit();
1201-
if self.manager.restart_on_exit.load(atomic::Ordering::Relaxed) {
1202-
crate::process::restart(&self.env());
1203-
}
1204-
}
1205-
_ => {
1206-
let event = on_event_loop_event(&app_handle, event, &manager);
1207-
callback(&app_handle, event);
1208-
}
1209-
});
1187+
self
1188+
.runtime
1189+
.take()
1190+
.unwrap()
1191+
.run(self.make_run_event_loop_callback(callback));
12101192
}
12111193

12121194
/// Runs the application, returning its intended exit code.
12131195
///
1196+
/// Note when using [`AppHandle::restart`] and [`AppHandle::request_restart`],
1197+
/// this function will handle the restart request, exit and restart the app without returning
1198+
///
12141199
/// ## Platform-specific
12151200
///
12161201
/// - **iOS**: Unsupported. The application will fallback to [`run`](Self::run).
@@ -1235,32 +1220,44 @@ impl<R: Runtime> App<R> {
12351220
///
12361221
/// std::process::exit(exit_code);
12371222
/// ```
1238-
pub fn run_return<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) -> i32 {
1239-
let manager = self.manager.clone();
1240-
let app_handle = self.handle().clone();
1223+
pub fn run_return<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, callback: F) -> i32 {
1224+
self.handle.event_loop.lock().unwrap().main_thread_id = std::thread::current().id();
12411225

12421226
self
12431227
.runtime
12441228
.take()
12451229
.unwrap()
1246-
.run_return(move |event| match event {
1247-
RuntimeRunEvent::Ready => {
1248-
if let Err(e) = setup(&mut self) {
1249-
panic!("Failed to setup app: {e}");
1250-
}
1251-
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Ready, &manager);
1252-
callback(&app_handle, event);
1253-
}
1254-
RuntimeRunEvent::Exit => {
1255-
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager);
1256-
callback(&app_handle, event);
1257-
app_handle.cleanup_before_exit();
1230+
.run_return(self.make_run_event_loop_callback(callback))
1231+
}
1232+
1233+
fn make_run_event_loop_callback<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
1234+
mut self,
1235+
mut callback: F,
1236+
) -> impl FnMut(RuntimeRunEvent<EventLoopMessage>) {
1237+
let app_handle = self.handle().clone();
1238+
let manager = self.manager.clone();
1239+
1240+
move |event| match event {
1241+
RuntimeRunEvent::Ready => {
1242+
if let Err(e) = setup(&mut self) {
1243+
panic!("Failed to setup app: {e}");
12581244
}
1259-
_ => {
1260-
let event = on_event_loop_event(&app_handle, event, &manager);
1261-
callback(&app_handle, event);
1245+
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Ready, &manager);
1246+
callback(&app_handle, event);
1247+
}
1248+
RuntimeRunEvent::Exit => {
1249+
let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager);
1250+
callback(&app_handle, event);
1251+
app_handle.cleanup_before_exit();
1252+
if self.manager.restart_on_exit.load(atomic::Ordering::Relaxed) {
1253+
crate::process::restart(&self.env());
12621254
}
1263-
})
1255+
}
1256+
_ => {
1257+
let event = on_event_loop_event(&app_handle, event, &manager);
1258+
callback(&app_handle, event);
1259+
}
1260+
}
12641261
}
12651262

12661263
/// Runs an iteration of the runtime event loop and immediately return.

0 commit comments

Comments
 (0)