new EventEmitter() (constructor)
.on(name, callback, [scope], [once]) ⇒ EventHandler
.once(name, callback, [scope]) ⇒ EventHandler
.emit(name, [...args])
.off([name], [callback], [scope])
Provides ability to subscribe and emit events in sync manner. Each subscribtion (on, once) returns EventHandler that simplifies callbacks management.
Example
const world = new EventEmitter();
world.on('event', function (number) {
console.log('event', number);
});
world.emit('event', 42);
.on(name, callback, [scope], [once]) ⇒ EventHandler
Attach an event handler.
Returns: EventHandler
- Object that can be used to manage the event.
Param | Type | Description |
---|---|---|
name | string |
Name of the event to bind the callback to. |
callback | function |
Function that is called when event is emitted. |
[scope] | object |
Object to use as 'this' when the event is emitted, defaults to current this. |
[once] | boolean |
Boolean to indicate if this event should emit only once. Defaults to false. |
Example
obj.on('event', function (a, b) {
console.log(a + b);
});
obj.emit('event', 4, 2);
.once(name, callback, [scope]) ⇒ EventHandler
Attach an event handler which will emit only once.
Returns: EventHandler
- Object that can be used to manage the event.
Param | Type | Description |
---|---|---|
name | string |
Name of the event to bind the callback to. |
callback | function |
Function that is called when event is emitted. |
[scope] | object |
Object to use as 'this' when the event is emitted, defaults to current this. |
Example
obj.once('event', function (a) {
console.log(a);
});
obj.emit('event', 4);
obj.emit('event', 2); // will not trigger
Emit the event by name and optional list of arguments.
Param | Type | Description |
---|---|---|
name | string |
Name of the event to bind the callback to. |
[...args] | * |
Arguments to be passed to event callbacks. |
Example
obj.emit('event', 'hello', 42);
Remove event handlers based on provided arguments.
Param | Type | Description |
---|---|---|
[name] | string |
Name of the events to remove. If not specified all events will be removed. |
[callback] | function |
Function that is used as callback. If not defined, then all events of specified name will be removed. |
[scope] | object |
Object that is used as a scope for event handlers. If not defined, then all events with matching name and callback function will be removed. |
Example
obj.off(); // removes all events
obj.off('event'); // removes all events named `event`.
obj.off(/input:\w+/); // removes all events with name matching regular expression
obj.off('event', fn); // removes events named `event` with `fn`
obj.off('event', fn, obj); // removes events named `event` with `fn` callback and `obj` as a scope.
new EventHandler() (constructor)
.emit([...args])
.off()
Constructed by EventEmitter and provides easy ability to manage event.
Emit the event with optional list of arguments.
Param | Type | Description |
---|---|---|
[...args] | * |
Arguments to be passed to event callbacks. |
Example
evt.emit(42, 'hello');
Removes event from EventEmitter.
Example
evt.off();