new Mediator<Events>()
Create an independent EventTarget-based application event bus, optionally with a compile-time detail map.
- Returns
- A Mediator instance.
- Runtime
- Browser + Node.js
Application events
A small synchronous standards-based EventTarget for exchanging named application intent without direct module coupling.
import Mediator from 'white-label-mediator';const mediator = new Mediator();Delivery is synchronous through the native EventTarget contract. Payloads belong in CustomEvent.detail, and dispatching with no subscribers is valid.
The publisher does not know who listens. Keep callback references so the owner of a subscription can remove it later. dispatchEvent() returns native cancellation state, not whether a subscriber existed.
const renderCart = event => console.log(event.detail.quantity);
mediator.addEventListener('cart:updated', renderCart);
mediator.dispatchEvent(new CustomEvent('cart:updated', {detail: {quantity: 3}}));Individual components should remove only their own subscriptions with removeEventListener(). destroy() is for the mediator itself leaving the application and removes listeners owned by that mediator instance.
Typed event maps constrain listener registration and narrow CustomEvent.detail inside typed callbacks; dispatchEvent() stays native and payload construction remains application-owned.
type Events = {
ready: string;
};
const mediator = new Mediator<Events>();Router can listen for router:navigate CustomEvents and reads navigation metadata from event.detail. Model can relay namespaced model:<name>:<event> CustomEvents to any structurally compatible EventTarget.
Mediator does not own payload schemas, state changes, rendering, navigation, or lifecycle policy outside its own listeners.
Mediator is useful for decoupling modules inside one request or function instance. Prefer a request-scoped Mediator when listeners or payloads are request-specific, and destroy it when that request-owned lifecycle ends.
Mediator is an in-memory event bus, not distributed infrastructure. Use the cloud platform or another external system for queues, pub/sub, durable events, retries, or communication between separate function instances.
new Mediator<Events>()Create an independent EventTarget-based application event bus, optionally with a compile-time detail map.
initialize()Start the lifecycle for composition/chaining.
addEventListener(type, callback, options?)Subscribe using native EventTarget semantics; typed maps narrow CustomEvent.detail for known event names.
| Parameter | Type | Default | Description |
|---|---|---|---|
type | string | — | Event name. |
callback | EventListener | EventListenerObject | — | Listener invoked synchronously during dispatch. |
options | boolean | AddEventListenerOptions | — | Native capture, once, passive, and signal options. |
removeEventListener(type, callback, options?)Release an owned subscription using native EventTarget matching semantics.
dispatchEvent(event)Synchronously dispatch an Event or CustomEvent through the native EventTarget signature.
| Parameter | Type | Default | Description |
|---|---|---|---|
event | Event | CustomEvent | — | Application event; put payload data in CustomEvent.detail. |
destroy()Remove every listener owned by this mediator instance and reset lifecycle tracking for reuse.
Mediator<Events> narrows addEventListener() event names and typed listener CustomEvent.detail values at compile time.
dispatchEvent() remains the native EventTarget method; event maps do not type-check publication payloads or validate them at runtime.
Mediator is an in-process coordination primitive, not an authorization or tenant/request-isolation boundary.
CustomEvent.detail is passed by reference; Mediator does not clone, freeze, sanitize, or validate it.
Code that holds a privileged mediator can dispatch events or call destroy(), and synchronous listeners can block or throw according to native EventTarget behavior. Do not expose a privileged mediator directly to untrusted plugins.
Mediator dispatches named application events. It does not own application state, rendering, routing, networking, persistence, or business behavior.