Class Callbacks<Types>

Manages the registration and execution of callbacks for specific event types. Supports adding, removing, and triggering callbacks, with optional features such as protected callbacks, one-time execution, and execution delays.

Type Parameters

Hierarchy

  • Callbacks

Constructors

Accessors

Methods

Constructors

  • Initializes a new Callbacks instance with an empty list of callbacks.

    Type Parameters

    Returns Callbacks<Types>

    Example

    interface ICallbacks {
    onAdd: undefined;
    onDelete: undefined;
    }

    const callbacks = new Callbacks<ICallbacks>();

    callbacks.add('onAdd', () => console.log('Callback for add'));
    callbacks.add('onDelete', () => console.log('Callback for delete'));

    callbacks.tbt('onAdd', undefined); // Trigger all "onAdd" callbacks
    callbacks.tbt('onDelete', undefined); // Trigger all "onDelete" callbacks

Accessors

Methods

  • Registers a new callback for a specific target event.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • target: T

      The event name or target to associate the callback with.

    • action: TAction<Types[T]>

      The function to be executed when the event is triggered.

    • Optional settings: ISettings = {}

      Additional settings, such as whether the callback is protected or should execute only once.

    Returns IAddedCallback

    Example

    const callback = callbacks.add('onAdd', () => console.log('Callback added'));
    callback.remove(); // Removes this specific callback
  • Retrieves a callback by its ID.

    Parameters

    • callbackId: string

      The unique identifier of the callback.

    Returns undefined | ICallback<Types>

    The matching callback, or undefined if not found.

  • Removes a callback by its unique ID.

    Parameters

    • id: string

      The unique identifier of the callback to remove.

    Returns boolean

    true if the callback was successfully removed; false otherwise.

  • Triggers all callbacks associated with the specified target.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • target: T

      The target event name for which callbacks should be executed.

    • arg: Types[T]

      The argument to pass to each callback.

    Returns void

    Example

    callbacks.tbt('onAdd', undefined); // Trigger all "onAdd" callbacks
    
  • Enables or disables a callback by its ID.

    Parameters

    • id: string

      The unique identifier of the callback.

    • isEnabled: boolean

      Set to false to disable the callback; true to enable it.

    Returns void