Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus interface { Dispatch(msg Msg) error AddHandler(handler HandlerFunc) error AddEventListener(handler HandlerFunc) Publish(msg Msg) error }
It is a simple but powerful publish-subscribe event system. It requires object to register themselves with the event bus to receive events.
type ErrBadFuncError ¶
type ErrBadFuncError string
ErrBadFuncError is raised via panic() when AddEventListener or AddHandler is called with an invalid listener function.
func (ErrBadFuncError) Error ¶
func (bhf ErrBadFuncError) Error() string
type ErrHandlerNotFound ¶
type ErrHandlerNotFound struct {
Name string
}
func (*ErrHandlerNotFound) Error ¶
func (e *ErrHandlerNotFound) Error() string
type ErrOverwrite ¶
type ErrOverwrite struct {
Name string
}
func (*ErrOverwrite) Error ¶
func (e *ErrOverwrite) Error() string
type HandlerFunc ¶
type HandlerFunc interface{}
The type of the function's first and only argument. declares the msg to listen for.
type InProcBus ¶
func (*InProcBus) AddEventListener ¶
func (b *InProcBus) AddEventListener(handler HandlerFunc)
AddListener registers a listener function that will be called when a matching msg is dispatched.
Example ¶
Creates a bus and adds a listener to a message afterward it publishes a new message.
package main import ( "fmt" "github.com/donutloop/toolkit/bus" ) func main() { type msg struct { ID int64 body string } b := bus.New() b.AddEventListener(func(m *msg) error { fmt.Println("db insert listener id", m.ID) return nil }) m := new(msg) m.ID = 1 m.body = "test" if err := b.Publish(m); err != nil { fmt.Printf("error: (%v) \n", err) } }
Output: db insert listener id 1
func (*InProcBus) AddHandler ¶
func (b *InProcBus) AddHandler(handler HandlerFunc) error
AddHandler registers a handler function that will be called when a matching msg is dispatched.
Example ¶
Creates a bus and adds a handler for a message afterward it dispatch a new message.
package main import ( "fmt" "github.com/donutloop/toolkit/bus" ) func main() { type msg struct { ID int64 body string } b := bus.New() err := b.AddHandler(func(m *msg) error { fmt.Println("db insert listener id", m.ID) return nil }) if err != nil { fmt.Printf("error: (%v) \n", err) return } m := new(msg) m.ID = 1 m.body = "test" if err := b.Dispatch(m); err != nil { fmt.Printf("error: (%v) \n", err) } }
Output: db insert listener id 1