Documentation ¶
Overview ¶
Package event implements an event bus. for a great introduction to the event bus pattern in go, see: https://levelup.gitconnected.com/lets-write-a-simple-event-bus-in-go-79b9480d8997
Example ¶
const ( ETMainSaidHello = Topic("main:SaidHello") ETMainOpSucceeded = Topic("main:OperationSucceeded") ETMainOpFailed = Topic("main:OperationFailed") ) ctx, done := context.WithCancel(context.Background()) bus := NewBus(ctx) ch1 := bus.Subscribe(ETMainSaidHello) ch2 := bus.Subscribe(ETMainSaidHello) ch3 := bus.Subscribe(ETMainSaidHello) go bus.Publish(ETMainSaidHello, "hello") tasks := 3 for { select { case d := <-ch1: fmt.Println(d.Payload) case d := <-ch2: fmt.Println(d.Payload) case d := <-ch3: fmt.Println(d.Payload) } tasks-- if tasks == 0 { break } } opCh := bus.SubscribeOnce(ETMainOpSucceeded, ETMainOpFailed) go bus.Publish(ETMainOpFailed, fmt.Errorf("it didn't work?")) event := <-opCh fmt.Println(event.Payload) done()
Output: hello hello hello it didn't work?
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ETFSICreateLinkEvent type for when FSI creates a link between a dataset and working directory ETFSICreateLinkEvent = Topic("fsi:createLinkEvent") )
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus interface { // Publish an event to the bus Publish(t Topic, data interface{}) // Subscribe to one or more topics Subscribe(topics ...Topic) <-chan Event // Unsubscribe cleans up a channel that no longer need to receive events Unsubscribe(<-chan Event) // SubscribeOnce to one or more topics. the returned channel will only fire // once, when the first event that matches any of the given topics // the common use case for multiple subscriptions is subscribing to both // success and error events SubscribeOnce(types ...Topic) <-chan Event // NumSubscriptions returns the number of subscribers to the bus's events NumSubscribers() int }
Bus is a central coordination point for event publication and subscription zero or more subscribers register topics to be notified of, a publisher writes a topic event to the bus, which broadcasts to all subscribers of that topic
type FSICreateLinkEvent ¶
FSICreateLinkEvent describes an FSI created link
type NilPublisher ¶ added in v0.9.6
type NilPublisher struct { }
NilPublisher replaces a nil value, does nothing
func (*NilPublisher) Publish ¶ added in v0.9.6
func (n *NilPublisher) Publish(t Topic, data interface{})
Publish does nothing with the event
Click to show internal directories.
Click to hide internal directories.