Quick Start
Build and complete a durable approval with WorkflowRuntime.
What You Will Build
- Reserve an order with a versioned activity.
- Wait for an assigned approval decision.
- Return a typed terminal result.
- Persist everything in
InMemoryWorkflowStorage.
Complete Example
dart
import 'package:vyuh_workflow_engine/vyuh_workflow_runtime.dart';
import 'package:vyuh_workflow_testkit/vyuh_workflow_testkit.dart';
final reserve = Workflow.activity<Order, Reservation>(
name: 'orders.reserve',
input: orderCodec,
output: reservationCodec,
);
final approve = Workflow.userTask<Reservation, Approval>(
name: 'orders.approve',
input: reservationCodec,
response: approvalCodec,
);
final orderApproval = Workflow.define<Order, Result>(
code: 'orders.approval',
version: 1,
fingerprint: 'orders.approval:v1',
input: orderCodec,
output: resultCodec,
execute: (flow, order) async {
final reservation = await flow.serviceTask(
reserve,
order,
id: 'reserve',
);
final decision = await flow.userTask(
approve,
reservation,
id: 'approve',
title: 'Approve order',
assignment: Assignment(roleIds: ['order-approver']),
);
return decision.approved ? Result.approved() : Result.rejected();
},
);
Future<void> main() async {
final test = WorkflowTestHarness()
..workflow(orderApproval)
..activity(reserve, (context, order) async {
return Reservation(orderId: order.id);
});
final run = await test.start(orderApproval, Order(id: 'ORD-001'));
await run.drain();
final tasks = await run.openUserTasks();
await run.completeUserTask(approve, Approval.approved());
expect((await run.run).status, WorkflowRunStatus.completed);
}Runtime Shape
The Dart function is the workflow. if and return are control flow. Only serviceTask and userTask are durable awaits. After a process restart the runtime invokes the same function from the top; completed awaits return their recorded results immediately.
Current API Points
| API | Behavior |
|---|---|
Workflow.define | Typed Dart workflow with codecs and a fingerprint. |
Workflow.activity / Workflow.userTask | Versioned contracts for automated and assigned work. |
WorkflowRuntime | Replays the function against immutable history. |
WorkflowModule | Installs workflows and handlers at construction. |
WorkflowTestHarness | Production-faithful in-memory loop for tests. |
startEncoded | JSON/wire start that still decodes through the Dart definition. |