Skip to content

Commit

Permalink
Refactored application shutdown process to streamline app termination.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leaanthony committed Jan 9, 2024
1 parent 1195464 commit 68b12d4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,5 @@ export const EventTypes = {
WindowDPIChanged: "common:WindowDPIChanged",
WindowFilesDropped: "common:WindowFilesDropped",
ThemeChanged: "common:ThemeChanged",
ApplicationTerminate: "common:ApplicationTerminate",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,5 @@ export declare const EventTypes: {
WindowDPIChanged: string,
WindowFilesDropped: string,
ThemeChanged: string,
ApplicationTerminate: string,
},
};
19 changes: 12 additions & 7 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
83 changes: 40 additions & 43 deletions v3/pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -560,5 +558,4 @@ var eventToJS = map[uint]string{
1191: "common:WindowDPIChanged",
1192: "common:WindowFilesDropped",
1193: "common:ThemeChanged",
1194: "common:ApplicationTerminate",
}
3 changes: 1 addition & 2 deletions v3/pkg/events/events.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,4 @@ common:WindowShow
common:WindowHide
common:WindowDPIChanged
common:WindowFilesDropped
common:ThemeChanged
common:ApplicationTerminate
common:ThemeChanged

0 comments on commit 68b12d4

Please sign in to comment.