Options
All
  • Public
  • Public/Protected
  • All
Menu

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: any[] = []

Hierarchy

  • AsyncEvent

Implements

  • AsyncSubscriptionFunctions<Parent, Args>

Index

Constructors

constructor

  • new AsyncEvent<Parent, Args>(parent: Parent, options?: AsyncEventOptions): AsyncEvent<Parent, Args>
  • Create a new event.

    Type parameters

    • Parent

    • Args: 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

Readonly subscribable

subscribable: AsyncSubscribable<Parent, Args>

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

Accessors

hasListeners

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

    Returns boolean

    true if listeners are present

listeners

  • get listeners(): Listener<Parent, Args>[]
  • Get a copy of the listeners as an array.

    Returns Listener<Parent, Args>[]

    listeners as array

Methods

[asyncIterator]

  • [asyncIterator](): AsyncIterator<Args, any, undefined>
  • Return an async iterator for this event.

    Returns AsyncIterator<Args, any, undefined>

    async iterator for this event

clear

  • clear(): void
  • Clear all listeners for this event.

    Returns void

emit

  • emit(...args: Args): Promise<void>
  • 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>

filter

  • filter(filter: (...args: Args) => boolean | Promise<boolean>): AsyncSubscribable<Parent, 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<Parent, 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

monitorListeners

  • monitorListeners(monitor: (event: AsyncEvent<Parent, Args>) => void): void
  • 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: AsyncEvent<Parent, Args>) => void
      • function used to monitor for changes to listeners

    Returns void

once

  • once(): Promise<Args>
  • 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

parallelEmit

  • parallelEmit(...args: Args): Promise<void>
  • 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 {@link 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

removeMonitor

  • removeMonitor(): void
  • Stop monitoring for listener changes.

    Returns void

subscribe

  • subscribe(listener: Listener<Parent, 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<Parent, Args>
      • listener to subscribe

    Returns Promise<AsyncSubscriptionHandle>

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

Protected subscribe0

  • subscribe0(listener: Listener<Parent, Args>): Promise<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

  • unsubscribe(listener: Listener<Parent, 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<Parent, Args>
      • listener to unsubscribe

    Returns Promise<void>

    promise that resolves when the listener is fully unsubscribed

Protected unsubscribe0

  • unsubscribe0(listener: Listener<Parent, 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<Parent, Args>
      • listener to unsubscribe

    Returns Promise<void>

    promise that resolves when the listener is removed

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