Options
All
  • Public
  • Public/Protected
  • All
Menu

Extends SubEvent with event onCount, to observe the number of subscriptions.

Type parameters

  • T = unknown

Hierarchy

Index

Constructors

constructor

Events

Readonly onCount

onCount: SubEvent<ISubCountChange> = ...

Triggered on any change in the number of subscriptions.

Accessors

count

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

    Returns number

maxSubs

  • get maxSubs(): number
  • Maximum number of subscribers that can receive events. Default is 0, meaning no limit applies.

    Newer subscriptions outside of the maximum quota will start receiving events when the older subscriptions get cancelled.

    It can only be set with the constructor.

    Returns number

Methods

cancelAll

  • cancelAll(): number
  • Cancels all existing subscriptions for the event.

    It overrides the base implementation, to trigger event onCount when there was at least one subscription.

    see

    cancel

    Returns number

    Number of subscriptions cancelled.

emit

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

    Parameters

    • data: T

      Data to be sent, according to the template type.

    • Optional options: IEmitOptions

      Event-emitting options.

    Returns SubEventCount<T>

    The event object itself.

getStat

  • Retrieves subscriptions statistics, to help with diagnosing subscription leaks.

    For this method to be useful, you need to set option name when calling subscribe.

    See also: Diagnostics

    see

    ISubStat

    Parameters

    • Optional options: {}

      Statistics Options:

      • minUse: number - Minimum subscription usage/count to be included into the list of named subscriptions. If subscription is used less times, it will be excluded from the named list.

    Returns ISubStat

once

  • Subscribes to receive just one event, and cancel the subscription immediately.

    You may still want to call cancel on the returned Subscription 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.

    see

    toPromise

    Parameters

    • cb: SubFunction<T>

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

    • Optional options: ISubOptions

      Subscription Options.

    Returns Subscription

    Object for cancelling the subscription safely.

subscribe

  • Subscribes to the event.

    When subscription is no longer needed, method cancel should be called on the returned object, to avoid performance degradation caused by abandoned subscribers.

    Method getStat can help with diagnosing leaked subscriptions.

    see

    once

    Parameters

    • cb: SubFunction<T>

      Event notification callback function.

    • Optional options: ISubOptions

      Subscription Options.

    Returns Subscription

    Object for cancelling the subscription safely.

toConsumer

  • Returns a new EventConsumer for the event, which physically hides methods emit and 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>

toPromise

  • toPromise(options?: {}): Promise<T>
  • 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.

    see

    once

    Parameters

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

    Returns Promise<T>

Legend

  • Property
  • Method
  • Property
  • Inherited method

Generated using TypeDoc