From 68b12d4fff65b6549d1949bbc62b5f0120d8e250 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 9 Jan 2024 18:19:09 +1100 Subject: [PATCH] Refactored application shutdown process to streamline app termination. The application shutdown process has been significantly reworked to be more efficient and robust. The refactored code removes the event listener for the 'ApplicationTerminate' event. Instead, an in-process flag is added to the 'Quit' method to prevent recursive calls. Additionally, an optional 'OnShutdown' function variable is introduced to allow custom cleanup operations upon app termination. --- .../@wailsio/runtime/src/event_types.js | 1 - .../@wailsio/runtime/types/event_types.d.ts | 1 - v3/pkg/application/application.go | 19 +++-- v3/pkg/events/events.go | 83 +++++++++---------- v3/pkg/events/events.txt | 3 +- 5 files changed, 53 insertions(+), 54 deletions(-) diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/src/event_types.js b/v3/internal/runtime/desktop/@wailsio/runtime/src/event_types.js index c49dda478..9c4cfabfc 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/src/event_types.js +++ b/v3/internal/runtime/desktop/@wailsio/runtime/src/event_types.js @@ -177,6 +177,5 @@ export const EventTypes = { WindowDPIChanged: "common:WindowDPIChanged", WindowFilesDropped: "common:WindowFilesDropped", ThemeChanged: "common:ThemeChanged", - ApplicationTerminate: "common:ApplicationTerminate", }, }; diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/types/event_types.d.ts b/v3/internal/runtime/desktop/@wailsio/runtime/types/event_types.d.ts index 5588123c0..dc24eb164 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/types/event_types.d.ts +++ b/v3/internal/runtime/desktop/@wailsio/runtime/types/event_types.d.ts @@ -177,6 +177,5 @@ export declare const EventTypes: { WindowDPIChanged: string, WindowFilesDropped: string, ThemeChanged: string, - ApplicationTerminate: string, }, }; diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index a506be819..65b33d5bc 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -122,11 +122,6 @@ func New(appOptions Options) *App { result.keyBindings = processKeyBindingOptions(result.options.KeyBindings) } - // Handle the terminate event - result.On(events.Common.ApplicationTerminate, func(e *Event) { - result.Quit() - }) - return result } @@ -291,8 +286,11 @@ type App struct { // Keybindings keyBindings map[string]func(window *WebviewWindow) - // - shutdownOnce sync.Once + // OnShutdown is called when the application is about to quit. + // This is useful for cleanup tasks. + // The shutdown process blocks until this function returns + OnShutdown func() + performingShutdown bool } func (a *App) init() { @@ -602,6 +600,13 @@ func (a *App) CurrentWindow() *WebviewWindow { } func (a *App) Quit() { + if a.performingShutdown { + return + } + a.performingShutdown = true + if a.OnShutdown != nil { + a.OnShutdown() + } InvokeSync(func() { a.windowsLock.RLock() for _, window := range a.windows { diff --git a/v3/pkg/events/events.go b/v3/pkg/events/events.go index 9c6a616b5..1ad1f8c67 100644 --- a/v3/pkg/events/events.go +++ b/v3/pkg/events/events.go @@ -6,52 +6,50 @@ type WindowEventType uint var Common = newCommonEvents() type commonEvents struct { - ApplicationStarted ApplicationEventType - WindowMaximise WindowEventType - WindowUnMaximise WindowEventType - WindowFullscreen WindowEventType - WindowUnFullscreen WindowEventType - WindowRestore WindowEventType - WindowMinimise WindowEventType - WindowUnMinimise WindowEventType - WindowClosing WindowEventType - WindowZoom WindowEventType - WindowZoomIn WindowEventType - WindowZoomOut WindowEventType - WindowZoomReset WindowEventType - WindowFocus WindowEventType - WindowLostFocus WindowEventType - WindowShow WindowEventType - WindowHide WindowEventType - WindowDPIChanged WindowEventType - WindowFilesDropped WindowEventType - ThemeChanged ApplicationEventType - ApplicationTerminate ApplicationEventType + ApplicationStarted ApplicationEventType + WindowMaximise WindowEventType + WindowUnMaximise WindowEventType + WindowFullscreen WindowEventType + WindowUnFullscreen WindowEventType + WindowRestore WindowEventType + WindowMinimise WindowEventType + WindowUnMinimise WindowEventType + WindowClosing WindowEventType + WindowZoom WindowEventType + WindowZoomIn WindowEventType + WindowZoomOut WindowEventType + WindowZoomReset WindowEventType + WindowFocus WindowEventType + WindowLostFocus WindowEventType + WindowShow WindowEventType + WindowHide WindowEventType + WindowDPIChanged WindowEventType + WindowFilesDropped WindowEventType + ThemeChanged ApplicationEventType } func newCommonEvents() commonEvents { return commonEvents{ - ApplicationStarted: 1174, - WindowMaximise: 1175, - WindowUnMaximise: 1176, - WindowFullscreen: 1177, - WindowUnFullscreen: 1178, - WindowRestore: 1179, - WindowMinimise: 1180, - WindowUnMinimise: 1181, - WindowClosing: 1182, - WindowZoom: 1183, - WindowZoomIn: 1184, - WindowZoomOut: 1185, - WindowZoomReset: 1186, - WindowFocus: 1187, - WindowLostFocus: 1188, - WindowShow: 1189, - WindowHide: 1190, - WindowDPIChanged: 1191, - WindowFilesDropped: 1192, - ThemeChanged: 1193, - ApplicationTerminate: 1194, + ApplicationStarted: 1174, + WindowMaximise: 1175, + WindowUnMaximise: 1176, + WindowFullscreen: 1177, + WindowUnFullscreen: 1178, + WindowRestore: 1179, + WindowMinimise: 1180, + WindowUnMinimise: 1181, + WindowClosing: 1182, + WindowZoom: 1183, + WindowZoomIn: 1184, + WindowZoomOut: 1185, + WindowZoomReset: 1186, + WindowFocus: 1187, + WindowLostFocus: 1188, + WindowShow: 1189, + WindowHide: 1190, + WindowDPIChanged: 1191, + WindowFilesDropped: 1192, + ThemeChanged: 1193, } } @@ -560,5 +558,4 @@ var eventToJS = map[uint]string{ 1191: "common:WindowDPIChanged", 1192: "common:WindowFilesDropped", 1193: "common:ThemeChanged", - 1194: "common:ApplicationTerminate", } diff --git a/v3/pkg/events/events.txt b/v3/pkg/events/events.txt index 50828244f..58b05ae38 100644 --- a/v3/pkg/events/events.txt +++ b/v3/pkg/events/events.txt @@ -167,5 +167,4 @@ common:WindowShow common:WindowHide common:WindowDPIChanged common:WindowFilesDropped -common:ThemeChanged -common:ApplicationTerminate \ No newline at end of file +common:ThemeChanged \ No newline at end of file