Skip to content

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).

終端機視窗
npm install aieventjs
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 manage
const off = bus.on("user:login", (u) => console.log("hi", u.id));
// Or hand the cleanup timing to an AbortSignal
const 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 handlers
bus.on("*", (type, payload) => trace(type, payload));
// Emit
bus.emit("user:login", { id: "alice" });
// Clean up
off(); // Cancel a single subscription
ctrl.abort(); // Cancel via signal
bus.dispose(); // Dispose the whole emitter (idempotent)
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
  • 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’s on() event semantics match the shape of aieventjs, which makes them easy to swap