Sima is a simple object to object or broadcast dispatching system. Any number of interested parties can subscribe to events. Signal receives can receive also signals from specific senders.
$ go get -u github.com/theodesp/sima
- Create a topic factory and re-use it for all signals yo want to create:
tf := NewTopicFactory()
onStart := NewSima(tf)
onEnd := NewSima(tf)
- After you have created your signals, just connect handlers for a particular topic or not. If you don't specify a topic then the handler will be assigned a ALL topic that defaults as a broadcast address.
// Subscribe to ALL
onStart.Connect(func(context context.Context, sender *Topic) interface{} {
fmt.PrintF("OnStart called from Sender %+v", sender)
return sender
}, "")
// Subscribe to specific sender/topic
onEnd.Connect(func(context context.Context, sender *Topic) interface{} {
fmt.PrintF("onEnd called from Sender %+v", sender)
return sender
}, "on-end-sender")
- Now just send some messages and any registered participant will call the handler.
response := onStart.Dispatch(context.Background(), "") // will handle
response := onStart.Dispatch(context.Background(), "on-start-sender") // will not handle
response := onEnd.Dispatch(context.Background(), "") // will not handle
response := onEnd.Dispatch(context.Background(), "on-end-sender") // will handle
- Checking for receivers existance
onEnd.HasRecieversFor("on-end-sender") // true
onEnd.HasRecieversFor("on-start-sender") // false
Copyright © 2017 Theo Despoudis MIT license