@nocobase/flow-engine 2.1.0-beta.6 → 2.1.0-beta.8

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.
Files changed (42) hide show
  1. package/lib/JSRunner.d.ts +10 -1
  2. package/lib/JSRunner.js +50 -5
  3. package/lib/ViewScopedFlowEngine.js +5 -1
  4. package/lib/components/dnd/gridDragPlanner.js +6 -2
  5. package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +16 -2
  6. package/lib/components/subModel/AddSubModelButton.js +15 -0
  7. package/lib/executor/FlowExecutor.js +25 -8
  8. package/lib/flowContext.js +4 -1
  9. package/lib/flowEngine.d.ts +19 -0
  10. package/lib/flowEngine.js +29 -1
  11. package/lib/runjs-context/registry.d.ts +1 -1
  12. package/lib/runjs-context/setup.js +19 -12
  13. package/lib/scheduler/ModelOperationScheduler.d.ts +5 -1
  14. package/lib/scheduler/ModelOperationScheduler.js +3 -2
  15. package/lib/utils/parsePathnameToViewParams.js +1 -1
  16. package/lib/views/useDialog.js +10 -0
  17. package/lib/views/useDrawer.js +10 -0
  18. package/package.json +4 -4
  19. package/src/JSRunner.ts +68 -4
  20. package/src/ViewScopedFlowEngine.ts +4 -0
  21. package/src/__tests__/JSRunner.test.ts +27 -1
  22. package/src/__tests__/runjsContext.test.ts +13 -0
  23. package/src/__tests__/runjsPreprocessDefault.test.ts +23 -0
  24. package/src/components/__tests__/gridDragPlanner.test.ts +88 -0
  25. package/src/components/dnd/gridDragPlanner.ts +8 -2
  26. package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +18 -2
  27. package/src/components/subModel/AddSubModelButton.tsx +16 -0
  28. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +50 -0
  29. package/src/executor/FlowExecutor.ts +28 -9
  30. package/src/executor/__tests__/flowExecutor.test.ts +26 -0
  31. package/src/flowContext.ts +5 -3
  32. package/src/flowEngine.ts +33 -1
  33. package/src/models/__tests__/dispatchEvent.when.test.ts +214 -0
  34. package/src/runjs-context/registry.ts +1 -1
  35. package/src/runjs-context/setup.ts +21 -12
  36. package/src/scheduler/ModelOperationScheduler.ts +14 -3
  37. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
  38. package/src/utils/parsePathnameToViewParams.ts +2 -2
  39. package/src/views/__tests__/useDialog.closeDestroy.test.tsx +1 -0
  40. package/src/views/useDialog.tsx +14 -0
  41. package/src/views/useDrawer.tsx +14 -0
  42. package/src/views/usePage.tsx +1 -0
package/src/JSRunner.ts CHANGED
@@ -16,12 +16,74 @@ export interface JSRunnerOptions {
16
16
  version?: string;
17
17
  /**
18
18
  * Enable RunJS template compatibility preprocessing for `{{ ... }}`.
19
- * When enabled via `ctx.runjs(code, vars, { preprocessTemplates: true })` (default),
19
+ * When enabled (or falling back to version default),
20
20
  * the code will be rewritten to call `ctx.resolveJsonTemplate(...)` at runtime.
21
21
  */
22
22
  preprocessTemplates?: boolean;
23
23
  }
24
24
 
