Documentation
¶
Overview ¶
Package eventdispatcher contains a set of tools making up a simple and reliable event dispatcher
Package eventdispatcher contains a set of tools making up a simple and reliable event dispatcher
Index ¶
- Constants
- type Dispatcher
- type DispatcherAware
- type Event
- type EventDispatcher
- func (d *EventDispatcher) Dispatch(e Event) Event
- func (d *EventDispatcher) HasListeners(n string) bool
- func (d *EventDispatcher) Off(n string, l Listener)
- func (d *EventDispatcher) OffAll(n string)
- func (d *EventDispatcher) On(n string, l Listener)
- func (d *EventDispatcher) Once(n string, l Listener)
- type Listener
- type ParamsEvent
- func (event *ParamsEvent) GetParam(k string) (value interface{}, ok bool)
- func (event *ParamsEvent) HasParam(k string) bool
- func (event *ParamsEvent) IsPropagationStopped() bool
- func (event *ParamsEvent) Name() string
- func (event *ParamsEvent) RemoveParam(k string) *ParamsEvent
- func (event *ParamsEvent) SetParam(k string, v interface{}) *ParamsEvent
- func (event *ParamsEvent) StopPropagation()
Constants ¶
const (
DefaultDispatcherKey = "event_dispatcher"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher interface {
// Dispatch dispatches the event and returns it after all listeners do
// their jobs.
Dispatch(e Event) Event
// On registers a listener for given event name.
On(n string, l Listener)
// Once registers a listener to be executed only once. The first param
// n is the name of the event the listener will listen on, second is
// the Listener type function.
Once(n string, l Listener)
// Off removes the registered event listener for given event name.
Off(n string, l Listener)
// RemoveAll removes all listeners for given name.
OffAll(n string)
// HasListeners returns true if any listener for given event name has
// been assigned and false otherwise. This applies also to once triggered
// listeners registered with `One` method
HasListeners(n string) bool
}
Dispatcher interface defines the event dispatcher behavior
type DispatcherAware ¶
type DispatcherAware interface {
// Dispatcher provides the event dispatcher instance pointer
Dispatcher() Dispatcher
}
Forces the instance to be aware of event dispatcher
type Event ¶
type Event interface {
// Returns the event name
Name() string
// IsPropagationStopped informs weather the event should
// be further propagated or not
IsPropagationStopped() bool
// StopPropagation makes the event no longer
// propagate.
StopPropagation()
}
Event is an interface used by event dispatcher. Contains name and more custom data May be forced to stop being propagated
type EventDispatcher ¶
The EventDispatcher type is the default implementation of the DispatcherInterface
func GetDispatcher ¶
func GetDispatcher(k interface{}) *EventDispatcher
GetDispatcher provides event dispatcher for given key string. If the key string is nil, takes the default key
func NewDispatcher ¶
func NewDispatcher() *EventDispatcher
NewDispatcher creates a new instance of event dispatcher
func (*EventDispatcher) Dispatch ¶
func (d *EventDispatcher) Dispatch(e Event) Event
Dispatch dispatches the event and returns it after all listeners do their jobs
func (*EventDispatcher) HasListeners ¶
func (d *EventDispatcher) HasListeners(n string) bool
HasListeners returns true if any listener for given event name has been assigned and false otherwise. This applies also to once triggered listeners registered with `One` method
func (*EventDispatcher) Off ¶
func (d *EventDispatcher) Off(n string, l Listener)
Off removes the registered event listener for given event name.
func (*EventDispatcher) OffAll ¶
func (d *EventDispatcher) OffAll(n string)
RemoveAll removes all listeners for given name.
func (*EventDispatcher) On ¶
func (d *EventDispatcher) On(n string, l Listener)
On registers a listener for given event name.
func (*EventDispatcher) Once ¶
func (d *EventDispatcher) Once(n string, l Listener)
Once registers a listener to be executed only once. The first param n is the name of the event the listener will listen on, second is the Listener type function.
type ParamsEvent ¶
type ParamsEvent struct {
// contains filtered or unexported fields
}
ParamsEvent is the default implementation of Event interface. Contains additional string parameters
func NewParamsEvent ¶
func NewParamsEvent(n string) *ParamsEvent
NewParamsEvent is a factory for creating a basic event
func (*ParamsEvent) GetParam ¶
func (event *ParamsEvent) GetParam(k string) (value interface{}, ok bool)
GetParam returns a parameter value for given key. If the param does not exist, returns an empty string. Second value returned contains boolean value that says if the param existed.
func (*ParamsEvent) HasParam ¶
func (event *ParamsEvent) HasParam(k string) bool
HasParam defines if a param with given key exists. Returns a boolean value
func (*ParamsEvent) IsPropagationStopped ¶
func (event *ParamsEvent) IsPropagationStopped() bool
IsPropagationStopped informs weather the event should be further propagated or not
func (*ParamsEvent) Name ¶
func (event *ParamsEvent) Name() string
Name returns the name of the event
func (*ParamsEvent) RemoveParam ¶
func (event *ParamsEvent) RemoveParam(k string) *ParamsEvent
RemoveParam deletes a param with given key. Does nothing, if the param does not exst. Returns this event instance
func (*ParamsEvent) SetParam ¶
func (event *ParamsEvent) SetParam(k string, v interface{}) *ParamsEvent
AddParam registers a parameter for the event. Returns this event instance
func (*ParamsEvent) StopPropagation ¶
func (event *ParamsEvent) StopPropagation()
StopPropagation sets a flag that make the event no longer propagate.