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
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:68
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:92
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
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
eventBus.emit("USER_LOGGED_IN", { userId: "123" }, { source: authService });hasSubscribers()
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
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 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:160
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:208
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:220
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.