atvik
    Preparing search index...

    Class AsyncEvent<Parent, Args>

    An event that handles subscription in an asynchronous way.

    Each instance represents a single event:

    const event = new AsyncEvent(valueForThisInListeners);
    

    The event can be emitted via the emit method:

    await event.emit('first-param', 'second-param');
    

    Listeners can be added directly on the event, but it is recommended to use .subscribable for any API that is public:

    // Adding a listener directly on the event
    await event.subscribe(() => ...);

    // Subscribable provides a public API
    await event.subscribable(() => ..)
    await event.subscribable.subscribe(() => ...);

    Listeners can be unsubscribed either via their handle or via the event:

    // Use handle for easier unsubscription
    const handle = await event.subscribe(() => ...);
    await handle.unsubscribe();

    // Unsubscribe the actual listener
    const listener = () => ...;
    await eventOrSubscribable.subscribe(listener);
    await eventOrSubscribable.unsubscribe(listener);

    Type Parameters

    • Parent
    • Args extends unknown[] = []

    Implements

    Index
    subscribable: AsyncSubscribable<Parent, Args>

    Public AsyncSubscribable that can safely be shared with consumers that should be able to listen for events.

    • get hasListeners(): boolean

      Get if there are any listeners available.

      Returns boolean

      true if listeners are present

    • get listenerCount(): number

      Get the number of listeners.

      Returns number

      number of listeners

    • Emit this event. This will invoke all of the listeners with the passed arguments. The listeners are invoked in order, and each listener is awaited before the next listener is invoked.

      Parameters

      • ...args: Args

        arguments that listeners will receive

      Returns Promise<void>

      promise that resolves when all listeners have handled the event, or rejects with the first error

    • Monitor for changes to listeners. The monitor is called after every subscribe, unsubscribe and clear. This is intended to be used to react to if listeners are currently registered. This can be used for things such as only listening to events from other objects when this event is active.

      More than one monitor can be added. The same monitor is only added one time.

      Parameters

      • monitor: (event: this) => void

        function used to monitor for changes to listeners

      Returns SubscriptionHandle

      handle that removes the monitor, also with using

    • Emit this event in parallel. This will invoke all of the listeners with the passed arguments. Triggering of the listeners will be done in parallel.

      This method will not use the current ErrorStrategy. All listeners are invoked, also if one of them fails. If one listener fails the promise rejects with its error. If more than one listener fails the promise rejects with an AggregateError that holds all of the errors.

      Parameters

      • ...args: Args

        arguments that the listeners will receive

      Returns Promise<void>

      • promise that resolves when all listeners have handled the event
    • Stop monitoring for listener changes.

      Parameters

      • Optionalmonitor: (event: this) => void

        monitor to remove. If not given, all monitors are removed

      Returns void

    • Subscribe to this event using the given listener. The listener will be invoked any time the event is emitted.

      Parameters

      Returns Promise<void>

      promise

    • Unsubscribe a listener from this handler. The specified listener will no longer be invoked when the event is emitted.

      Parameters

      Returns Promise<void>

      promise that resolves when the listener is removed