Event constructor.
Optional
options: ICountOptions<T>Configuration Options.
Current number of live subscriptions.
Last emitted event, if there was any, or undefined
otherwise.
It is set after all subscribers have received the event, but just before optional IEmitOptions.onFinished callback is invoked.
Maximum number of subscribers that can receive events.
Default is 0, meaning no limit applies
.
Newer subscriptions outside the maximum quota will start receiving events when the older subscriptions get cancelled.
It can only be set with the constructor.
Cancels all existing subscriptions for the event.
It overrides the base implementation, to trigger event onCount when there was at least one subscription.
Number of subscriptions cancelled.
Broadcasts data to all subscribers, according to the emit
schedule,
which is synchronous by default.
Event data to be sent, according to the template type.
Optional
options: IEmitOptionsEvent-emitting options.
The event object itself.
Retrieves subscriptions statistics, to help with diagnosing subscription leaks.
For this method to be useful, you need to set option name
when calling SubEvent.subscribe.
See also: Diagnostics
Optional
options: { Statistics Options:
minUse: number
- Minimum subscription usage/count to be included into the list of named
subscriptions. If subscription is used fewer times, it will be excluded from the named
list.Optional
minSubscribes to receive just one event, and cancel the subscription immediately.
You may still want to call Subscription.cancel on the returned object, if you suddenly need to prevent the first event, or to avoid dead once-off subscriptions that never received their event, and thus were not cancelled.
Event notification function, invoked after self-cancelling the subscription.
Optional
options: ISubOptionsSubscription Options.
Object for cancelling the subscription safely.
Subscribes to the event.
When subscription is no longer needed, method Subscription.cancel should be called on the returned object, to avoid performance degradation caused by abandoned subscribers.
Method SubEvent.getStat can help with diagnosing leaked subscriptions.
Event notification callback function.
Optional
options: ISubOptionsSubscription Options.
Object for cancelling the subscription safely.
Returns a new EventConsumer for the event, which physically hides methods SubEvent.emit and SubEvent.cancelAll.
This method simplifies creation of a receive-only event object representation.
const e = new SubEvent<number>(); // full-access, emit-receive event
const c = e.toConsumer(); // the same "e" event, but with receive-only access
// It is equivalent to the full syntax of:
// const c = new EventConsumer<number>(e);
Creates a new subscription as a promise, to resolve with the next received event value, and cancel the subscription.
Examples of where it can be useful include:
try {
const nextValue = await myEvent.toPromise({timeout: 1000});
} catch(e) {
// Either subscription didn't produce any event after 1 second,
// or myEvent.cancelAll() was called somewhere.
}
The returned promise can reject in two cases:
timeout
), it rejects with Event timed out
error;Event cancelled
error.Note that if you use this method consecutively, you can miss events in between, because the subscription is auto-cancelled after receiving the first event.
Optional
options: { Subscription Options:
name
- for the internal subscription name. See name
in ISubOptions.
In this context, it is also included within any rejection error.
timeout
- sets timeout in ms (when timeout
>= 0), to auto-reject with
Event timed out
error.
Optional
name?: stringOptional
timeout?: number
Extends SubEvent with event onCount, to observe the number of subscriptions.