Skip to content

Class: CommandBus

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

Dispatches commands to the active handler for each command type.

Remarks

Commands represent write-oriented work such as save, login, reset, or send. Handlers are stacked by type: the newest handler is active until it unregisters. Required execution throws when no handler exists. Optional execution returns undefined for a miss.

Example

typescript
import { CommandBus, CommandsPlugin, Container } from "@wirestate/core";

const container = new Container({ plugins: [new CommandsPlugin()] });
const bus = container.get(CommandBus);
const unregister = bus.register<void, User>("SAVE_USER", async (user: User) => {
  // persist the user
});

await bus.executeAsync<void, User>("SAVE_USER", { id: "u1" });

unregister();

Extends

Constructors

Constructor

ts
new CommandBus(): CommandBus;

Returns

CommandBus

Inherited from

ts
HandlerStackBus<CommandType>.constructor

Methods

createMissingHandlerError()

ts
protected createMissingHandlerError(type: CommandType): WirestateError;

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

Builds the error thrown when a required command dispatch finds no handler.

Parameters

ParameterTypeDescription
typeCommandTypeCommand type that failed to resolve.

Returns

WirestateError

The error to throw.

Overrides

ts
HandlerStackBus.createMissingHandlerError

dispatch()

ts
protected dispatch<R, P>(type: CommandType, payload?: P): R;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:76

Dispatches to the active handler and returns its result as-is.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken to dispatch.
payload?PPayload passed to the handler.

Returns

R

The handler result.

Remarks

If a handler returns a Promise, that Promise is returned untouched.

Throws

WirestateError If no handler is registered.

Inherited from

ts
HandlerStackBus.dispatch

dispatchAsync()

ts
protected dispatchAsync<R, P>(type: CommandType, payload?: P): Promise<R>;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:101

Dispatches to the active handler and Promise-wraps the result.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken to dispatch.
payload?PPayload passed to the handler.

Returns

Promise<R>

A Promise resolving to the handler result.

Remarks

Sync values are wrapped. Async values are passed through.

Throws

WirestateError If no handler is registered.

Inherited from

ts
HandlerStackBus.dispatchAsync

dispatchOptional()

ts
protected dispatchOptional<R, P>(type: CommandType, payload?: P): Optional<R>;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:121

Dispatches to the active handler if one exists, otherwise returns undefined.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken to dispatch.
payload?PPayload passed to the handler.

Returns

Optional<R>

The handler result, or undefined when no handler exists.

Inherited from

ts
HandlerStackBus.dispatchOptional

dispatchOptionalAsync()

ts
protected dispatchOptionalAsync<R, P>(type: CommandType, payload?: P): Promise<Optional<R>>;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:138

Dispatches to the active handler if one exists and Promise-wraps the result, otherwise resolves to undefined.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken to dispatch.
payload?PPayload passed to the handler.

Returns

Promise<Optional<R>>

A Promise resolving to the handler result, or undefined when no handler exists.

Inherited from

ts
HandlerStackBus.dispatchOptionalAsync

execute()

Call Signature

ts
execute<R, P, T>(
   type: T, 
   payload: Optional<P>, 
   options: CommandDispatchOptions & {
  optional: true;
}): Optional<R>;

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

Dispatches an optional command and returns the handler result as-is.

Type Parameters
Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.
Parameters
ParameterTypeDescription
typeTCommand token.
payloadOptional<P>Command payload.
optionsCommandDispatchOptions & { optional: true; }Dispatch options with optional: true.
Returns

Optional<R>

The command result, or undefined when no handler exists.

Remarks

Returns undefined when no handler exists. If a handler returns a Promise, this returns that Promise. Pass a literal { optional: true } so the result narrows to Optional<R>.

Call Signature

ts
execute<R, P, T>(
   type: T, 
   payload?: P, 
   options?: CommandDispatchOptions): R;

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

Dispatches a required command and returns the handler result as-is.

Type Parameters
Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.
Parameters
ParameterTypeDescription
typeTCommand token.
payload?PCommand payload.
options?CommandDispatchOptionsDispatch options.
Returns

