@nocobase/flow-engine 2.1.0-beta.5 → 2.1.0-beta.7
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/lib/executor/FlowExecutor.js +25 -8
- package/lib/scheduler/ModelOperationScheduler.d.ts +5 -1
- package/lib/scheduler/ModelOperationScheduler.js +3 -2
- package/package.json +4 -4
- package/src/executor/FlowExecutor.ts +28 -9
- package/src/executor/__tests__/flowExecutor.test.ts +26 -0
- package/src/models/__tests__/dispatchEvent.when.test.ts +214 -0
- package/src/scheduler/ModelOperationScheduler.ts +14 -3
|
@@ -213,11 +213,13 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
213
213
|
flowContext.logger.info(`[FlowEngine] ${error.message}`);
|
|
214
214
|
await this.emitModelEventIf(eventName, `flow:${flowKey}:step:${stepKey}:end`, {
|
|
215
215
|
...flowEventBasePayload,
|
|
216
|
-
stepKey
|
|
216
|
+
stepKey,
|
|
217
|
+
aborted: true
|
|
217
218
|
});
|
|
218
219
|
await this.emitModelEventIf(eventName, `flow:${flowKey}:end`, {
|
|
219
220
|
...flowEventBasePayload,
|
|
220
|
-
result: error
|
|
221
|
+
result: error,
|
|
222
|
+
aborted: true
|
|
221
223
|
});
|
|
222
224
|
return Promise.resolve(error);
|
|
223
225
|
}
|
|
@@ -287,6 +289,7 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
287
289
|
}) : flows;
|
|
288
290
|
const scheduledCancels = [];
|
|
289
291
|
const execute = /* @__PURE__ */ __name(async () => {
|
|
292
|
+
let abortedByExitAll = false;
|
|
290
293
|
if (sequential) {
|
|
291
294
|
const flowsWithIndex = flowsToRun.map((f, i) => ({ f, i }));
|
|
292
295
|
const ordered = flowsWithIndex.slice().sort((a, b) => {
|
|
@@ -355,9 +358,10 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
355
358
|
return;
|
|
356
359
|
}
|
|
357
360
|
if (!whenKey) return;
|
|
361
|
+
const shouldSkipOnAborted = whenKey === `event:${eventName}:end` || phase === "afterFlow" || phase === "afterStep";
|
|
358
362
|
scheduled.add(flow.key);
|
|
359
363
|
const list = scheduleGroups.get(whenKey) || [];
|
|
360
|
-
list.push({ flow, order: indexInOrdered });
|
|
364
|
+
list.push({ flow, order: indexInOrdered, shouldSkipOnAborted });
|
|
361
365
|
scheduleGroups.set(whenKey, list);
|
|
362
366
|
});
|
|
363
367
|
for (const [whenKey, list] of scheduleGroups.entries()) {
|
|
@@ -368,6 +372,12 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
368
372
|
return a.order - b.order;
|
|
369
373
|
});
|
|
370
374
|
for (const it of sorted) {
|
|
375
|
+
const when = it.shouldSkipOnAborted ? Object.assign(
|
|
376
|
+
(event) => event.type === whenKey && event.aborted !== true,
|
|
377
|
+
{
|
|
378
|
+
__eventType: whenKey
|
|
379
|
+
}
|
|
380
|
+
) : whenKey;
|
|
371
381
|
const cancel = model.scheduleModelOperation(
|
|
372
382
|
model.uid,
|
|
373
383
|
async (m) => {
|
|
@@ -377,7 +387,7 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
377
387
|
}
|
|
378
388
|
results2.push(res);
|
|
379
389
|
},
|
|
380
|
-
{ when
|
|
390
|
+
{ when }
|
|
381
391
|
);
|
|
382
392
|
scheduledCancels.push(cancel);
|
|
383
393
|
}
|
|
@@ -391,12 +401,14 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
391
401
|
const result = await this.runFlow(model, flow.key, inputArgs, runId, eventName);
|
|
392
402
|
if (result instanceof import_exceptions.FlowExitAllException) {
|
|
393
403
|
logger.debug(`[FlowEngine.dispatchEvent] ${result.message}`);
|
|
404
|
+
abortedByExitAll = true;
|
|
394
405
|
break;
|
|
395
406
|
}
|
|
396
407
|
results2.push(result);
|
|
397
408
|
} catch (error) {
|
|
398
409
|
if (error instanceof import_exceptions.FlowExitAllException) {
|
|
399
410
|
logger.debug(`[FlowEngine.dispatchEvent] ${error.message}`);
|
|
411
|
+
abortedByExitAll = true;
|
|
400
412
|
break;
|
|
401
413
|
}
|
|
402
414
|
logger.error(
|
|
@@ -406,7 +418,7 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
406
418
|
throw error;
|
|
407
419
|
}
|
|
408
420
|
}
|
|
409
|
-
return results2;
|
|
421
|
+
return { result: results2, abortedByExitAll };
|
|
410
422
|
}
|
|
411
423
|
const results = await Promise.all(
|
|
412
424
|
flowsToRun.map(async (flow) => {
|
|
@@ -423,7 +435,11 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
423
435
|
}
|
|
424
436
|
})
|
|
425
437
|
);
|
|
426
|
-
|
|
438
|
+
const filteredResults = results.filter((x) => x !== void 0);
|
|
439
|
+
if (filteredResults.some((x) => x instanceof import_exceptions.FlowExitAllException)) {
|
|
440
|
+
abortedByExitAll = true;
|
|
441
|
+
}
|
|
442
|
+
return { result: filteredResults, abortedByExitAll };
|
|
427
443
|
}, "execute");
|
|
428
444
|
const argsKey = useCache ? JSON.stringify(inputArgs ?? {}) : "";
|
|
429
445
|
const cacheKey = useCache ? import_flowEngine.FlowEngine.generateApplyFlowCacheKey(
|
|
@@ -432,7 +448,7 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
432
448
|
model.uid
|
|
433
449
|
) : null;
|
|
434
450
|
try {
|
|
435
|
-
const result = await this.withApplyFlowCache(cacheKey, execute);
|
|
451
|
+
const { result, abortedByExitAll } = await this.withApplyFlowCache(cacheKey, execute);
|
|
436
452
|
try {
|
|
437
453
|
await ((_c = model.onDispatchEventEnd) == null ? void 0 : _c.call(model, eventName, options, inputArgs, result));
|
|
438
454
|
} catch (hookErr) {
|
|
@@ -440,7 +456,8 @@ const _FlowExecutor = class _FlowExecutor {
|
|
|
440
456
|
}
|
|
441
457
|
await this.emitModelEventIf(eventName, "end", {
|
|
442
458
|
...eventBasePayload,
|
|
443
|
-
result
|
|
459
|
+
result,
|
|
460
|
+
...abortedByExitAll ? { aborted: true } : {}
|
|
444
461
|
});
|
|
445
462
|
return result;
|
|
446
463
|
} catch (error) {
|
|
@@ -9,7 +9,10 @@
|
|
|
9
9
|
import type { FlowEngine } from '../flowEngine';
|
|
10
10
|
import type { FlowModel } from '../models/flowModel';
|
|
11
11
|
type LifecycleType = 'created' | 'mounted' | 'unmounted' | 'destroyed' | `event:${string}:start` | `event:${string}:end` | `event:${string}:error`;
|
|
12
|
-
|
|
12
|
+
type EventPredicateWhen = ((e: LifecycleEvent) => boolean) & {
|
|
13
|
+
__eventType?: string;
|
|
14
|
+
};
|
|
15
|
+
export type ScheduleWhen = LifecycleType | EventPredicateWhen;
|
|
13
16
|
export interface ScheduleOptions {
|
|
14
17
|
when?: ScheduleWhen;
|
|
15
18
|
}
|
|
@@ -22,6 +25,7 @@ export interface LifecycleEvent {
|
|
|
22
25
|
error?: any;
|
|
23
26
|
inputArgs?: Record<string, any>;
|
|
24
27
|
result?: any;
|
|
28
|
+
aborted?: boolean;
|
|
25
29
|
flowKey?: string;
|
|
26
30
|
stepKey?: string;
|
|
27
31
|
}
|
|
@@ -171,8 +171,9 @@ const _ModelOperationScheduler = class _ModelOperationScheduler {
|
|
|
171
171
|
this.unbindHandlers.push(() => emitter.off("model:destroyed", onDestroyed));
|
|
172
172
|
}
|
|
173
173
|
ensureEventSubscriptionIfNeeded(when) {
|
|
174
|
-
|
|
175
|
-
|
|
174
|
+
const eventType = typeof when === "string" ? when : typeof when === "function" ? when.__eventType : void 0;
|
|
175
|
+
if (!eventType) return;
|
|
176
|
+
const parsed = this.parseEventWhen(eventType);
|
|
176
177
|
if (!parsed) return;
|
|
177
178
|
const { name } = parsed;
|
|
178
179
|
if (this.subscribedEventNames.has(name)) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/flow-engine",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A standalone flow engine for NocoBase, managing workflows, models, and actions.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@formily/antd-v5": "1.x",
|
|
10
10
|
"@formily/reactive": "2.x",
|
|
11
|
-
"@nocobase/sdk": "2.1.0-beta.
|
|
12
|
-
"@nocobase/shared": "2.1.0-beta.
|
|
11
|
+
"@nocobase/sdk": "2.1.0-beta.7",
|
|
12
|
+
"@nocobase/shared": "2.1.0-beta.7",
|
|
13
13
|
"ahooks": "^3.7.2",
|
|
14
14
|
"dayjs": "^1.11.9",
|
|
15
15
|
"dompurify": "^3.0.2",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
],
|
|
37
37
|
"author": "NocoBase Team",
|
|
38
38
|
"license": "Apache-2.0",
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "da7dfef2b6d6854988a56119463c8c38e3221e79"
|
|
40
40
|
}
|
|
@@ -224,10 +224,12 @@ export class FlowExecutor {
|
|
|
224
224
|
await this.emitModelEventIf(eventName, `flow:${flowKey}:step:${stepKey}:end`, {
|
|
225
225
|
...flowEventBasePayload,
|
|
226
226
|
stepKey,
|
|
227
|
+
aborted: true,
|
|
227
228
|
});
|
|
228
229
|
await this.emitModelEventIf(eventName, `flow:${flowKey}:end`, {
|
|
229
230
|
...flowEventBasePayload,
|
|
230
231
|
result: error,
|
|
232
|
+
aborted: true,
|
|
231
233
|
});
|
|
232
234
|
return Promise.resolve(error);
|
|
233
235
|
}
|
|
@@ -316,9 +318,9 @@ export class FlowExecutor {
|
|
|
316
318
|
|
|
317
319
|
// 记录本次 dispatchEvent 内注册的调度任务,用于在结束/错误后兜底清理未触发的任务
|
|
318
320
|
const scheduledCancels: ScheduledCancel[] = [];
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
321
|
+
// 组装执行函数(返回值用于缓存,包含事件结果与是否被 exitAll 中止)
|
|
322
|
+
const execute = async (): Promise<{ result: any[]; abortedByExitAll: boolean }> => {
|
|
323
|
+
let abortedByExitAll = false;
|
|
322
324
|
if (sequential) {
|
|
323
325
|
// 顺序执行:动态流(实例级)优先,其次静态流;各自组内再按 sort 升序,最后保持原始顺序稳定
|
|
324
326
|
const flowsWithIndex = flowsToRun.map((f, i) => ({ f, i }));
|
|
@@ -351,7 +353,7 @@ export class FlowExecutor {
|
|
|
351
353
|
.map((f) => [f.key, f] as const),
|
|
352
354
|
);
|
|
353
355
|
const scheduled = new Set<string>();
|
|
354
|
-
const scheduleGroups = new Map<string, Array<{ flow: any; order: number }>>();
|
|
356
|
+
const scheduleGroups = new Map<string, Array<{ flow: any; order: number; shouldSkipOnAborted: boolean }>>();
|
|
355
357
|
ordered.forEach((flow, indexInOrdered) => {
|
|
356
358
|
const on = flow.on;
|
|
357
359
|
const onObj = typeof on === 'object' ? (on as any) : undefined;
|
|
@@ -402,9 +404,11 @@ export class FlowExecutor {
|
|
|
402
404
|
}
|
|
403
405
|
|
|
404
406
|
if (!whenKey) return;
|
|
407
|
+
const shouldSkipOnAborted =
|
|
408
|
+
whenKey === `event:${eventName}:end` || phase === 'afterFlow' || phase === 'afterStep';
|
|
405
409
|
scheduled.add(flow.key);
|
|
406
410
|
const list = scheduleGroups.get(whenKey) || [];
|
|
407
|
-
list.push({ flow, order: indexInOrdered });
|
|
411
|
+
list.push({ flow, order: indexInOrdered, shouldSkipOnAborted });
|
|
408
412
|
scheduleGroups.set(whenKey, list);
|
|
409
413
|
});
|
|
410
414
|
|
|
@@ -417,6 +421,14 @@ export class FlowExecutor {
|
|
|
417
421
|
return a.order - b.order;
|
|
418
422
|
});
|
|
419
423
|
for (const it of sorted) {
|
|
424
|
+
const when = it.shouldSkipOnAborted
|
|
425
|
+
? Object.assign(
|
|
426
|
+
(event: { type: string; aborted?: boolean }) => event.type === whenKey && event.aborted !== true,
|
|
427
|
+
{
|
|
428
|
+
__eventType: whenKey,
|
|
429
|
+
},
|
|
430
|
+
)
|
|
431
|
+
: (whenKey as any);
|
|
420
432
|
const cancel = model.scheduleModelOperation(
|
|
421
433
|
model.uid,
|
|
422
434
|
async (m) => {
|
|
@@ -426,7 +438,7 @@ export class FlowExecutor {
|
|
|
426
438
|
}
|
|
427
439
|
results.push(res);
|
|
428
440
|
},
|
|
429
|
-
{ when
|
|
441
|
+
{ when },
|
|
430
442
|
);
|
|
431
443
|
scheduledCancels.push(cancel);
|
|
432
444
|
}
|
|
@@ -441,12 +453,14 @@ export class FlowExecutor {
|
|
|
441
453
|
const result = await this.runFlow(model, flow.key, inputArgs, runId, eventName);
|
|
442
454
|
if (result instanceof FlowExitAllException) {
|
|
443
455
|
logger.debug(`[FlowEngine.dispatchEvent] ${result.message}`);
|
|
456
|
+
abortedByExitAll = true;
|
|
444
457
|
break; // 终止后续
|
|
445
458
|
}
|
|
446
459
|
results.push(result);
|
|
447
460
|
} catch (error) {
|
|
448
461
|
if (error instanceof FlowExitAllException) {
|
|
449
462
|
logger.debug(`[FlowEngine.dispatchEvent] ${error.message}`);
|
|
463
|
+
abortedByExitAll = true;
|
|
450
464
|
break; // 终止后续
|
|
451
465
|
}
|
|
452
466
|
logger.error(
|
|
@@ -456,7 +470,7 @@ export class FlowExecutor {
|
|
|
456
470
|
throw error;
|
|
457
471
|
}
|
|
458
472
|
}
|
|
459
|
-
return results;
|
|
473
|
+
return { result: results, abortedByExitAll };
|
|
460
474
|
}
|
|
461
475
|
|
|
462
476
|
// 并行
|
|
@@ -475,7 +489,11 @@ export class FlowExecutor {
|
|
|
475
489
|
}
|
|
476
490
|
}),
|
|
477
491
|
);
|
|
478
|
-
|
|
492
|
+
const filteredResults = results.filter((x) => x !== undefined);
|
|
493
|
+
if (filteredResults.some((x) => x instanceof FlowExitAllException)) {
|
|
494
|
+
abortedByExitAll = true;
|
|
495
|
+
}
|
|
496
|
+
return { result: filteredResults, abortedByExitAll };
|
|
479
497
|
};
|
|
480
498
|
|
|
481
499
|
// 缓存键:按事件+scope 统一管理(beforeRender 也使用事件名 beforeRender)
|
|
@@ -489,7 +507,7 @@ export class FlowExecutor {
|
|
|
489
507
|
: null;
|
|
490
508
|
|
|
491
509
|
try {
|
|
492
|
-
const result = await this.withApplyFlowCache(cacheKey, execute);
|
|
510
|
+
const { result, abortedByExitAll } = await this.withApplyFlowCache(cacheKey, execute);
|
|
493
511
|
// 事件结束钩子
|
|
494
512
|
try {
|
|
495
513
|
await model.onDispatchEventEnd?.(eventName, options, inputArgs, result);
|
|
@@ -499,6 +517,7 @@ export class FlowExecutor {
|
|
|
499
517
|
await this.emitModelEventIf(eventName, 'end', {
|
|
500
518
|
...eventBasePayload,
|
|
501
519
|
result,
|
|
520
|
+
...(abortedByExitAll ? { aborted: true } : {}),
|
|
502
521
|
});
|
|
503
522
|
return result;
|
|
504
523
|
} catch (error) {
|
|
@@ -288,6 +288,32 @@ describe('FlowExecutor', () => {
|
|
|
288
288
|
expect(handler).toHaveBeenCalledTimes(2); // 每个 flow 各 1 次,共 2 次
|
|
289
289
|
});
|
|
290
290
|
|
|
291
|
+
it("dispatchEvent('beforeRender') keeps aborted flag on end event when cache hits", async () => {
|
|
292
|
+
const handler = vi.fn().mockImplementation((ctx) => {
|
|
293
|
+
ctx.exitAll();
|
|
294
|
+
});
|
|
295
|
+
const flows = {
|
|
296
|
+
abortFlow: { steps: { s: { handler } } },
|
|
297
|
+
} satisfies Record<string, Omit<FlowDefinitionOptions, 'key'>>;
|
|
298
|
+
const model = createModelWithFlows('m-br-cache-aborted', flows);
|
|
299
|
+
|
|
300
|
+
const endEvents: any[] = [];
|
|
301
|
+
const onEnd = (payload: any) => {
|
|
302
|
+
endEvents.push(payload);
|
|
303
|
+
};
|
|
304
|
+
engine.emitter.on('model:event:beforeRender:end', onEnd);
|
|
305
|
+
|
|
306
|
+
await engine.executor.dispatchEvent(model, 'beforeRender', undefined, { sequential: true, useCache: true });
|
|
307
|
+
await engine.executor.dispatchEvent(model, 'beforeRender', undefined, { sequential: true, useCache: true });
|
|
308
|
+
|
|
309
|
+
engine.emitter.off('model:event:beforeRender:end', onEnd);
|
|
310
|
+
|
|
311
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
312
|
+
expect(endEvents).toHaveLength(2);
|
|
313
|
+
expect(endEvents[0]?.aborted).toBe(true);
|
|
314
|
+
expect(endEvents[1]?.aborted).toBe(true);
|
|
315
|
+
});
|
|
316
|
+
|
|
291
317
|
it('dispatchEvent supports sequential execution order and exitAll break', async () => {
|
|
292
318
|
const calls: string[] = [];
|
|
293
319
|
const mkFlow = (key: string, sort: number, opts?: { exitAll?: boolean }) => ({
|
|
@@ -105,6 +105,45 @@ describe('dispatchEvent dynamic event flow phase (scheduleModelOperation integra
|
|
|
105
105
|
expect(calls).toEqual(['static-a', 'dynamic']);
|
|
106
106
|
});
|
|
107
107
|
|
|
108
|
+
test("phase='afterAllFlows': skips when event aborted by ctx.exitAll()", async () => {
|
|
109
|
+
const engine = new FlowEngine();
|
|
110
|
+
class M extends FlowModel {}
|
|
111
|
+
engine.registerModels({ M });
|
|
112
|
+
|
|
113
|
+
const calls: string[] = [];
|
|
114
|
+
|
|
115
|
+
M.registerFlow({
|
|
116
|
+
key: 'S',
|
|
117
|
+
on: { eventName: 'go' },
|
|
118
|
+
steps: {
|
|
119
|
+
a: { handler: async () => void calls.push('static-a') } as any,
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const model = engine.createModel({ use: 'M' });
|
|
124
|
+
model.registerFlow('Abort', {
|
|
125
|
+
on: { eventName: 'go' },
|
|
126
|
+
sort: -10,
|
|
127
|
+
steps: {
|
|
128
|
+
d: {
|
|
129
|
+
handler: async (ctx: any) => {
|
|
130
|
+
calls.push('abort');
|
|
131
|
+
ctx.exitAll();
|
|
132
|
+
},
|
|
133
|
+
} as any,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
model.registerFlow('AfterAll', {
|
|
137
|
+
on: { eventName: 'go', phase: 'afterAllFlows' },
|
|
138
|
+
steps: {
|
|
139
|
+
d: { handler: async () => void calls.push('after-all') } as any,
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await model.dispatchEvent('go');
|
|
144
|
+
expect(calls).toEqual(['abort']);
|
|
145
|
+
});
|
|
146
|
+
|
|
108
147
|
test("phase='beforeFlow': instance flow runs before the target static flow", async () => {
|
|
109
148
|
const engine = new FlowEngine();
|
|
110
149
|
class M extends FlowModel {}
|
|
@@ -161,6 +200,39 @@ describe('dispatchEvent dynamic event flow phase (scheduleModelOperation integra
|
|
|
161
200
|
expect(calls).toEqual(['static-a', 'static-b', 'dynamic']);
|
|
162
201
|
});
|
|
163
202
|
|
|
203
|
+
test("phase='afterFlow': skips when anchor flow is aborted by ctx.exitAll()", async () => {
|
|
204
|
+
const engine = new FlowEngine();
|
|
205
|
+
class M extends FlowModel {}
|
|
206
|
+
engine.registerModels({ M });
|
|
207
|
+
|
|
208
|
+
const calls: string[] = [];
|
|
209
|
+
|
|
210
|
+
M.registerFlow({
|
|
211
|
+
key: 'S',
|
|
212
|
+
on: { eventName: 'go' },
|
|
213
|
+
steps: {
|
|
214
|
+
a: {
|
|
215
|
+
handler: async (ctx: any) => {
|
|
216
|
+
calls.push('static-a');
|
|
217
|
+
ctx.exitAll();
|
|
218
|
+
},
|
|
219
|
+
} as any,
|
|
220
|
+
b: { handler: async () => void calls.push('static-b') } as any,
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const model = engine.createModel({ use: 'M' });
|
|
225
|
+
model.registerFlow('D', {
|
|
226
|
+
on: { eventName: 'go', phase: 'afterFlow', flowKey: 'S' },
|
|
227
|
+
steps: {
|
|
228
|
+
d: { handler: async () => void calls.push('dynamic') } as any,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
await model.dispatchEvent('go');
|
|
233
|
+
expect(calls).toEqual(['static-a']);
|
|
234
|
+
});
|
|
235
|
+
|
|
164
236
|
test("phase='beforeStep': instance flow runs before the target static step", async () => {
|
|
165
237
|
const engine = new FlowEngine();
|
|
166
238
|
class M extends FlowModel {}
|
|
@@ -217,6 +289,39 @@ describe('dispatchEvent dynamic event flow phase (scheduleModelOperation integra
|
|
|
217
289
|
expect(calls).toEqual(['static-a', 'dynamic', 'static-b']);
|
|
218
290
|
});
|
|
219
291
|
|
|
292
|
+
test("phase='afterStep': skips when anchor step is aborted by ctx.exitAll()", async () => {
|
|
293
|
+
const engine = new FlowEngine();
|
|
294
|
+
class M extends FlowModel {}
|
|
295
|
+
engine.registerModels({ M });
|
|
296
|
+
|
|
297
|
+
const calls: string[] = [];
|
|
298
|
+
|
|
299
|
+
M.registerFlow({
|
|
300
|
+
key: 'S',
|
|
301
|
+
on: { eventName: 'go' },
|
|
302
|
+
steps: {
|
|
303
|
+
a: {
|
|
304
|
+
handler: async (ctx: any) => {
|
|
305
|
+
calls.push('static-a');
|
|
306
|
+
ctx.exitAll();
|
|
307
|
+
},
|
|
308
|
+
} as any,
|
|
309
|
+
b: { handler: async () => void calls.push('static-b') } as any,
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const model = engine.createModel({ use: 'M' });
|
|
314
|
+
model.registerFlow('D', {
|
|
315
|
+
on: { eventName: 'go', phase: 'afterStep', flowKey: 'S', stepKey: 'a' },
|
|
316
|
+
steps: {
|
|
317
|
+
d: { handler: async () => void calls.push('dynamic') } as any,
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
await model.dispatchEvent('go');
|
|
322
|
+
expect(calls).toEqual(['static-a']);
|
|
323
|
+
});
|
|
324
|
+
|
|
220
325
|
test("phase='beforeFlow': ctx.exitAll() stops anchor flow and subsequent flows", async () => {
|
|
221
326
|
const engine = new FlowEngine();
|
|
222
327
|
class M extends FlowModel {}
|
|
@@ -430,6 +535,115 @@ describe('dispatchEvent dynamic event flow phase (scheduleModelOperation integra
|
|
|
430
535
|
expect(calls).toEqual(['static-a', 'dynamic']);
|
|
431
536
|
});
|
|
432
537
|
|
|
538
|
+
test('fallback to event:end (missing anchor) skips when event aborted by ctx.exitAll()', async () => {
|
|
539
|
+
const engine = new FlowEngine();
|
|
540
|
+
class M extends FlowModel {}
|
|
541
|
+
engine.registerModels({ M });
|
|
542
|
+
|
|
543
|
+
const calls: string[] = [];
|
|
544
|
+
|
|
545
|
+
M.registerFlow({
|
|
546
|
+
key: 'S',
|
|
547
|
+
on: { eventName: 'go' },
|
|
548
|
+
steps: {
|
|
549
|
+
a: { handler: async () => void calls.push('static-a') } as any,
|
|
550
|
+
},
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
const model = engine.createModel({ use: 'M' });
|
|
554
|
+
model.registerFlow('Abort', {
|
|
555
|
+
on: { eventName: 'go' },
|
|
556
|
+
sort: -10,
|
|
557
|
+
steps: {
|
|
558
|
+
d: {
|
|
559
|
+
handler: async (ctx: any) => {
|
|
560
|
+
calls.push('abort');
|
|
561
|
+
ctx.exitAll();
|
|
562
|
+
},
|
|
563
|
+
} as any,
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
model.registerFlow('FallbackToEnd', {
|
|
567
|
+
on: { eventName: 'go', phase: 'beforeFlow', flowKey: 'missing' },
|
|
568
|
+
steps: {
|
|
569
|
+
d: { handler: async () => void calls.push('fallback-end') } as any,
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
await model.dispatchEvent('go');
|
|
574
|
+
expect(calls).toEqual(['abort']);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test('event:end is still emitted with aborted=true when exitAll happens', async () => {
|
|
578
|
+
const engine = new FlowEngine();
|
|
579
|
+
class M extends FlowModel {}
|
|
580
|
+
engine.registerModels({ M });
|
|
581
|
+
|
|
582
|
+
const endEvents: any[] = [];
|
|
583
|
+
const onEnd = (payload: any) => {
|
|
584
|
+
endEvents.push(payload);
|
|
585
|
+
};
|
|
586
|
+
engine.emitter.on('model:event:go:end', onEnd);
|
|
587
|
+
|
|
588
|
+
const model = engine.createModel({ use: 'M' });
|
|
589
|
+
model.registerFlow('Abort', {
|
|
590
|
+
on: { eventName: 'go' },
|
|
591
|
+
steps: {
|
|
592
|
+
d: {
|
|
593
|
+
handler: async (ctx: any) => {
|
|
594
|
+
ctx.exitAll();
|
|
595
|
+
},
|
|
596
|
+
} as any,
|
|
597
|
+
},
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
await model.dispatchEvent('go');
|
|
601
|
+
engine.emitter.off('model:event:go:end', onEnd);
|
|
602
|
+
|
|
603
|
+
expect(endEvents).toHaveLength(1);
|
|
604
|
+
expect(endEvents[0]?.aborted).toBe(true);
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
test('flow:end/step:end are emitted with aborted=true when exitAll happens', async () => {
|
|
608
|
+
const engine = new FlowEngine();
|
|
609
|
+
class M extends FlowModel {}
|
|
610
|
+
engine.registerModels({ M });
|
|
611
|
+
|
|
612
|
+
const flowEndEvents: any[] = [];
|
|
613
|
+
const stepEndEvents: any[] = [];
|
|
614
|
+
const onFlowEnd = (payload: any) => {
|
|
615
|
+
flowEndEvents.push(payload);
|
|
616
|
+
};
|
|
617
|
+
const onStepEnd = (payload: any) => {
|
|
618
|
+
stepEndEvents.push(payload);
|
|
619
|
+
};
|
|
620
|
+
engine.emitter.on('model:event:go:flow:S:end', onFlowEnd);
|
|
621
|
+
engine.emitter.on('model:event:go:flow:S:step:a:end', onStepEnd);
|
|
622
|
+
|
|
623
|
+
M.registerFlow({
|
|
624
|
+
key: 'S',
|
|
625
|
+
on: { eventName: 'go' },
|
|
626
|
+
steps: {
|
|
627
|
+
a: {
|
|
628
|
+
handler: async (ctx: any) => {
|
|
629
|
+
ctx.exitAll();
|
|
630
|
+
},
|
|
631
|
+
} as any,
|
|
632
|
+
},
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
const model = engine.createModel({ use: 'M' });
|
|
636
|
+
await model.dispatchEvent('go');
|
|
637
|
+
|
|
638
|
+
engine.emitter.off('model:event:go:flow:S:end', onFlowEnd);
|
|
639
|
+
engine.emitter.off('model:event:go:flow:S:step:a:end', onStepEnd);
|
|
640
|
+
|
|
641
|
+
expect(flowEndEvents).toHaveLength(1);
|
|
642
|
+
expect(stepEndEvents).toHaveLength(1);
|
|
643
|
+
expect(flowEndEvents[0]?.aborted).toBe(true);
|
|
644
|
+
expect(stepEndEvents[0]?.aborted).toBe(true);
|
|
645
|
+
});
|
|
646
|
+
|
|
433
647
|
test('multiple flows on same anchor: executes by flow.sort asc (stable)', async () => {
|
|
434
648
|
const engine = new FlowEngine();
|
|
435
649
|
class M extends FlowModel {}
|
|
@@ -21,7 +21,11 @@ type LifecycleType =
|
|
|
21
21
|
| `event:${string}:end`
|
|
22
22
|
| `event:${string}:error`;
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
type EventPredicateWhen = ((e: LifecycleEvent) => boolean) & {
|
|
25
|
+
__eventType?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type ScheduleWhen = LifecycleType | EventPredicateWhen;
|
|
25
29
|
|
|
26
30
|
export interface ScheduleOptions {
|
|
27
31
|
when?: ScheduleWhen;
|
|
@@ -37,6 +41,7 @@ export interface LifecycleEvent {
|
|
|
37
41
|
error?: any;
|
|
38
42
|
inputArgs?: Record<string, any>;
|
|
39
43
|
result?: any;
|
|
44
|
+
aborted?: boolean;
|
|
40
45
|
flowKey?: string;
|
|
41
46
|
stepKey?: string;
|
|
42
47
|
}
|
|
@@ -216,8 +221,14 @@ export class ModelOperationScheduler {
|
|
|
216
221
|
}
|
|
217
222
|
|
|
218
223
|
private ensureEventSubscriptionIfNeeded(when?: ScheduleWhen) {
|
|
219
|
-
|
|
220
|
-
|
|
224
|
+
const eventType =
|
|
225
|
+
typeof when === 'string'
|
|
226
|
+
? when
|
|
227
|
+
: typeof when === 'function'
|
|
228
|
+
? (when as EventPredicateWhen).__eventType
|
|
229
|
+
: undefined;
|
|
230
|
+
if (!eventType) return;
|
|
231
|
+
const parsed = this.parseEventWhen(eventType as ScheduleWhen);
|
|
221
232
|
if (!parsed) return;
|
|
222
233
|
const { name } = parsed;
|
|
223
234
|
if (this.subscribedEventNames.has(name)) return;
|