Skip to main content

Event

API for creating event dispatchers, binding listeners to those dispatchers, and dispatching events to those bound listeners.

It's safe to expose this API to addons, but you should avoid exposing the dispatcher objects themselves. The dispatcher is a regular table containing all bound event listeners, and while the structure of that table (i.e. the supported events) is immutable, the individual lists of callbacks for each event are not!

Instead, you should create wrappers for adding and removing listeners. For example:

def addEventListenerForAddons = fn(eventName, arg1, arg2)
  return Event.addListener(myDispatcher, eventName, arg1, arg2)
end

Functions

Event.createDispatcher

Creates a new dispatcher with the given supported events. Calling any of the functions taking a dispatcher and event name will raise an error if the given event is not in this list.

Event.createDispatcher(eventNames: string[]): Dispatcher

Parameters

NameTypeDescription
eventNamesstring[]List of supported event names.

Returns

NameTypeDescription
dispatcherDispatcherNo description

Event.dispatch

Dispatch the given event to all listeners bound to it.

Event.dispatch(dispatcher: Dispatcher, eventName: string, ...: ...any)

Parameters

NameTypeDescription
dispatcherDispatcherDispatcher to trigger the event on.
eventNamestringEvent to dispatch. Must be supported by the dispatcher.
......anyAny additional arguments to pass to the listeners.

Event.addListener

Bind a callback to listen to a dispatcher's event.

Event.addListener(dispatcher: Dispatcher, eventName: string, callback: fun(...: ...any)): function

Parameters

NameTypeDescription
dispatcherDispatcherDispatcher to listen to.
eventNamestringEvent to listen to. Must be supported by the dispatcher.
callbackfun(...: ...any)Called when the event is fired with arguments passed by Event.dispatch.

Returns

NameTypeDescription
callbackfunctionCallback function that was passed in (for convenience to remove the listener later)

Event.removeListener

Remove a callback that was bound to a dispatcher's event.

Event.removeListener(dispatcher: Dispatcher, eventName: string, callback: function)

Parameters

NameTypeDescription
dispatcherDispatcherDispatcher to remove the listener from.
eventNamestringEvent to remove the listener from. Must be supported by the dispatcher.
callbackfunctionCallback to remove.

Types

Dispatcher

Table containing all event listeners bound to the dispatcher. Note that the dispatcher itself is immutable, however the lists of callbacks on each event name are not.

type Dispatcher = { .listeners: table }

Properties

FieldTypeDescription
listenerstableArrays of callbacks keyed off of the event names supported by the dispatcher.