Class: EventBus
Defined in: src/wirestate-core/events/event-bus.ts:27
Broadcasts events to every subscriber in one container.
Remarks
Events are fire-and-forget. No handler owns the result because there is no result. Use them for "this happened" notifications.
Example
import { EventBus, createContainer } from "@wirestate/core";
const container = createContainer();
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(): EventBus;Returns
EventBus
Methods
clear()
clear(): void;Defined in: src/wirestate-core/events/event-bus.ts:132
Internal
Removes all registered handlers from the bus.
Returns
void
emit()
emit<P, T, F>(
type: T,
payload?: P,
from?: F): void;Defined in: src/wirestate-core/events/event-bus.ts:51
Broadcasts an event to all subscribers.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
P | unknown | Type of the event payload. |
T extends EventType | EventType | Type of the event identifier. |
F | unknown | Type of the event source. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Event token. |
payload? | P | Event payload. |
from? | F | Event source. |
Returns
void
Remarks
The bus snapshots subscribers before dispatch. A handler can unsubscribe while an event is being emitted. A thrown handler is logged and the next handler still runs.
Example
eventBus.emit("USER_LOGGED_IN", { userId: "123" }, AuthService);has()
has(): boolean;Defined in: src/wirestate-core/events/event-bus.ts:123
Checks if the bus has any active subscribers.
Returns
boolean
true if at least one handler is registered, false otherwise.
subscribe()
subscribe(handler: EventHandler): EventUnsubscriber;Defined in: src/wirestate-core/events/event-bus.ts:90
Subscribes to every event.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler | Event handler. |
Returns
Function that removes this subscription.
Example
const unsubscribe: EventUnsubscriber = eventBus.subscribe((event) => {
console.log('Received event:', event);
});unsubscribe()
unsubscribe(handler: EventHandler): void;Defined in: src/wirestate-core/events/event-bus.ts:109
Removes a previously registered event handler.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler | The handler function instance to remove. |
Returns
void
Remarks
If the handler was not subscribed, this operation does nothing.