API reference

Six surfaces.
One fluent class.

All dispatch is synchronous. Public mutators return the same instance. Event types must be strings; registration handlers are validated immediately.

EventPubSub public API
MemberSignatureContractReturns
onon(type, handler, once = false)Registers a function. * creates a wildcard subscription.this
onceonce(type, handler)Registers a record removed immediately before its first invocation.this
offoff(type = '*', handler = '*')Removes all matching function registrations, or the whole type when handler is exactly *. A missing type returns before optional-handler validation, preserving 5.x behavior. Only the public string * maps to the internal wildcard Symbol; Symbol(event-pubsub-all) remains an ordinary typed name.this
emitemit(type, ...payload)Runs wildcard then typed handlers synchronously over live registration arrays; appended handlers can run in the current emit.this
resetreset()Clears every typed and wildcard registration.this
listget list()Returns an isolated null-prototype object snapshot of topic arrays. Wildcards use Symbol.for('event-pubsub-all').object

Typed subscriber

const handler = (payload) => {
    console.log(payload);
};

events.on('topic', handler);
events.emit('topic', { id: 1 });
events.off('topic', handler);

Wildcard subscriber

events.on('*', (type, ...payload) => {
    console.log(type, payload);
});

events.emit('topic', { id: 1 });
events.off(); // wildcard subscribers only
Synchronous exceptions are publisher-visible.

A synchronously thrown handler stops the current dispatch and propagates through emit. A one-shot registration is removed immediately before its own invocation, so reached one-shot handlers stay removed; unvisited registrations remain. Persistent handlers remain registered. Handler return values and promises are ignored, so callers must handle asynchronous rejections in application code.