Skip to content

Quick Start

Build and complete a durable approval with WorkflowRuntime.

What You Will Build

  1. Reserve an order with a versioned activity.
  2. Wait for an assigned approval decision.
  3. Return a typed terminal result.
  4. 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

APIBehavior
Workflow.defineTyped Dart workflow with codecs and a fingerprint.
Workflow.activity / Workflow.userTaskVersioned contracts for automated and assigned work.
WorkflowRuntimeReplays the function against immutable history.
WorkflowModuleInstalls workflows and handlers at construction.
WorkflowTestHarnessProduction-faithful in-memory loop for tests.
startEncodedJSON/wire start that still decodes through the Dart definition.

Next Steps