Skip to content

Class: EventBus

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

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:68

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:92

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

Handlers are snapshotted before dispatch, so subscriptions can change while an event is being emitted. If a handler throws, 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:244

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:138

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:160

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:208

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:220

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.