@nanobpm/workflow 0.8.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -2
- package/dist/declarative.d.ts +178 -10
- package/dist/declarative.js +216 -356
- package/dist/nodes/branch.d.ts +11 -0
- package/dist/nodes/branch.js +40 -0
- package/dist/nodes/break.d.ts +8 -0
- package/dist/nodes/break.js +19 -0
- package/dist/nodes/continue.d.ts +8 -0
- package/dist/nodes/continue.js +18 -0
- package/dist/nodes/forEach.d.ts +24 -0
- package/dist/nodes/forEach.js +109 -0
- package/dist/nodes/index.d.ts +12 -0
- package/dist/nodes/index.js +19 -0
- package/dist/nodes/loop.d.ts +9 -0
- package/dist/nodes/loop.js +25 -0
- package/dist/nodes/parallel.d.ts +9 -0
- package/dist/nodes/parallel.js +40 -0
- package/dist/nodes/race.d.ts +54 -0
- package/dist/nodes/race.js +134 -0
- package/dist/nodes/registry.d.ts +33 -0
- package/dist/nodes/registry.js +71 -0
- package/dist/nodes/run.d.ts +10 -0
- package/dist/nodes/run.js +21 -0
- package/dist/nodes/signal.d.ts +11 -0
- package/dist/nodes/signal.js +26 -0
- package/dist/nodes/switch.d.ts +11 -0
- package/dist/nodes/switch.js +51 -0
- package/dist/nodes/task.d.ts +47 -0
- package/dist/nodes/task.js +97 -0
- package/dist/nodes/timer.d.ts +9 -0
- package/dist/nodes/timer.js +34 -0
- package/dist/test-support/diff.d.ts +8 -0
- package/dist/test-support/diff.js +70 -0
- package/dist/test-support/index.d.ts +8 -0
- package/dist/test-support/index.js +49 -0
- package/dist/test-support/normalize.d.ts +19 -0
- package/dist/test-support/normalize.js +245 -0
- package/dist/test-support/parity.d.ts +28 -0
- package/dist/test-support/parity.js +61 -0
- package/dist/test-support/xml.d.ts +17 -0
- package/dist/test-support/xml.js +135 -0
- package/dist/types.d.ts +20 -56
- package/package.json +12 -4
- package/src/declarative.ts +339 -360
- package/src/nodes/README.md +123 -0
- package/src/nodes/branch.ts +48 -0
- package/src/nodes/break.ts +25 -0
- package/src/nodes/continue.ts +24 -0
- package/src/nodes/forEach.ts +142 -0
- package/src/nodes/index.ts +20 -0
- package/src/nodes/loop.ts +34 -0
- package/src/nodes/parallel.ts +48 -0
- package/src/nodes/race.ts +185 -0
- package/src/nodes/registry.ts +105 -0
- package/src/nodes/run.ts +29 -0
- package/src/nodes/signal.ts +35 -0
- package/src/nodes/switch.ts +56 -0
- package/src/nodes/task.ts +150 -0
- package/src/nodes/timer.ts +42 -0
- package/src/test-support/diff.ts +69 -0
- package/src/test-support/index.ts +53 -0
- package/src/test-support/normalize.ts +274 -0
- package/src/test-support/parity.ts +85 -0
- package/src/test-support/xml.ts +140 -0
- package/src/types.ts +20 -30
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Built-in flow-node kind: `branch` — a two-way exclusive choice on a FEEL
|
|
2
|
+
// boolean condition (a BPMN exclusive gateway); the `then` arm is guarded, the
|
|
3
|
+
// `else` arm is the gateway default. Contributed through the extension seam.
|
|
4
|
+
import { feel } from "../declarative.js";
|
|
5
|
+
import { registerNodeKind } from "./registry.js";
|
|
6
|
+
registerNodeKind("branch", {
|
|
7
|
+
build: (api) => (condition, arms) => {
|
|
8
|
+
if (typeof condition !== "string" || condition.trim() === "") {
|
|
9
|
+
throw new Error(`branch() needs a non-empty FEEL condition`);
|
|
10
|
+
}
|
|
11
|
+
if (!arms || typeof arms.then !== "function")
|
|
12
|
+
throw new Error(`branch("${condition}") needs a then arm`);
|
|
13
|
+
if (arms.else !== undefined && typeof arms.else !== "function") {
|
|
14
|
+
throw new Error(`branch("${condition}") else arm must be a block (b) => {…}`);
|
|
15
|
+
}
|
|
16
|
+
api.out.push({
|
|
17
|
+
kind: "branch",
|
|
18
|
+
condition,
|
|
19
|
+
then: api.child(arms.then, false),
|
|
20
|
+
else: arms.else ? api.child(arms.else, false) : undefined,
|
|
21
|
+
});
|
|
22
|
+
return api.self();
|
|
23
|
+
},
|
|
24
|
+
walk: (node, recurse) => {
|
|
25
|
+
recurse(node.then);
|
|
26
|
+
if (node.else)
|
|
27
|
+
recurse(node.else);
|
|
28
|
+
},
|
|
29
|
+
emit: (node, incoming, loop, api) => {
|
|
30
|
+
const id = `Gw_${api.nextGw()}`;
|
|
31
|
+
const gw = api.addGateway(id);
|
|
32
|
+
api.connect(incoming, id);
|
|
33
|
+
const te = api.newEdge(id, { condition: feel(node.condition), name: "then" });
|
|
34
|
+
const out = api.emitList(node.then, [te], loop);
|
|
35
|
+
const ee = api.newEdge(id, { name: "else" });
|
|
36
|
+
gw.defaultFlow = ee.id;
|
|
37
|
+
out.push(...api.emitList(node.else ?? [], [ee], loop));
|
|
38
|
+
return out;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Built-in flow-node kind: `break` — exit the enclosing loop (routes to whatever
|
|
2
|
+
// follows it). Only valid inside a `loop`. Contributed through the extension seam.
|
|
3
|
+
import { registerNodeKind } from "./registry.js";
|
|
4
|
+
registerNodeKind("break", {
|
|
5
|
+
build: (api) => () => {
|
|
6
|
+
if (api.loopDepth === 0)
|
|
7
|
+
throw new Error(`break() is only valid inside a loop`);
|
|
8
|
+
api.out.push({ kind: "break" });
|
|
9
|
+
return api.self();
|
|
10
|
+
},
|
|
11
|
+
emit: (_node, incoming, loop, api) => {
|
|
12
|
+
if (!loop)
|
|
13
|
+
throw new Error(`break outside a loop in flow "${api.flowId}"`);
|
|
14
|
+
// The incoming edges (from the previous node) become the loop's exit
|
|
15
|
+
// danglers; this path does not fall through.
|
|
16
|
+
loop.breaks.push(...incoming);
|
|
17
|
+
return [];
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Built-in flow-node kind: `continue` — jump straight back to the top of the
|
|
2
|
+
// enclosing loop, skipping the rest of the body. Only valid inside a `loop`.
|
|
3
|
+
// Contributed through the extension seam (epic #314, S0/#315).
|
|
4
|
+
import { registerNodeKind } from "./registry.js";
|
|
5
|
+
registerNodeKind("continue", {
|
|
6
|
+
build: (api) => () => {
|
|
7
|
+
if (api.loopDepth === 0)
|
|
8
|
+
throw new Error(`continue() is only valid inside a loop`);
|
|
9
|
+
api.out.push({ kind: "continue" });
|
|
10
|
+
return api.self();
|
|
11
|
+
},
|
|
12
|
+
emit: (_node, incoming, loop, api) => {
|
|
13
|
+
if (!loop)
|
|
14
|
+
throw new Error(`continue outside a loop in flow "${api.flowId}"`);
|
|
15
|
+
api.connect(incoming, loop.headId);
|
|
16
|
+
return [];
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FlowNode } from "../types.js";
|
|
2
|
+
/** The `forEach` node shape (its `FlowNode` variant). */
|
|
3
|
+
interface ForEachNode {
|
|
4
|
+
kind: "forEach";
|
|
5
|
+
/** FEEL expression evaluating to the list to fan out over. */
|
|
6
|
+
collection: string;
|
|
7
|
+
/** Variable each item is bound to in the child's scope (`inputElement`). */
|
|
8
|
+
itemVar: string;
|
|
9
|
+
body: FlowNode[];
|
|
10
|
+
/** Run children one at a time (sequential MI) instead of all at once. */
|
|
11
|
+
sequential?: boolean;
|
|
12
|
+
/** List variable that each child's `outputElement` is collected into. */
|
|
13
|
+
outputCollection?: string;
|
|
14
|
+
/** FEEL expression producing each child's contribution to `outputCollection`. */
|
|
15
|
+
outputElement?: string;
|
|
16
|
+
/** FEEL boolean that, once true, completes the body early. */
|
|
17
|
+
completionCondition?: string;
|
|
18
|
+
}
|
|
19
|
+
declare module "../types.js" {
|
|
20
|
+
interface FlowNodeRegistry {
|
|
21
|
+
forEach: ForEachNode;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Built-in flow-node kind: `forEach` — a data-driven fan-out over a runtime
|
|
2
|
+
// collection (a BPMN parallel/sequential multi-instance activity). A single
|
|
3
|
+
// service-task body lifts the MI characteristics directly; a multi-step body is
|
|
4
|
+
// wrapped in an embedded MI sub-process. Contributed through the extension seam.
|
|
5
|
+
import { assertIdent, escapeXml } from "../xml.js";
|
|
6
|
+
import { registerNodeKind } from "./registry.js";
|
|
7
|
+
/** The `<bpmn:multiInstanceLoopCharacteristics>` block for a `forEach` node — the
|
|
8
|
+
* same shape whether lifted onto a leaf service task or a sub-process. */
|
|
9
|
+
function miCharacteristics(node) {
|
|
10
|
+
const seq = node.sequential ? "true" : "false";
|
|
11
|
+
const attrs = [
|
|
12
|
+
`inputCollection="${escapeXml(`=${node.collection}`)}"`,
|
|
13
|
+
`inputElement="${escapeXml(node.itemVar)}"`,
|
|
14
|
+
];
|
|
15
|
+
if (node.outputCollection)
|
|
16
|
+
attrs.push(`outputCollection="${escapeXml(node.outputCollection)}"`);
|
|
17
|
+
if (node.outputElement)
|
|
18
|
+
attrs.push(`outputElement="${escapeXml(`=${node.outputElement}`)}"`);
|
|
19
|
+
const completion = node.completionCondition
|
|
20
|
+
? ` <bpmn:completionCondition>${escapeXml(`=${node.completionCondition}`)}</bpmn:completionCondition>\n`
|
|
21
|
+
: "";
|
|
22
|
+
return (` <bpmn:multiInstanceLoopCharacteristics isSequential="${seq}">\n` +
|
|
23
|
+
` <bpmn:extensionElements>\n` +
|
|
24
|
+
` <zeebe:loopCharacteristics ${attrs.join(" ")} />\n` +
|
|
25
|
+
` </bpmn:extensionElements>\n` +
|
|
26
|
+
completion +
|
|
27
|
+
` </bpmn:multiInstanceLoopCharacteristics>\n`);
|
|
28
|
+
}
|
|
29
|
+
registerNodeKind("forEach", {
|
|
30
|
+
build: (api) => (collection, itemVar, body, opts) => {
|
|
31
|
+
if (typeof collection !== "string" || collection.trim() === "") {
|
|
32
|
+
throw new Error(`forEach() needs a non-empty FEEL collection expression`);
|
|
33
|
+
}
|
|
34
|
+
// Authors may paste a leading "=" (common in Zeebe FEEL examples). Strip it
|
|
35
|
+
// so the emitter's own "=" prefix never produces an invalid "==...".
|
|
36
|
+
const collExpr = collection.trim().replace(/^=/, "").trim();
|
|
37
|
+
if (collExpr === "") {
|
|
38
|
+
throw new Error(`forEach() collection expression is empty after stripping the leading "="`);
|
|
39
|
+
}
|
|
40
|
+
assertIdent("forEach itemVar", itemVar);
|
|
41
|
+
if (typeof body !== "function")
|
|
42
|
+
throw new Error(`forEach("${itemVar}") needs a body function`);
|
|
43
|
+
const nodes = api.childScoped(body);
|
|
44
|
+
if (nodes.length === 0)
|
|
45
|
+
throw new Error(`forEach("${itemVar}") body declared no steps`);
|
|
46
|
+
const node = { kind: "forEach", collection: collExpr, itemVar, body: nodes };
|
|
47
|
+
if (opts?.sequential)
|
|
48
|
+
node.sequential = true;
|
|
49
|
+
if (opts?.outputCollection !== undefined) {
|
|
50
|
+
assertIdent("forEach outputCollection", opts.outputCollection);
|
|
51
|
+
node.outputCollection = opts.outputCollection;
|
|
52
|
+
}
|
|
53
|
+
if (opts?.outputElement !== undefined) {
|
|
54
|
+
if (typeof opts.outputElement !== "string" || opts.outputElement.trim() === "") {
|
|
55
|
+
throw new Error(`forEach("${itemVar}") outputElement must be a non-empty FEEL expression`);
|
|
56
|
+
}
|
|
57
|
+
const outEl = opts.outputElement.trim().replace(/^=/, "").trim();
|
|
58
|
+
if (outEl === "") {
|
|
59
|
+
throw new Error(`forEach("${itemVar}") outputElement is empty after stripping the leading "="`);
|
|
60
|
+
}
|
|
61
|
+
node.outputElement = outEl;
|
|
62
|
+
}
|
|
63
|
+
if (opts?.completionCondition !== undefined) {
|
|
64
|
+
if (typeof opts.completionCondition !== "string" || opts.completionCondition.trim() === "") {
|
|
65
|
+
throw new Error(`forEach("${itemVar}") completionCondition must be a non-empty FEEL expression`);
|
|
66
|
+
}
|
|
67
|
+
const compCond = opts.completionCondition.trim().replace(/^=/, "").trim();
|
|
68
|
+
if (compCond === "") {
|
|
69
|
+
throw new Error(`forEach("${itemVar}") completionCondition is empty after stripping the leading "="`);
|
|
70
|
+
}
|
|
71
|
+
node.completionCondition = compCond;
|
|
72
|
+
}
|
|
73
|
+
if (node.outputElement && !node.outputCollection) {
|
|
74
|
+
throw new Error(`forEach("${itemVar}") outputElement needs an outputCollection to collect into`);
|
|
75
|
+
}
|
|
76
|
+
api.out.push(node);
|
|
77
|
+
return api.self();
|
|
78
|
+
},
|
|
79
|
+
walk: (node, recurse) => recurse(node.body),
|
|
80
|
+
emit: (node, incoming, _loop, api) => {
|
|
81
|
+
const mi = miCharacteristics(node);
|
|
82
|
+
const only = node.body.length === 1 ? node.body[0] : undefined;
|
|
83
|
+
// A single service-task body carries the MI characteristics directly; a
|
|
84
|
+
// multi-step body is wrapped in an embedded MI sub-process (its own token
|
|
85
|
+
// scope, with its own start/end). A prompt-carrying `task` is deliberately
|
|
86
|
+
// EXCLUDED from this fast path: computing its `zeebe:linkedResources`/
|
|
87
|
+
// `appendPrompt` delta lives in the `task` kind's own emitter, so lifting it
|
|
88
|
+
// here would silently drop that binding. It falls through to the sub-process
|
|
89
|
+
// path, where the `task` emitter renders the agent service task (prompt
|
|
90
|
+
// intact) inside the MI scope.
|
|
91
|
+
if (only && (only.kind === "run" || (only.kind === "task" && only.prompt === undefined))) {
|
|
92
|
+
api.addServiceTask(only, { mi });
|
|
93
|
+
api.connect(incoming, only.name);
|
|
94
|
+
return [api.newEdge(only.name)];
|
|
95
|
+
}
|
|
96
|
+
const subId = `Sub_${api.nextGw()}`;
|
|
97
|
+
api.addSubProcess(subId, mi);
|
|
98
|
+
api.connect(incoming, subId);
|
|
99
|
+
api.pushScope(subId);
|
|
100
|
+
const startId = `${subId}_start`;
|
|
101
|
+
api.addPlainStart(startId);
|
|
102
|
+
const innerOut = api.emitList(node.body, [api.newEdge(startId)], null);
|
|
103
|
+
const endId = `${subId}_end`;
|
|
104
|
+
api.addPlainEnd(endId);
|
|
105
|
+
api.connect(innerOut, endId);
|
|
106
|
+
api.popScope();
|
|
107
|
+
return [api.newEdge(subId)];
|
|
108
|
+
},
|
|
109
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "./branch.js";
|
|
2
|
+
import "./break.js";
|
|
3
|
+
import "./continue.js";
|
|
4
|
+
import "./forEach.js";
|
|
5
|
+
import "./loop.js";
|
|
6
|
+
import "./parallel.js";
|
|
7
|
+
import "./race.js";
|
|
8
|
+
import "./run.js";
|
|
9
|
+
import "./signal.js";
|
|
10
|
+
import "./switch.js";
|
|
11
|
+
import "./task.js";
|
|
12
|
+
import "./timer.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// GENERATED by scripts/gen-node-registry.mjs — do not edit by hand.
|
|
2
|
+
//
|
|
3
|
+
// The barrel of built-in flow-node kind modules. Importing this module runs
|
|
4
|
+
// every kind's `registerNodeKind(...)` side effect, populating the dispatch
|
|
5
|
+
// table `makeBuilder`/`walkNodes`/the emitter route through. A new kind is
|
|
6
|
+
// picked up automatically by re-running the generator (a prebuild step) — no
|
|
7
|
+
// hand-edit here.
|
|
8
|
+
import "./branch.js";
|
|
9
|
+
import "./break.js";
|
|
10
|
+
import "./continue.js";
|
|
11
|
+
import "./forEach.js";
|
|
12
|
+
import "./loop.js";
|
|
13
|
+
import "./parallel.js";
|
|
14
|
+
import "./race.js";
|
|
15
|
+
import "./run.js";
|
|
16
|
+
import "./signal.js";
|
|
17
|
+
import "./switch.js";
|
|
18
|
+
import "./task.js";
|
|
19
|
+
import "./timer.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Built-in flow-node kind: `loop` — a durable loop (a back-edge to the loop
|
|
2
|
+
// head): the body runs, then control returns to the top unless a branch calls
|
|
3
|
+
// `break()`. Contributed through the extension seam (epic #314, S0/#315).
|
|
4
|
+
import { registerNodeKind } from "./registry.js";
|
|
5
|
+
registerNodeKind("loop", {
|
|
6
|
+
build: (api) => (body) => {
|
|
7
|
+
if (typeof body !== "function")
|
|
8
|
+
throw new Error(`loop() needs a body function`);
|
|
9
|
+
api.out.push({ kind: "loop", body: api.child(body, true) });
|
|
10
|
+
return api.self();
|
|
11
|
+
},
|
|
12
|
+
walk: (node, recurse) => recurse(node.body),
|
|
13
|
+
emit: (node, incoming, _loop, api) => {
|
|
14
|
+
const headId = `Loop_${api.nextGw()}`;
|
|
15
|
+
api.addGateway(headId);
|
|
16
|
+
api.connect(incoming, headId);
|
|
17
|
+
const ctx = { headId, breaks: [] };
|
|
18
|
+
const headOut = api.newEdge(headId);
|
|
19
|
+
const bodyOut = api.emitList(node.body, [headOut], ctx);
|
|
20
|
+
// Normal fall-through of the body loops back to the head (continue).
|
|
21
|
+
api.connect(bodyOut, headId);
|
|
22
|
+
// The loop exits only via break edges.
|
|
23
|
+
return ctx.breaks;
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Built-in flow-node kind: `parallel` — a static parallel fork/join (a pair of
|
|
2
|
+
// BPMN parallel gateways): every block runs concurrently on its own branch, and
|
|
3
|
+
// control continues only once ALL branches reach the AND-join. Contributed
|
|
4
|
+
// through the extension seam (epic #314, S0/#315).
|
|
5
|
+
import { registerNodeKind } from "./registry.js";
|
|
6
|
+
registerNodeKind("parallel", {
|
|
7
|
+
build: (api) => (branches) => {
|
|
8
|
+
if (!Array.isArray(branches) || branches.length < 2) {
|
|
9
|
+
throw new Error(`parallel() needs at least two branch blocks`);
|
|
10
|
+
}
|
|
11
|
+
for (const fn of branches) {
|
|
12
|
+
if (typeof fn !== "function")
|
|
13
|
+
throw new Error(`parallel() branches must all be blocks (b) => {…}`);
|
|
14
|
+
}
|
|
15
|
+
// Each branch runs in its own token scope up to the AND-join, so a
|
|
16
|
+
// `break`/`continue` must not escape into an enclosing loop. Scope branches
|
|
17
|
+
// (loopDepth resets to 0) so cross-boundary break/continue is rejected.
|
|
18
|
+
api.out.push({ kind: "parallel", branches: branches.map((fn) => api.childScoped(fn)) });
|
|
19
|
+
return api.self();
|
|
20
|
+
},
|
|
21
|
+
walk: (node, recurse) => {
|
|
22
|
+
for (const branch of node.branches)
|
|
23
|
+
recurse(branch);
|
|
24
|
+
},
|
|
25
|
+
emit: (node, incoming, loop, api) => {
|
|
26
|
+
// A diverging parallel gateway forks a token onto every branch; a converging
|
|
27
|
+
// one joins them (AND-join) into a single continuation.
|
|
28
|
+
const splitId = `Gw_${api.nextGw()}`;
|
|
29
|
+
api.addParallelGateway(splitId);
|
|
30
|
+
api.connect(incoming, splitId);
|
|
31
|
+
const joinId = `Gw_${api.nextGw()}`;
|
|
32
|
+
api.addParallelGateway(joinId);
|
|
33
|
+
for (const branch of node.branches) {
|
|
34
|
+
const e = api.newEdge(splitId);
|
|
35
|
+
const branchOut = api.emitList(branch, [e], loop);
|
|
36
|
+
api.connect(branchOut, joinId);
|
|
37
|
+
}
|
|
38
|
+
return [api.newEdge(joinId)];
|
|
39
|
+
},
|
|
40
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { FlowNode } from "../types.js";
|
|
2
|
+
import type { Block } from "../declarative.js";
|
|
3
|
+
/** One arm of a {@link FlowBuilder.race}: exactly one waited-for event — a
|
|
4
|
+
* `signal` (a message catch correlated on a process variable) OR a `timer` (a
|
|
5
|
+
* delay `{ after }` or an instant `{ at }`) — plus the `do` body that runs when
|
|
6
|
+
* that event wins the race. */
|
|
7
|
+
export type RaceArm = ({
|
|
8
|
+
signal: {
|
|
9
|
+
correlationKey: string;
|
|
10
|
+
};
|
|
11
|
+
} | {
|
|
12
|
+
timer: {
|
|
13
|
+
after: string;
|
|
14
|
+
} | {
|
|
15
|
+
at: string;
|
|
16
|
+
};
|
|
17
|
+
}) & {
|
|
18
|
+
do: Block;
|
|
19
|
+
};
|
|
20
|
+
/** A compiled race arm: its catch event (a `signal` or `timer` `FlowNode`, reused
|
|
21
|
+
* from the built-ins) and the body that runs when it wins. */
|
|
22
|
+
interface CompiledArm {
|
|
23
|
+
event: FlowNode;
|
|
24
|
+
body: FlowNode[];
|
|
25
|
+
}
|
|
26
|
+
declare module "../types.js" {
|
|
27
|
+
interface FlowNodeRegistry {
|
|
28
|
+
race: {
|
|
29
|
+
kind: "race";
|
|
30
|
+
arms: CompiledArm[];
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
declare module "../declarative.js" {
|
|
35
|
+
interface FlowBuilder<C extends object> {
|
|
36
|
+
/**
|
|
37
|
+
* A first-of event race — a BPMN event-based gateway. Each named arm waits on
|
|
38
|
+
* exactly one event: a `signal` (a durable message catch correlated on a
|
|
39
|
+
* process variable) or a `timer` (`{ after }` a delay, or `{ at }` an
|
|
40
|
+
* instant). The engine arms every catch at once; the FIRST to fire wins, its
|
|
41
|
+
* arm's `do` body runs, and the losing catch events are cancelled. Every arm's
|
|
42
|
+
* body converges on whatever follows the race.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* w.race({
|
|
46
|
+
* "review-ready": { signal: { correlationKey: "prKey" }, do: (b) => b.run("re-review", reReview) },
|
|
47
|
+
* "review-timeout": { timer: { after: "=reviewWaitTimeout" }, do: (b) => b.run("escalate", escalate) },
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
race(arms: Record<string, RaceArm>): FlowBuilder<C>;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Flow-node kind: `race` — a first-of event race (a BPMN event-based gateway).
|
|
2
|
+
// The gateway forks to one intermediate catch event per arm — a MESSAGE catch for
|
|
3
|
+
// a `signal` arm (correlated on a process variable) or a TIMER catch for a `timer`
|
|
4
|
+
// arm — and the engine arms them all at once. The FIRST event to fire wins: its
|
|
5
|
+
// arm's `do` body runs and the losing catch events are cancelled; each arm's body
|
|
6
|
+
// then converges on whatever follows the race.
|
|
7
|
+
//
|
|
8
|
+
// Contributed entirely from this module through the wave-0 extension seam (epic
|
|
9
|
+
// #314, S1/#316): its `FlowNode` variant (declaration-merged into the augmentable
|
|
10
|
+
// `FlowNodeRegistry`), its `w.race(...)` builder method (merged into
|
|
11
|
+
// `FlowBuilder<C>`), and its `walk`/`emit` handlers — with NO edit to the shared
|
|
12
|
+
// `FlowNode` union, the `walkNodes`/`emitNode` dispatch, or the generated barrel.
|
|
13
|
+
//
|
|
14
|
+
// The catch arms are modelled as nested `signal`/`timer` `FlowNode`s so the whole
|
|
15
|
+
// machine reuses the built-ins: `walk` recurses into them (so the shared message
|
|
16
|
+
// emitter lifts each `signal` arm's `<bpmn:message>` + correlation subscription
|
|
17
|
+
// automatically, and `WorkflowClient.signal` can correlate to it), and `emit`
|
|
18
|
+
// dispatches each arm's catch through its own registered handler (so a race's
|
|
19
|
+
// catch is byte-identical to a standalone `w.signal`/`w.timer`). Only the
|
|
20
|
+
// event-based gateway itself is placed here.
|
|
21
|
+
import { incomingOutgoing } from "../declarative.js";
|
|
22
|
+
import { assertIdent, assertTimerDate, assertTimerDuration, escapeXml } from "../xml.js";
|
|
23
|
+
import { registerNodeKind, requireNodeKind } from "./registry.js";
|
|
24
|
+
function hasSignal(spec) {
|
|
25
|
+
return "signal" in spec && spec.signal !== null && typeof spec.signal === "object";
|
|
26
|
+
}
|
|
27
|
+
function hasTimer(spec) {
|
|
28
|
+
return "timer" in spec && spec.timer !== null && typeof spec.timer === "object";
|
|
29
|
+
}
|
|
30
|
+
registerNodeKind("race", {
|
|
31
|
+
build: (api) => (arms) => {
|
|
32
|
+
if (arms === null || typeof arms !== "object") {
|
|
33
|
+
throw new Error(`race() needs an arms object { "<name>": { signal | timer, do } }`);
|
|
34
|
+
}
|
|
35
|
+
const entries = Object.entries(arms);
|
|
36
|
+
if (entries.length < 2) {
|
|
37
|
+
throw new Error(`race() needs at least two arms (an event-based gateway with fewer than two events is degenerate)`);
|
|
38
|
+
}
|
|
39
|
+
const compiled = [];
|
|
40
|
+
for (const [name, spec] of entries) {
|
|
41
|
+
if (spec === null || typeof spec !== "object") {
|
|
42
|
+
throw new Error(`race() arm "${name}" must be an object { signal | timer, do }`);
|
|
43
|
+
}
|
|
44
|
+
const isSignal = hasSignal(spec);
|
|
45
|
+
const isTimer = hasTimer(spec);
|
|
46
|
+
if (isSignal === isTimer) {
|
|
47
|
+
throw new Error(`race() arm "${name}" needs exactly one of { signal: { correlationKey } } or { timer: { after | at } }`);
|
|
48
|
+
}
|
|
49
|
+
if (typeof spec.do !== "function") {
|
|
50
|
+
throw new Error(`race() arm "${name}" needs a do block (b) => {…}`);
|
|
51
|
+
}
|
|
52
|
+
api.claim(name);
|
|
53
|
+
let event;
|
|
54
|
+
if (isSignal) {
|
|
55
|
+
const rawKey = spec.signal.correlationKey;
|
|
56
|
+
if (typeof rawKey !== "string" || rawKey.trim() === "") {
|
|
57
|
+
throw new Error(`race() arm "${name}" signal needs a non-empty { correlationKey }`);
|
|
58
|
+
}
|
|
59
|
+
// Trim before validating/storing, in parity with the timer arm ({ after }/
|
|
60
|
+
// { at } are trimmed above): a value like " prKey " is usable after
|
|
61
|
+
// trimming, so it must not fail the NCName check on stray whitespace. The
|
|
62
|
+
// assertIdent context names the arm so a failure says WHICH arm is invalid.
|
|
63
|
+
const correlationKey = rawKey.trim();
|
|
64
|
+
assertIdent(`race() arm "${name}" signal correlationKey`, correlationKey);
|
|
65
|
+
event = { kind: "signal", name, correlationKey, payload: api.contracts[name]?.in };
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const timer = spec.timer;
|
|
69
|
+
// Enforce exactly-one on KEY PRESENCE, not string-ness: a runtime-invalid
|
|
70
|
+
// arm that supplies both keys where only one is a string (e.g.
|
|
71
|
+
// `{ after: 1, at: "…" }`) must be rejected here, not silently dispatched
|
|
72
|
+
// into `after.trim()` — which would throw an opaque TypeError. Once the
|
|
73
|
+
// branch is chosen, validate the value is a string before trimming.
|
|
74
|
+
const hasAfterKey = "after" in timer;
|
|
75
|
+
const hasAtKey = "at" in timer;
|
|
76
|
+
if (hasAfterKey === hasAtKey) {
|
|
77
|
+
throw new Error(`race() arm "${name}" timer needs exactly one of { after } (a delay) or { at } (an instant)`);
|
|
78
|
+
}
|
|
79
|
+
if ("after" in timer) {
|
|
80
|
+
if (typeof timer.after !== "string") {
|
|
81
|
+
throw new Error(`race() arm "${name}" timer { after } must be a string duration`);
|
|
82
|
+
}
|
|
83
|
+
const after = timer.after.trim();
|
|
84
|
+
assertTimerDuration(`race() arm "${name}" timer after`, after);
|
|
85
|
+
event = { kind: "timer", name, after };
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (typeof timer.at !== "string") {
|
|
89
|
+
throw new Error(`race() arm "${name}" timer { at } must be a string instant`);
|
|
90
|
+
}
|
|
91
|
+
const at = timer.at.trim();
|
|
92
|
+
assertTimerDate(`race() arm "${name}" timer at`, at);
|
|
93
|
+
event = { kind: "timer", name, at };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
compiled.push({ event, body: api.child(spec.do, false) });
|
|
97
|
+
}
|
|
98
|
+
api.out.push({ kind: "race", arms: compiled });
|
|
99
|
+
return api.self();
|
|
100
|
+
},
|
|
101
|
+
walk: (node, recurse) => {
|
|
102
|
+
// Recurse into each arm's catch event AND its body: the catch `signal` nodes
|
|
103
|
+
// must be visited so the shared message emitter lifts their `<bpmn:message>`
|
|
104
|
+
// + correlation subscription, and body `task` nodes so their external job
|
|
105
|
+
// types are discovered — exactly as a standalone `signal`/`task` would be.
|
|
106
|
+
for (const arm of node.arms)
|
|
107
|
+
recurse([arm.event, ...arm.body]);
|
|
108
|
+
},
|
|
109
|
+
emit: (node, incoming, loop, api) => {
|
|
110
|
+
const gwId = `Gw_${api.nextGw()}`;
|
|
111
|
+
api.addNode(eventBasedGateway(gwId, api.currentScope()));
|
|
112
|
+
api.connect(incoming, gwId);
|
|
113
|
+
const out = [];
|
|
114
|
+
for (const arm of node.arms) {
|
|
115
|
+
// Fork the gateway straight to the arm's catch event (BPMN requires an
|
|
116
|
+
// event-based gateway's targets to be intermediate catch events), reusing
|
|
117
|
+
// the arm kind's own registered emit so the catch is identical to a
|
|
118
|
+
// standalone `w.signal`/`w.timer`; then wire its `do` body onward.
|
|
119
|
+
const fork = api.newEdge(gwId);
|
|
120
|
+
const afterCatch = requireNodeKind(arm.event.kind).emit(arm.event, [fork], loop, api);
|
|
121
|
+
out.push(...api.emitList(arm.body, afterCatch, loop));
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
/** A `<bpmn:eventBasedGateway>` render node (no typed `EmitApi` primitive exists —
|
|
127
|
+
* built as a custom {@link RenderNode} from the exported string helpers). */
|
|
128
|
+
function eventBasedGateway(id, scope) {
|
|
129
|
+
return {
|
|
130
|
+
id,
|
|
131
|
+
scope,
|
|
132
|
+
render: (inc, outg) => ` <bpmn:eventBasedGateway id="${escapeXml(id)}">\n` + incomingOutgoing(inc, outg) + ` </bpmn:eventBasedGateway>`,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FlowNode } from "../types.js";
|
|
2
|
+
import type { BuildApi, BuilderMethod, Edge, EmitApi, LoopCtx } from "../declarative.js";
|
|
3
|
+
/** Recurse the tree walker into a node's nested bodies (see `walkNodes`). */
|
|
4
|
+
export type WalkRecurse = (body: FlowNode[]) => void;
|
|
5
|
+
/** The three handlers a flow-node kind contributes. `N` is the kind's own
|
|
6
|
+
* `FlowNode` variant (narrowed from the union by the registry key). */
|
|
7
|
+
export interface NodeKindHandlers<N extends FlowNode = FlowNode> {
|
|
8
|
+
/** Factory for the `FlowBuilder` method that appends this kind's node. Given
|
|
9
|
+
* the per-body {@link BuildApi}, returns the method installed on the builder
|
|
10
|
+
* as `w.<builderMethod ?? kind>`. Omit for a kind with no authoring method. */
|
|
11
|
+
build?: (api: BuildApi) => BuilderMethod;
|
|
12
|
+
/** The builder method name, when it differs from the kind (defaults to the
|
|
13
|
+
* kind). */
|
|
14
|
+
builderMethod?: string;
|
|
15
|
+
/** Depth-first recursion into the node's nested bodies. Omit for a leaf. */
|
|
16
|
+
walk?: (node: N, recurse: WalkRecurse) => void;
|
|
17
|
+
/** Emit this node into the BPMN graph, returning the outgoing (dangling)
|
|
18
|
+
* edges its continuation attaches to. */
|
|
19
|
+
emit: (node: N, incoming: Edge[], loop: LoopCtx | null, api: EmitApi) => Edge[];
|
|
20
|
+
}
|
|
21
|
+
/** Register the handlers for one flow-node kind. Called once, at module import
|
|
22
|
+
* time, from `src/nodes/<kind>.ts`. */
|
|
23
|
+
export declare function registerNodeKind<K extends FlowNode["kind"]>(kind: K, handlers: NodeKindHandlers<Extract<FlowNode, {
|
|
24
|
+
kind: K;
|
|
25
|
+
}>>): void;
|
|
26
|
+
/** The handlers registered for a kind, or `undefined` if none. */
|
|
27
|
+
export declare function nodeKind(kind: string): NodeKindHandlers | undefined;
|
|
28
|
+
/** The registered kind, or a thrown error naming the unknown kind — used by the
|
|
29
|
+
* emitter, which cannot proceed without a handler. */
|
|
30
|
+
export declare function requireNodeKind(kind: string): NodeKindHandlers;
|
|
31
|
+
/** Every registered kind + its handlers, in registration order. Used by
|
|
32
|
+
* `makeBuilder` to install each kind's builder method. */
|
|
33
|
+
export declare function eachNodeKind(): Iterable<[string, NodeKindHandlers]>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The builder-extension seam (epic #314, S0/#315).
|
|
2
|
+
//
|
|
3
|
+
// Every flow-node KIND (`run`/`task`/`signal`/`timer`/`switch`/… and any kind a
|
|
4
|
+
// feature slice adds) contributes THREE things from its own module under
|
|
5
|
+
// `src/nodes/`, and NOTHING is edited centrally:
|
|
6
|
+
//
|
|
7
|
+
// 1. a `FlowNode` variant — by declaration-merging its shape into the
|
|
8
|
+
// augmentable `FlowNodeRegistry` interface (see `types.ts`), so the
|
|
9
|
+
// `FlowNode` union grows without editing a central union;
|
|
10
|
+
// 2. a builder method — by declaration-merging its signature into the
|
|
11
|
+
// `FlowBuilder<C>` interface (see `declarative.ts`) and registering a
|
|
12
|
+
// `build` factory here, so `w.<method>(…)` is contributed from the module;
|
|
13
|
+
// 3. `walk` + `emit` handlers — registered here, so the tree walker and the
|
|
14
|
+
// BPMN emitter DISPATCH through this table instead of a central `switch`.
|
|
15
|
+
//
|
|
16
|
+
// A module registers all three with a single `registerNodeKind(kind, {…})` call
|
|
17
|
+
// at import time; the generated barrel `src/nodes/index.ts` imports every kind
|
|
18
|
+
// module for its registration side effect. Adding a kind is therefore: drop one
|
|
19
|
+
// `src/nodes/<kind>.ts` file — no edit to the union, the dispatch, or the barrel.
|
|
20
|
+
//
|
|
21
|
+
// See `src/nodes/README.md` for the exact pattern a slice follows.
|
|
22
|
+
// The dispatcher always looks a handler up by `node.kind` before calling it, so
|
|
23
|
+
// each stored handler only ever receives a node of its own kind. Storing the
|
|
24
|
+
// widened `NodeKindHandlers<FlowNode>` is therefore sound; a kind registers with
|
|
25
|
+
// its own narrowed variant type for authoring ergonomics.
|
|
26
|
+
const kinds = new Map();
|
|
27
|
+
/** Register the handlers for one flow-node kind. Called once, at module import
|
|
28
|
+
* time, from `src/nodes/<kind>.ts`. */
|
|
29
|
+
export function registerNodeKind(kind, handlers) {
|
|
30
|
+
if (kinds.has(kind)) {
|
|
31
|
+
throw new Error(`flow-node kind "${kind}" is already registered`);
|
|
32
|
+
}
|
|
33
|
+
const walk = handlers.walk;
|
|
34
|
+
const emit = handlers.emit;
|
|
35
|
+
kinds.set(kind, {
|
|
36
|
+
builderMethod: handlers.builderMethod,
|
|
37
|
+
build: handlers.build,
|
|
38
|
+
walk: walk ? (node, recurse) => walk(narrow(node, kind), recurse) : undefined,
|
|
39
|
+
emit: (node, incoming, loop, api) => emit(narrow(node, kind), incoming, loop, api),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Narrow a dispatched node back to a kind's own variant. The discriminant
|
|
43
|
+
* check is the proof — no `as` cast. The dispatcher guarantees `node.kind ===
|
|
44
|
+
* kind`, so the throw is unreachable in practice. */
|
|
45
|
+
function narrow(node, kind) {
|
|
46
|
+
if (isKind(node, kind))
|
|
47
|
+
return node;
|
|
48
|
+
throw new Error(`internal: node of kind "${node.kind}" dispatched to handler for "${kind}"`);
|
|
49
|
+
}
|
|
50
|
+
function isKind(node, kind) {
|
|
51
|
+
return node.kind === kind;
|
|
52
|
+
}
|
|
53
|
+
/** The handlers registered for a kind, or `undefined` if none. */
|
|
54
|
+
export function nodeKind(kind) {
|
|
55
|
+
return kinds.get(kind);
|
|
56
|
+
}
|
|
57
|
+
/** The registered kind, or a thrown error naming the unknown kind — used by the
|
|
58
|
+
* emitter, which cannot proceed without a handler. */
|
|
59
|
+
export function requireNodeKind(kind) {
|
|
60
|
+
const h = kinds.get(kind);
|
|
61
|
+
if (!h) {
|
|
62
|
+
throw new Error(`no handler registered for flow-node kind "${kind}" — ` +
|
|
63
|
+
`did its module under src/nodes/ import and call registerNodeKind()?`);
|
|
64
|
+
}
|
|
65
|
+
return h;
|
|
66
|
+
}
|
|
67
|
+
/** Every registered kind + its handlers, in registration order. Used by
|
|
68
|
+
* `makeBuilder` to install each kind's builder method. */
|
|
69
|
+
export function eachNodeKind() {
|
|
70
|
+
return kinds.entries();
|
|
71
|
+
}
|