eventpump.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. /*
  10. Package flowutil contains utilities to manage control flow.
  11. */
  12. package flowutil
  13. import "sync"
  14. /*
  15. EventPump implements the observer pattern. Observers can subscribe to receive
  16. notifications on certain events. Observed objects can send notifications.
  17. */
  18. type EventPump struct {
  19. eventsObservers map[string]map[interface{}][]EventCallback
  20. eventsObserversLock *sync.Mutex
  21. }
  22. /*
  23. EventCallback is the callback function which is called when an event was observed.
  24. */
  25. type EventCallback func(event string, eventSource interface{})
  26. /*
  27. NewEventPump creates a new event pump.
  28. */
  29. func NewEventPump() *EventPump {
  30. return &EventPump{make(map[string]map[interface{}][]EventCallback), &sync.Mutex{}}
  31. }
  32. /*
  33. AddObserver adds a new observer to the event pump. An observer can subscribe to
  34. a given event from a given event source. If the event is an empty string then
  35. the observer subscribes to all events from the event source. If the
  36. eventSource is nil then the observer subscribes to all event sources.
  37. */
  38. func (ep *EventPump) AddObserver(event string, eventSource interface{}, callback EventCallback) {
  39. // Ignore requests with non-existent callbacks
  40. if callback == nil {
  41. return
  42. }
  43. ep.eventsObserversLock.Lock()
  44. defer ep.eventsObserversLock.Unlock()
  45. sources, ok := ep.eventsObservers[event]
  46. if !ok {
  47. sources = make(map[interface{}][]EventCallback)
  48. ep.eventsObservers[event] = sources
  49. }
  50. callbacks, ok := sources[eventSource]
  51. if !ok {
  52. callbacks = []EventCallback{callback}
  53. sources[eventSource] = callbacks
  54. } else {
  55. sources[eventSource] = append(callbacks, callback)
  56. }
  57. }
  58. /*
  59. PostEvent posts an event to this event pump from a given event source.
  60. */
  61. func (ep *EventPump) PostEvent(event string, eventSource interface{}) {
  62. if event == "" || eventSource == nil {
  63. panic("Posting an event requires the event and its source")
  64. }
  65. postEvent := func(event string, eventSource interface{}) {
  66. ep.eventsObserversLock.Lock()
  67. sources, ok := ep.eventsObservers[event]
  68. if ok {
  69. origsources := sources
  70. sources = make(map[interface{}][]EventCallback)
  71. for source, callbacks := range origsources {
  72. sources[source] = callbacks
  73. }
  74. }
  75. ep.eventsObserversLock.Unlock()
  76. if ok {
  77. for source, callbacks := range sources {
  78. if source == eventSource || source == nil {
  79. for _, callback := range callbacks {
  80. callback(event, eventSource)
  81. }
  82. }
  83. }
  84. }
  85. }
  86. postEvent(event, eventSource)
  87. postEvent("", eventSource)
  88. }
  89. /*
  90. RemoveObservers removes observers from the event pump. If the event is an
  91. empty string then the observer is removed from all events. If the
  92. eventSource is nil then all observers of the event are dropped.
  93. */
  94. func (ep *EventPump) RemoveObservers(event string, eventSource interface{}) {
  95. ep.eventsObserversLock.Lock()
  96. defer ep.eventsObserversLock.Unlock()
  97. // Clear everything
  98. if event == "" && eventSource == nil {
  99. ep.eventsObservers = make(map[string]map[interface{}][]EventCallback)
  100. } else if eventSource == nil {
  101. delete(ep.eventsObservers, event)
  102. } else if event == "" {
  103. for _, sources := range ep.eventsObservers {
  104. delete(sources, eventSource)
  105. }
  106. } else {
  107. if sources, ok := ep.eventsObservers[event]; ok {
  108. delete(sources, eventSource)
  109. }
  110. }
  111. }