gobus

package module
v0.0.0-...-85118ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2016 License: MIT Imports: 5 Imported by: 0

README

GoBus Build Status

Simple asynchronous, content-based event bus for Go.

Usage

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, ...).

(Un)Subscription

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).

Publishing

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.

Contributing

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.

License

This library is distributed under the MIT License found in the LICENSE file.

Written by Danilo Cianfrone.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewEventBusBuffered(chanSize int) *EventBus

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

func (bus *EventBus) Publish(event interface{}) *EventBus

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

func (bus *EventBus) Subscribe(listeners ...interface{}) *EventBus

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

func (bus *EventBus) UnSubscribe(listeners ...interface{}) *EventBus

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL