EventEmitter
Events are how UnderScript tells your code that something happened, such as a page finishing loading or a chat message arriving. Plugins get one through plugin.events, and underscript.utils.eventEmitter() creates a new one for your own use.
Methods
- on(event, listener)
- Call
listenerevery timeeventhappens. Several events can be listened to at once by separating them with a space. - once(event, listener)
- Call
listenerthe next timeeventhappens, then stop.one()does the same thing. - until(event, listener)
- Call
listenerevery timeeventhappens, until it returnstrue. - off(event, listener)
- Stop calling
listener. - emit(event, …data)
- Tell everyone listening to
event, passingdataalong. Returns{ ran, canceled }.
Listener
The listener is called with the event’s data. this is an object describing the event:
{
event, // name of the event
cancelable, // true if listeners may cancel it
canceled, // true once something canceled it, set it to cancel
async, // true if listeners may return promises
delayed, // true if the event already happened before you listened
singleton, // true if the event only happens once
}Emit options
Options are chained before emit:
// Listeners can cancel this event
const { canceled } = events.emit.cancelable('myevent', data);
// Listeners can return promises to wait for
await events.emit.async('myevent', data);
// Chain them together
await events.emit.cancelable.async('myevent', data);- cancelable
- Listeners can set
this.canceled = true. The result ofemittells you if they did. - canceled
- Start the event off as canceled. Listeners can change it back.
- async
emitreturns a promise that waits for every listener that returned one.- singleton
- The event only happens once. Anyone who starts listening later still gets called, with the original data.
Examples
const events = underscript.utils.eventEmitter();
events.on('hello', function listener(name) {
console.log(`Hello ${name}`, this.event); // Hello world hello
});
events.emit('hello', 'world');Read the list of events UnderScript emits.