Skip to content

Function: OnCommand() ​

ts
function OnCommand(type: CommandType): MessagingHandlerDecorator;

Defined in: src/wirestate-core/plugin/commands/on-command.ts:86

Marks an injectable service method as a provision-scoped command handler.

Parameters ​

ParameterTypeDescription
typeCommandTypeCommand token.

Returns ​

MessagingHandlerDecorator

Method decorator.

Remarks ​

The handler is registered when the owning container is provisioned and unregistered when that provision cycle ends. Register CommandsPlugin on the container, or on an ancestor container, to enable command handlers.

One command call goes to one handler: the newest registered handler for the command token. The method receives the command payload and may return either a plain value or a Promise.

Example ​

typescript
import { Injectable, OnCommand } from "@wirestate/core";

@Injectable()
class UserService {
  @OnCommand("USER_LOGIN")
  public onUserLogin(credentials: Credentials): Promise<Session> {
    return auth.login(credentials);
  }
}