Skip to content

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

typescript
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

ts
new CommandBus(): CommandBus;

Returns

CommandBus

Methods

clear()

ts
clear(): void;

Defined in: src/wirestate-core/commands/command-bus.ts:48

Removes all registered command handlers from the bus.

Returns

void


command()

ts
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 ParameterDefault typeDescription
RunknownType of the command result.
DunknownType of the command payload data.

Parameters

ParameterTypeDescription
typeCommandTypeCommand token.
data?DCommand payload.

Returns

CommandDescriptor<R>

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

typescript
const descriptor: CommandDescriptor<User> = commandBus.command<User, string>("GET_USER", "id-123");
const user: User = await descriptor.task;

commandOptional()

ts
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 ParameterDefault typeDescription
RunknownType of the command result.
DunknownType of the command payload data.

Parameters

ParameterTypeDescription
typeCommandTypeCommand identifier.
data?DOptional payload for the handler.

Returns

Optional<CommandDescriptor<R>>

Command descriptor, or null when no handler exists.


has()

ts
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

ParameterTypeDescription
typeCommandTypeCommand identifier.

Returns

boolean

true if a handler is available, false otherwise.


register()

ts
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 ParameterDefault typeDescription
DunknownType of the command payload data.
RunknownType of the command execution result.

Parameters

ParameterTypeDescription
typeCommandTypeCommand identifier.
handlerCommandHandler<D, R>Function to execute when the command is dispatched.

Returns

CommandUnregister

A function to unregister the handler.

Remarks

Multiple handlers for one type form a stack. The newest handler is active.

Example

typescript
const unregister: CommandUnregister = commandBus.register("LOG_MESSAGE", (message: string) => {
  console.log(message);
});

unregister()

ts
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 ParameterDefault typeDescription
DunknownType of the command payload data.
RunknownType of the command execution result.

Parameters

ParameterTypeDescription
typeCommandTypeCommand identifier.
handlerCommandHandler<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.