Class EventManager<_Events>

A type-safe event management system that allows for registering, dispatching, and waiting for events.

// Define your events
interface MyEvents {
'player_join': [playerName: string];
'item_pickup': [itemId: number, count: number];
}

// Create an event manager
const events = new EventManager<MyEvents>();

// Register event handlers
events.on('player_join', (playerName) => {
console.log(`Welcome ${playerName}!`);
});

Type Parameters

  • _Events extends Record<string, any[]>

    A record type mapping event names to arrays of argument types

Constructors

Methods

  • Dispatches an event, calling all registered handlers with the provided arguments.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • name: T

      The name of the event to dispatch

    • ...args: _Events[T]

      The arguments to pass to the event handlers

    Returns Promise<void>

    await events.dispatch('item_pickup', 64, 1); // Dispatch diamond pickup event
    
  • Type Parameters

    • T extends string | number | symbol

    Parameters

    Returns void

  • Registers an event handler that will be called when the specified event is dispatched.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • name: T

      The name of the event to listen for

    • handlerFunction: (...args: _Events[T]) => void | Promise<void>

      The function to call when the event occurs

    • filter: null | (...args: _Events[T]) => boolean = null

      Optional function to filter events based on their arguments

    • once: boolean = false

      If true, the handler will be removed after being called once

    Returns () => void

    A function that can be called to remove the event handler

    const unsubscribe = events.on('item_pickup',
    (itemId, count) => console.log(`Picked up ${count} of item ${itemId}`),
    (itemId, count) => count > 10 // Only handle pickups of more than 10 items
    );

    // Later, to remove the handler:
    unsubscribe();
  • Registers a one-time event handler that will be automatically removed after being called once.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • name: T

      The name of the event to listen for

    • handlerFunction: (...args: _Events[T]) => void | Promise<void>

      The function to call when the event occurs

    • filter: null | (...args: _Events[T]) => boolean = null

      Optional function to filter events based on their arguments

    Returns () => void

    A function that can be called to remove the event handler

    events.once('player_join',
    (playerName) => console.log(`First player to join: ${playerName}`)
    );
  • Returns a promise that resolves when any of the specified events occur.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • names: T[]

      Array of event names to wait for

    • check: (event: T, ...args: _Events[T]) => boolean = ...

      Optional function to filter events based on their name and arguments

    • timeout: number = 0

      Optional timeout in milliseconds. If 0, waits indefinitely

    Returns Promise<[T, ..._Events[T][]]>

    A promise that resolves with a tuple of the event name and arguments

    If the timeout is reached before any of the events occur

    try {
    const [event, ...args] = await events.waitForAnyEvent(
    ['player_join', 'player_leave'],
    (event, ...args) => event === 'player_join' || args[0] === 'admin',
    10000
    );
    if (event === 'player_join') {
    console.log(`Player ${args[0]} joined`);
    } else {
    console.log(`Player ${args[0]} left`);
    }
    } catch (error) {
    console.log('Timed out waiting for player events');
    }
  • Returns a promise that resolves when the specified event occurs.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • name: T

      The name of the event to wait for

    • check: (...args: _Events[T]) => boolean = ...

      Optional function to filter events based on their arguments

    • timeout: number = 0

      Optional timeout in milliseconds. If 0, waits indefinitely

    Returns Promise<_Events[T]>

    A promise that resolves with the event arguments

    If the timeout is reached before the event occurs

    try {
    const [itemId, count] = await events.waitForEvent(
    'item_pickup',
    (itemId) => itemId === 64, // Wait for diamond pickup
    5000 // Timeout after 5 seconds
    );
    console.log(`Picked up ${count} diamonds!`);
    } catch (error) {
    console.log('Timed out waiting for diamond pickup');
    }