Class WorkflowPolicy<R>Abstract
Type Parameters
- R = unknown
The roles its hooks request, which every workflow it is attached to must provide: an interface, or several joined with
&.
Hierarchy (View Summary)
- WorkflowPolicy
Index
Constructors
constructor
-
Type Parameters
- R = unknown
The roles its hooks request, which every workflow it is attached to must provide: an interface, or several joined with
&.
Returns WorkflowPolicy<R>
- R = unknown
Properties
Readonly[roles]
The roles the policy requests. It exists only in the types, where a workflow it is attached to must provide them.
Methods
Staticbuilder
-
builder<C extends WorkflowPolicyClass<unknown>>(
this: WithoutStaticData<C, typeof WorkflowPolicy>,
name?: string,
): WorkflowPolicyBuilder<C>Starts putting a workflow policy of this class together, for a class declared without decorators, named
name, or else after the class, asNotify.builder("notify").import assert from "node:assert/strict";
import { type Contributor, contributor } from "itinera/journey";
import {
failureCause,
fromStep,
type OnFailure,
type OnSuccess,
type PolicyDescriptor,
role,
type StepFailureCause,
StepPolicy,
type StepPolicyBuilder,
WorkflowPolicy,
type WorkflowPolicyBuilder,
} from "itinera/policy";
import { Step, type Outcome } from "itinera/step";
import { Workflow } from "itinera/workflow";
import { integer } from "itinera/value";
/** A role the shop provides: telling a customer something. */
interface Messages {
tell(message: string): void;
}
class Charge extends Step {
override run(): Outcome {
return this.succeed();
}
}
class Audit extends StepPolicy {
audited(charged: number, contributing: Contributor): OnSuccess {
contributing.contribute("audit", `charged ${String(charged)}`);
return this.keepDefault();
}
failed(cause: StepFailureCause): OnFailure {
return this.failWorkflow(cause);
}
}
class Notify extends WorkflowPolicy<Messages> {
paid(messages: Messages): void {
messages.tell("paid");
}
}
class Orders extends Workflow implements Messages {
readonly told: string[] = [];
tell(message: string): void {
this.told.push(message);
}
}
const audit: StepPolicyBuilder<typeof Audit> = Audit.builder("audit")
.onStepSuccess("audited", fromStep("charged", integer), contributor())
.onStepFailure("failed", failureCause());
const notify: WorkflowPolicyBuilder<typeof Notify> = Notify.builder(
"notify",
).onWorkflowSuccess("paid", role<Messages>());
const orders = Orders.builder()
.name("orders")
.step(Charge.builder("charge").policy(audit))
.policy(notify)
.build();
const descriptor = orders.instance().descriptor;
const attached: readonly PolicyDescriptor[] =
descriptor.steps()[0]?.policies() ?? [];
assert.deepEqual(
attached.map((policy) => policy.name),
["audit"],
);
assert.deepEqual(attached[0]?.hooks(), ["on step success", "on step failure"]);
const built = attached[0].build();
assert.ok(built instanceof Audit);
assert.deepEqual(attached[0].call(built, "on step failure", ["failure"]), {
kind: "FailWorkflow",
reason: { code: "failure" },
});
assert.deepEqual(
descriptor.policies().map((policy) => policy.name),
["notify"],
);
class Counter extends Workflow {}
// @ts-expect-error Counter does not implement Messages, which notify requests
Counter.builder().policy(notify);A workflow policy keeps no static data, which every journey would share, refused as WithoutStaticData says: at compile time, or else when the builder starts.
Type Parameters
- C extends WorkflowPolicyClass<unknown>
Parameters
- this: WithoutStaticData<C, typeof WorkflowPolicy>
Optionalname: string
Returns WorkflowPolicyBuilder<C>
The base class of every workflow policy: hooks called once a journey's outcome is known, each a method marked with the decorator of the hook it implements, such as
@onWorkflowSuccess(). itinera builds one instance per journey, with a constructor that takes nothing.A hook returns nothing, and fails by throwing, which aborts the journey. Only a class extending
WorkflowPolicyis a workflow policy, whether it is declared with@workflowPolicyor given to a workflow's builder.