🎭 The main EventEmitter class that handles all your event needs!

// Create a type-safe redstone event system
interface RedstoneEvents {
'pulse': [side: string, strength: number];
'off': [side: string];
}

const redstone = new EventEmitter<RedstoneEvents>();

// Type-safe event handling
redstone.on('pulse', (side, strength) => {
print(`Redstone signal on ${side}: ${strength}`);
});

// Clean up when done
const cleanup = redstone.on('off', (side) => {
print(`Signal lost on ${side}`);
});

// Later...
cleanup(); // Removes the listener

Type Parameters

  • Events extends EventMap = EventMap

    A record type mapping event names to arrays of argument types

Constructors

Methods

  • Emit an event with arguments.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      The event name to emit

    • ...args: Events[K]

      The arguments to pass to listeners

    Returns void

    // Emit events with type safety
    emitter.emit('turtle:error', new Error('Out of fuel!'));
  • Unsubscribe a listener from an event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      The event name to unsubscribe from

    • listener: (...args: Events[K]) => void

      The callback function to remove

    Returns void

  • Subscribe to an event with a listener function.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      The event name to listen for

    • listener: (...args: Events[K]) => void

      The callback function that handles the event

    Returns () => void

    A cleanup function that removes the listener when called

    const cleanup = emitter.on('inventory:changed', (slot, count) => {
    print(`Slot ${slot} now has ${count} items`);
    });

    // Later when you're done listening:
    cleanup();
  • Subscribe to an event that automatically unsubscribes after first trigger.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      The event name to listen for

    • listener: (...args: Events[K]) => void

      The one-time callback function

    Returns () => void

    A cleanup function (in case you need to remove before trigger)

    // Perfect for initialization events
    emitter.once('system:ready', () => {
    print('System initialized!');
    });
  • Remove all listeners for an event, or all events if no event specified.

    Parameters

    • Optionalevent: keyof Events

      Optional event name to clear listeners for

    Returns void

    // Clear specific event
    emitter.removeAllListeners('user:input');

    // Clear everything
    emitter.removeAllListeners();