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
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
new EventBus(container?: Container): EventBus;Defined in: src/wirestate-core/plugin/events/event-bus.ts:82
Parameters
| Parameter | Type |
|---|---|
container | Container |
Returns
EventBus
Methods
emit()
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 Parameter | Default type | Description |
|---|---|---|
P | unknown | Payload type. |
T extends EventType | EventType | Event type. |
S | unknown | Source type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Event token. |
payload? | P | Event 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
eventBus.emit("USER_LOGGED_IN", { userId: "123" }, { source: authService });hasSubscribers()
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
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 Parameter | Default type |
|---|---|
E extends WireEvent<unknown, EventType, unknown> | WireEvent<unknown, EventType, unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler<E> | Event handler invoked for every emitted event. |
Returns
Function that removes this subscription.
Remarks
Equivalent to subscribe(null, handler).
Example
const unsubscribe: EventUnsubscribe = eventBus.subscribe((event) => {
console.log("Received event:", event);
});Call Signature
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 Parameter | Default type |
|---|---|
E extends WireEvent<unknown, EventType, unknown> | WireEvent<unknown, EventType, unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
types | Nullable< | EventType | readonly EventType[]> | Event type, list of event types, or null for every event. |
handler | EventHandler<E> | Event handler invoked for matching events. |
Returns
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
const unsubscribe: EventUnsubscribe = eventBus.subscribe(["USER_ADDED", "USER_REMOVED"], (event) => {
refreshList();
});unsubscribe()
Call Signature
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 Parameter | Default type |
|---|---|
E extends WireEvent<unknown, EventType, unknown> | WireEvent<unknown, EventType, unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler<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
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 Parameter | Default type |
|---|---|
E extends WireEvent<unknown, EventType, unknown> | WireEvent<unknown, EventType, unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
types | Nullable< | EventType | readonly EventType[]> | Event type, list of event types, or null for catch-all. |
handler | EventHandler<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.