Application events

white-label-mediator

A small synchronous Node-compatible event bus for exchanging named application intent without direct module coupling.

Install and requirements

  • Node.js ^22.18.0 or >=24.11.0 for installation/development
  • No DOM dependency
import Mediator from 'white-label-mediator';
const mediator = new Mediator();

Publish and subscribe

Delivery is synchronous and follows EventEmitter listener ordering. Emitting with no subscribers is valid and returns false.

The publisher does not know who listens. Keep callback references so the owner of a subscription can remove it later.

mediator.on('cart:updated', renderCart);
const delivered = mediator.emit('cart:updated', {quantity: 3});

Lifecycle and ownership

Individual components should remove only their own subscriptions. destroy() is for the event bus itself leaving the application and removes every listener owned by that mediator instance.

Typed event maps provide compile-time event/payload checking only; they add no runtime validation.

type Events = {
    ready: [name: string];
    stopped: [];
};
const mediator = new Mediator<Events>();

White Label integrations

Router can listen for router:navigate messages. Model can relay namespaced model:<name>:<event> messages to any structurally compatible emitter.

Mediator does not own payload schemas, state changes, rendering, navigation, or lifecycle policy outside its own listeners.

Mediator API

new Mediator<Events>()

Create an independent event bus, optionally with a compile-time event map.

Returns
A Mediator instance.
Runtime
Browser + Node.js

initialize()

Start the lifecycle for composition/chaining.

Returns
The same Mediator instance.
Runtime
Browser + Node.js

destroy()

Remove every listener from this mediator instance.

Returns
The same Mediator instance after cleanup.
Runtime
Browser + Node.js

on(name, callback)

Subscribe to a named event.

Returns
The same Mediator instance for chaining.
Runtime
Browser + Node.js

once(name, callback)

Subscribe for one delivery.

Returns
The same Mediator instance for chaining.
Runtime
Browser + Node.js

emit(name, ...payload)

Synchronously publish an event.

Returns
true when at least one listener exists; otherwise false.
Runtime
Browser + Node.js

removeListener(name, callback)

Release one owned subscription using the same callback reference.

Returns
The same Mediator instance for chaining.
Runtime
Browser + Node.js

removeAllListeners(name?)

Use the standard EventEmitter broad-cleanup contract.

Returns
The same Mediator instance.
Runtime
Browser + Node.js
  • Avoid broad cleanup from a component that does not own every affected listener.

listenerCount(name)

Inspect current listener count for an event.

Returns
number.
Runtime
Browser + Node.js

off(name, callback)

Node EventEmitter alias for removeListener().

Returns
The same Mediator instance.
Runtime
Browser + Node.js

addListener(name, callback)

Node EventEmitter alias for on().

Returns
The same Mediator instance.
Runtime
Browser + Node.js

TypeScript

Mediator<Events> narrows event names and payload tuples at compile time.

The runtime remains ordinary EventEmitter behavior; type maps do not validate payloads at runtime.

Security and trust boundaries

Treat event payloads according to their original trust boundary; Mediator does not sanitize or validate them.

Design boundaries

Mediator moves named messages. It does not own application state, rendering, routing, networking, persistence, or business behavior.