Simple asynchronous, content-based event bus for Go.
GoBus provides a straightforward implementation for an Event Bus.
Start using the Event Bus this way:
bus := gobus.NewEventBus() // Un-buffered channel
bus := gobus.NewEventBusBuffered(chanSize) // Buffered channel
defer bus.Destruct()
GoBus can use a buffered and an un-buffered channel to dispatch events as they arrive.
Always remember to call bus.Destruct()
at the end of the Event Bus usage, as it's needed for
cleanup purposes (closing channels, returning asynchronous goroutines, ...).
You can subscribe (and unsubscribe) one or more listeners to the Event Bus like this:
func useString(event string) {
// Do something
}
bus.Subscribe(useString)
bus.UnSubscribe(useString)
// Method chaining
bus.Subscribe(function1).Subscribe(function2).Subscribe(function3).
UnSubscribe(function2).UnSubscribe(function3)
// Variadic arguments
bus.Subscribe(function1, function2, function3)
bus.UnSubscribe(function1, function3, function2)
// Having fun :-)
bus.Subscribe(function1, function2, function3).
UnSubscribe(function1, function2, function3)
Listeners must be unary procedures, functions with one input argument and no return arguments.
Listeners are grouped together by their input argument types (meaning that publishing a string will call every string listeners registered to the bus).
You can publish events to the Event Bus this way:
bus.Publish("HelloWorld!")
// Method chaining
bus.Publish("HelloWorld!").Publish(12)
Events are pushed to a dispatcher channel which will asynchronously calls all the listeners registered to the event type.
Being asynchronous through goroutines, there are no guarantees on the listeners calling order.
Biggest contribution towards this library is to use it and give us feedback for further improvements and additions.
For direct contributions, branch of from master and do pull request.
This library is distributed under the MIT License found in the LICENSE file.
Written by Danilo Cianfrone.