From 8573a415c064ce3a268d2cbef09b5050faf7ee2a Mon Sep 17 00:00:00 2001 From: sawka Date: Thu, 16 May 2024 12:39:14 -0700 Subject: [PATCH] don't use app.Emit(), instead dispatch to individual windows for synchronous event dispatch --- pkg/eventbus/eventbus.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/eventbus/eventbus.go b/pkg/eventbus/eventbus.go index 0a382f314..8300d76b8 100644 --- a/pkg/eventbus/eventbus.go +++ b/pkg/eventbus/eventbus.go @@ -63,6 +63,18 @@ func emitEventToWindow(event WindowEvent) { } } +func emitEventToAllWindows(event *application.WailsEvent) { + globalLock.Lock() + wins := make([]*application.WebviewWindow, 0, len(wailsWindowMap)) + for _, window := range wailsWindowMap { + wins = append(wins, window) + } + globalLock.Unlock() + for _, window := range wins { + window.DispatchWailsEvent(event) + } +} + func SendEvent(event application.WailsEvent) { EventCh <- event } @@ -107,10 +119,7 @@ func processEvents() { for { select { case event := <-EventCh: - // no lock needed for wailsApp since it is never updated - if wailsApp != nil { - wailsApp.Events.Emit(&event) - } + emitEventToAllWindows(&event) case windowEvent := <-WindowEventCh: emitEventToWindow(windowEvent)