From a6757c352f7c720104356efa2d87f6468989cf22 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 14 Jan 2024 08:17:17 +1100 Subject: [PATCH] Add shutdown tasks functionality to application A new method OnShutdown was introduced to allow adding tasks to be run when the application shuts down. The shutdown tasks are run on the main thread and in the order they were added, with the application option `OnShutdown` being run first. This allows more controlled and managed shutdown of the application. --- v3/pkg/application/application.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index c68a03057..6a126939c 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -127,6 +127,10 @@ func New(appOptions Options) *App { result.keyBindings = processKeyBindingOptions(result.options.KeyBindings) } + if appOptions.OnShutdown != nil { + result.OnShutdown(appOptions.OnShutdown) + } + return result } @@ -291,7 +295,12 @@ type App struct { // Keybindings keyBindings map[string]func(window *WebviewWindow) + // Shutdown performingShutdown bool + // Shutdown tasks are run when the application is shutting down. + // They are run in the order they are added and run on the main thread. + // The application option `OnShutdown` is run first. + shutdownTasks []func() } func (a *App) init() { @@ -600,13 +609,22 @@ func (a *App) CurrentWindow() *WebviewWindow { return result.(*WebviewWindow) } +// OnShutdown adds a function to be run when the application is shutting down. +func (a *App) OnShutdown(f func()) { + if f == nil { + return + } + a.shutdownTasks = append(a.shutdownTasks, f) +} + func (a *App) Quit() { if a.performingShutdown { return } a.performingShutdown = true - if a.options.OnShutdown != nil { - a.options.OnShutdown() + // Run the shutdown tasks + for _, shutdownTask := range a.shutdownTasks { + InvokeSync(shutdownTask) } InvokeSync(func() { a.windowsLock.RLock()