@flare-engine/events
Type-safe event primitives — EventEmitter<EventMap> for keyed events and Signal<T> for single-payload broadcast. Both support once(), deferred-remove during emit(), and zero-alloc Unsubscribe returns.
Install
bun add @flare-engine/events
Usage
import { EventEmitter, Signal } from '@flare-engine/events';
type GameEvents = { 'enemy-killed': (e: { id: number; score: number }) => void };
const bus = new EventEmitter<GameEvents>();
const off = bus.on('enemy-killed', (e) => console.log(e.score));
const onHit = new Signal<number>();
onHit.add((dmg) => console.log(dmg));
onHit.emit(10);
Emit semantics
emit() iterates the handler set live (no per-emit snapshot — this is a hot path):
- A handler added during an emit runs in that same emit.
- A handler removed during an emit still runs for that emit (and any nested emit); the removal takes effect once the outermost emit completes. Exception: a
once()handler fires at most once, even under a nested emit. emit()is re-entrant (a handler may callemit()again) and throw-safe: a throwing handler propagates to the caller but never wedges deferred removal.
See API spec.
License
Apache-2.0