Documentation ¶
Overview ¶
Copy from https://github.com/chuckpreslar/emission/blob/7d2aae804ca2142c874751cba8f35425372c6ff4/emitter.go Package emission provides an event emitter.
Index ¶
- Constants
- Variables
- func DefaultGenerateConnid(r *http.Request) string
- type Context
- func (ctx *Context) Emit(key string, data []byte, cb ...Handle) error
- func (ctx *Context) Error() error
- func (ctx *Context) GetConnid() string
- func (ctx *Context) GetData() []byte
- func (ctx *Context) GetHttpRequest() *http.Request
- func (ctx *Context) GetKey() string
- func (ctx *Context) GetPlugin(name string) Plugin
- func (ctx *Context) IsError() bool
- func (ctx *Context) LongtimeEmit(key string, data []byte, cb ...Handle) error
- func (ctx *Context) Write(data []byte)
- type Emitter
- func (emitter *Emitter) AddListener(event, listener interface{}) ListenerHandle
- func (emitter *Emitter) Emit(event interface{}, arguments ...interface{}) *Emitter
- func (emitter *Emitter) EmitSync(event interface{}, arguments ...interface{}) *Emitter
- func (emitter *Emitter) GetListenerCount(event interface{}) (count int)
- func (emitter *Emitter) Off(event, listener ListenerHandle)
- func (emitter *Emitter) On(event, listener interface{}) ListenerHandle
- func (emitter *Emitter) Once(event, listener interface{}) ListenerHandle
- func (emitter *Emitter) RecoverWith(listener RecoveryListener) *Emitter
- func (emitter *Emitter) RemoveListener(event interface{}, listenerHandle ListenerHandle)
- type Enzo
- type Handle
- type ListenerHandle
- type Plugin
- type RecoveryListener
Constants ¶
Variables ¶
var ErrNoneFunction = errors.New("kind of Value for listener is not function")
Error presented when an invalid argument is provided as a listener function
Functions ¶
func DefaultGenerateConnid ¶
Types ¶
type Emitter ¶
type Emitter struct { // Mutex to prevent race conditions within the Emitter. *sync.Mutex // contains filtered or unexported fields }
Emitter ...
func (*Emitter) AddListener ¶
func (emitter *Emitter) AddListener(event, listener interface{}) ListenerHandle
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 reflect 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 ¶
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) EmitSync ¶
EmitSync 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 synchronously. 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 ¶
GetListenerCount gets count of listeners for a given event.
func (*Emitter) Off ¶
func (emitter *Emitter) Off(event, listener ListenerHandle)
Off is an alias for RemoveListener.
func (*Emitter) On ¶
func (emitter *Emitter) On(event, listener interface{}) ListenerHandle
On is an alias for AddListener.
func (*Emitter) Once ¶
func (emitter *Emitter) Once(event, listener interface{}) ListenerHandle
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 interface{}, listenerHandle ListenerHandle)
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.
type Enzo ¶
type ListenerHandle ¶
type ListenerHandle uint32
ListenerHandle is an opaque reference to a previously added listener. You need the handle to remove the listener.
type RecoveryListener ¶
type RecoveryListener func(interface{}, interface{}, error)
RecoveryListener ...