Skip to content

EventBus

EventBus: object

Defined in: Core/EventBus.ts:43

emit: object = emitEventBus

Emits a message payload to one or more target event buses.

Uses a synchronous breadth-first trampoline queue to handle re-entrant emissions deterministically.

EventBus.emit(busA, {
  kind: "userLoggedIn",
  value: { userId: "user-1" },
});

EventBus.emit([busA, busB], {
  kind: "userLoggedIn",
  value: { userId: "user-1" },
});

forward: object = forwardEventBus

Forwards messages from one event bus to another (or multiple).

Forwarding a message back to a bus it already visited within the same emission throws an error naming the bus and message kind, breaking cycles such as A → B → A on the first repeat instead of delivering indefinitely.

const stop = EventBus.forward({
  from: authBus,
  to: analyticsBus,
  only: ["userLoggedIn"],
});

make: object = makeEventBus

Constructs a new EventBus instance.

const bus = EventBus.make<AppMessages>({ id: "app" });

subscribe: object = subscribeEventBus

Initiates subscriber registration on an event bus for all events, specific event kind(s), or sequence.

When called with only an event bus (EventBus.subscribe(bus)), observes every event defined on the bus schema. When called with event kinds, observes only matching events or sequences.

// Schema-wide subscription:
const unsubscribe = EventBus.subscribe(appBus).tap((msg) => {
  console.log("Telemetry event:", msg.kind, msg.value);
});

// Targeted sequence subscription:
const sub = EventBus.subscribe(
  appBus,
  ["userLoggedIn", "checkoutStarted"],
  { ordered: true }
).collect((payloads) => {
  console.log("Checkout initiated by", payloads.userLoggedIn.userId);
});