Workflow
Workflow =
object
Defined in: core/src/core/workflow.ts:29
Represents a workflow that processes events through registered handlers.
A workflow is the central orchestrator for event-driven processing, allowing you to register handlers for specific events and create execution contexts to process those events.
Example
Section titled “Example”const workflow = createWorkflow();
// Register a handler for user eventsworkflow.handle([UserEvent], async (context, event) => \{ console.log('Processing user:', event.data.name); return ProcessedEvent.with({ status: 'complete' \});});
// Create context and process eventsconst context = workflow.createContext();await context.send(UserEvent.with(\{ name: 'John' \}));
Methods
Section titled “Methods”handle()
Section titled “handle()”handle<
AcceptEvents
,Result
>(accept
,handler
):void
Defined in: core/src/core/workflow.ts:55
Registers a handler function for one or more workflow events.
The handler will be invoked whenever any of the accepted events are sent through a workflow context. Handlers can process events and optionally return new events to continue the workflow.
Type Parameters
Section titled “Type Parameters”AcceptEvents
Section titled “AcceptEvents”AcceptEvents
extends WorkflowEvent
<any
>[]
Array of event types this handler accepts
Result
Section titled “Result”Result
extends void
| WorkflowEventData
<any
, string
>
Return type of the handler (event data or void)
Parameters
Section titled “Parameters”accept
Section titled “accept”AcceptEvents
Array of event types that trigger this handler
handler
Section titled “handler”Handler
<AcceptEvents
, Result
>
Function to execute when matching events are received
Returns
Section titled “Returns”void
Example
Section titled “Example”// Handle multiple event typesworkflow.handle([StartEvent, RestartEvent], async (context, event) => \{ if (StartEvent.include(event)) { return ProcessEvent.with({ action: 'start' \}); } else \{ return ProcessEvent.with({ action: 'restart' \}); }});
createContext()
Section titled “createContext()”createContext():
WorkflowContext
Defined in: core/src/core/workflow.ts:83
Creates a new workflow context for event processing.
The context provides the runtime environment for executing handlers and managing event flow. Each context maintains its own execution state and event queue.
Returns
Section titled “Returns”A new workflow context instance
Example
Section titled “Example”const context = workflow.createContext();
// Send events through the contextawait context.send(MyEvent.with(\{ data: 'hello' \}));
// Listen for specific eventsconst result = await context.waitFor(CompletionEvent);