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.addListener(() => ...);
await handle.unsubscribe();

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

Type Parameters

  • Parent

  • Args extends any[] = []

Hierarchy

  • AsyncEvent

Implements

Constructors

  • Create a new event.

    Type Parameters

    • Parent

    • Args extends any[] = []

    Parameters

    • parent: Parent

      the parent that will be passed to listener as their this

    • Optional options: AsyncEventOptions

      options for this event

    Returns AsyncEvent<Parent, Args>

Properties

subscribable: AsyncSubscribable<Parent, Args>

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

Accessors

  • get hasListeners(): boolean
  • Get if there are any listeners available.

    Returns boolean

    true if listeners are present

Methods

  • Emit this event. This will invoke all of the listeners with the passed arguments.

    Parameters

    • Rest ...args: Args

      arguments that listeners will receive

    Returns Promise<void>

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

    Parameters

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

      function used to filter events

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

          • Rest ...args: Args

          Returns boolean | Promise<boolean>

    Returns AsyncSubscribable<Parent, Args>

    filtered AsyncSubscribable

  • Monitor for changes to listeners. Only a single monitor is supported at a single time. 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.

    Parameters

    • monitor: ((event) => void)

      function used to monitor for changes to listeners

        • (event): void
        • Parameters

          Returns void

    Returns void

  • Get a promise that will resolve the first time this event is fired after this call.

    Returns Promise<Args>

    listener that resolves the next time the event is emitted

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

    This method will not use the current ErrorStrategy and will instead reject if an error occurs.

    Parameters

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

    Returns void

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

    Parameters

    • listener: Listener<Parent, Args>

      listener to subscribe

    Returns Promise<void>

    promise

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

    Parameters

    • listener: Listener<Parent, Args>

      listener to unsubscribe

    Returns Promise<void>

    promise that resolves when the listener is fully unsubscribed

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

    Parameters

    • listener: Listener<Parent, Args>

      listener to unsubscribe

    Returns Promise<void>

    promise that resolves when the listener is removed

Generated using TypeDoc