atvik
    Preparing search index...

    Interface AsyncSubscribable<This, Args>

    Function that can be used to asynchronously subscribe, filter and iterate over events. Async subscribables are commonly fetched from an AsyncEvent or manually created using createAsyncSubscribable.

    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 asyncSubscribable.subscribe(listener);
    await asyncSubscribable.unsubscribe(listener);

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

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

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

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

    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 OverflowBehavior values.

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

    for await (const [ arg1 ] of subscribable.iterator(iteratorOptions)) {
    console.log('event', arg1);
    }
    interface AsyncSubscribable<This, Args extends unknown[] = []> {
        "[asyncIterator]"(): EventIterator<Args>;
        filter(
            filter: (this: This, ...args: Args) => boolean | Promise<boolean>,
        ): AsyncSubscribable<This, Args>;
        iterator(options?: EventIteratorOptions): EventIterator<Args>;
        map<NewArgs extends [] | [unknown, ...unknown[]]>(
            mapper: (this: This, ...args: Args) => NewArgs | Promise<NewArgs>,
        ): AsyncSubscribable<This, NewArgs>;
        once(options?: OnceOptions): Promise<Args>;
        subscribe(
            listener: Listener<This, Args>,
            options?: SubscribeOptions,
        ): Promise<AsyncSubscriptionHandle>;
        unsubscribe(listener: Listener<This, Args>): Promise<void>;
        withThis<NewThis>(newThis: NewThis): AsyncSubscribable<NewThis, Args>;
        (
            listener: Listener<This, Args>,
            options?: SubscribeOptions,
        ): Promise<AsyncSubscriptionHandle>;
    }

    Type Parameters

    • This
    • Args extends unknown[] = []

    Hierarchy (View Summary)

    Index