Skip to content
SinisterRectus edited this page Dec 5, 2019 · 13 revisions

Implements an asynchronous event emitter where callbacks can be subscribed to specific named events. When events are emitted, the callbacks are called in the order that they were originally registered.

Constructor

Emitter()

Methods

emit(name, ...)

Parameter Type Optional
name string
... *

Emits the named event and a variable number of arguments to pass to the event callbacks.

This method only operates on data in memory.

Returns: nil


getListenerCount(name)

Parameter Type
name string

Returns the number of callbacks registered to the named event.

This method only operates on data in memory.

Returns: number


getListeners(name)

Parameter Type
name string

Returns an iterator for all callbacks registered to the named event.

This method only operates on data in memory.

Returns: function


on(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called every time the named event is emitted. Callbacks registered with this method will automatically be wrapped as a new coroutine when they are called. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


onSync(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called every time the named event is emitted. Callbacks registered with this method are not automatically wrapped as a coroutine. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


once(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called only the first time this event is emitted. Callbacks registered with this method will automatically be wrapped as a new coroutine when they are called. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


onceSync(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called only the first time this event is emitted. Callbacks registered with this method are not automatically wrapped as a coroutine. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


removeAllListeners(name)

Parameter Type
name string/nil

Unregisters all callbacks for the emitter. If a name is passed, then only callbacks for that specific event are unregistered.

This method only operates on data in memory.

Returns: nil


removeListener(name, fn)

Parameter Type
name string
fn function

Unregisters all instances of the callback from the named event.

This method only operates on data in memory.

Returns: nil


waitFor(name, timeout, predicate)

Parameter Type Optional
name string
timeout number
predicate function

When called inside of a coroutine, this will yield the coroutine until the named event is emitted. If a timeout (in milliseconds) is provided, the function will return after the time expires, regardless of whether the event is emitted, and false will be returned; otherwise, true is returned. If a predicate is provided, events that do not pass the predicate will be ignored.

This method only operates on data in memory.

Returns: boolean, ...


Clone this wiki locally