25
+ /**
26
+ * Decide whether RunJS `{{ ... }}` compatibility preprocessing should run.
27
+ *
28
+ * Priority:
29
+ * 1. Explicit `preprocessTemplates` option always wins.
30
+ * 2. Otherwise, `version === 'v2'` disables preprocessing.
31
+ * 3. Fallback keeps v1-compatible behavior (enabled).
32
+ */
33
+ export function shouldPreprocessRunJSTemplates(
34
+ options?: Pick<JSRunnerOptions, 'preprocessTemplates' | 'version'>,
35
+ ): boolean {
36
+ if (typeof options?.preprocessTemplates === 'boolean') {
37
+ return options.preprocessTemplates;
38
+ }
39
+ return options?.version !== 'v2';
40
+ }
41
+
42
+ // Heuristic: detect likely bare `{{ctx.xxx}}` usage in executable positions (not quoted string literals).
43
+ const BARE_CTX_TEMPLATE_RE = /(^|[=(:,[\s)])(\{\{\s*(ctx(?:\.|\[|\?\.)[^}]*)\s*\}\})/m;
44
+
45
+ function extractDeprecatedCtxTemplateUsage(code: string): { placeholder: string; expression: string } | null {
46
+ const src = String(code || '');
47
+ const m = src.match(BARE_CTX_TEMPLATE_RE);
48
+ if (!m) return null;
49
+ const placeholder = String(m[2] || '').trim();
50
+ const expression = String(m[3] || '').trim();
51
+ if (!placeholder || !expression) return null;
52
+ return { placeholder, expression };
53
+ }
54
+
55
+ function shouldHintCtxTemplateSyntax(err: any, usage: { placeholder: string; expression: string } | null): boolean {
56
+ const isSyntaxError = err instanceof SyntaxError || String((err as any)?.name || '') === 'SyntaxError';
57
+ if (!isSyntaxError) return false;
58
+ if (!usage) return false;
59
+ const msg = String((err as any)?.message || err || '');
60
+ return /unexpected token/i.test(msg);
61
+ }
62
+
63
+ function toCtxTemplateSyntaxHintError(
64
+ err: any,
65
+ usage: {
66
+ placeholder: string;
67
+ expression: string;
68
+ },
69
+ ): Error {
70
+ const hint = `"${usage.placeholder}" has been deprecated and cannot be used as executable RunJS syntax. Use await ctx.getVar("${usage.expression}") instead, or keep "${usage.placeholder}" as a plain string.`;
71
+ const out = new SyntaxError(hint);
72
+ try {
73
+ (out as any).cause = err;
74
+ } catch (_) {
75
+ // ignore
76
+ }
77
+ try {
78
+ // Hint-only error: avoid leaking internal bundle line numbers from stack parsers in preview UI.
79
+ (out as any).__runjsHideLocation = true;
80
+ out.stack = `${out.name}: ${out.message}`;
81
+ } catch (_) {
82
+ // ignore
83
+ }
84
+ return out;
85
+ }
86
+
25
87
  export class JSRunner {
26
88
  private globals: Record<string, any>;
27
89
  private timeoutMs: number;
@@ -118,11 +180,13 @@ export class JSRunner {
118
180
  if (err instanceof FlowExitAllException) {
119
181
  throw err;
120
182
  }
121
- console.error(err);
183
+ const usage = extractDeprecatedCtxTemplateUsage(code);
184
+ const outErr = shouldHintCtxTemplateSyntax(err, usage) && usage ? toCtxTemplateSyntaxHintError(err, usage) : err;
185
+ console.error(outErr);
122
186
  return {
123
187
  success: false,
124
- error: err,
125
- timeout: err.message === 'Execution timed out',
188
+ error: outErr,
189
+ timeout: (outErr as any)?.message === 'Execution timed out',
126
190
  };
127
191
  }
128
192
  }
@@ -62,6 +62,10 @@ export function createViewScopedEngine(parent: FlowEngine): FlowEngine {
62
62
  '_nextEngine',
63
63
  // getModel 需要在本地执行以确保全局查找时正确遍历整个引擎栈
64
64
  'getModel',
65
+ // 视图销毁回调需要在本地存储,每个视图引擎有自己的销毁逻辑
66
+ '_destroyView',
67
+ 'setDestroyView',
68
+ 'destroyView',
65
69
  ]);
66
70
 
