Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ListenersNotFoundErr = errors.New("Cannot found listeners, check for unhandled event subscriptions") ListenerInvalidErr = errors.New("Invalid listener passed through, must be unary function with no return argument") )
Functions ¶
This section is empty.
Types ¶
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus, of course
func NewEventBus ¶
func NewEventBus() *EventBus
Factory method for EventBus objects. Creates a new EventBus with a dispatcher un-buffered channel.
func NewEventBusBuffered ¶
Factory method for EventBus objects. Creates a new EventBus with a dispatcher buffered channel.
func (*EventBus) Destruct ¶
func (bus *EventBus) Destruct()
Closes the EventBus, waiting for all the goroutines to complete and signals the poller goroutine to quit. Should be deferred after the factory call:
func main() { bus := gobus.NewEventBus() defer bus.Destruct() ... }
func (*EventBus) Publish ¶
Publish an event to EventBus. The event bus notifies the poller goroutine, which will retrieve the correct subscribed listeners and calls them with a copy of the event published.
func (*EventBus) Subscribe ¶
Subscribe a listener to certain events. The listener must be an unary function with no return arguments (a.k.a. procedure). Uses variadic arguments and chaining methods pattern for great expressiveness.
func (*EventBus) UnSubscribe ¶
Unsubscribe a listener from the event bus. Uses variadic arguments and chaining methods pattern for great expressiveness.
type IListenerSet ¶
type IListenerSet interface { Add(listener interface{}) IListenerSet Remove(listener interface{}) IListenerSet Values() []interface{} Empty() bool }
Interface for listener set.
type ListenerSet ¶
type ListenerSet struct {
// contains filtered or unexported fields
}
ListenerSet is a struct that uses an interface{} slice to implement a listeners set (IListenerSet interface).
func (*ListenerSet) Add ¶
func (set *ListenerSet) Add(listener interface{}) IListenerSet
Add a new listener into the ListenerSet only if it's actually a new listener. Returns a ListenerSet pointer for method chaining pattern.
func (*ListenerSet) Empty ¶
func (set *ListenerSet) Empty() bool
Checks if the ListenerSet is empty.
func (*ListenerSet) Remove ¶
func (set *ListenerSet) Remove(listener interface{}) IListenerSet
Remove a listener from the ListenerSet using a new slice of listeners. Returns a ListenerSet pointer for method chaining pattern.
N.B. the append() method could be really performance bad...
func (*ListenerSet) Values ¶
func (set *ListenerSet) Values() []interface{}
Returns all the values of the ListenerSet.
type Subscription ¶
type Subscription map[string]IListenerSet
Map that holds all listener references, indexed through input argument name. Uses an IListenerSet interface as return type, for optimization purposes. Example of subscriptions map:
map |--> string (built-in) | |--> printString1(str string), printString2(str string) | |--> Struct1 (user-defined) |--> printStruct1(s1 Struct1), doSomethingStruct1(s1 Struct1)
func (*Subscription) AddListener ¶
func (s *Subscription) AddListener(listener interface{}) *Subscription
Adds a new listener to the IListenerSet into the Subscription map.
func (*Subscription) GetListeners ¶
func (s *Subscription) GetListeners(typ reflect.Type) (IListenerSet, error)
Returns all the listeners associated to the event type typ, or returns a ListenersNotFoundErr.
func (*Subscription) RemoveListener ¶
func (s *Subscription) RemoveListener(listener interface{}) *Subscription
Remove a listener from the IListenerSet into the Subscription map.