eventdispatcher

package module
v0.0.0-...-8ad2a3c Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 3 Imported by: 0

README

GO event dispatcher

Go Language Event dispatcher

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

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

type EventDispatcher struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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 Listener

type Listener func(Event)

Listener type for defining functions as listeners

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.

Jump to

Keyboard shortcuts

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