Class: CommandBus
Defined in: src/wirestate-core/commands/command-bus.ts:38
Dispatches named commands to one active handler.
Remarks
Commands are writes: save, login, reset, send. The caller gets a CommandDescriptor immediately and can await descriptor.task.
Handlers are stacked by type. The newest handler wins. Unregister it and the previous handler takes over again.
Example
import { CommandBus } from "@wirestate/core";
const bus = new CommandBus();
bus.register("SAVE_USER", async (user: User) => saveUser(user));
const command = bus.command<void, User>("SAVE_USER", user);
await command.task;Constructors
Constructor
new CommandBus(): CommandBus;Returns
CommandBus
Methods
clear()
clear(): void;Defined in: src/wirestate-core/commands/command-bus.ts:48
Removes all registered command handlers from the bus.
Returns
void
command()
command<R, D>(type: CommandType, data?: D): CommandDescriptor<R>;Defined in: src/wirestate-core/commands/command-bus.ts:74
Dispatches a command to the newest handler.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the command result. |
D | unknown | Type of the command payload data. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command token. |
data? | D | Command payload. |
Returns
Descriptor for the running command.
Remarks
The handler result is always wrapped in a Promise. The descriptor starts as PENDING, then becomes SETTLED or ERROR.
Throws
WirestateError If no handler is registered.
Example
const descriptor: CommandDescriptor<User> = commandBus.command<User, string>("GET_USER", "id-123");
const user: User = await descriptor.task;commandOptional()
commandOptional<R, D>(type: CommandType, data?: D): Optional<CommandDescriptor<R>>;Defined in: src/wirestate-core/commands/command-bus.ts:117
Dispatches a command if a handler exists.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the command result. |
D | unknown | Type of the command payload data. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
data? | D | Optional payload for the handler. |
Returns
Optional<CommandDescriptor<R>>
Command descriptor, or null when no handler exists.
has()
has(type: CommandType): boolean;Defined in: src/wirestate-core/commands/command-bus.ts:129
Checks if at least one handler is registered for the given command type.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
Returns
boolean
true if a handler is available, false otherwise.
register()
register<D, R>(type: CommandType, handler: CommandHandler<D, R>): CommandUnregister;Defined in: src/wirestate-core/commands/command-bus.ts:153
Registers a command handler.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the command payload data. |
R | unknown | Type of the command execution result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
handler | CommandHandler<D, R> | Function to execute when the command is dispatched. |
Returns
A function to unregister the handler.
Remarks
Multiple handlers for one type form a stack. The newest handler is active.
Example
const unregister: CommandUnregister = commandBus.register("LOG_MESSAGE", (message: string) => {
console.log(message);
});unregister()
unregister<D, R>(type: CommandType, handler: CommandHandler<D, R>): void;Defined in: src/wirestate-core/commands/command-bus.ts:184
Removes a previously registered command handler.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the command payload data. |
R | unknown | Type of the command execution result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
handler | CommandHandler<D, R> | The handler function instance to remove. |
Returns
void
Remarks
If the handler was not registered for the given type, this operation does nothing.