Interface Subscribable<This, Args>

Function that can be used to subscribe, filter and iterate over events. Subscribables are commonly fetched from an Event, adapted from another event emitters using createEventAdapter or manually created via createSubscribable.

Subscribing and unsubscribing

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

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

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

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

const listener = arg1 => console.log('event', arg1);
subscribable.subscribe(listener);
subscribable.unsubscribe(listener);

Filtering

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

const filteredSubscribable = subscribable.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 subscribable.once();

Async iteration of events

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

With default values:

for await (const [ arg1 ] of subscribable) {
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 subscribable.iterator({ limit: 10 })) {
console.log('event', arg1);
}

The behavior to use when the queue is full can be controlled by setting the overflow behavior to either DropNewest or DropOldest.

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

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

Type Parameters

  • This

  • Args extends any[] = []

Hierarchy

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

    handle to the subscription, can be used to unsubscribe

Methods

  • Returns AsyncIterator<Args, any, undefined>

  • Create a subscribable that will apply the specified filter to any listeners added.

    Parameters

    • filter: ((this, ...args) => boolean | Promise<boolean>)

      function used to filter events

        • (this, ...args): boolean | Promise<boolean>
        • Parameters

          • this: This
          • Rest ...args: Args

          Returns boolean | Promise<boolean>

    Returns Subscribable<This, Args>

    filtered Subscription

Generated using TypeDoc