Interface WorkflowInstance<W>

One instance of a workflow, for one journey: what the executor is given to run. It holds the workflow descriptor and the workflow object side by side, and what belongs to this journey alone.

Only instance() on a workflow object makes one: an object written by hand, with the same members, is not a workflow instance and does not compile where one is expected, and run rejects with a TypeError one that a cast lets through.

import assert from "node:assert/strict";
import type { Event } from "itinera/event";
import type { JourneyId } from "itinera/journey";
import type { Reporter } from "itinera/report";
import { type Outcome, Step, step } from "itinera/step";
import { withStep, Workflow, workflow } from "itinera/workflow";

@step()
class Ship extends Step {
override run(): Outcome {
return this.succeed();
}
}

class Audit implements Reporter {
readonly events: Event[] = [];

report(event: Event): void {
this.events.push(event);
}
}

@workflow({ name: "orders", reporters: [Audit] })
@withStep(Ship, { name: "ship" })
class Orders extends Workflow {}

class Tagged implements Reporter {
constructor(readonly journeyId: JourneyId) {}

report(event: Event): void {
assert.equal(event.journeyId, this.journeyId);
}
}

function tagged(journeyId: JourneyId): Reporter {
return new Tagged(journeyId);
}

const instance = Orders.build().instance(new Map([["order", "1"]]), {
reporters: [tagged],
reportersGlobalMetadata: { tenant: "acme" },
});

const [audit, reporter] = instance.takeReporters();
assert.ok(audit instanceof Audit);
assert.ok(reporter instanceof Tagged);
assert.equal(reporter.journeyId, instance.journeyId);
assert.deepEqual(instance.takeReporters(), []);
assert.deepEqual([...instance.takeGlobalMetadata()], [["tenant", "acme"]]);
interface WorkflowInstance<W extends Workflow = Workflow> {
    descriptor: WorkflowDescriptor;
    initialData: ReadonlyMap<string, Value>;
    journeyId: JourneyId;
    workflow: W;
    takeGlobalMetadata(): ReadonlyMap<string, Value>;
    takeReporters(): readonly Reporter[];
}

Type Parameters

Index

descriptor: WorkflowDescriptor

How the workflow is described to the executor.

initialData: ReadonlyMap<string, Value>

The initial content of the data bag, as given.

journeyId: JourneyId

The journey ID, made when the instance was created.

workflow: W

The workflow object, which the descriptor's calls are given.

  • Hands the initial global metadata over, once: empty afterwards.

    Returns ReadonlyMap<string, Value>

  • Hands the instance's reporters over, once: those its workflow lists, then its own. Empty afterwards.

    Returns readonly Reporter[]