mwg API
    Preparing search index...

    Interface Hook<TArgs>

    A named-event registry whose handlers can be removed in bulk by whatever registered them.

    Distinct from Signal, which is one event with one payload and a consuming, LIFO listener order: this is many events keyed by name, dispatched in registration order, with a source tag so everything an ability or a worn item put here comes off in one call when that thing leaves. mwg names no events of its own - the strings are whatever a game's own loop actually fires.

    battle.BattleHooks and roguelike.CombatHooks are both this, with their argument types fixed and their own documentation attached. They used to be two line-for-line copies of the same class; the difference between them was never the registry, only what a handler is handed when it runs.

    import { HookRegistry } from '@datamoc/mw_games/core';

    const hooks = new HookRegistry<[amount: number]>();
    const poisonRing = {};

    hooks.on('turnStart', (amount) => console.log('poison ticks for', amount), poisonRing);
    hooks.emit('turnStart', 3);

    // the ring is removed - every hook it registered comes off in one call
    hooks.offSource(poisonRing);
    interface Hook<TArgs extends unknown[]> {
        event: string;
        handler: (...args: TArgs) => void;
        source?: unknown;
    }

    Type Parameters

    • TArgs extends unknown[]
    Index
    event: string
    handler: (...args: TArgs) => void
    source?: unknown

    whatever registered this hook - an ability, a held item - for bulk removal via offSource