Class HierarchicalEmitter<Events>

Overview

A lightweight event system that allows hierarchical event propagation (similar to DOM event bubbling).

Events triggered on a child (e.g., a Collection) will propagate up to its parent (e.g., the parent Db and its parent DataAPIClient), unless explicitly stopped.

This allows to quickly (and granular-ly) enable listen for events at any level of the hierarchy, and stop propagation at any level if needed.

Event Hierarchy

Events follow a structured hierarchy:

  • Collection | TableDbClient
  • AstraAdmin | DbAdminClient

If, for instance, you have two different Collection objects which both point to the same collection, only the one that triggered the event will be notified.

Example

const client = new DataAPIClient({ logging: 'all' });

client.on('commandFailed', (event) => {
console.error('Some command failed:', event.commandName, event.error);
});

const db = client.db('*ENDPOINT*', { token: '*TOKEN*' });
const collection = db.collection('test_coll');

collection.on('commandFailed', (event) => {
event.stopPropagation(); // Prevents bubbling up to the client
console.error('test_coll command failed:', event.commandName, event.error);
});

collection.insertOne({ '$invalid-key': 'value' });
On errors in listeners

If an error is thrown in a listener, it will be silently ignored and will not stop the propagation of the event.

If you need to handle errors in listeners, you must wrap the listener in a try/catch block yourself.

Remarks

Having a custom implementation avoids a dependency on events for maximum compatibility across environments & module systems.

See

DataAPIClientEventMap

Type Parameters

Hierarchy (view full)

Constructors

Properties

#listeners: Partial<Record<keyof Events, ((event) => void)[]>> = {}
#parent: null | Pick<HierarchicalEmitter<Events>, "emit">

Methods

  • Emit an event.

    Should probably never be used by the user directly.

    Note: Errors thrown by any listeners will be silently ignored. It will not stop the propagation of the event.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • eventName: E

      The event to emit.

    • event: Events[E]

      Any arguments to pass to the listeners.

    Returns void

    true if the event had listeners, false otherwise.

  • Unsubscribe from an event.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • eventName: E

      The event to unsubscribe from.

    • listener: ((event) => void)

      The listener to remove.

        • (event): void
        • Parameters

          Returns void

    Returns void

  • Subscribe to an event.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • eventName: E

      The event to listen for.

    • listener: ((event) => void)

      The callback to invoke when the event is emitted.

        • (event): void
        • Parameters

          Returns void

    Returns (() => void)

    A function to unsubscribe the listener.

      • (): void
      • Returns void

  • Subscribe to an event once.

    The listener will be automatically unsubscribed after the first time it is called.

    Note that the listener will be unsubscribed BEFORE the actual listener callback is invoked.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • eventName: E

      The event to listen for.

    • listener: ((event) => void)

      The callback to invoke when the event is emitted.

        • (event): void
        • Parameters

          Returns void

    Returns (() => void)

    A function to prematurely unsubscribe the listener.

      • (): void
      • Returns void

  • Remove all listeners for an event.

    If no event is provided, all listeners for all events will be removed.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • Optional eventName: E

      The event to remove listeners for.

    Returns void