67
71
  const handler: ProxyHandler<FlowEngine> = {
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
11
- import { JSRunner } from '../JSRunner';
11
+ import { JSRunner, shouldPreprocessRunJSTemplates } from '../JSRunner';
12
12
  import { createSafeWindow } from '../utils';
13
13
 
14
14
  describe('JSRunner', () => {
@@ -30,6 +30,18 @@ describe('JSRunner', () => {
30
30
  vi.restoreAllMocks();
31
31
  });
32
32
 
33
+ it('shouldPreprocessRunJSTemplates: explicit option has highest priority', () => {
34
+ expect(shouldPreprocessRunJSTemplates({ version: 'v2', preprocessTemplates: true })).toBe(true);
35
+ expect(shouldPreprocessRunJSTemplates({ version: 'v1', preprocessTemplates: false })).toBe(false);
36
+ });
37
+
38
+ it('shouldPreprocessRunJSTemplates: falls back to version policy', () => {
39
+ expect(shouldPreprocessRunJSTemplates({ version: 'v1' })).toBe(true);
40
+ expect(shouldPreprocessRunJSTemplates({ version: 'v2' })).toBe(false);
41
+ expect(shouldPreprocessRunJSTemplates({})).toBe(true);
42
+ expect(shouldPreprocessRunJSTemplates()).toBe(true);
43
+ });
44
+
33
45
  it('executes simple code and returns value', async () => {
34
46
  const runner = new JSRunner();
35
47
  const result = await runner.run('return 1 + 2 + 3');
@@ -152,6 +164,20 @@ describe('JSRunner', () => {
152
164
  expect((result.error as Error).message).toBe('Execution timed out');
153
165
  });
154
166
 
167
+ it('returns friendly hint when bare {{ctx.xxx}} appears in syntax error', async () => {
168
+ const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
169
+ const runner = new JSRunner();
170
+ const result = await runner.run('const z = {{ctx.user.id}}');
171
+ expect(result.success).toBe(false);
172
+ expect(result.error).toBeInstanceOf(SyntaxError);
173
+ const msg = String((result.error as any)?.message || '');
174
+ expect(msg).toContain('"{{ctx.user.id}}" has been deprecated');
175
+ expect(msg).toContain('await ctx.getVar("ctx.user.id")');
176
+ expect(msg).not.toContain('(at ');
177
+ expect((result.error as any)?.__runjsHideLocation).toBe(true);
178
+ expect(spy).toHaveBeenCalled();
179
+ });
180
+
155
181
  it('skips execution when URL contains skipRunJs=true', async () => {
156
182
  // 模拟预览模式下通过 URL 参数跳过代码执行
157
183
  if (typeof window !== 'undefined' && typeof window.history?.pushState === 'function') {
@@ -29,6 +29,10 @@ describe('flowRunJSContext registry and doc', () => {
29
29
  expect(RunJSContextRegistry['resolve']('v1' as any, '*')).toBeTruthy();
30
30
  });
31
31
 
32
+ it('should register v2 mapping', () => {
33
+ expect(RunJSContextRegistry['resolve']('v2' as any, '*')).toBeTruthy();
34
+ });
35
+
32
36
  it('should register all context types', () => {
33
37
  const contextTypes = [
34
38
  'JSBlockModel',
@@ -44,12 +48,20 @@ describe('flowRunJSContext registry and doc', () => {
44
48
  const ctor = RunJSContextRegistry['resolve']('v1' as any, modelClass);
45
49
  expect(ctor).toBeTruthy();
46
50
  });
51
+
52
+ contextTypes.forEach((modelClass) => {
53
+ const ctor = RunJSContextRegistry['resolve']('v2' as any, modelClass);
54
+ expect(ctor).toBeTruthy();
55
+ });
47
56
  });
48
57
 
49
58
  it('should expose scene metadata for contexts', () => {
50
59
  expect(getRunJSScenesForModel('JSBlockModel', 'v1')).toEqual(['block']);
51
60
  expect(getRunJSScenesForModel('JSFieldModel', 'v1')).toEqual(['detail']);
61
+ expect(getRunJSScenesForModel('JSBlockModel', 'v2')).toEqual(['block']);
62
+ expect(getRunJSScenesForModel('JSFieldModel', 'v2')).toEqual(['detail']);
52
63
  expect(getRunJSScenesForModel('UnknownModel', 'v1')).toEqual([]);
64
+ expect(getRunJSScenesForModel('UnknownModel', 'v2')).toEqual([]);
53
65
  });
54
66
 
55
67
  it('should only execute once (idempotent)', async () => {
@@ -175,6 +187,7 @@ describe('flowRunJSContext registry and doc', () => {
175
187
  const ctx = new FlowContext();
176
188
  ctx.defineProperty('model', { value: { constructor: { name: 'JSColumnModel' } } });
177
189
  expect(getRunJSScenesForContext(ctx as any, { version: 'v1' })).toEqual(['table']);
190
+ expect(getRunJSScenesForContext(ctx as any, { version: 'v2' })).toEqual(['table']);
178
191
  });
179
192
 
180
193
  it('JSBlockModel context should have element property in doc', () => {
@@ -36,6 +36,29 @@ describe('ctx.runjs preprocessTemplates default', () => {
36
36
  expect(r.value).toBe('{{ctx.user.id}}');
37
37
  });
38
38
 
39
+ it('disables template preprocess by default for version v2', async () => {
40
+ const engine = new FlowEngine();
41
+ const ctx = engine.context as any;
42
+ ctx.defineProperty('user', { value: { id: 123 } });
43
+
44
+ const r = await ctx.runjs('return "{{ctx.user.id}}";', undefined, { version: 'v2' });
45
+ expect(r.success).toBe(true);
46
+ expect(r.value).toBe('{{ctx.user.id}}');
47
+ });
48
+
49
+ it('keeps explicit preprocessTemplates override higher priority than version', async () => {
50
+ const engine = new FlowEngine();
51
+ const ctx = engine.context as any;
52
+ ctx.defineProperty('user', { value: { id: 123 } });
53
+
54
+ const r = await ctx.runjs('return "{{ctx.user.id}}";', undefined, {
55
+ version: 'v2',
56
+ preprocessTemplates: true,
57
+ });
58
+ expect(r.success).toBe(true);
59
+ expect(r.value).toBe('123');
60
+ });
61
+
39
62
  it('does not double-preprocess already prepared code', async () => {
40
63
  const engine = new FlowEngine();
41
64
  const ctx = engine.context as any;
@@ -15,6 +15,7 @@ import {
15
15
  getSlotKey,
16
16
  resolveDropIntent,
17
17
  Point,
18
+ buildLayoutSnapshot,
18
19
  } from '../dnd/gridDragPlanner';
19
20
 
20
21
  const rect = { top: 0, left: 0, width: 100, height: 100 };
@@ -29,6 +30,93 @@ const createLayout = (
29
30
  rowOrder,
30
31
  });
31
32
 
33
+ const createDomRect = ({ top, left, width, height }: { top: number; left: number; width: number; height: number }) => {
34
+ return {
35
+ top,
36
+ left,
37
+ width,
38
+ height,
39
+ right: left + width,
40
+ bottom: top + height,
41
+ x: left,
42
+ y: top,
43
+ toJSON: () => ({}),
44
+ } as DOMRect;
45
+ };
46
+
47
+ const mockRect = (
48
+ element: Element,
49
+ rect: {
50
+ top: number;
51
+ left: number;
52
+ width: number;
53
+ height: number;
54
+ },
55
+ ) => {
56
+ Object.defineProperty(element, 'getBoundingClientRect', {
57
+ configurable: true,
58
+ value: () => createDomRect(rect),
59
+ });
60
+ };
61
+
62
+ describe('buildLayoutSnapshot', () => {
63
+ it('should ignore nested grid columns/items even when rowId is duplicated', () => {
64
+ const container = document.createElement('div');
65
+ const row = document.createElement('div');
66
+ row.setAttribute('data-grid-row-id', 'row-1');
67
+ container.appendChild(row);
68
+
69
+ const column = document.createElement('div');
70
+ column.setAttribute('data-grid-column-row-id', 'row-1');
71
+ column.setAttribute('data-grid-column-index', '0');
72
+ row.appendChild(column);
73
+
74
+ const item = document.createElement('div');
75
+ item.setAttribute('data-grid-item-row-id', 'row-1');
76
+ item.setAttribute('data-grid-column-index', '0');
77
+ item.setAttribute('data-grid-item-index', '0');
78
+ column.appendChild(item);
79
+
80
+ // 在外层 item 内构建一个嵌套 grid,并复用相同 rowId/columnIndex
81
+ const nestedRow = document.createElement('div');
82
+ nestedRow.setAttribute('data-grid-row-id', 'row-1');
83
+ item.appendChild(nestedRow);
84
+
85
+ const nestedColumn = document.createElement('div');
86
+ nestedColumn.setAttribute('data-grid-column-row-id', 'row-1');
87
+ nestedColumn.setAttribute('data-grid-column-index', '0');
88
+ nestedRow.appendChild(nestedColumn);
89
+
90
+ const nestedItem = document.createElement('div');
91
+ nestedItem.setAttribute('data-grid-item-row-id', 'row-1');
92
+ nestedItem.setAttribute('data-grid-column-index', '0');
93
+ nestedItem.setAttribute('data-grid-item-index', '0');
94
+ nestedColumn.appendChild(nestedItem);
95
+
96
+ mockRect(container, { top: 0, left: 0, width: 600, height: 600 });
97
+ mockRect(row, { top: 10, left: 10, width: 320, height: 120 });
98
+ mockRect(column, { top: 10, left: 10, width: 320, height: 120 });
99
+ mockRect(item, { top: 20, left: 20, width: 300, height: 80 });
100
+
101
+ // 嵌套 grid 给一个明显偏离的位置,用于判断是否被错误命中
102
+ mockRect(nestedRow, { top: 360, left: 360, width: 200, height: 120 });
103
+ mockRect(nestedColumn, { top: 360, left: 360, width: 200, height: 120 });
104
+ mockRect(nestedItem, { top: 370, left: 370, width: 180, height: 90 });
105
+
106
+ const snapshot = buildLayoutSnapshot({ container });
107
+ const columnEdgeSlots = snapshot.slots.filter((slot) => slot.type === 'column-edge');
108
+ const columnSlots = snapshot.slots.filter((slot) => slot.type === 'column');
109
+
110
+ // 外层单行单列单项应只有 6 个 slot:上/下 row-gap + 左/右 column-edge + before/after column
111
+ expect(snapshot.slots).toHaveLength(6);
112
+ expect(columnEdgeSlots).toHaveLength(2);
113
+ expect(columnSlots).toHaveLength(2);
114
+
115
+ // 不应混入嵌套 grid(其 top >= 360)
116
+ expect(snapshot.slots.every((slot) => slot.rect.top < 300)).toBe(true);
117
+ });
118
+ });
119
+
32
120
  describe('getSlotKey', () => {
33
121
  it('should generate unique key for column slot', () => {
34
122
  const slot: LayoutSlot = {
@@ -333,7 +333,10 @@ export const buildLayoutSnapshot = ({ container }: BuildLayoutSnapshotOptions):
333
333
 
334
334
  const columnElements = Array.from(
335
335
  container.querySelectorAll(`[data-grid-column-row-id="${rowId}"][data-grid-column-index]`),
336
- ) as HTMLElement[];
336
+ ).filter((el) => {
337
+ // 只保留当前 row 下的直接列,避免嵌套 Grid 中相同 rowId 的列混入
338
+ return (el as HTMLElement).closest('[data-grid-row-id]') === rowElement;
339
+ }) as HTMLElement[];
337
340
 
338
341
  const sortedColumns = columnElements.sort((a, b) => {
339
342
  const indexA = Number(a.dataset.gridColumnIndex || 0);
@@ -363,7 +366,10 @@ export const buildLayoutSnapshot = ({ container }: BuildLayoutSnapshotOptions):
363
366
 
364
367
  const itemElements = Array.from(
365
368
  columnElement.querySelectorAll(`[data-grid-item-row-id="${rowId}"][data-grid-column-index="${columnIndex}"]`),
366
- ) as HTMLElement[];
369
+ ).filter((el) => {
370
+ // 只保留当前 column 下的直接 item,避免命中更深层嵌套 column 的 item
371
+ return (el as HTMLElement).closest('[data-grid-column-row-id][data-grid-column-index]') === columnElement;
372
+ }) as HTMLElement[];
367
373
 
368
374
  const sortedItems = itemElements.sort((a, b) => {
369
375
  const indexA = Number(a.dataset.gridItemIndex || 0);
@@ -134,6 +134,17 @@ const openStepSettingsDialog = async ({
134
134
  };
135
135
 
136
136
  const openView = model.context.viewer[mode].bind(model.context.viewer);
137
+ const resolvedUiModeProps = toJS(uiModeProps) || {};
138
+ const { zIndex: uiModeZIndex, ...restUiModeProps } = resolvedUiModeProps;
139
+ const resolveDialogZIndex = (rawZIndex?: number) => {
140
+ const nextZIndex =
141
+ typeof model.context.viewer?.getNextZIndex === 'function'
142
+ ? model.context.viewer.getNextZIndex()
143
+ : (model.context.themeToken?.zIndexPopupBase || 1000) + 1;
144
+ const inputZIndex = Number(rawZIndex) || 0;
145
+ return Math.max(nextZIndex, inputZIndex);
146
+ };
147
+ const mergedZIndex = resolveDialogZIndex(uiModeZIndex);
137
148
 
138
149
  const form = createForm({
139
150
  initialValues: compileUiSchema(scopes, initialValues),
@@ -152,7 +163,8 @@ const openStepSettingsDialog = async ({
152
163
  title: dialogTitle || t(title),
153
164
  width: dialogWidth,
154
165
  destroyOnClose: true,
155
- ...toJS(uiModeProps),
166
+ ...restUiModeProps,
167
+ zIndex: mergedZIndex,
156
168
  // 透传 navigation,便于变量元信息根据真实视图栈推断父级弹窗
157
169
  inputArgs,
158
170
  onClose: () => {
@@ -165,7 +177,11 @@ const openStepSettingsDialog = async ({
165
177
  useEffect(() => {
166
178
  return autorun(() => {
167
179
  const dynamicProps = toJS(uiModeProps);
168
- currentDialog.update(dynamicProps);
180
+ const { zIndex, ...restDynamicProps } = dynamicProps || {};
181
+ currentDialog.update({
182
+ ...restDynamicProps,
183
+ zIndex: resolveDialogZIndex(zIndex),
184
+ });
169
185
  });
170
186
  }, []);
171
187
 
@@ -542,6 +542,22 @@ const AddSubModelButtonCore = function AddSubModelButton({
542
542
  [model, subModelKey, subModelType],
543
543
  );
544
544
 
545
+ React.useEffect(() => {
546
+ const handleSubModelChanged = () => {
547
+ setRefreshTick((x) => x + 1);
548
+ };
549
+
550
+ model.emitter?.on('onSubModelAdded', handleSubModelChanged);
551
+ model.emitter?.on('onSubModelRemoved', handleSubModelChanged);
552
+ model.emitter?.on('onSubModelReplaced', handleSubModelChanged);
553
+
554
+ return () => {
555
+ model.emitter?.off('onSubModelAdded', handleSubModelChanged);
556
+ model.emitter?.off('onSubModelRemoved', handleSubModelChanged);
557
+ model.emitter?.off('onSubModelReplaced', handleSubModelChanged);
558
+ };
559
+ }, [model]);
560
+
545
561
  // 点击处理逻辑
546
562
  const onClick = async (info: any) => {
547
563
  const clickedItem = info.originalItem || info;
@@ -995,6 +995,56 @@ describe('AddSubModelButton - toggle interactions', () => {
995
995
  const subModels = ((parent.subModels as any).items as FlowModel[]) || [];
996
996
  expect(subModels).toHaveLength(1);
997
997
  });
998
+
999
+ it('updates toggle state after external sub model removal', async () => {
1000
+ const engine = new FlowEngine();
1001
+ engine.flowSettings.forceEnable();
1002
+
1003
+ class ToggleParent extends FlowModel {}
1004
+ class ToggleChild extends FlowModel {}
1005
+
1006
+ engine.registerModels({ ToggleParent, ToggleChild });
1007
+ const parent = engine.createModel<ToggleParent>({ use: 'ToggleParent', uid: 'toggle-parent-external-remove' });
1008
+ const existing = engine.createModel<ToggleChild>({ use: 'ToggleChild', uid: 'toggle-child-external-remove' });
1009
+ parent.addSubModel('items', existing);
1010
+
1011
+ render(
1012
+ <FlowEngineProvider engine={engine}>
1013
+ <ConfigProvider>
1014
+ <App>
1015
+ <AddSubModelButton
1016
+ model={parent}
1017
+ subModelKey="items"
1018
+ items={[
1019
+ {
1020
+ key: 'toggle-child',
1021
+ label: 'Toggle Child',
1022
+ toggleable: true,
1023
+ useModel: 'ToggleChild',
1024
+ createModelOptions: { use: 'ToggleChild' },
1025
+ },
1026
+ ]}
1027
+ >
1028
+ Toggle Menu
1029
+ </AddSubModelButton>
1030
+ </App>
1031
+ </ConfigProvider>
1032
+ </FlowEngineProvider>,
1033
+ );
1034
+
1035
+ await act(async () => {
1036
+ await userEvent.click(screen.getByText('Toggle Menu'));
1037
+ });
1038
+
1039
+ await waitFor(() => expect(screen.getByText('Toggle Child')).toBeInTheDocument());
1040
+ await waitFor(() => expect(screen.getByRole('switch')).toHaveAttribute('aria-checked', 'true'));
1041
+
1042
+ await act(async () => {
1043
+ await existing.destroy();
1044
+ });
1045
+
1046
+ await waitFor(() => expect(screen.getByRole('switch')).toHaveAttribute('aria-checked', 'false'));
1047
+ });
998
1048
  });
999
1049
 
1000
1050
  // ========================
@@ -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
- // 组装执行函数(返回值用于缓存;beforeRender 返回 results:any[],其它返回 true)
321
- const execute = async () => {
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: whenKey as any },
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
- return results.filter((x) => x !== undefined);
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 }) => ({
@@ -27,7 +27,7 @@ import { ContextPathProxy } from './ContextPathProxy';
27
27
  import { DataSource, DataSourceManager } from './data-source';
28
28
  import { FlowEngine } from './flowEngine';
29
29
  import { FlowI18n } from './flowI18n';
30
- import { JSRunner, JSRunnerOptions } from './JSRunner';
30
+ import { JSRunner, JSRunnerOptions, shouldPreprocessRunJSTemplates } from './JSRunner';
31
31
  import type { FlowModel } from './models/flowModel';
32
32
  import type { ForkFlowModel } from './models/forkFlowModel';
33
33
  import { FlowResource, FlowSQLRepository } from './resources';
@@ -3035,8 +3035,10 @@ class BaseFlowEngineContext extends FlowContext {
3035
3035
  ...(runnerOptions || {}),
3036
3036
  globals: mergedGlobals,
3037
3037
  });
3038
- // Enable by default; use `preprocessTemplates: false` to explicitly disable.
3039
- const shouldPreprocessTemplates = preprocessTemplates !== false;
3038
+ const shouldPreprocessTemplates = shouldPreprocessRunJSTemplates({
3039
+ version: runnerOptions?.version,
3040
+ preprocessTemplates,
3041
+ });
3040
3042
  const jsCode = await prepareRunJsCode(String(code ?? ''), { preprocessTemplates: shouldPreprocessTemplates });
3041
3043
  return runner.run(jsCode);
3042
3044
  },