parent
6bf980a1a7
commit
c530611642
|
@ -10,6 +10,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/status-im/status-go/geth/log"
|
"github.com/status-im/status-go/geth/log"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -47,14 +48,21 @@ type NodeNotificationHandler func(jsonEvent string)
|
||||||
|
|
||||||
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
|
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
|
||||||
|
|
||||||
|
// notificationHandlerMutex guards notificationHandler for concurrent calls
|
||||||
|
var notificationHandlerMutex sync.RWMutex
|
||||||
|
|
||||||
// SetDefaultNodeNotificationHandler sets notification handler to invoke on Send
|
// SetDefaultNodeNotificationHandler sets notification handler to invoke on Send
|
||||||
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
|
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
|
||||||
|
notificationHandlerMutex.Lock()
|
||||||
notificationHandler = fn
|
notificationHandler = fn
|
||||||
|
notificationHandlerMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetDefaultNodeNotificationHandler sets notification handler to default one
|
// ResetDefaultNodeNotificationHandler sets notification handler to default one
|
||||||
func ResetDefaultNodeNotificationHandler() {
|
func ResetDefaultNodeNotificationHandler() {
|
||||||
|
notificationHandlerMutex.Lock()
|
||||||
notificationHandler = TriggerDefaultNodeNotificationHandler
|
notificationHandler = TriggerDefaultNodeNotificationHandler
|
||||||
|
notificationHandlerMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
|
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
|
||||||
|
@ -71,6 +79,8 @@ func Send(signal Envelope) {
|
||||||
//export NotifyNode
|
//export NotifyNode
|
||||||
//nolint: golint
|
//nolint: golint
|
||||||
func NotifyNode(jsonEvent *C.char) {
|
func NotifyNode(jsonEvent *C.char) {
|
||||||
|
notificationHandlerMutex.RLock()
|
||||||
|
defer notificationHandlerMutex.RUnlock()
|
||||||
notificationHandler(C.GoString(jsonEvent))
|
notificationHandler(C.GoString(jsonEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue