Skip to content

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.

const workflow = createWorkflow();
// Register a handler for user events
workflow.handle([UserEvent], async (context, event) => \{
console.log('Processing user:', event.data.name);
return ProcessedEvent.with({ status: 'complete' \});
});
// Create context and process events
const context = workflow.createContext();
await context.send(UserEvent.with(\{ name: 'John' \}));

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.

AcceptEvents extends WorkflowEvent<any>[]

Array of event types this handler accepts

Result extends void | WorkflowEventData<any, string>

Return type of the handler (event data or void)

AcceptEvents

Array of event types that trigger this handler

Handler<AcceptEvents, Result>

Function to execute when matching events are received

void

// Handle multiple event types
workflow.handle([StartEvent, RestartEvent], async (context, event) => \{
if (StartEvent.include(event)) {
return ProcessEvent.with({ action: 'start' \});
} else \{
return ProcessEvent.with({ action: 'restart' \});
}
});

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.

WorkflowContext

A new workflow context instance

const context = workflow.createContext();
// Send events through the context
await context.send(MyEvent.with(\{ data: 'hello' \}));
// Listen for specific events
const result = await context.waitFor(CompletionEvent);