github.com/soypat/vectytemplater@v0.0.0-20220501050640-d40b24e35168/_templates/default/store/dispatcher/dispatcher.go (about) 1 package dispatcher 2 3 // ID is a unique identifier representing a registered callback function. 4 type ID int 5 6 var ( 7 idCounter ID 8 callbacks = make(map[ID]func(action interface{})) 9 ) 10 11 // Dispatch dispatches the given action to all registered callbacks. 12 func Dispatch(action interface{}) { 13 for _, c := range callbacks { 14 c(action) 15 } 16 } 17 18 // Register registers the callback to handle dispatched actions, the returned 19 // ID may be used to unregister the callback later. 20 func Register(callback func(action interface{})) ID { 21 idCounter++ 22 id := idCounter 23 callbacks[id] = callback 24 return id 25 } 26 27 // Unregister unregisters the callback previously registered via a call to 28 // Register. 29 func Unregister(id ID) { 30 delete(callbacks, id) 31 }