Emitter
An Emitter
can dispatch events by name to registered listeners. The extension API defines several built-in emitters that are used to dispatch events, and extensions may also create their own emitters to use for event handling.
Emitter objects conform to the Disposable interface, allowing all event listeners to be removed when the emitter is disposed.
The Emitter
class is not subclassable.
let emitter = new Emitter();
emitter.on("myEvent", function(arg1, arg2, arg3) {
console.log(arg1, arg2, arg3);
});
function doTask() {
emitter.emit("myEvent", "foo", "bar", 12);
}
doTask();
// Logs to console: "foo", "bar", 12
Class Methods
constructor()
Creates a new Emitter object that may be used to register and emit events.
Methods
on(eventName, callback)
Adds a listener for the provided event name after any other current listeners. The callback
argument will be called each time the emitter receives a matching event.
once(eventName, callback)
Adds a listener for the provided event name after any other current listeners. The callback
argument will be called the next time the emitter receives a matching event, after which it will be unregistered.
preempt(eventName, callback)
Adds a listener for the provided event name before any other current listeners. The callback
argument will be called each time the emitter receives a matching event.
emit(eventName, [arguments…])
Emits a new event with the provided name, optionally including any other provided arguments to the event handler callbacks.
clear([eventName])
Removes all registered listeners for the provided event name, or all listeners if no event name is provided.