Skip to content

API reference / @node-ts/bus-core / WorkflowMapper

Class: WorkflowMapper<WorkflowStateType, WorkflowType> ​

Defined in: packages/bus-core/src/workflow/workflow.ts:156

A workflow configuration that describes how to map incoming messages to handlers within the workflow.

Type Parameters ​

Type Parameter
WorkflowStateType extends WorkflowState
WorkflowType extends Workflow<WorkflowStateType>

Constructors ​

Constructor ​

ts
new WorkflowMapper<WorkflowStateType, WorkflowType>(workflow): WorkflowMapper<WorkflowStateType, WorkflowType>;

Defined in: packages/bus-core/src/workflow/workflow.ts:170

Parameters ​

ParameterType
workflowClassConstructor<Workflow<WorkflowState>>

Returns ​

WorkflowMapper<WorkflowStateType, WorkflowType>

Properties ​

PropertyModifierTypeDefined in
onStartedByreadonlyMap<MessageDeclaration<Message>, { workflowCtor: ClassConstructor<Workflow<WorkflowState>>; workflowHandler: string; }>packages/bus-core/src/workflow/workflow.ts:160
onWhenreadonlyMap<MessageDeclaration<Message>, OnWhenHandler>packages/bus-core/src/workflow/workflow.ts:167

Accessors ​

workflowStateCtor ​

Get Signature ​

ts
get workflowStateCtor(): 
  | ClassConstructor<WorkflowStateType>
  | undefined;

Defined in: packages/bus-core/src/workflow/workflow.ts:174

Returns ​

| ClassConstructor<WorkflowStateType> | undefined

Methods ​

startedBy() ​

ts
startedBy<MessageType, THandlerName>(message, workflowHandler): this;

Defined in: packages/bus-core/src/workflow/workflow.ts:198

Starts a new instance of the workflow each time message is handled.

Messages are delivered at least once and starts aren't deduplicated, so a message that's retried after the new workflow state was saved (for example because another handler of the same message failed) starts a second workflow instance. Make the handler idempotent, such as by returning discardWorkflow() when a workflow already exists for the message, if that matters.

Type Parameters ​

Type Parameter
MessageType extends Message
THandlerName extends string

Parameters ​

ParameterTypeDescription
messageMessageDeclaration<MessageType>The message that starts the workflow: a message class, or a definition from defineCommand or defineEvent
workflowHandlerWorkflowHandlerArgument<WorkflowType, THandlerName, MessageType, WorkflowStateType>The name of the workflow method that handles message. It must take message and return changes to the workflow state, or nothing, with no fields that aren't in the state.

Returns ​

this

Throws ​

WorkflowAlreadyStartedByMessage if the workflow is already started by message

Example ​

ts
mapper.withState(OrderState).startedBy(OrderPlaced, 'start')

when() ​

ts
when<MessageType, THandlerName>(
   message, 
   workflowHandler, 
   customLookup?
): this;

Defined in: packages/bus-core/src/workflow/workflow.ts:231

Dispatches message to the workflow instances it maps to

Type Parameters ​

Type Parameter
MessageType extends Message
THandlerName extends string

Parameters ​

ParameterTypeDescription
messageMessageDeclaration<MessageType>The message to handle: a message class, or a definition from defineCommand or defineEvent
workflowHandlerWorkflowHandlerArgument<WorkflowType, THandlerName, MessageType, WorkflowStateType>The name of the workflow method that handles message. It must take message and return changes to the workflow state, or nothing, with no fields that aren't in the state.
customLookup?MessageWorkflowMapping<MessageType, WorkflowStateType>How to find the workflow instance for message. By default it's found by the workflowId sticky attribute that's added to messages sent from the workflow.

Returns ​

this

Throws ​

WorkflowAlreadyHandlesMessage if the workflow already handles message

Example ​

ts
mapper.when(CardCharged, 'charged', { lookup: message => message.orderId, mapsTo: 'orderId' })

withState() ​

ts
withState(workflowStateType): this;

Defined in: packages/bus-core/src/workflow/workflow.ts:178

Parameters ​

ParameterType
workflowStateTypeClassConstructor<WorkflowStateType>

Returns ​

this

Released under the MIT License.