diff --git a/src/bus.ts b/src/bus.ts index 0e6f097..5a3d229 100644 --- a/src/bus.ts +++ b/src/bus.ts @@ -60,6 +60,21 @@ export class Bus { return () => listenerSet.delete(listener); } + /** + * Whether anything is listening to `event` — for skipping the *construction* of a payload nobody + * will read. + * + * Not a micro-optimisation looking for a problem. A correlated payload is built by spreading the + * request's identity into it, and that spread was measured at ~197 ns per request on a two-step + * pipeline — against a ~4.5 µs in-process request, on a path where the overwhelmingly common + * case is an application that subscribes to nothing at all. `emit` cannot help: its argument is + * already built by the time it is called. Only the caller can decline to build it. + */ + hasListeners(event: LifecycleEvent): boolean { + const listenerSet = this.listeners.get(event); + return listenerSet !== undefined && listenerSet.size > 0; + } + /** Dispatch `payload` to every listener of `event`; a throwing listener is isolated and does not affect the others. */ emit(event: LifecycleEvent, payload: EventPayload): void { for (const listener of this.listeners.get(event) ?? []) { diff --git a/src/http/core.ts b/src/http/core.ts index bb3375d..d59999c 100644 --- a/src/http/core.ts +++ b/src/http/core.ts @@ -47,6 +47,13 @@ export function correlateRequest(headers: Record }; return renderError( error,