Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface AsyncSubscribable<This, Args>

Type parameters

  • This

  • Args: any[] = []

Hierarchy

  • AsyncSubscriptionFunctions<This, Args>
    • AsyncSubscribable

Callable

  • AsyncSubscribable(listener: Listener<This, Args>): Promise<AsyncSubscriptionHandle>
  • Subscribe to this event using the given listener. The listener will be invoked any time the event is emitted. The returned handle can be used to unsubscribe.

    Subscribing and unsubscribing

    Async subscribables are functions that allow them to be called directly to subscribe to the event:

    // Subscribe to the event
    const handle = await asyncSubscribable(arg1 => console.log('event', arg1));

    // Unsubscribe via the returned handle
    await handle.unsubscribe();

    It is also possible to subscribe/unsubscribe a listener using methods on the subscribable:

    const listener = arg1 => console.log('event', arg1);
    await subscribable.asyncSubscribable(listener);
    await subscribable.asyncSubscribable(listener);

    Filtering

    Async subscribables may be filtered to create an instance that only emits certain events:

    const filteredSubscribable = asyncSubscribable.filter(arg1 => arg1 > 10);
    

    Listening to something once

    Listening for a single event may be done via once which returns a promise:

    const [ arg1 ] = await asyncSubscribable.once();
    

    Async iteration of events

    Async subscribables can also be used with an async iterator to allow for event loops:

    With default values:

    for await (const [ arg1 ] of asyncSubscribable) {
    console.log('event', arg1);
    }

    Sometimes events are emitted faster than they can be consumed, limiting and controlling overflow of events can be done via iterator.

    As an example this will limit to 10 queued events and then start dropping the earliest ones:

    for await (const [ arg1 ] of asyncSubscribable.iterator({ limit: 100 })) {
    console.log('event', arg1);
    }

    The behavior to use when the queue is full can be controlled by setting the overflow behavior using one of the {@link OverflowBehavior} values.

    const iteratorOptions = {
    limit: 10,
    overflowBehavior: OverflowBehavior.Block
    };

    for await (const [ arg1 ] of subscribable.iterator(iteratorOptions)) {
    console.log('event', arg1);
    }

    Parameters

    • listener: Listener<This, Args>
      • listener to subscribe

    Returns Promise<AsyncSubscriptionHandle>

    handle to the subscription, can be used to unsubscribe. Resolves when the subscription is fully registered

Index

Methods

[asyncIterator]

  • [asyncIterator](): AsyncIterator<Args, any, undefined>
  • Returns AsyncIterator<Args, any, undefined>

filter

  • filter(filter: (...args: Args) => boolean | Promise<boolean>): AsyncSubscribable<This, Args>
  • Create a subscribable that will apply the specified filter to any listeners added.

    Parameters

    • filter: (...args: Args) => boolean | Promise<boolean>
      • function used to filter events
        • (...args: Args): boolean | Promise<boolean>
        • Parameters

          • Rest ...args: Args

          Returns boolean | Promise<boolean>

    Returns AsyncSubscribable<This, Args>

    filtered AsyncSubscribable

iterator

  • iterator(options?: EventIteratorOptions): AsyncIterableIterator<Args>
  • Create an iterator that supports async iteration of events emitted.

    Parameters

    • Optional options: EventIteratorOptions
      • options for this iterator

    Returns AsyncIterableIterator<Args>

    iterable/iterator

once

  • once(): Promise<Args>
  • Get a promise that will resolve the first time this event is fired after this call.

    Returns Promise<Args>

    promise that resolves the next time the event is fired

subscribe

  • subscribe(listener: Listener<This, Args>): Promise<AsyncSubscriptionHandle>
  • Subscribe to this event using the given listener. The listener will be invoked any time the event is emitted. The returned handle can be used to unsubscribe.

    Parameters

    • listener: Listener<This, Args>
      • listener to subscribe

    Returns Promise<AsyncSubscriptionHandle>

    promise with handle to the subscription, can be used to unsubscribe. Resolves when the subscription is fully registered

unsubscribe

  • unsubscribe(listener: Listener<This, Args>): Promise<void>
  • Unsubscribe a listener from this handler. The specified listener will no longer be invoked when the event is emitted.

    Parameters

    • listener: Listener<This, Args>
      • listener to unsubscribe

    Returns Promise<void>

    promise that resolves when the listener is unsubscribed

withThis

  • Create a Subscribable that changes the this argument used for listeners.

    Type parameters

    • NewThis

    Parameters

    • newThis: NewThis
      • what should be treated as this for event listeners

    Returns AsyncSubscribable<NewThis, Args>

    modified AsyncSubscribable

Generated using TypeDoc