Documentation ¶
Overview ¶
Package events provides methods and structs for creating event-driven systems
Index ¶
- Variables
- type Dispatcher
- func (d *Dispatcher) AddHandler(typ string, handler Handler) error
- func (d *Dispatcher) Dispatch(typ string, payload any) error
- func (d *Dispatcher) DispatchAndWait(typ string, payload any) error
- func (d *Dispatcher) HasHandler(typ string, handler Handler) bool
- func (d *Dispatcher) RemoveHandler(typ string, handler Handler) error
- type Handler
- type Handlers
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher is event dispatcher
func NewDispatcher ¶
func NewDispatcher() *Dispatcher
NewDispatcher creates new event dispatcher instance
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) d.Dispatch("myEvent", "Hello!")
Output:
func (*Dispatcher) AddHandler ¶
func (d *Dispatcher) AddHandler(typ string, handler Handler) error
AddHandler registers handler for events with given event type
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) d.AddHandler("unknownEvent", testHandler) err := d.Dispatch("myEvent", "Hello!") fmt.Println(err) // nil
Output:
func (*Dispatcher) Dispatch ¶
func (d *Dispatcher) Dispatch(typ string, payload any) error
Dispatch dispatches event with given type and payload
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) err := d.Dispatch("myEvent", "Hello!") fmt.Println(err) // nil
Output:
func (*Dispatcher) DispatchAndWait ¶
func (d *Dispatcher) DispatchAndWait(typ string, payload any) error
DispatchAndWait dispatches event with given type and payload and waits until all handlers will be executed
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) err := d.DispatchAndWait("myEvent", "Hello!") fmt.Println(err) // nil
Output:
func (*Dispatcher) HasHandler ¶
func (d *Dispatcher) HasHandler(typ string, handler Handler) bool
HasHandler returns true if given handler is registered for given event type
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) fmt.Println("myEvent:", d.HasHandler("myEvent", testHandler)) fmt.Println("unknownEvent:", d.HasHandler("unknownEvent", testHandler))
Output: myEvent: true unknownEvent: false
func (*Dispatcher) RemoveHandler ¶
func (d *Dispatcher) RemoveHandler(typ string, handler Handler) error
RemoveHandler removes handler for given event type
Example ¶
d := NewDispatcher() d.AddHandler("myEvent", testHandler) d.RemoveHandler("myEvent", testHandler) err := d.Dispatch("myEvent", "Hello!") fmt.Println(err) // not nil
Output:
Click to show internal directories.
Click to hide internal directories.