Emitter
Event emitter.
This class allows to register listeners that will get notified by the emitter.
Usage example:
You can create your own emitters:
You can notify listeners in to your emitter using notify:
.constructor<T>() ⇒ Emitter
Template Param | Type Definition |
---|---|
T | extends unknown[] |
.isEmpty: boolean
true
if it has no listeners, false
otherwise.
.listenerCount: number
Number of listeners.
Note: Doesn’t account for pending listeners, i.e., listeners added / removed during a notification.
.add(listener: ListenerCallback, opts: Partial<ListenerOptions>) ⇒ Emitter
Register a new listener to be triggered on notify.
Basic usage:
Automatically remove the listener when an event is received:
Returns: Reference to self (for method chaining)
Param | Type | Description |
---|---|---|
listener | ListenerCallback | The callback to register. |
opts | Partial<ListenerOptions> | The listener options. For more information, please have a look at the ListenerOptions interface. |
.has(listener: any) ⇒ boolean
Check whether the listener is registered.
Note: This method performs a linear search.
Note: Doesn’t account for pending listeners, i.e., listeners added / removed during a notification.
Returns: true
if the handle is found, false
otherwise.
Param | Type | Description |
---|---|---|
listener | any | The registered callback or a value representing the id . |
.notify(…data: T[]) ⇒ void
Notify listeners with the given data object.
Note: This method ensures all listeners are called even if an exception is thrown. For (possibly) faster notification, please use notifyUnsafe.
Param | Type | Description |
---|---|---|
data | T | The data to pass to listener when invoked. |
.notifyUnsafe(…data: T[]) ⇒ void
Notify listeners with the given data object.
Note: Because this method doesn’t catch exceptions, some listeners will be skipped on a throw. Please use notify for safe notification.
Param | Type | Description |
---|---|---|
data | T | The data to pass to listener when invoked. |
.once(listener: ListenerCallback) ⇒ Emitter
Register a new listener to be triggered on notify.
Once notified, the listener will be automatically removed.
The method is equivalent to calling add with:
1emitter.add(listener, {once: true});
Returns: Reference to self (for method chaining).
Param | Type | Description |
---|---|---|
listener | ListenerCallback | The callback to register. |
.promise() ⇒ Promise
Return a promise that will resolve on the next event.
Note: The promise might never resolve if no event is sent.
Returns: A promise that resolves with the data passed to notify.
.push(…listeners: ListenerCallback[][]) ⇒ Emitter
Equivalent to add.
Returns: Reference to self (for method chaining).
Deprecated: Please use add instead.
Param | Type | Description |
---|---|---|
listeners | ListenerCallback[] | The callback(s) to register. |
.remove(listener: any) ⇒ Emitter
Remove a registered listener.
Usage with a callback:
Usage with an id:
Using identifiers, you will need to ensure your value is unique to avoid removing listeners from other libraries, e.g.,:
The identifier can be any type. However, remember that the comparison will be by-value for primitive types (string, number), but by reference for objects.
Example:
Here, both emitters have id {value: 42}
, but the comparison is made by reference. Thus,
the remove()
call has no effect. We can make it work by doing:
Returns: Reference to self (for method chaining)
Param | Type | Description |
---|---|---|
listener | any | The registered callback or a value representing the id . |