5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-21 11:29:29 +08:00

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.
This commit is contained in:
Lea Anthony 2024-01-14 08:17:17 +11:00
parent 2d97776caa
commit a6757c352f
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405

View File

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