Skip to content

Class: EventBus ​

Defined in: src/wirestate-core/plugin/events/event-bus.ts:76

Broadcasts events to every subscriber registered on one container bus.

Remarks ​

Events are fire-and-forget "this happened" notifications. A container that registers EventsPlugin gets one bus for that container. Children that do not register their own event plugin resolve the nearest ancestor bus.

Example ​

typescript
import { Container, EventBus, EventsPlugin } from "@wirestate/core";

const container = new Container({ plugins: [new EventsPlugin()] });
const bus = container.get(EventBus);
const unsubscribe = bus.subscribe((event) => console.log(event.type));

bus.emit("USER_LOGGED_IN", { userId: "u1" });
unsubscribe();

Constructors ​

Constructor ​

ts
new EventBus(container?: Container): EventBus;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:82

Parameters ​

ParameterType
containerContainer

Returns ​

EventBus

Methods ​

emit() ​

ts
emit<P, T, S>(
   type: T, 
   payload?: P, 
   options?: EventEmitOptions<S>): void;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:108

Emits an event to matching subscribers.

Type Parameters ​

Type ParameterDefault typeDescription
PunknownPayload type.
T extends EventTypeEventTypeEvent type.
SunknownSource type.

Parameters ​

ParameterTypeDescription
typeTEvent token.
payload?PEvent payload.
options?EventEmitOptions<S>Event emission options.

Returns ​

void

Remarks ​

Every matching handler is snapshotted before the first one runs, so subscriptions can change while an event is being emitted: one emit reaches exactly the subscribers that existed when it started, whether a handler subscribes or unsubscribes mid-dispatch. Those changes take effect from the next emit. Dispatch does not await handler promises. If a handler throws or rejects, Wirestate reports it through the container error handler and continues with the next subscriber. Catch-all subscribers run before type-specific subscribers.

Example ​

typescript
eventBus.emit("USER_LOGGED_IN", { userId: "123" }, { source: authService });

hasSubscribers() ​

ts
hasSubscribers(): boolean;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:263

Checks if the bus has any active subscribers.

Returns ​

boolean

true if at least one handler is registered, false otherwise.


subscribe() ​

Call Signature ​

ts
subscribe<E>(handler: EventHandler<E>): EventUnsubscribe;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:157

Subscribes to every event on this bus.

Type Parameters ​
Type ParameterDefault type
E extends WireEvent<unknown, EventType, unknown>WireEvent<unknown, EventType, unknown>
Parameters ​
ParameterTypeDescription
handlerEventHandler<E>Event handler invoked for every emitted event.
Returns ​

EventUnsubscribe

Function that removes this subscription.

Remarks ​

Equivalent to subscribe(null, handler).

Example ​
typescript
const unsubscribe: EventUnsubscribe = eventBus.subscribe((event) => {
  console.log("Received event:", event);
});

Call Signature ​

ts
subscribe<E>(types: Nullable<
  | EventType
  | readonly EventType[]>, handler: EventHandler<E>): EventUnsubscribe;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:179

Subscribes to one or more event types.

Type Parameters ​
Type ParameterDefault type
E extends WireEvent<unknown, EventType, unknown>WireEvent<unknown, EventType, unknown>
Parameters ​
ParameterTypeDescription
typesNullable< | EventType | readonly EventType[]>Event type, list of event types, or null for every event.
handlerEventHandler<E>Event handler invoked for matching events.
Returns ​

EventUnsubscribe

Function that removes this subscription.

Remarks ​

Pass null to subscribe to every event. Each call is independent: subscribing the same function twice delivers the event twice, and each returned unsubscriber removes only its own subscription. An empty type list subscribes to nothing.

Example ​
typescript
const unsubscribe: EventUnsubscribe = eventBus.subscribe(["USER_ADDED", "USER_REMOVED"], (event) => {
  refreshList();
});

unsubscribe() ​

Call Signature ​

ts
unsubscribe<E>(handler: EventHandler<E>): void;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:227

Removes one of a handler's catch-all subscriptions.

Type Parameters ​
Type ParameterDefault type
E extends WireEvent<unknown, EventType, unknown>WireEvent<unknown, EventType, unknown>
Parameters ​
ParameterTypeDescription
handlerEventHandler<E>The handler function instance to remove.
Returns ​

void

Remarks ​

Prefer the unsubscriber returned by subscribe. This by-reference form removes the newest catch-all subscription that uses the handler.

Call Signature ​

ts
unsubscribe<E>(types: Nullable<
  | EventType
  | readonly EventType[]>, handler: EventHandler<E>): void;

Defined in: src/wirestate-core/plugin/events/event-bus.ts:239

Removes one of a handler's subscriptions for one or more event types.

Type Parameters ​
Type ParameterDefault type
E extends WireEvent<unknown, EventType, unknown>WireEvent<unknown, EventType, unknown>
Parameters ​
ParameterTypeDescription
typesNullable< | EventType | readonly EventType[]>Event type, list of event types, or null for catch-all.
handlerEventHandler<E>The handler function instance to remove.
Returns ​

void

Remarks ​

For each given type, removes the newest subscription that uses the handler. Pass null to target catch-all subscriptions.