emission

package
v0.0.0-...-ac9eb09 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxListeners = 10

Default number of maximum listeners for an event.

Variables

View Source
var ErrNoneFunction = errors.New("Kind of Value for listener is not Func.")

Error presented when an invalid argument is provided as a listener function

Functions

This section is empty.

Types

type Emitter

type Emitter struct {
	// Mutex to prevent race conditions within the Emitter.
	*sync.Mutex
	// contains filtered or unexported fields
}

Emitter ...

func NewEmitter

func NewEmitter() (emitter *Emitter)

NewEmitter returns a new Emitter object, defaulting the number of maximum listeners per event to the DefaultMaxListeners constant and initializing its events map.

func (*Emitter) AddListener

func (emitter *Emitter) AddListener(event, listener interface{}) *Emitter

AddListener appends the listener argument to the event arguments slice in the Emitter's events map. If the number of listeners for an event is greater than the Emitter's maximum listeners then a warning is printed. If the relect Value of the listener does not have a Kind of Func then AddListener panics. If a RecoveryListener has been set then it is called recovering from the panic.

func (*Emitter) Emit

func (emitter *Emitter) Emit(event interface{}, arguments ...interface{}) *Emitter

Emit attempts to use the reflect package to Call each listener stored in the Emitter's events map with the supplied arguments. Each listener is called within its own go routine. The reflect package will panic if the agruments supplied do not align the parameters of a listener function. If a RecoveryListener has been set then it is called after recovering from the panic.

func (*Emitter) GetListenerCount

func (emitter *Emitter) GetListenerCount(event interface{}) (count int)

GetListenerCount gets count of listeners for a given event.

func (*Emitter) Off

func (emitter *Emitter) Off(event, listener interface{}) *Emitter

Off is an alias for RemoveListener.

func (*Emitter) On

func (emitter *Emitter) On(event, listener interface{}) *Emitter

On is an alias for AddListener.

func (*Emitter) Once

func (emitter *Emitter) Once(event, listener interface{}) *Emitter

Once generates a new function which invokes the supplied listener only once before removing itself from the event's listener slice in the Emitter's events map. If the reflect Value of the listener does not have a Kind of Func then Once panics. If a RecoveryListener has been set then it is called after recovering from the panic.

func (*Emitter) RecoverWith

func (emitter *Emitter) RecoverWith(listener RecoveryListener) *Emitter

RecoverWith sets the listener to call when a panic occurs, recovering from panics and attempting to keep the application from crashing.

func (*Emitter) RemoveListener

func (emitter *Emitter) RemoveListener(event, listener interface{}) *Emitter

RemoveListener removes the listener argument from the event arguments slice in the Emitter's events map. If the reflect Value of the listener does not have a Kind of Func then RemoveListener panics. If a RecoveryListener has been set then it is called after recovering from the panic.

func (*Emitter) SetMaxListeners

func (emitter *Emitter) SetMaxListeners(max int) *Emitter

SetMaxListeners sets the maximum number of listeners per event for the Emitter. If -1 is passed as the maximum, all events may have unlimited listeners. By default, each event can have a maximum number of 10 listeners which is useful for finding memory leaks.

type RecoveryListener

type RecoveryListener func(interface{}, interface{}, error)

RecoveryListener ...

Jump to

Keyboard shortcuts

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