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
new WorkflowMapper<WorkflowStateType, WorkflowType>(workflow): WorkflowMapper<WorkflowStateType, WorkflowType>;Defined in: packages/bus-core/src/workflow/workflow.ts:170
Parameters
| Parameter | Type |
|---|---|
workflow | ClassConstructor<Workflow<WorkflowState>> |
Returns
WorkflowMapper<WorkflowStateType, WorkflowType>
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
onStartedBy | readonly | Map<MessageDeclaration<Message>, { workflowCtor: ClassConstructor<Workflow<WorkflowState>>; workflowHandler: string; }> | packages/bus-core/src/workflow/workflow.ts:160 |
onWhen | readonly | Map<MessageDeclaration<Message>, OnWhenHandler> | packages/bus-core/src/workflow/workflow.ts:167 |
Accessors
workflowStateCtor
Get Signature
get workflowStateCtor():
| ClassConstructor<WorkflowStateType>
| undefined;Defined in: packages/bus-core/src/workflow/workflow.ts:174
Returns
| ClassConstructor<WorkflowStateType> | undefined
Methods
startedBy()
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
| Parameter | Type | Description |
|---|---|---|
message | MessageDeclaration<MessageType> | The message that starts the workflow: a message class, or a definition from defineCommand or defineEvent |
workflowHandler | WorkflowHandlerArgument<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
mapper.withState(OrderState).startedBy(OrderPlaced, 'start')when()
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
| Parameter | Type | Description |
|---|---|---|
message | MessageDeclaration<MessageType> | The message to handle: a message class, or a definition from defineCommand or defineEvent |
workflowHandler | WorkflowHandlerArgument<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
mapper.when(CardCharged, 'charged', { lookup: message => message.orderId, mapsTo: 'orderId' })withState()
withState(workflowStateType): this;Defined in: packages/bus-core/src/workflow/workflow.ts:178
Parameters
| Parameter | Type |
|---|---|
workflowStateType | ClassConstructor<WorkflowStateType> |
Returns
this