Core class, implementing event subscription + emitting the event.

Type Parameters

  • T = unknown

Hierarchy (view full)

Constructors

Accessors

  • get count(): number
  • Current number of live subscriptions.

    Returns number

  • get lastEvent(): undefined | T
  • 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.

    Returns undefined | T

  • get maxSubs(): number
  • 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.

    Returns number

Methods

  • Cancels all existing subscriptions for the event.

    This is a convenience method for some special cases, when you want to cancel all subscriptions for the event at once. Usually, subscribers just call cancel when they want to cancel their own subscription.

    This method will always offer much better performance than cancelling each subscription individually, which may become increasingly important when working with a large number of subscribers.

    Returns number

    Number of subscriptions cancelled.

  • Broadcasts data to all subscribers, according to the emit schedule, which is synchronous by default.

    Parameters

    • data: T

      Event data to be sent, according to the template type.

    • Optionaloptions: IEmitOptions

      Event-emitting options.

    Returns this

    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

    Parameters

    • Optionaloptions: {
          minUse?: number;
      }

      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.
      • OptionalminUse?: number

    Returns ISubStat

  • Subscribes 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.

    Parameters

    • cb: SubFunction<T>

      Event notification function, invoked after self-cancelling the subscription.

    • Optionaloptions: ISubOptions

      Subscription Options.

    Returns Subscription

    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.

    Parameters

    Returns Subscription

    Object for cancelling the subscription safely.

    once

  • 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);

    Type Parameters

    Returns EventConsumer<T, 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:

    • verify that a fast-pace subscription keeps receiving data;
    • peek at fast-pace subscription data for throttled updates;
    • for simpler receive-once / signal async processing logic.
    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:

    • when the timeout has been reached (if set via option timeout), it rejects with Event timed out error;
    • when cancelAll is called on the event object, it rejects with 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.

    Parameters

    • Optionaloptions: {
          name?: string;
          timeout?: number;
      }

      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.

      • Optionalname?: string
      • Optionaltimeout?: number

    Returns Promise<T>

    once