Emitter - truemedian/Discordia GitHub Wiki

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.

Methods

on
once
onSync
onceSync
emit
getListeners
getListenerCount
removeListener
removeAllListeners
waitFor

Methods

:on( name, fn )

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.

Name Type
name string
fn function

Returns: function

:once( name, fn )

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.

Name Type
name string
fn function

Returns: function

:onSync( name, fn )

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.

Name Type
name string
fn function

Returns: function

:onceSync( name, fn )

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.

Name Type
name string
fn function

Returns: function

:emit( name, [ ... ] )

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

Name Type Optional
name string
... * x

Returns: nil

:getListeners( name )

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

Name Type
name string

Returns: function

:getListenerCount( name )

Returns the number of callbacks registered to the named event.

Name Type
name string

Returns: number

:removeListener( name )

Unregisters all instances of the callback from the named event.

Name Type
name string

Returns: nil

:removeAllListeners( name )

Unregisters all callbacks from the named event.

Name Type
name string

Returns: nil

:waitFor( name, [ timeout ], [ predicate ] )

When called inside of a coroutine, this will yield the coroutine until the specific named event is emitted or until a timeout (in milliseconds) expires. If the coroutine is resumed by the event, then true is returned with any event arguments. If the coroutine is resumed by the timeout's expiration, then false is returned without any other arguments.

Name Type Optional Optional
name string
timeout number x
predicate function x

Returns: boolean, ...

⚠️ **GitHub.com Fallback** ⚠️