aieventjs: Event System
aieventjs is the publish/subscribe core of the ai*js family. Its API shape is close to mitt, but it adds the ai*js lifecycle constraints: on() returns an unsubscribe function, AbortSignal is first-class, and dispose() is idempotent and strict (any further operation after it throws EmitterDisposedError).
Install
Section titled “Install”npm install aieventjsQuick Start
Section titled “Quick Start”import { createEmitter } from "aieventjs";
// Declare the event map with type (not interface)type Events = { "user:login": { id: string }; "score:tick": { delta: number }; "user:logout": void;};
const bus = createEmitter<Events>();
// on() returns an unsubscribe function, so there is no listener reference to manageconst off = bus.on("user:login", (u) => console.log("hi", u.id));
// Or hand the cleanup timing to an AbortSignalconst ctrl = new AbortController();bus.on("score:tick", (e) => render(e.delta), { signal: ctrl.signal });
// The wildcard * listens to all events and fires after the type-matched handlersbus.on("*", (type, payload) => trace(type, payload));
// Emitbus.emit("user:login", { id: "alice" });
// Clean upoff(); // Cancel a single subscriptionctrl.abort(); // Cancel via signalbus.dispose(); // Dispose the whole emitter (idempotent)Core Features
Section titled “Core Features”| Feature | Description |
|---|---|
on() returns unsubscribe |
Call the return value directly to cancel; there is no need to keep a separate handler reference |
AbortSignal |
Pass the { signal } option and the listener is removed automatically when the signal aborts |
once() |
Built in: fires once, then is removed automatically; once("*", handler) is equivalent to on("*", handler, { once: true }) |
wildcard * |
bus.on("*", (type, payload) => ...) |
throttleMs |
Per-handler leading-edge throttle: minimum milliseconds between calls to that handler; the first dispatch always fires and dispatches inside the window are dropped, not queued. Uses performance.now() (monotonic); 0 disables it; valid on typed and wildcard * handlers, and a non-finite or negative value throws EmitterError |
sampleRate |
Wildcard * only: probability in (0, 1] that a dispatch reaches the handler, drawn with Math.random() per dispatch; on a typed handler or outside (0, 1], on() throws EmitterError |
captureErrors |
Typed handlers only; overrides the emitter-level default from createEmitter({ captureHandlerErrors }): true swallows the error and dispatch continues, false forces a re-throw, (err, type, payload) => void reports it and dispatch continues; on * it throws EmitterError |
| Synchronous dispatch | emit() runs synchronously and depth-first over a snapshot of the handlers; a handler removed mid-dispatch (unsubscribe, off, clear, dispose, signal abort) is not called again for the rest of that dispatch |
| Misuse checks | on() / once() throw EmitterError before registering when the handler is not a function or signal is not an AbortSignal; the message starts with aieventjs: |
Idempotent dispose() |
Repeated calls do not throw; later on/once/emit/off/clear calls throw EmitterDisposedError |
| Destructurable | const { on, emit } = bus works normally and does not depend on this |
Common Pairings
Section titled “Common Pairings”- With
aifsmjs: on FSM state transitions, broadcast to the UI layer through an emitter - With
aiecsjs: after an ECS system finishes a frame, it emits an event to notify other modules - With
aibridgejs: the bridge’son()event semantics match the shape of aieventjs, which makes them easy to swap