Binds an event handler to an operating system event

⚠️ NOTE: Requires an active event loop to process events

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

    Type Parameters

    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();