Typed subscriber
const handler = (payload) => {
console.log(payload);
};
events.on('topic', handler);
events.emit('topic', { id: 1 });
events.off('topic', handler);API reference
All dispatch is synchronous. Public mutators return the same instance. Event types must be strings; registration handlers are validated immediately.
| Member | Signature | Contract | Returns |
|---|---|---|---|
on | on(type, handler, once = false) | Registers a function. * creates a wildcard subscription. | this |
once | once(type, handler) | Registers a record removed immediately before its first invocation. | this |
off | off(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 |
emit | emit(type, ...payload) | Runs wildcard then typed handlers synchronously over live registration arrays; appended handlers can run in the current emit. | this |
reset | reset() | Clears every typed and wildcard registration. | this |
list | get list() | Returns an isolated null-prototype object snapshot of topic arrays. Wildcards use Symbol.for('event-pubsub-all'). | object |
const handler = (payload) => {
console.log(payload);
};
events.on('topic', handler);
events.emit('topic', { id: 1 });
events.off('topic', handler);events.on('*', (type, ...payload) => {
console.log(type, payload);
});
events.emit('topic', { id: 1 });
events.off(); // wildcard subscribers onlyA 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.