R

The command handler result.

Remarks

Throws when no handler is registered. If a handler returns a Promise, this returns that Promise. Use executeAsync when the caller should always receive a Promise.

Throws

WirestateError If no handler is registered.

Example
typescript
const saved: SaveResult = commandBus.execute<SaveResult, Draft>("SAVE_DRAFT", draft);

executeAsync()

Call Signature

ts
executeAsync<R, P, T>(
   type: T, 
   payload: Optional<P>, 
   options: CommandDispatchOptions & {
  optional: true;
}): Promise<Optional<R>>;

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

Dispatches an optional command and returns a Promise for the result.

Type Parameters
Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.
Parameters
ParameterTypeDescription
typeTCommand token.
payloadOptional<P>Command payload.
optionsCommandDispatchOptions & { optional: true; }Dispatch options with optional: true.
Returns

Promise<Optional<R>>

A Promise resolving to the command result, or undefined when no handler exists.

Remarks

Synchronous handler results are wrapped. Resolves to undefined when no handler exists. Pass a literal { optional: true } so the result narrows to Optional<R>.

Call Signature

ts
executeAsync<R, P, T>(
   type: T, 
   payload?: P, 
options?: CommandDispatchOptions): Promise<R>;

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

Dispatches a required command and returns a Promise for the result.

Type Parameters
Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.
Parameters
ParameterTypeDescription
typeTCommand token.
payload?PCommand payload.
options?CommandDispatchOptionsDispatch options.
Returns

Promise<R>

A Promise resolving to the command result.

Remarks

Throws when no handler is registered. Synchronous handler results are wrapped. Promises returned by handlers are passed through.

Throws

WirestateError If no handler is registered.


hasHandler()

ts
hasHandler(type: CommandType): boolean;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:45

Checks if at least one handler is registered for the given type.

Parameters

ParameterTypeDescription
typeCommandTypeToken to inspect.

Returns

boolean

true if a handler is available, false otherwise.

Inherited from

ts
HandlerStackBus.hasHandler

register()

ts
register<R, P, T>(type: T, handler: CommandHandler<R, P, T>): CommandUnregister;

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

Registers a command handler.

Type Parameters

Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.

Parameters

ParameterTypeDescription
typeTCommand type.
handlerCommandHandler<R, P, T>Function to execute when the command is dispatched.

Returns

CommandUnregister

A function to unregister the handler.

Remarks

Registering another handler for the same type shadows the previous one. Unregistering the newest restores it.

Example

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

registerHandler()

ts
protected registerHandler<R, P>(type: CommandType, handler: (payload: P) => MaybePromise<R>): () => void;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:157

Pushes a handler onto the stack for a type.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken the handler answers.
handler(payload: P) => MaybePromise<R>Handler function.

Returns

A callback that removes this exact registration.

() => void

Remarks

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

Inherited from

ts
HandlerStackBus.registerHandler

unregister()

ts
unregister<R, P, T>(type: T, handler: CommandHandler<R, P, T>): void;

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

Removes a previously registered command handler.

Type Parameters

Type ParameterDefault typeDescription
RunknownResult type.
PunknownPayload type.
T extends CommandTypeCommandTypeCommand type.

Parameters

ParameterTypeDescription
typeTCommand type.
handlerCommandHandler<R, P, T>The handler function instance to remove.

Returns

void

Remarks

If the handler was not registered for the given type, this operation does nothing.


unregisterHandler()

ts
protected unregisterHandler<R, P>(type: CommandType, handler: (payload: P) => MaybePromise<R>): void;

Defined in: src/wirestate-core/plugin/bus/handler-stack-bus.ts:184

Removes the newest registration whose handler matches by reference.

Type Parameters

Type ParameterDescription
RType of the handler result.
PType of the payload.

Parameters

ParameterTypeDescription
typeCommandTypeToken whose stack to update.
handler(payload: P) => MaybePromise<R>The handler function instance to remove.

Returns

void

Remarks

If the handler was not registered for the given type, this does nothing.

Inherited from

ts
HandlerStackBus.unregisterHandler