|
| 1 | +package Helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +var eventManagerWithDataInstance *EventManagerWithData |
| 9 | +var eventManagerWithDataOnce sync.Once |
| 10 | + |
| 11 | +// EventHandlerWithData - eventWithData handler prototype |
| 12 | +type EventHandlerWithData func([]byte) |
| 13 | + |
| 14 | +type eventWithData struct { |
| 15 | + subscribers []EventHandlerWithData |
| 16 | + lastData []byte |
| 17 | + happenedOnce bool |
| 18 | +} |
| 19 | + |
| 20 | +// EventManagerWithData - eventWithData manager |
| 21 | +type EventManagerWithData struct { |
| 22 | + events map[string]*eventWithData |
| 23 | + eventsSync sync.RWMutex |
| 24 | +} |
| 25 | + |
| 26 | +// EventsWithData - Singleton to get the events manager instance |
| 27 | +func EventsWithData() *EventManagerWithData { |
| 28 | + eventManagerWithDataOnce.Do(func() { |
| 29 | + eventManagerWithDataInstance = &EventManagerWithData{ |
| 30 | + events: make(map[string]*eventWithData), |
| 31 | + eventsSync: sync.RWMutex{}, |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + return eventManagerWithDataInstance |
| 36 | +} |
| 37 | + |
| 38 | +// On - Tells EventManagerWithData to add a subscriber for an eventWithData |
| 39 | +func (thisRef *EventManagerWithData) On(eventName string, eventHandler EventHandlerWithData) { |
| 40 | + thisRef.addEventIfNotExists(eventName) |
| 41 | + thisRef.addSubscriberIfNotExists(eventName, eventHandler) |
| 42 | + |
| 43 | + thisRef.eventsSync.RLock() |
| 44 | + defer thisRef.eventsSync.RUnlock() |
| 45 | + |
| 46 | + if thisRef.events[eventName].happenedOnce { |
| 47 | + go eventHandler(thisRef.events[eventName].lastData) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Off - Tells EventManagerWithData to remove a subscriber for an eventWithData |
| 52 | +func (thisRef *EventManagerWithData) Off(eventName string, eventHandler EventHandlerWithData) { |
| 53 | + thisRef.addEventIfNotExists(eventName) |
| 54 | + |
| 55 | + thisRef.eventsSync.RLock() |
| 56 | + defer thisRef.eventsSync.RUnlock() |
| 57 | + |
| 58 | + var foundIndex = -1 |
| 59 | + for index, existingEventHandler := range thisRef.events[eventName].subscribers { |
| 60 | + if reflect.ValueOf(eventHandler) == reflect.ValueOf(existingEventHandler) { |
| 61 | + foundIndex = index |
| 62 | + break |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if foundIndex != -1 { |
| 67 | + thisRef.events[eventName].subscribers = append( |
| 68 | + thisRef.events[eventName].subscribers[:foundIndex], |
| 69 | + thisRef.events[eventName].subscribers[foundIndex+1:]..., |
| 70 | + ) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Raise - Informs all subscribers about the eventWithData |
| 75 | +func (thisRef *EventManagerWithData) Raise(eventName string, data []byte) { |
| 76 | + thisRef.addEventIfNotExists(eventName) |
| 77 | + |
| 78 | + thisRef.eventsSync.RLock() |
| 79 | + defer thisRef.eventsSync.RUnlock() |
| 80 | + |
| 81 | + thisRef.events[eventName].happenedOnce = true |
| 82 | + thisRef.events[eventName].lastData = data |
| 83 | + for _, eventHandler := range thisRef.events[eventName].subscribers { |
| 84 | + go eventHandler(data) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (thisRef *EventManagerWithData) addEventIfNotExists(eventName string) { |
| 89 | + thisRef.eventsSync.Lock() |
| 90 | + defer thisRef.eventsSync.Unlock() |
| 91 | + |
| 92 | + if _, ok := thisRef.events[eventName]; !ok { |
| 93 | + thisRef.events[eventName] = &eventWithData{ |
| 94 | + subscribers: []EventHandlerWithData{}, |
| 95 | + happenedOnce: false, |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func (thisRef *EventManagerWithData) addSubscriberIfNotExists(eventName string, eventHandler EventHandlerWithData) bool { |
| 101 | + thisRef.eventsSync.RLock() |
| 102 | + defer thisRef.eventsSync.RUnlock() |
| 103 | + |
| 104 | + // 1. Check if delegate for the eventWithData already there, assumes map-key exists |
| 105 | + var alreadyThere = false |
| 106 | + |
| 107 | + for _, existingEventHandler := range thisRef.events[eventName].subscribers { |
| 108 | + alreadyThere = (reflect.ValueOf(eventHandler) == reflect.ValueOf(existingEventHandler)) |
| 109 | + if alreadyThere { |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // 2. Add the delegate |
| 115 | + if !alreadyThere { |
| 116 | + thisRef.events[eventName].subscribers = append( |
| 117 | + thisRef.events[eventName].subscribers, |
| 118 | + eventHandler, |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + return alreadyThere |
| 123 | +} |
0 commit comments