-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
55 lines (48 loc) · 1.65 KB
/
Copy pathexample.js
File metadata and controls
55 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* rail.js v0.3.0 — overview demo.
*
* A small workflow with: an atomic step, a sub-activity, a parallel
* fan-out with merge, and a railway pipeline. Run with `npm run example`.
*/
import {
activity, atom, step, parallel, railway, pin, flow,
} from './rail.js';
const validateInput = step(async (ctx) => {
if (!ctx.userId) throw new Error('userId required');
});
const enrich = parallel({
profile: step(async (ctx) => { ctx.profile = `profile:${ctx.userId}`; }),
orders: step(async (ctx) => { ctx.orders = `orders:${ctx.userId}`; }),
}, atom(async (ctx) => {
const userId = ctx.profile.userId;
const profile = ctx.profile.profile;
const orders = ctx.orders.orders;
for (const k of Object.keys(ctx)) delete ctx[k];
ctx.userId = userId;
ctx.profile = profile;
ctx.orders = orders;
return 'out';
}, { outputs: ['out'] }));
const report = railway((r) => {
r.step('format', async (ctx) => {
ctx.report = `${ctx.userId} → ${ctx.profile} | ${ctx.orders}`;
});
r.pass('audit', async () => { /* best-effort logging */ });
});
const main = activity((a) => {
a.entry('in');
a.addNode('validate', validateInput);
a.addNode('enrich', enrich);
a.addNode('report', pin(report, 'success'));
a.exit('done');
a.exit('invalid');
a.wire('.in', 'validate.success');
a.wire('validate.success', 'enrich.in');
a.wire('validate.failure', '.invalid');
a.wire('enrich.out', 'report.in');
a.wire('report.success', '.done');
a.wire('report.failure', '.invalid');
});
const r = await flow('overview', main).run({ userId: 'u-1' });
console.log('exit:', r.exit);
console.log('ctx :', r.ctx);