CommandBus

  
class CommandBus<CommandBase extends ICommand = ICommand> extends ObservableBus implements ICommandBus {
  publisher: ICommandPublisher<CommandBase>
  execute<T extends CommandBase, R = any>(command: T, context?: AsyncContext): Promise<R>
  bind<T extends CommandBase>(handler: InstanceWrapper<ICommandHandler<T, any>>, id: string)
  register(handlers: InstanceWrapper<ICommandHandler<CommandBase, any>>[] = [])
  protected registerHandler(handler: InstanceWrapper<ICommandHandler<CommandBase, any>>)

  // inherited from cqrs/src/ObservableBus
  constructor()
  protected _subject$: Subject<T>
  subject$
}

Properties

Property Description
publisher: ICommandPublisher<CommandBase>

Returns the publisher. Default publisher is DefaultCommandPubSub (in memory).

Methods

execute()

Executes a command.

Overload #1

execute<R = void>(command: Command<R>): Promise<R>

Parameters

Option Type Description
command Command

The command to execute.

Returns

Promise<R> A promise that, when resolved, will contain the result returned by the command's handler.


Overload #2

execute<R = void>(command: Command<R>, context?: AsyncContext): Promise<R>

Parameters

Option Type Description
command Command

The command to execute.

context AsyncContext

The context to use. Optional.


Optional. Default is `undefined`.

Returns

Promise<R> A promise that, when resolved, will contain the result returned by the command's handler.


Overload #3

execute<T extends CommandBase, R = any>(command: T, context?: AsyncContext): Promise<R>

Parameters

Option Type Description
command T

The command to execute.

context AsyncContext

The context to use. Optional.


Optional. Default is `undefined`.

Returns

Promise<R> A promise that, when resolved, will contain the result returned by the command's handler.

bind()


bind<T extends CommandBase>(handler: InstanceWrapper<ICommandHandler<T, any>>, id: string)

Parameters

Option Type Description
handler InstanceWrapper>
id string

register()


register(handlers: InstanceWrapper<ICommandHandler<CommandBase, any>>[] = [])

Parameters

Option Type Description
handlers InstanceWrapper>[]

Optional. Default is [].

registerHandler()


protected registerHandler(handler: InstanceWrapper<ICommandHandler<CommandBase, any>>)

Parameters

Option Type Description
handler InstanceWrapper>