RetainEmitter
RetainEmitter
Event emitter that retains event data when notified.
After a notification happens, subsequent calls to add will get automatically notified.
You can use another emitter in order to cancel the last retained event:
You can reset the state of the emitter, i.e., making it forget about the last event using:
|
|
For more information about emitters, please have a look at the base Emitter class.
- RetainEmitter
- .constructor<T>() ⇒ RetainEmitter
- .data
- .isDataRetained
- .isEmpty
- .listenerCount
- .add(listener, opts) ⇒ RetainEmitter
- .has(listener) ⇒ boolean
- .notify(…data) ⇒ void
- .notifyUnsafe(…data) ⇒ void
- .once(listener, immediate) ⇒ RetainEmitter
- .promise() ⇒ Promise
.push(…listeners) ⇒ RetainEmitter- .remove(listener) ⇒ RetainEmitter
- .reset() ⇒ RetainEmitter
- .constructor<T>() ⇒ RetainEmitter
.constructor<T>() ⇒ RetainEmitter
Template Param | Type Definition |
---|---|
T | extends unknown[] |
.data
Returns the retained data, or undefined
if no data was retained.
.isDataRetained
true
if data is retained from the last event, false
otherwise.
.isEmpty
true
if it has no listeners, false
otherwise.
.listenerCount
Number of listeners.
.add(listener: ListenerCallback, opts: Partial<RetainListenerOptions>) ⇒ RetainEmitter
Param | Type | Description |
---|---|---|
listener | ListenerCallback | |
opts | Partial<RetainListenerOptions> |
.has(listener: any) ⇒ boolean
Check whether the listener is registered.
Note: This method performs a linear search.
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
Param | Type | Description |
---|---|---|
data | T |
.notifyUnsafe(…data: T[]) ⇒ void
Param | Type | Description |
---|---|---|
data | T |
.once(listener: ListenerCallback, immediate: boolean) ⇒ RetainEmitter
Returns: Reference to self (for method chaining).
Param | Type | Description |
---|---|---|
listener | ListenerCallback | The callback to register. |
immediate | boolean | If true , directly resolves if the emitter retains a value. |
.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[][]) ⇒ RetainEmitter
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) ⇒ RetainEmitter
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 . |
.reset() ⇒ RetainEmitter
Reset the state of the emitter.
Further call to add will not automatically resolve, until a new call to notify is performed.
Returns: Reference to self (for method chaining)