From 85bb835e6f9a5f7b81cc7d74bba824522146556d Mon Sep 17 00:00:00 2001 From: Yahia Sweid Date: Tue, 30 Apr 2019 12:46:30 +0800 Subject: [PATCH 1/2] BrowserWindow: On method accepts string argument as event name --- gotron.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/gotron.go b/gotron.go index 6c8113f..69728c9 100644 --- a/gotron.go +++ b/gotron.go @@ -273,11 +273,20 @@ var upgrader = websocket.Upgrader{ //Upgrader for websockets } // On register handler for messages incoming from js frontend -func (gbw *BrowserWindow) On(msg EventInterface, handler func(bin []byte)) { - logger.Debug().Msgf("Adding handler for message: " + msg.EventString()) +func (gbw *BrowserWindow) On(ev interface{}, handler func(bin []byte)) { + eventString := "" + switch e := ev.(type) { + case string: + eventString = e + case EventInterface: + eventString = e.EventString() + default: + logger.Panic().Msgf("unknown event %v", ev) + } + logger.Debug().Msgf("Adding handler for message: " + eventString) //TODO check if a handler was already registered. - gbw.handledMessages[msg.EventString()] = handler + gbw.handledMessages[eventString] = handler } // Send send message (with data) to js frontend From 6f563c188b58713b2b7250a8ed9205bb3e7065d4 Mon Sep 17 00:00:00 2001 From: Yahia Sweid Date: Tue, 30 Apr 2019 13:00:09 +0800 Subject: [PATCH 2/2] Log warning in case event handler is being overriden --- gotron.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gotron.go b/gotron.go index 69728c9..800c4ff 100644 --- a/gotron.go +++ b/gotron.go @@ -285,7 +285,9 @@ func (gbw *BrowserWindow) On(ev interface{}, handler func(bin []byte)) { } logger.Debug().Msgf("Adding handler for message: " + eventString) - //TODO check if a handler was already registered. + if _, ok := gbw.handledMessages[eventString]; ok { + logger.Warn().Msgf("%s event handler is being overriden", eventString) + } gbw.handledMessages[eventString] = handler }