@meshflow/core 0.1.7 → 0.1.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.
package/index.d.mts CHANGED
@@ -5,48 +5,48 @@ interface MeshErrorContext {
5
5
 
6
6
  interface MeshEvents {
7
7
  'node:start': {
8
- path: string;
8
+ path: MeshPath;
9
9
  };
10
10
  'node:success': {
11
- path: string;
11
+ path: MeshPath;
12
12
  };
13
13
  'node:bucket:success': {
14
- path: string;
14
+ path: MeshPath;
15
15
  key: string;
16
16
  value: any;
17
17
  };
18
18
  'node:error': {
19
- path: string;
19
+ path: MeshPath;
20
20
  error: any;
21
21
  };
22
22
  'node:intercept': {
23
- path: string;
23
+ path: MeshPath;
24
24
  type: number;
25
25
  detail?: any;
26
26
  };
27
27
  'node:release': {
28
- path: string;
28
+ path: MeshPath;
29
29
  type: number;
30
30
  detail?: any;
31
31
  };
32
32
  'node:stagnate': {
33
- path: string;
33
+ path: MeshPath;
34
34
  type: number;
35
35
  };
36
36
  'node:processing': {
37
- path: string;
37
+ path: MeshPath;
38
38
  };
39
39
  'flow:wait': {
40
40
  type: number;
41
41
  detail?: any;
42
42
  };
43
43
  'flow:fire': {
44
- path: string;
44
+ path: MeshPath;
45
45
  type: number;
46
46
  detail?: any;
47
47
  };
48
48
  'flow:start': {
49
- path: string;
49
+ path: MeshPath;
50
50
  };
51
51
  'flow:success': {
52
52
  duration: string;
@@ -55,10 +55,66 @@ interface MeshEvents {
55
55
  type: number;
56
56
  };
57
57
  'node:pending': {
58
- path: string;
58
+ path: MeshPath;
59
59
  };
60
60
  }
61
61
  type MeshEventName = keyof MeshEvents;
62
+ type MeshEmit = <K extends MeshEventName>(event: K, data: MeshEvents[K]) => void;
63
+ type HistoryActionItem = {
64
+ undoAction: () => void;
65
+ redoAction: () => void;
66
+ };
67
+ type MeshFlowHistory = {
68
+ Undo: () => void;
69
+ Redo: () => void;
70
+ initCanUndo: any;
71
+ initCanRedo: any;
72
+ PushIntoHistory: (action: HistoryActionItem, cleanRedo?: boolean) => void;
73
+ CreateHistoryAction: (metadata: [
74
+ {
75
+ path: string;
76
+ value: any;
77
+ },
78
+ {
79
+ path: string;
80
+ value: any;
81
+ }
82
+ ], cb: any) => {
83
+ undoAction: () => any;
84
+ redoAction: () => any;
85
+ };
86
+ };
87
+ interface MeshFlowEngineMap {
88
+ }
89
+ type MeshPath = string | number | symbol;
90
+ interface MeshBucket<P> {
91
+ evaluate: (context: any) => Promise<any> | any;
92
+ [key: string]: any;
93
+ }
94
+ interface MeshFlowTaskNode<P extends MeshPath = MeshPath, S = any, V = any> {
95
+ path: P;
96
+ uid: number;
97
+ type: string;
98
+ state: {
99
+ value: V;
100
+ };
101
+ buckets: Record<string, MeshBucket<P>>;
102
+ notifyKeys: Set<keyof S>;
103
+ dirtySignal: any;
104
+ meta: S;
105
+ dependOn: (cb: (val: V) => V) => void;
106
+ }
107
+ interface MeshFlowGroupNode<P extends MeshPath = MeshPath> {
108
+ path: P;
109
+ uid: number;
110
+ type: 'group';
111
+ children: Array<P>;
112
+ meta: Record<string, any>;
113
+ }
114
+ interface DependOnContext<P extends MeshPath> {
115
+ path: P;
116
+ getNode: (path: P) => MeshFlowTaskNode<P>;
117
+ }
62
118
 
63
119
  type ContractType = 'boolean' | 'scalar' | 'array' | 'object';
64
120
  declare enum DefaultStarategy {
@@ -83,7 +139,6 @@ declare class SchemaBucket<P> {
83
139
  forceNotify(): void;
84
140
  isForceNotify(): boolean;
85
141
  setStrategy(type: DefaultStarategy): void;
86
- updateInputValueRule(newVal: any): void;
87
142
  setDefaultRule(value: any): void;
88
143
  setRules(value: any, DepsArray?: Array<[P, any]>): () => void;
89
144
  updateDeps(DepsArray: Array<[P, any]>): void;
@@ -104,6 +159,16 @@ declare class SchemaBucket<P> {
104
159
  type FinalFlatten<T> = T extends infer O ? {
105
160
  [K in keyof O]: O[K];
106
161
  } : never;
162
+ type Unwrap<T> = T extends ReadonlyArray<infer U> ? U : T;
163
+ type InferLeafPath<T, Prefix extends string = ""> = Unwrap<T> extends infer Node ? Node extends {
164
+ readonly name: infer N;
165
+ } ? N extends string ? N extends "" ? Node extends {
166
+ readonly children: infer C;
167
+ } ? InferLeafPath<C, Prefix> : never : (Node extends {
168
+ readonly children: infer C;
169
+ } ? InferLeafPath<C, Prefix extends "" ? N : `${Prefix}.${N}`> : (Prefix extends "" ? N : `${Prefix}.${N}`)) : N extends number | symbol ? Node extends {
170
+ readonly children: infer C;
171
+ } ? InferLeafPath<C, Prefix> : N : never : never : never;
107
172
  type KeysOfUnion<T> = T extends any ? keyof T : never;
108
173
 
109
174
  type BaseField = {
@@ -163,34 +228,19 @@ interface logicApi {
163
228
  };
164
229
  }
165
230
 
166
- type GetType<T, P> = P extends keyof T ? T[P] : never;
167
- type Engine<T> = {
168
- data: {
169
- [K in "schema" | "GetFormData" | "AddNewSchema" | 'SetValue' | 'GetValue' | 'GetGroupByPath']: GetType<T, K>;
170
- };
231
+ declare function useEngineInstance<T, P extends MeshPath>(data: any, options: {
171
232
  config: {
172
- [K in "SetRule" | "SetRules" | "SetStrategy" | "SetValidators" | "notifyAll" | "SetTrace" | "usePlugin"]: GetType<T, K>;
173
- };
174
- dependency: {
175
- [K in 'GetAllDependency' | 'GetDependencyOrder']: GetType<T, K>;
176
- };
177
- history: {
178
- [K in "Undo" | "Redo" | "initCanUndo" | "initCanRedo"]: GetType<T, K>;
179
- };
180
- hooks: {
181
- [K in "onError" | "onSuccess" | "onStart"]: GetType<T, K>;
182
- };
183
- };
184
- /** @deprecated 请使用新的 useMeshFlow 别名 */
185
- declare const useEngineManager: <T, P extends string>(id: string | symbol, Schema: any, options: {
186
- config?: {
187
233
  useGreedy: boolean;
188
234
  };
189
235
  UITrigger: {
190
236
  signalCreateor: () => T;
191
237
  signalTrigger: (signal: T) => void;
192
238
  };
193
- }) => Engine<{
239
+ modules: {
240
+ useHistory?: () => MeshFlowHistory;
241
+ };
242
+ plugins: {};
243
+ }): {
194
244
  schema: RenderSchema;
195
245
  SetRule: (outDegreePath: P, inDegreePath: P, key: KeysOfUnion<InputField | CheckboxField | SelectField>, options?: {
196
246
  value?: any;
@@ -222,22 +272,52 @@ declare const useEngineManager: <T, P extends string>(id: string | symbol, Schem
222
272
  SetValue: (path: P, value: any) => void;
223
273
  GetValue: (path: P, key?: string) => any;
224
274
  GetFormData: () => any;
225
- GetGroupByPath: (path: string) => RenderSchema | undefined;
275
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
226
276
  notifyAll: () => Promise<void>;
227
277
  AddNewSchema: (path: string, data: any) => RenderSchema;
228
278
  GetAllDependency: () => Map<P, Set<P>>;
229
279
  GetDependencyOrder: () => P[][];
230
- Undo: () => void;
231
- Redo: () => void;
232
- initCanUndo: (cb: (newVal: number) => any) => void;
233
- initCanRedo: (cb: (newVal: number) => any) => void;
280
+ historyExports: Partial<MeshFlowHistory>;
234
281
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
235
282
  onSuccess: (cb: (data: unknown) => void) => () => void;
236
283
  onStart: (cb: (data: {
237
284
  path: P;
238
285
  }) => void) => () => void;
239
- }>;
240
- declare const useEngine: <T = any, P extends string = string>(id: string | symbol) => Engine<{
286
+ };
287
+
288
+ type SchedulerType<T, P extends MeshPath> = ReturnType<typeof useEngineInstance<T, P>>;
289
+ type GetType<T, P> = P extends keyof T ? T[P] : never;
290
+ type BaseEngine<T> = {
291
+ data: {
292
+ [K in "schema" | "GetFormData" | "AddNewSchema" | 'SetValue' | 'GetValue' | 'GetGroupByPath']: GetType<T, K>;
293
+ };
294
+ config: {
295
+ [K in "SetRule" | "SetRules" | "SetStrategy" | "SetValidators" | "notifyAll" | "SetTrace" | "usePlugin"]: GetType<T, K>;
296
+ };
297
+ dependency: {
298
+ [K in 'GetAllDependency' | 'GetDependencyOrder']: GetType<T, K>;
299
+ };
300
+ hooks: {
301
+ [K in "onError" | "onSuccess" | "onStart"]: GetType<T, K>;
302
+ };
303
+ };
304
+ type TransformKey<T> = T extends `use${infer Rest}` ? Uncapitalize<Rest> : T;
305
+ type EngineModules<M> = {
306
+ [K in keyof M as TransformKey<string & K>]: M[K] extends (...args: any) => infer R ? R : never;
307
+ };
308
+ type Engine<T, M> = BaseEngine<T> & EngineModules<M>;
309
+ /** @deprecated 请使用新的 useMeshFlow 别名 */
310
+ declare const useEngineManager: <const S extends Record<string, any>, T, //UITrigger的类型
311
+ M extends Record<string, any>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : InferLeafPath<S> | (string & {})>(id: MeshPath, Schema: S, options: {
312
+ config?: {
313
+ useGreedy: boolean;
314
+ };
315
+ modules?: M;
316
+ UITrigger: {
317
+ signalCreateor: () => T;
318
+ signalTrigger: (signal: T) => void;
319
+ };
320
+ }) => Engine<{
241
321
  schema: RenderSchema;
242
322
  SetRule: (outDegreePath: P, inDegreePath: P, key: KeysOfUnion<InputField | CheckboxField | SelectField>, options?: {
243
323
  value?: any;
@@ -269,26 +349,38 @@ declare const useEngine: <T = any, P extends string = string>(id: string | symbo
269
349
  SetValue: (path: P, value: any) => void;
270
350
  GetValue: (path: P, key?: string) => any;
271
351
  GetFormData: () => any;
272
- GetGroupByPath: (path: string) => RenderSchema | undefined;
352
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
273
353
  notifyAll: () => Promise<void>;
274
354
  AddNewSchema: (path: string, data: any) => RenderSchema;
275
355
  GetAllDependency: () => Map<P, Set<P>>;
276
356
  GetDependencyOrder: () => P[][];
277
- Undo: () => void;
278
- Redo: () => void;
279
- initCanUndo: (cb: (newVal: number) => any) => void;
280
- initCanRedo: (cb: (newVal: number) => any) => void;
357
+ historyExports: Partial<MeshFlowHistory>;
281
358
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
282
359
  onSuccess: (cb: (data: unknown) => void) => () => void;
283
360
  onStart: (cb: (data: {
284
361
  path: P;
285
362
  }) => void) => () => void;
286
- }>;
287
- declare const deleteEngine: (id: string | symbol) => void;
288
- declare const useMeshFlow: <T, P extends string>(id: string | symbol, Schema: any, options: {
363
+ }, M>;
364
+ declare const useMeshFlowDefiner: <P extends string>() => <T, M extends Record<string, any>>(id: MeshPath, schema: any, options: {
365
+ UITrigger: {
366
+ signalCreateor: () => T;
367
+ signalTrigger: (s: T) => void;
368
+ };
369
+ modules?: M;
370
+ config?: any;
371
+ }) => Engine<ReturnType<typeof useEngineInstance<T, P>>, M>;
372
+ /**
373
+ * 获取 Engine 实例
374
+ * @template M 手动注入的模块映射 (例如 { useHistory: typeof useHistory })
375
+ * @template K ID 类型 (支持 string | number | symbol)
376
+ */
377
+ declare const useEngine: <M, ID extends keyof MeshFlowEngineMap | (MeshPath & {}) = MeshPath>(id: ID) => [M] extends [never] ? (ID extends keyof MeshFlowEngineMap ? MeshFlowEngineMap[ID] : Engine<SchedulerType<any, any>, {}>) : Engine<SchedulerType<any, any>, M>;
378
+ declare const deleteEngine: (id: MeshPath) => void;
379
+ declare const useMeshFlow: <const S extends Record<string, any>, T, M extends Record<string, any>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : InferLeafPath<S> | (string & {})>(id: MeshPath, Schema: S, options: {
289
380
  config?: {
290
381
  useGreedy: boolean;
291
382
  };
383
+ modules?: M;
292
384
  UITrigger: {
293
385
  signalCreateor: () => T;
294
386
  signalTrigger: (signal: T) => void;
@@ -325,20 +417,17 @@ declare const useMeshFlow: <T, P extends string>(id: string | symbol, Schema: an
325
417
  SetValue: (path: P, value: any) => void;
326
418
  GetValue: (path: P, key?: string) => any;
327
419
  GetFormData: () => any;
328
- GetGroupByPath: (path: string) => RenderSchema | undefined;
420
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
329
421
  notifyAll: () => Promise<void>;
330
422
  AddNewSchema: (path: string, data: any) => RenderSchema;
331
423
  GetAllDependency: () => Map<P, Set<P>>;
332
424
  GetDependencyOrder: () => P[][];
333
- Undo: () => void;
334
- Redo: () => void;
335
- initCanUndo: (cb: (newVal: number) => any) => void;
336
- initCanRedo: (cb: (newVal: number) => any) => void;
425
+ historyExports: Partial<MeshFlowHistory>;
337
426
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
338
427
  onSuccess: (cb: (data: unknown) => void) => () => void;
339
428
  onStart: (cb: (data: {
340
429
  path: P;
341
430
  }) => void) => () => void;
342
- }>;
431
+ }, M>;
343
432
 
344
- export { deleteEngine, useEngine, useEngineManager, useMeshFlow };
433
+ export { type DependOnContext, type HistoryActionItem, type MeshBucket, type MeshEmit, type MeshEventName, type MeshEvents, type MeshFlowEngineMap, type MeshFlowGroupNode, type MeshFlowHistory, type MeshFlowTaskNode, type MeshPath, deleteEngine, useEngine, useEngineManager, useMeshFlow, useMeshFlowDefiner };
package/index.d.ts CHANGED
@@ -5,48 +5,48 @@ interface MeshErrorContext {
5
5
 
6
6
  interface MeshEvents {
7
7
  'node:start': {
8
- path: string;
8
+ path: MeshPath;
9
9
  };
10
10
  'node:success': {
11
- path: string;
11
+ path: MeshPath;
12
12
  };
13
13
  'node:bucket:success': {
14
- path: string;
14
+ path: MeshPath;
15
15
  key: string;
16
16
  value: any;
17
17
  };
18
18
  'node:error': {
19
- path: string;
19
+ path: MeshPath;
20
20
  error: any;
21
21
  };
22
22
  'node:intercept': {
23
- path: string;
23
+ path: MeshPath;
24
24
  type: number;
25
25
  detail?: any;
26
26
  };
27
27
  'node:release': {
28
- path: string;
28
+ path: MeshPath;
29
29
  type: number;
30
30
  detail?: any;
31
31
  };
32
32
  'node:stagnate': {
33
- path: string;
33
+ path: MeshPath;
34
34
  type: number;
35
35
  };
36
36
  'node:processing': {
37
- path: string;
37
+ path: MeshPath;
38
38
  };
39
39
  'flow:wait': {
40
40
  type: number;
41
41
  detail?: any;
42
42
  };
43
43
  'flow:fire': {
44
- path: string;
44
+ path: MeshPath;
45
45
  type: number;
46
46
  detail?: any;
47
47
  };
48
48
  'flow:start': {
49
- path: string;
49
+ path: MeshPath;
50
50
  };
51
51
  'flow:success': {
52
52
  duration: string;
@@ -55,10 +55,66 @@ interface MeshEvents {
55
55
  type: number;
56
56
  };
57
57
  'node:pending': {
58
- path: string;
58
+ path: MeshPath;
59
59
  };
60
60
  }
61
61
  type MeshEventName = keyof MeshEvents;
62
+ type MeshEmit = <K extends MeshEventName>(event: K, data: MeshEvents[K]) => void;
63
+ type HistoryActionItem = {
64
+ undoAction: () => void;
65
+ redoAction: () => void;
66
+ };
67
+ type MeshFlowHistory = {
68
+ Undo: () => void;
69
+ Redo: () => void;
70
+ initCanUndo: any;
71
+ initCanRedo: any;
72
+ PushIntoHistory: (action: HistoryActionItem, cleanRedo?: boolean) => void;
73
+ CreateHistoryAction: (metadata: [
74
+ {
75
+ path: string;
76
+ value: any;
77
+ },
78
+ {
79
+ path: string;
80
+ value: any;
81
+ }
82
+ ], cb: any) => {
83
+ undoAction: () => any;
84
+ redoAction: () => any;
85
+ };
86
+ };
87
+ interface MeshFlowEngineMap {
88
+ }
89
+ type MeshPath = string | number | symbol;
90
+ interface MeshBucket<P> {
91
+ evaluate: (context: any) => Promise<any> | any;
92
+ [key: string]: any;
93
+ }
94
+ interface MeshFlowTaskNode<P extends MeshPath = MeshPath, S = any, V = any> {
95
+ path: P;
96
+ uid: number;
97
+ type: string;
98
+ state: {
99
+ value: V;
100
+ };
101
+ buckets: Record<string, MeshBucket<P>>;
102
+ notifyKeys: Set<keyof S>;
103
+ dirtySignal: any;
104
+ meta: S;
105
+ dependOn: (cb: (val: V) => V) => void;
106
+ }
107
+ interface MeshFlowGroupNode<P extends MeshPath = MeshPath> {
108
+ path: P;
109
+ uid: number;
110
+ type: 'group';
111
+ children: Array<P>;
112
+ meta: Record<string, any>;
113
+ }
114
+ interface DependOnContext<P extends MeshPath> {
115
+ path: P;
116
+ getNode: (path: P) => MeshFlowTaskNode<P>;
117
+ }
62
118
 
63
119
  type ContractType = 'boolean' | 'scalar' | 'array' | 'object';
64
120
  declare enum DefaultStarategy {
@@ -83,7 +139,6 @@ declare class SchemaBucket<P> {
83
139
  forceNotify(): void;
84
140
  isForceNotify(): boolean;
85
141
  setStrategy(type: DefaultStarategy): void;
86
- updateInputValueRule(newVal: any): void;
87
142
  setDefaultRule(value: any): void;
88
143
  setRules(value: any, DepsArray?: Array<[P, any]>): () => void;
89
144
  updateDeps(DepsArray: Array<[P, any]>): void;
@@ -104,6 +159,16 @@ declare class SchemaBucket<P> {
104
159
  type FinalFlatten<T> = T extends infer O ? {
105
160
  [K in keyof O]: O[K];
106
161
  } : never;
162
+ type Unwrap<T> = T extends ReadonlyArray<infer U> ? U : T;
163
+ type InferLeafPath<T, Prefix extends string = ""> = Unwrap<T> extends infer Node ? Node extends {
164
+ readonly name: infer N;
165
+ } ? N extends string ? N extends "" ? Node extends {
166
+ readonly children: infer C;
167
+ } ? InferLeafPath<C, Prefix> : never : (Node extends {
168
+ readonly children: infer C;
169
+ } ? InferLeafPath<C, Prefix extends "" ? N : `${Prefix}.${N}`> : (Prefix extends "" ? N : `${Prefix}.${N}`)) : N extends number | symbol ? Node extends {
170
+ readonly children: infer C;
171
+ } ? InferLeafPath<C, Prefix> : N : never : never : never;
107
172
  type KeysOfUnion<T> = T extends any ? keyof T : never;
108
173
 
109
174
  type BaseField = {
@@ -163,34 +228,19 @@ interface logicApi {
163
228
  };
164
229
  }
165
230
 
166
- type GetType<T, P> = P extends keyof T ? T[P] : never;
167
- type Engine<T> = {
168
- data: {
169
- [K in "schema" | "GetFormData" | "AddNewSchema" | 'SetValue' | 'GetValue' | 'GetGroupByPath']: GetType<T, K>;
170
- };
231
+ declare function useEngineInstance<T, P extends MeshPath>(data: any, options: {
171
232
  config: {
172
- [K in "SetRule" | "SetRules" | "SetStrategy" | "SetValidators" | "notifyAll" | "SetTrace" | "usePlugin"]: GetType<T, K>;
173
- };
174
- dependency: {
175
- [K in 'GetAllDependency' | 'GetDependencyOrder']: GetType<T, K>;
176
- };
177
- history: {
178
- [K in "Undo" | "Redo" | "initCanUndo" | "initCanRedo"]: GetType<T, K>;
179
- };
180
- hooks: {
181
- [K in "onError" | "onSuccess" | "onStart"]: GetType<T, K>;
182
- };
183
- };
184
- /** @deprecated 请使用新的 useMeshFlow 别名 */
185
- declare const useEngineManager: <T, P extends string>(id: string | symbol, Schema: any, options: {
186
- config?: {
187
233
  useGreedy: boolean;
188
234
  };
189
235
  UITrigger: {
190
236
  signalCreateor: () => T;
191
237
  signalTrigger: (signal: T) => void;
192
238
  };
193
- }) => Engine<{
239
+ modules: {
240
+ useHistory?: () => MeshFlowHistory;
241
+ };
242
+ plugins: {};
243
+ }): {
194
244
  schema: RenderSchema;
195
245
  SetRule: (outDegreePath: P, inDegreePath: P, key: KeysOfUnion<InputField | CheckboxField | SelectField>, options?: {
196
246
  value?: any;
@@ -222,22 +272,52 @@ declare const useEngineManager: <T, P extends string>(id: string | symbol, Schem
222
272
  SetValue: (path: P, value: any) => void;
223
273
  GetValue: (path: P, key?: string) => any;
224
274
  GetFormData: () => any;
225
- GetGroupByPath: (path: string) => RenderSchema | undefined;
275
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
226
276
  notifyAll: () => Promise<void>;
227
277
  AddNewSchema: (path: string, data: any) => RenderSchema;
228
278
  GetAllDependency: () => Map<P, Set<P>>;
229
279
  GetDependencyOrder: () => P[][];
230
- Undo: () => void;
231
- Redo: () => void;
232
- initCanUndo: (cb: (newVal: number) => any) => void;
233
- initCanRedo: (cb: (newVal: number) => any) => void;
280
+ historyExports: Partial<MeshFlowHistory>;
234
281
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
235
282
  onSuccess: (cb: (data: unknown) => void) => () => void;
236
283
  onStart: (cb: (data: {
237
284
  path: P;
238
285
  }) => void) => () => void;
239
- }>;
240
- declare const useEngine: <T = any, P extends string = string>(id: string | symbol) => Engine<{
286
+ };
287
+
288
+ type SchedulerType<T, P extends MeshPath> = ReturnType<typeof useEngineInstance<T, P>>;
289
+ type GetType<T, P> = P extends keyof T ? T[P] : never;
290
+ type BaseEngine<T> = {
291
+ data: {
292
+ [K in "schema" | "GetFormData" | "AddNewSchema" | 'SetValue' | 'GetValue' | 'GetGroupByPath']: GetType<T, K>;
293
+ };
294
+ config: {
295
+ [K in "SetRule" | "SetRules" | "SetStrategy" | "SetValidators" | "notifyAll" | "SetTrace" | "usePlugin"]: GetType<T, K>;
296
+ };
297
+ dependency: {
298
+ [K in 'GetAllDependency' | 'GetDependencyOrder']: GetType<T, K>;
299
+ };
300
+ hooks: {
301
+ [K in "onError" | "onSuccess" | "onStart"]: GetType<T, K>;
302
+ };
303
+ };
304
+ type TransformKey<T> = T extends `use${infer Rest}` ? Uncapitalize<Rest> : T;
305
+ type EngineModules<M> = {
306
+ [K in keyof M as TransformKey<string & K>]: M[K] extends (...args: any) => infer R ? R : never;
307
+ };
308
+ type Engine<T, M> = BaseEngine<T> & EngineModules<M>;
309
+ /** @deprecated 请使用新的 useMeshFlow 别名 */
310
+ declare const useEngineManager: <const S extends Record<string, any>, T, //UITrigger的类型
311
+ M extends Record<string, any>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : InferLeafPath<S> | (string & {})>(id: MeshPath, Schema: S, options: {
312
+ config?: {
313
+ useGreedy: boolean;
314
+ };
315
+ modules?: M;
316
+ UITrigger: {
317
+ signalCreateor: () => T;
318
+ signalTrigger: (signal: T) => void;
319
+ };
320
+ }) => Engine<{
241
321
  schema: RenderSchema;
242
322
  SetRule: (outDegreePath: P, inDegreePath: P, key: KeysOfUnion<InputField | CheckboxField | SelectField>, options?: {
243
323
  value?: any;
@@ -269,26 +349,38 @@ declare const useEngine: <T = any, P extends string = string>(id: string | symbo
269
349
  SetValue: (path: P, value: any) => void;
270
350
  GetValue: (path: P, key?: string) => any;
271
351
  GetFormData: () => any;
272
- GetGroupByPath: (path: string) => RenderSchema | undefined;
352
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
273
353
  notifyAll: () => Promise<void>;
274
354
  AddNewSchema: (path: string, data: any) => RenderSchema;
275
355
  GetAllDependency: () => Map<P, Set<P>>;
276
356
  GetDependencyOrder: () => P[][];
277
- Undo: () => void;
278
- Redo: () => void;
279
- initCanUndo: (cb: (newVal: number) => any) => void;
280
- initCanRedo: (cb: (newVal: number) => any) => void;
357
+ historyExports: Partial<MeshFlowHistory>;
281
358
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
282
359
  onSuccess: (cb: (data: unknown) => void) => () => void;
283
360
  onStart: (cb: (data: {
284
361
  path: P;
285
362
  }) => void) => () => void;
286
- }>;
287
- declare const deleteEngine: (id: string | symbol) => void;
288
- declare const useMeshFlow: <T, P extends string>(id: string | symbol, Schema: any, options: {
363
+ }, M>;
364
+ declare const useMeshFlowDefiner: <P extends string>() => <T, M extends Record<string, any>>(id: MeshPath, schema: any, options: {
365
+ UITrigger: {
366
+ signalCreateor: () => T;
367
+ signalTrigger: (s: T) => void;
368
+ };
369
+ modules?: M;
370
+ config?: any;
371
+ }) => Engine<ReturnType<typeof useEngineInstance<T, P>>, M>;
372
+ /**
373
+ * 获取 Engine 实例
374
+ * @template M 手动注入的模块映射 (例如 { useHistory: typeof useHistory })
375
+ * @template K ID 类型 (支持 string | number | symbol)
376
+ */
377
+ declare const useEngine: <M, ID extends keyof MeshFlowEngineMap | (MeshPath & {}) = MeshPath>(id: ID) => [M] extends [never] ? (ID extends keyof MeshFlowEngineMap ? MeshFlowEngineMap[ID] : Engine<SchedulerType<any, any>, {}>) : Engine<SchedulerType<any, any>, M>;
378
+ declare const deleteEngine: (id: MeshPath) => void;
379
+ declare const useMeshFlow: <const S extends Record<string, any>, T, M extends Record<string, any>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : InferLeafPath<S> | (string & {})>(id: MeshPath, Schema: S, options: {
289
380
  config?: {
290
381
  useGreedy: boolean;
291
382
  };
383
+ modules?: M;
292
384
  UITrigger: {
293
385
  signalCreateor: () => T;
294
386
  signalTrigger: (signal: T) => void;
@@ -325,20 +417,17 @@ declare const useMeshFlow: <T, P extends string>(id: string | symbol, Schema: an
325
417
  SetValue: (path: P, value: any) => void;
326
418
  GetValue: (path: P, key?: string) => any;
327
419
  GetFormData: () => any;
328
- GetGroupByPath: (path: string) => RenderSchema | undefined;
420
+ GetGroupByPath: (path: MeshPath) => RenderSchema | undefined;
329
421
  notifyAll: () => Promise<void>;
330
422
  AddNewSchema: (path: string, data: any) => RenderSchema;
331
423
  GetAllDependency: () => Map<P, Set<P>>;
332
424
  GetDependencyOrder: () => P[][];
333
- Undo: () => void;
334
- Redo: () => void;
335
- initCanUndo: (cb: (newVal: number) => any) => void;
336
- initCanRedo: (cb: (newVal: number) => any) => void;
425
+ historyExports: Partial<MeshFlowHistory>;
337
426
  onError: (cb: (error: MeshErrorContext) => void) => () => void;
338
427
  onSuccess: (cb: (data: unknown) => void) => () => void;
339
428
  onStart: (cb: (data: {
340
429
  path: P;
341
430
  }) => void) => () => void;
342
- }>;
431
+ }, M>;
343
432
 
344
- export { deleteEngine, useEngine, useEngineManager, useMeshFlow };
433
+ export { type DependOnContext, type HistoryActionItem, type MeshBucket, type MeshEmit, type MeshEventName, type MeshEvents, type MeshFlowEngineMap, type MeshFlowGroupNode, type MeshFlowHistory, type MeshFlowTaskNode, type MeshPath, deleteEngine, useEngine, useEngineManager, useMeshFlow, useMeshFlowDefiner };
package/index.js CHANGED
@@ -1 +1 @@
1
- 'use strict';var ne=class{computedRules=[];store={OR:(e,r)=>{let t,l,n=this.computedRules;for(let i=0;i<n.length;i++){let a=n[i],o=a.logic(e);if(o instanceof Promise)return (async()=>{let d=await o;if(a.entityId==="__base__"?l=d:d&&(t=a.value),typeof t>"u")for(let u=i+1;u<n.length;u++){let y=n[u],S=y.logic(e),f=S instanceof Promise?await S:S;if(y.entityId==="__base__"){l=f;continue}if(f){t=y.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}})();let s=o;if(a.entityId==="__base__"){l=s;continue}if(s){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}},PRIORITY:(e,r)=>{let t=null,l=this.computedRules;for(let n=0;n<l.length;n++){let a=l[n].logic(e);if(a instanceof Promise)return (async()=>{let o=await a;if(o!==void 0)return {res:o,version:r};for(let s=n+1;s<l.length;s++){let d=l[s].logic(e),u=d instanceof Promise?await d:d;if(u!==void 0)return {res:u,version:r}}return {res:void 0,version:r}})();if(a!==void 0)return {res:a,version:r}}return {res:t,version:r}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat().sort((r,t)=>t.priority-r.priority):this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,r){return this.CurrentStrategy(e,r)}},$=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,r,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=r==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}updateInputValueRule(e){this.isValue&&this.setRule({priority:1,entityId:"__input_value__",logic:()=>e});}setDefaultRule(e){let r=new Set;r.add(e),this.rules.set(e.id,r);}setRules(e,r){r&&this.updateDeps(r);let t=++this.id,l={...e,entityId:t};for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[r,t]of e)this.deps.set(r,t);}setRule(e,r){if(r&&this.updateDeps(r),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let r=null;if(e.GetToken&&(r=e.GetToken()),this.pendingPromise&&this.promiseToken!==r&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let i=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof i=="object"||typeof a=="object")t=false;else {let o=Array.from(this.deps.keys());for(let s of o){let d=this.deps.get(s),u=e.GetValueByPath(s);if(d!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=r;let l=++this.version,n=this.strategy.evaluate(e,l);if(!(n instanceof Promise)){let{res:i,version:a}=n;return this.finalizeSync(i,a,e,r)}return this.pendingPromise=(async()=>{try{let{res:i,version:a}=await n;return this.finalizeSync(i,a,e,r)}catch(i){throw {path:this.path,error:i}}finally{this.promiseToken===r&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,r,t,l){return l!==this.promiseToken||r<this.version?this.cache:(this.cache=e,this.deps.forEach((n,i)=>{this.deps.set(i,t.GetValueByPath(i));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},te=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},r={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(r.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(r);}evaluate(e,r){let t=true,l=[...this.defaultValidators,...this.validators];for(let n of l){if(!n.condition(r))continue;let a=n.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var ae=(c={frameQuota:12})=>{let e=performance.now(),r=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),r=0,t=true;},shouldYield(){let n=performance.now();return r++,!!((r>=5||t)&&(r=0,n-e>c.frameQuota||l()))},async yieldToMain(){return new Promise(n=>{Pe(()=>{e=performance.now(),r=0,t&&(t=false),n();});})}}},Pe=c=>{let{port1:e,port2:r}=new MessageChannel;e.onmessage=c,r.postMessage(null);};function oe(c,e,r,t,l){let n=new Map,i=c.useGreedy,a=ae();return async(s,d)=>{let y=Symbol("token");n.set(s,y);let S=false;a.reset();let f=new Set,R=new Set,C=new Set(e.GetAllNextDependency(s));C.add(s);let E=new Map,N=new Map,w=new Set,V=e.GetPathToLevelMap(),_=V.get(s)??0,z=0,q=T=>{e.GetAllNextDependency(T).forEach(F=>{let A=V.get(F)||0;A>z&&(z=A);});};q(s),d.forEach(T=>{w.add(T);}),f.add(s);let W=performance.now();t.emit("flow:start",{path:s}),t.callOnStart({path:s});let H=false,J=30,Z=T=>{let{target:x,trigger:F}=T,A=false,O=false,k=r.GetRenderSchemaByPath(x),M=[],m=(p,g)=>{if(f.has(p)||R.has(p)||w.has(p))return;let D=0,v=V.get(p)??0;if(E.has(p))D=E.get(p)-1;else {if(v>_&&E.size>J){N.has(v)||N.set(v,new Set),N.get(v).add(p),t.emit("node:intercept",{path:p,type:7});return}let I=e.GetPrevDependency(p),B=0;for(let U of I){if(f.has(U))continue;(V.get(U)??0)>_&&B++;}D=B;}if(D<=0){let I=w.has(p),B=R.has(p);if(I||B){t.emit("node:intercept",{path:p,type:B?3:3.1});return}E.delete(p),w.add(p),t.emit("node:release",{path:p,type:g,detail:{path:x}});}else E.set(p,D);},b=(p=[])=>{if(n.get(s)!==y)return;if(p.length){let v={};for(let I of p){let B=(I.args||[]).reduce((U,ee)=>(U[ee]=k[ee],U),{});try{let U=I.fn(B);U&&typeof U=="object"&&Object.assign(v,U);}catch(U){}}for(let I in v)k[I]=v[I];A=true;}A&&l.flushPathSet.add(x),t.emit("node:success",{path:x}),f.add(x);let g=e.GetNextDependency(x);(A||O)&&e.GetAllNextDependency(x).forEach(I=>C.add(I));for(let v of g){if(f.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(R.has(v)||w.has(v)){t.emit("node:intercept",{path:v,type:R.has(v)?3:3.1});continue}if(A||O)m(v,1);else if(E.has(v))m(v,2);else {let B=V.get(v);N.has(B)||N.set(B,new Set);let U=N.get(B);U.has(v)||(U.add(v),t.emit("node:stagnate",{path:v,type:1}));}}R.delete(x),(async()=>{if(!S){let v=R.size,I=w.size;t.emit("flow:fire",{path:x,type:1,detail:{active:v,pending:I,blocked:E.size}}),h();}})();},G=p=>{t.emit("node:error",{path:x,error:p});let g=Symbol("abort");n.set(s,g),w.clear(),E.clear(),R.clear(),t.callOnError(p);},P=(p,g)=>{let D=false;p!==k[g]&&(k[g]=p,A=true,t.emit("node:bucket:success",{path:x,key:g,value:p}),g==="value"&&(D=true)),k.nodeBucket[g].isForceNotify()&&(O=true),(D||O)&&q(x);};t.emit("node:start",{path:x});try{let p=[];for(let g in k.nodeBucket){let D=k.nodeBucket[g];p.push(...D.getSideEffect());let v=D.evaluate({affectKey:g,triggerPath:F,GetRenderSchemaByPath:r.GetRenderSchemaByPath,GetValueByPath:I=>r.GetRenderSchemaByPath(I).value,GetToken:()=>y});if(v instanceof Promise){let I=v.then(B=>{n.get(s)===y&&P(B,g);});M.push(I);}else P(v,g);}if(M.length>0)return Promise.all(M).then(()=>{b(p);}).catch(G);b(p);return}catch(p){G(p);}},h=async()=>{if(n.get(s)!==y){S=false;return}S=true;let T=a.getIsFirstFrame(),x=0,F=()=>i&&T?30:1/0,A=0,O=F();try{for(;n.get(s)===y;){let k=A>=O,M=a.shouldYield();if(k||M){if(A>0&&(x++,(T||x%2===0)&&l.requestUpdate()),await a.yieldToMain(),n.get(s)!==y)break;A=0,T=a.getIsFirstFrame();}if(w.size>0&&R.size<5){for(let m of w){if(R.size>=5||A>=O)break;let b=V.get(m)??0,G=e.GetPrevDependency(m),P=G.length>1;if((!i||P)&&b>_){w.delete(m);let g=G.filter(D=>C.has(D)&&!f.has(D)).length;E.set(m,g||0),t.emit("node:intercept",{path:m,type:g>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:g}});continue}if(w.delete(m),R.add(m),t.emit("node:processing",{path:m}),Z({target:m,trigger:s}),A++,A>=O||a.shouldYield())break}if(w.size>0)continue}if(A<O&&i&&E.size>0&&R.size<5){let m=!1,b=0;for(let[G,P]of E)if(P<=0){let p=V.get(G)??0,g=e.GetPrevDependency(G);if(p>_&&g.length>1)continue;if(E.delete(G),w.add(G),b++,m=!0,t.emit("node:release",{path:G,type:4}),b>=O)break}if(b>0)continue;if(m){if(a.shouldYield()&&(await a.yieldToMain(),n.get(s)!==y))break;continue}}if(R.size===0&&w.size===0){let m=new Set;for(let P of N.keys())m.add(P);for(let[P]of E){let p=V.get(P)??0;p>_&&m.add(p);}let b=Array.from(m).sort((P,p)=>P-p),G=b[0];if(b.length>0&&G<=z){let P=b[0];if(P<=z){_=P;let p=N.get(P);p&&(p.forEach(g=>w.add(g)),N.delete(P));for(let[g]of E)(V.get(g)??0)===P&&(E.delete(g),w.add(g),t.emit("node:release",{path:g,type:3,detail:{level:P}}));continue}}else {N.forEach((P,p)=>{P.forEach(g=>{f.add(g),t.emit("node:intercept",{path:g,type:6});});}),N.clear();for(let[P]of E)f.add(P),t.emit("node:intercept",{path:P,type:6});E.clear();break}}w.size>0&&R.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,R.size+E.size+w.size===0){if(n.get(s)===y&&!H){H=true,t.emit("flow:end",{type:1}),l.requestUpdate();let M=performance.now();t.emit("flow:success",{duration:(M-W).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:R.size}});}};h();}}function se(c,e,r,t,l,n){let i=ve(c),a=0,o=new Map,s=new Map,d=new Map,u=false,y=new Set,S=false,f=true,R=null,C=h=>{let T=o.get(h);return s.get(T)},E=h=>d.get(h),N=async()=>{let h=Array.from(y);y.clear();for(let T of h){let x=C(T);n.signalTrigger(x.dirtySignal);}},w=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;y.size>0;)N();}finally{u=false;}}));},V=()=>{let h=(T,x,F)=>{if(typeof T!="object"||T===null||Array.isArray(T)){if(F.length>0){let O=F[F.length-1];x[O]=C(F.join(".")).value;}return}let A=Object.getOwnPropertyNames(T);for(let O of A)F.push(O),h(T[O],T,F),F.pop();};return h(i,null,[]),i},Y=oe({useGreedy:e.useGreedy},r,{GetRenderSchemaByPath:C},l,{requestUpdate:w,flushPathSet:y}),_=async()=>(S&&R||(S=true,R=(async()=>{let h=r.GetDependencyOrder(),T=performance.now(),x=performance.now();try{for(let A=0;A<h.length;A++){let O=h[A];await Promise.all(O.map(async k=>{let M=C(k),m=!1;for(let b in M.nodeBucket){let G=await M.nodeBucket[b].evaluate({affectKey:b,triggerPath:void 0,GetRenderSchemaByPath:C,GetValueByPath:P=>C(P).value,isSameToken:()=>!0});if(b==="options"){let P=!1,p=M.value;for(let g of G)if(g.value==p){P=!0;break}P||(M.value=void 0,m=!0);}G!==M[b]&&(M[b]=G,m=!0);}m&&y.add(k);})),performance.now()-x>12&&(await new Promise(k=>requestAnimationFrame(k)),x=performance.now());}y.size>0&&w(),f=!1;let F=performance.now();l.emit("flow:success",{duration:(F-T).toFixed(2)+"ms"}),l.callOnSuccess();}catch(F){throw l.emit("node:error",{path:F.path,error:F.error}),l.callOnError(F),F}finally{S=false,R=null,f=false;}})()),R),z=h=>{if(f)return;if(!C(h))throw Error("Node undefined");performance.now();y.add(h),w();let F=r.GetNextDependency(h);q(F,h);};function q(h,T){Y(T,h);}let W=h=>{if(!h)throw Error("path error");let T=C(h);T.nodeBucket.value&&T.nodeBucket.value.updateInputValueRule(T.value);},H=(h,T="")=>{let x="name"in h?h.name:void 0,F=x?T===""?x:`${T}.${x}`:T,A=n.signalCreateor(),O=a++,k={getRenderSchema:b=>C(b)},M=(b,G)=>{let P=b(G),p=C(G.path),g=t.createHistoryAction([{path:G.path,value:p.value},{path:G.path,value:P}],D=>{let v=C(D.path);v.value=D.value,W(D.path),z(D.path);});p.value=P,t.pushIntoHistory(g),W(G.path),z(G.path);},m={...h,disabled:!!h.disabled,hidden:"hidden"in h?h.hidden:false,readonly:"readonly"in h?h.readonly:false,required:"required"in h?h.required:false,path:F,dirtySignal:A,uid:O,nodeBucket:{},validators:new te(F),theme:"secondary",dependOn:b=>M(b,{...k,path:F})};return h.type==="group"&&(delete m.nodeBucket,delete m.validators,m.children=h.children.map(b=>H(b,F)),d.set(m.path,m)),o.set(m.path,m.uid),s.set(m.uid,m),m};return {schema:H(c),GetFormData:()=>V(),GetRenderSchemaByPath:C,GetGroupByPath:E,notifyAll:_,convertToRenderSchema:H}}function ve(c,e={}){let r=n=>{if(n.type=="group")return {key:n.name||"",isGroup:true,val:n.children.reduce((i,a)=>[...i,r(a)],[])};if(n.type=="input"||n.type=="number"||n.type=="select"||n.type=="checkbox")return {key:n.name,isGroup:false,val:n.value};throw Error(`undefined type:${n.type}`)},t=(n,i)=>{if(i.isGroup){let a={};i.key===""?a=n:n[i.key]=a,i.val.forEach(o=>{t(a,o);});}else n[i.key]=i.val;},l=r(c);return t(e,l),e}var ie=(c,e,r)=>{let l=n=>{let i=r.triggerPaths.map(s=>n.GetValueByPath(s)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>i}),Object.defineProperty(a,"affectedTatget",{get:()=>n.GetRenderSchemaByPath(c)[e]}),r.logic({slot:a})};return {value:r.value,targetPath:c,triggerPaths:r.triggerPaths,priority:r.priority??10,logic:l}},le=(c,e,r)=>{if(!c)throw Error("");let t=c,l=(a,o)=>{e.has(a)||e.set(a,new Set),e.get(a).add(o),r.has(o)||r.set(o,new Set),r.get(o).add(a);};return {SetRule:(a,o,s,d={logic:u=>{}})=>{let u=t(o),y=ie(o,s,{...d,triggerPaths:[a]}),S=[a].map(f=>[f,t(f).value]);if(l(a,o),u.nodeBucket[s])u.nodeBucket[s].setRule(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRule(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();},SetRules:(a,o,s,d={logic:u=>{}})=>{let u=t(o);for(let f of a)l(f,o);let y=ie(o,s,{...d,triggerPaths:a}),S=a.map(f=>[f,t(f).value]);if(u.nodeBucket[s])u.nodeBucket[s].setRules(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRules(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();}}},ce=c=>{let e=c||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,n)=>{e(t).nodeBucket[l].setStrategy(n);}}};var ue=c=>{let e=c||void 0;return {SetValidators:(t,l)=>{let n=e(t),i=(o,s,d)=>o(s,d),a=(o,s,d)=>o(s,d);if(!n.validators)throw Error("validator init error");n.validators.setValidators({logic:o=>i(l.logic,o,e),condition:typeof l.condition=="function"?o=>a(l.condition,o,e):()=>true});}}};function de(){let c=new Map,e=new Map,r=new Set,t=(i,a)=>{c.set(i,a);let o=e.get(i);o&&o(a);};return {SetTrace:(i,a)=>{e.set(i,a);let o=c.get(i)||"idle";return a(o),()=>{e.delete(i);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{r.forEach(o=>t(o,"idle")),r.clear(),c.clear();}),a.on("node:release",({path:o,type:s})=>{(s==1||s==2)&&(r.add(o),t(o,"pending"));}),a.on("node:pending",({path:o})=>{r.add(o),(!c.has(o)||c.get(o)==="idle")&&t(o,"pending");}),a.on("node:start",({path:o})=>{r.add(o),t(o,"calculating");}),a.on("node:success",({path:o})=>{t(o,"calculated");}),a.on("node:intercept",({path:o,type:s})=>{s==3&&t(o,"calculating"),s==6&&t(o,"idle");}),a.on("node:stagnate",({path:o})=>{t(o,"pending");}),a.on("node:error",({path:o})=>t(o,"error"));}})}}function pe(c,e,r,t){let l=d=>{let u=c(),y=e(),S=new Set;return u.get(d)?.forEach(f=>S.add(f)),S.size===0?[]:Array.from(S).filter(f=>{let R=y.get(f)||new Set;return !Array.from(R).some(N=>S.has(N))})};return {GetNextDependency:d=>{let u=t();return Array.from(u.get(d)||[])},GetPrevDependency:d=>{let u=r();return Array.from(u.get(d)||[])},GetAllPrevDependency:d=>{let u=e();return Array.from(u.get(d)||[])},GetAllNextDependency:d=>{let u=c();return Array.from(u.get(d)||[])},rebuildDirectDependencyMaps:d=>{let u=new Map,y=new Map;for(let S of d){let f=l(S);u.set(S,new Set(f));for(let R of f)y.has(R)||y.set(R,new Set),y.get(R).add(S);}return {directNextMap:u,directPrevMap:y}}}}function fe(c){let e=t=>{let l=[],n=[],i=new Map,a=t.size,o=0,s=0;for(let[d,u]of t)u===0&&n.push(d);if(n.length===0&&a>0)throw Error("Circular dependency detected");for(;n.length>0;){l.push([...n]);let d=[];for(let u of n){o++,i.set(u,s);let y=c.get(u);if(y)for(let S of y){let f=t.get(S)-1;t.set(S,f),f===0&&d.push(S);}}n=d,s++;}if(o<a)throw Error("Circular dependency detected");return {steps:l,levelMap:i}};return ()=>{let t=new Map;for(let l of c.keys()){let n=Array.from(c.get(l)||[]);t.has(l)||t.set(l,0);for(let i of n){let a=t.get(i)||0;t.set(i,++a);}}return e(t)}}function ye(){let c=[],e=[],t={canRedo:()=>{},canUndo:()=>{}},l=()=>{if(!c.length)return;let u=c.pop();u?.undoAction(),o(u);},n=()=>{if(!e.length)return;let u=e.pop();u?.redoAction(),s(u,false);},i=u=>{t.canUndo=()=>u(c.length);},a=u=>{t.canRedo=()=>u(e.length);},o=u=>{e.push(u),e.length>100&&e.shift(),t.canRedo(),t.canUndo();},s=(u,y=true)=>{y&&(e.length=0),c.push(u),c.length>100&&c.shift(),t.canUndo(),t.canRedo();};return {Undo:l,Redo:n,PushIntoHistory:s,CreateHistoryAction:(u,y)=>{let[S,f]=u;return {undoAction:()=>y(S),redoAction:()=>y(f)}},initCanUndo:i,initCanRedo:a}}var Q=()=>{let c=[];return {on:e=>(c.push(e),()=>{let r=c.indexOf(e);r>-1&&c.splice(r,1);}),call:e=>c.forEach(r=>r(e))}};function he(){let{on:c,call:e}=Q();return {onError:c,callOnError:e}}function me(){let{on:c,call:e}=Q();return {onSuccess:c,callOnSuccess:e}}var ge=()=>{let c=new Set,e=new Map,r=(n,i)=>{e.get(n)?.forEach(a=>a(i));},t=(n,i)=>(e.has(n)||e.set(n,new Set),e.get(n).add(i),()=>e.get(n).delete(i));return {usePlugin:n=>{let i=new Set,a=(o,s)=>{let d=t(o,s);return i.add(d),d};return n.apply({on:a}),c.add(n),()=>{i.forEach(o=>o()),i.clear(),c.delete(n);}},emit:r}};function Se(){let{on:c,call:e}=Q();return {onStart:c,callOnStart:e}}function Te(c,e,r){let t=false,l=false,n=new Map,i=new Map,a=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetAllNextDependency:R,rebuildDirectDependencyMaps:C}=pe(()=>n,()=>i,()=>o,()=>a),{Undo:E,Redo:N,PushIntoHistory:w,CreateHistoryAction:V,initCanUndo:Y,initCanRedo:_}=ye(),{onError:z,callOnError:q}=he(),{onSuccess:W,callOnSuccess:H}=me(),{onStart:J,callOnStart:Z}=Se(),{emit:h,usePlugin:T}=ge(),{SetTrace:x,useTrace:F}=de(),A=F();T(A);let{schema:O,GetFormData:k,GetRenderSchemaByPath:M,GetGroupByPath:m,notifyAll:b,convertToRenderSchema:G}=se(c,{useGreedy:e.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:R,GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetPathToLevelMap:()=>d},{pushIntoHistory:w,createHistoryAction:V},{callOnError:q,callOnSuccess:H,callOnStart:Z,emit:h},r),P=(K,L)=>{let j=m(K),re=G(L,K);return j.children.push(re),j.dirtySignal.value++,re},{SetRule:p,SetRules:g}=le(M,n,i),{SetStrategy:D}=ce(M),{SetValidators:v}=ue(M),I=fe(n),B=()=>{let K=I();s=K.steps,d=K.levelMap;};return {schema:O,SetRule:(...K)=>{p.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetRules:(...K)=>{g.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetStrategy:D,SetValidators:v,SetTrace:x,usePlugin:T,SetValue:(K,L)=>{M(K).dependOn(()=>L);},GetValue:(K,L="value")=>M(K)[L],GetFormData:k,GetGroupByPath:m,notifyAll:async()=>{B(),await b();},AddNewSchema:P,GetAllDependency:()=>n,GetDependencyOrder:()=>s,Undo:E,Redo:N,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:W,onStart:J}}var X=new Map,Re=(c,e,r)=>{try{if(typeof r.UITrigger.signalCreateor!="function"||typeof r.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(X.has(c))throw Error("engineID repeated");let t=Te(e,r.config||{useGreedy:!1},r.UITrigger),{schema:l,GetFormData:n,SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,SetValue:d,GetValue:u,usePlugin:y,GetGroupByPath:S,notifyAll:f,SetTrace:R,GetAllDependency:C,GetDependencyOrder:E,AddNewSchema:N,Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:q,onStart:W}=t,H={config:{SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,notifyAll:f,SetTrace:R,usePlugin:y},data:{schema:l,GetFormData:n,AddNewSchema:N,SetValue:d,GetValue:u,GetGroupByPath:S},history:{Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_},dependency:{GetAllDependency:C,GetDependencyOrder:E},hooks:{onError:z,onSuccess:q,onStart:W}};return X.set(c,H),H}catch(t){throw Error(t)}},pt=c=>{if(X.has(c))return X.get(c);throw Error("id undefined")},ft=c=>{X.delete(c);},yt=Re;exports.deleteEngine=ft;exports.useEngine=pt;exports.useEngineManager=Re;exports.useMeshFlow=yt;
1
+ 'use strict';var ne=class{computedRules=[];store={OR:(e,n)=>{let t,l,r=this.computedRules;for(let o=0;o<r.length;o++){let a=r[o],s=a.logic(e);if(s instanceof Promise)return (async()=>{let c=await s;if(a.entityId==="__base__"?l=c:c&&(t=c),typeof t>"u")for(let u=o+1;u<r.length;u++){let h=r[u],S=h.logic(e),p=S instanceof Promise?await S:S;if(h.entityId==="__base__"){l=p;continue}if(p){t=h.value;break}}return typeof t>"u"&&(t=l),{res:t,version:n}})();let i=s;if(a.entityId==="__base__"){l=i;continue}if(i){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:n}},PRIORITY:(e,n)=>{let t,l=this.computedRules;for(let r=0;r<l.length;r++){let a=l[r].logic(e);if(a instanceof Promise)return (async()=>{let s=await a;if(s!==void 0)return {res:s,version:n};for(let i=r+1;i<l.length;i++){let c=l[i].logic(e),u=c instanceof Promise?await c:c;if(u!==void 0)return {res:u,version:n}}return {res:void 0,version:n}})();if(a!==void 0)return {res:a,version:n}}return {res:t,version:n}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat().sort((n,t)=>t.priority-n.priority):this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,n){return this.CurrentStrategy(e,n)}},J=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,n,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=n==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}setDefaultRule(e){let n=new Set;n.add(e),this.rules.set(e.id,n);}setRules(e,n){n&&this.updateDeps(n);let t=++this.id,l={...e,entityId:t};for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(l);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let o=this.rules.get(r);o&&(o.delete(l),o.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[n,t]of e)this.deps.set(n,t);}setRule(e,n){if(n&&this.updateDeps(n),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(l);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let o=this.rules.get(r);o&&(o.delete(l),o.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let n=null;if(e.GetToken&&(n=e.GetToken()),this.pendingPromise&&this.promiseToken!==n&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let o=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof o=="object"||typeof a=="object")t=false;else {let s=Array.from(this.deps.keys());for(let i of s){let c=this.deps.get(i),u=e.GetValueByPath(i);if(c!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=n;let l=++this.version,r=this.strategy.evaluate(e,l);if(!(r instanceof Promise)){let{res:o,version:a}=r;return this.finalizeSync(o,a,e,n)}return this.pendingPromise=(async()=>{try{let{res:o,version:a}=await r;return this.finalizeSync(o,a,e,n)}catch(o){throw {path:this.path,error:o}}finally{this.promiseToken===n&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,n,t,l){return l!==this.promiseToken||n<this.version?this.cache:(this.cache=e,this.deps.forEach((r,o)=>{this.deps.set(o,t.GetValueByPath(o));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},ee=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},n={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(n.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(n);}evaluate(e,n){let t=true,l=[...this.defaultValidators,...this.validators];for(let r of l){if(!r.condition(n))continue;let a=r.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var re=(d={frameQuota:12})=>{let e=performance.now(),n=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),n=0,t=true;},shouldYield(){let r=performance.now();return n++,!!((n>=5||t)&&(n=0,r-e>d.frameQuota||l()))},async yieldToMain(){return new Promise(r=>{Te(()=>{e=performance.now(),n=0,t&&(t=false),r();});})}}},Te=d=>{let{port1:e,port2:n}=new MessageChannel;e.onmessage=d,n.postMessage(null);};function ae(d,e,n,t,l){let r=new Map,o=d.useGreedy,a=re();return async(i,c)=>{let h=Symbol("token");r.set(i,h);let S=false;a.reset();let p=new Set,T=new Set,N=new Set(e.GetAllNextDependency(i));N.add(i);let M=new Map,D=new Map,F=new Set,V=e.GetPathToLevelMap(),_=V.get(i)??0,H=0,q=G=>{e.GetAllNextDependency(G).forEach(U=>{let R=V.get(U)||0;R>H&&(H=R);});};q(i),c.forEach(G=>{F.add(G);}),p.add(i);let z=performance.now();t.emit("flow:start",{path:i}),t.callOnStart({path:i});let W=false,$=30,g=G=>{let{target:y,trigger:U}=G,R=false,k=false,C=n.GetNodeByPath(y),E=[],P=(f,m)=>{if(p.has(f)||T.has(f)||F.has(f))return;let B=0,v=V.get(f)??0;if(M.has(f))B=M.get(f)-1;else {if(v>_&&M.size>$){D.has(v)||D.set(v,new Set),D.get(v).add(f),t.emit("node:intercept",{path:f,type:7});return}let O=e.GetPrevDependency(f),L=0;for(let K of O){if(p.has(K))continue;(V.get(K)??0)>_&&L++;}B=L;}if(B<=0){let O=F.has(f),L=T.has(f);if(O||L){t.emit("node:intercept",{path:f,type:L?3:3.1});return}M.delete(f),F.add(f),t.emit("node:release",{path:f,type:m,detail:{path:y}});}else M.set(f,B);},b=(f=[])=>{if(r.get(i)!==h)return;if(f.length){let v={};for(let O of f){let L=(O.args||[]).reduce((K,I)=>(K[I]=C[I],K),{});try{let K=O.fn(L);K&&typeof K=="object"&&Object.assign(v,K);}catch(K){}}for(let O in v)C[O]=v[O];R=true;}R&&l.flushPathSet.add(y),t.emit("node:success",{path:y}),p.add(y);let m=e.GetNextDependency(y);(R||k)&&e.GetAllNextDependency(y).forEach(O=>N.add(O));for(let v of m){if(p.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(T.has(v)||F.has(v)){t.emit("node:intercept",{path:v,type:T.has(v)?3:3.1});continue}if(R||k)P(v,1);else if(M.has(v))P(v,2);else {let L=V.get(v);D.has(L)||D.set(L,new Set);let K=D.get(L);K.has(v)||(K.add(v),t.emit("node:stagnate",{path:v,type:1}));}}T.delete(y),(async()=>{if(!S){let v=T.size,O=F.size;t.emit("flow:fire",{path:y,type:1,detail:{active:v,pending:O,blocked:M.size}}),w();}})();},A=f=>{t.emit("node:error",{path:y,error:f});let m=Symbol("abort");r.set(i,m),F.clear(),M.clear(),T.clear(),t.callOnError(f);},x=(f,m)=>{let B=false;f!==C[m]&&(C[m]=f,R=true,t.emit("node:bucket:success",{path:y,key:m,value:f}),m==="value"&&(B=true)),C.nodeBucket[m].isForceNotify()&&(k=true),(B||k)&&q(y);};t.emit("node:start",{path:y});try{let f=[];for(let m in C.nodeBucket){let B=C.nodeBucket[m];f.push(...B.getSideEffect());let v=B.evaluate({affectKey:m,triggerPath:U,GetRenderSchemaByPath:n.GetNodeByPath,GetValueByPath:O=>n.GetNodeByPath(O).value,GetToken:()=>h});if(v instanceof Promise){let O=v.then(L=>{r.get(i)===h&&x(L,m);});E.push(O);}else x(v,m);}if(E.length>0)return Promise.all(E).then(()=>{b(f);}).catch(A);b(f);return}catch(f){A(f);}},w=async()=>{if(r.get(i)!==h){S=false;return}S=true;let G=a.getIsFirstFrame(),y=0,U=()=>o&&G?30:1/0,R=0,k=U();try{for(;r.get(i)===h;){let C=R>=k,E=a.shouldYield();if(C||E){if(R>0&&(y++,(G||y%2===0)&&l.requestUpdate()),await a.yieldToMain(),r.get(i)!==h)break;R=0,G=a.getIsFirstFrame();}if(F.size>0&&T.size<5){for(let P of F){if(T.size>=5||R>=k)break;let b=V.get(P)??0,A=e.GetPrevDependency(P),x=A.length>1;if((!o||x)&&b>_){F.delete(P);let m=A.filter(B=>N.has(B)&&!p.has(B)).length;M.set(P,m||0),t.emit("node:intercept",{path:P,type:m>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:m}});continue}if(F.delete(P),T.add(P),t.emit("node:processing",{path:P}),g({target:P,trigger:i}),R++,R>=k||a.shouldYield())break}if(F.size>0)continue}if(R<k&&o&&M.size>0&&T.size<5){let P=!1,b=0;for(let[A,x]of M)if(x<=0){let f=V.get(A)??0,m=e.GetPrevDependency(A);if(f>_&&m.length>1)continue;if(M.delete(A),F.add(A),b++,P=!0,t.emit("node:release",{path:A,type:4}),b>=k)break}if(b>0)continue;if(P){if(a.shouldYield()&&(await a.yieldToMain(),r.get(i)!==h))break;continue}}if(T.size===0&&F.size===0){let P=new Set;for(let x of D.keys())P.add(x);for(let[x]of M){let f=V.get(x)??0;f>_&&P.add(f);}let b=Array.from(P).sort((x,f)=>x-f),A=b[0];if(b.length>0&&A<=H){let x=b[0];if(x<=H){_=x;let f=D.get(x);f&&(f.forEach(m=>F.add(m)),D.delete(x));for(let[m]of M)(V.get(m)??0)===x&&(M.delete(m),F.add(m),t.emit("node:release",{path:m,type:3,detail:{level:x}}));continue}}else {D.forEach((x,f)=>{x.forEach(m=>{p.add(m),t.emit("node:intercept",{path:m,type:6});});}),D.clear();for(let[x]of M)p.add(x),t.emit("node:intercept",{path:x,type:6});M.clear();break}}F.size>0&&T.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,T.size+M.size+F.size===0){if(r.get(i)===h&&!W){W=true,t.emit("flow:end",{type:1}),l.requestUpdate();let E=performance.now();t.emit("flow:success",{duration:(E-z).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:T.size}});}};w();}}function se(d,e,n,t,l,r){let o=xe(d),a=0,s=new Map,i=new Map,c=new Map,u=false,h=new Set,S=false,p=true,T=null,N=g=>{let w=s.get(g);return i.get(w)},M=g=>c.get(g),D=async()=>{let g=Array.from(h);h.clear();for(let w of g){let G=N(w);r.signalTrigger(G.dirtySignal);}},F=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;h.size>0;)D();}finally{u=false;}}));},V=()=>{let g=(w,G,y)=>{if(typeof w!="object"||w===null||Array.isArray(w)){if(y.length>0){let R=y[y.length-1];G[R]=N(y.join(".")).value;}return}let U=Object.getOwnPropertyNames(w);for(let R of U)y.push(R),g(w[R],w,y),y.pop();};return g(o,null,[]),o},Y=ae({useGreedy:e.useGreedy},n,{GetNodeByPath:N},l,{requestUpdate:F,flushPathSet:h}),_=async()=>(S&&T||(S=true,T=(async()=>{let g=n.GetDependencyOrder(),w=performance.now(),G=performance.now();try{for(let U=0;U<g.length;U++){let R=g[U];await Promise.all(R.map(async k=>{let C=N(k),E=!1;for(let P in C.nodeBucket){let b=await C.nodeBucket[P].evaluate({affectKey:P,triggerPath:void 0,GetRenderSchemaByPath:N,GetValueByPath:A=>N(A).value,isSameToken:()=>!0});if(P==="options"){let A=!1,x=C.value;for(let f of b)if(f.value==x){A=!0;break}A||(C.value=void 0,E=!0);}b!==C[P]&&(C[P]=b,E=!0);}E&&h.add(k);})),performance.now()-G>12&&(await new Promise(k=>requestAnimationFrame(k)),G=performance.now());}h.size>0&&F(),p=!1;let y=performance.now();l.emit("flow:success",{duration:(y-w).toFixed(2)+"ms"}),l.callOnSuccess();}catch(y){throw l.emit("node:error",{path:y.path,error:y.error}),l.callOnError(y),y}finally{S=false,T=null,p=false;}})()),T),H=g=>{if(p)return;if(!N(g))throw Error("Node undefined");performance.now();h.add(g),F();let y=n.GetNextDependency(g);q(y,g);};function q(g,w){Y(w,g);}let z=(g,w="")=>{let G="name"in g?g.name:void 0,y=G?w===""?G:`${w}.${G}`:w,U=r.signalCreateor(),R=a++,k={getRenderSchema:P=>N(P)},C=(P,b)=>{let A=P(b),x=N(b.path),f=t.createHistoryAction([{path:b.path,value:x.value},{path:b.path,value:A}],m=>{let B=N(m.path);B.value=m.value,H(m.path);});x.value=A,t.pushIntoHistory(f),H(b.path);},E={...g,disabled:!!g.disabled,hidden:"hidden"in g?g.hidden:false,readonly:"readonly"in g?g.readonly:false,required:"required"in g?g.required:false,path:y,dirtySignal:U,uid:R,nodeBucket:{},validators:new ee(y),theme:"secondary",dependOn:P=>C(P,{...k,path:y})};return g.type==="group"&&(delete E.nodeBucket,delete E.validators,E.children=g.children.map(P=>z(P,y)),c.set(E.path,E)),s.set(E.path,E.uid),i.set(E.uid,E),E};return {schema:z(d),GetFormData:()=>V(),GetRenderSchemaByPath:N,GetGroupByPath:M,notifyAll:_,convertToRenderSchema:z}}function xe(d,e={}){let n=r=>{if(r.type=="group")return {key:r.name||"",isGroup:true,val:r.children.reduce((o,a)=>[...o,n(a)],[])};if(r.type=="input"||r.type=="number"||r.type=="select"||r.type=="checkbox")return {key:r.name,isGroup:false,val:r.value};throw Error(`undefined type:${r.type}`)},t=(r,o)=>{if(o.isGroup){let a={};o.key===""?a=r:r[o.key]=a,o.val.forEach(s=>{t(a,s);});}else r[o.key]=o.val;},l=n(d);return t(e,l),e}var oe=(d,e,n)=>{let l=r=>{let o=n.triggerPaths.map(i=>r.GetValueByPath(i)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>o}),Object.defineProperty(a,"affectedTatget",{get:()=>r.GetRenderSchemaByPath(d)[e]}),n.logic({slot:a})};return {value:n.value,targetPath:d,triggerPaths:n.triggerPaths,priority:n.priority??10,logic:l}},ie=(d,e,n)=>{if(!d)throw Error("");let t=d,l=(a,s)=>{e.has(a)||e.set(a,new Set),e.get(a).add(s),n.has(s)||n.set(s,new Set),n.get(s).add(a);};return {SetRule:(a,s,i,c={logic:u=>{}})=>{let u=t(s),h=oe(s,i,{...c,triggerPaths:[a]}),S=[a].map(p=>[p,t(p).value]);if(l(a,s),u.nodeBucket[i])u.nodeBucket[i].setRule(h,S),c.effect&&u.nodeBucket[i].setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]});else {let p=new J(u[i],i,s);p.setRule(h,S),c.effect&&p.setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]}),u.nodeBucket[i]=p;}c.forceNotify&&u.nodeBucket[i].forceNotify();},SetRules:(a,s,i,c={logic:u=>{}})=>{let u=t(s);for(let p of a)l(p,s);let h=oe(s,i,{...c,triggerPaths:a}),S=a.map(p=>[p,t(p).value]);if(u.nodeBucket[i])u.nodeBucket[i].setRules(h,S),c.effect&&u.nodeBucket[i].setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]});else {let p=new J(u[i],i,s);p.setRules(h,S),c.effect&&p.setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]}),u.nodeBucket[i]=p;}c.forceNotify&&u.nodeBucket[i].forceNotify();}}};var le=d=>{let e=d||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,r)=>{e(t).nodeBucket[l].setStrategy(r);}}};var ce=d=>{let e=d||void 0;return {SetValidators:(t,l)=>{let r=e(t),o=(s,i,c)=>s(i,c),a=(s,i,c)=>s(i,c);if(!r.validators)throw Error("validator init error");r.validators.setValidators({logic:s=>o(l.logic,s,e),condition:typeof l.condition=="function"?s=>a(l.condition,s,e):()=>true});}}};function de(){let d=new Map,e=new Map,n=new Set,t=(o,a)=>{d.set(o,a);let s=e.get(o);s&&s(a);};return {SetTrace:(o,a)=>{e.set(o,a);let s=d.get(o)||"idle";return a(s),()=>{e.delete(o);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{n.forEach(s=>t(s,"idle")),n.clear(),d.clear();}),a.on("node:release",({path:s,type:i})=>{(i==1||i==2)&&(n.add(s),t(s,"pending"));}),a.on("node:pending",({path:s})=>{n.add(s),(!d.has(s)||d.get(s)==="idle")&&t(s,"pending");}),a.on("node:start",({path:s})=>{n.add(s),t(s,"calculating");}),a.on("node:success",({path:s})=>{t(s,"calculated");}),a.on("node:intercept",({path:s,type:i})=>{i==3&&t(s,"calculating"),i==6&&t(s,"idle");}),a.on("node:stagnate",({path:s})=>{t(s,"pending");}),a.on("node:error",({path:s})=>t(s,"error"));}})}}function ue(d,e,n,t){let l=c=>{let u=d(),h=e(),S=new Set;return u.get(c)?.forEach(p=>S.add(p)),S.size===0?[]:Array.from(S).filter(p=>{let T=h.get(p)||new Set;return !Array.from(T).some(D=>S.has(D))})};return {GetNextDependency:c=>{let u=t();return Array.from(u.get(c)||[])},GetPrevDependency:c=>{let u=n();return Array.from(u.get(c)||[])},GetAllPrevDependency:c=>{let u=e();return Array.from(u.get(c)||[])},GetAllNextDependency:c=>{let u=d();return Array.from(u.get(c)||[])},rebuildDirectDependencyMaps:c=>{let u=new Map,h=new Map;for(let S of c){let p=l(S);u.set(S,new Set(p));for(let T of p)h.has(T)||h.set(T,new Set),h.get(T).add(S);}return {directNextMap:u,directPrevMap:h}}}}function fe(d){let e=t=>{let l=[],r=[],o=new Map,a=t.size,s=0,i=0;for(let[c,u]of t)u===0&&r.push(c);if(r.length===0&&a>0)throw Error("Circular dependency detected");for(;r.length>0;){l.push([...r]);let c=[];for(let u of r){s++,o.set(u,i);let h=d.get(u);if(h)for(let S of h){let p=t.get(S)-1;t.set(S,p),p===0&&c.push(S);}}r=c,i++;}if(s<a)throw Error("Circular dependency detected");return {steps:l,levelMap:o}};return ()=>{let t=new Map;for(let l of d.keys()){let r=Array.from(d.get(l)||[]);t.has(l)||t.set(l,0);for(let o of r){let a=t.get(o)||0;t.set(o,++a);}}return e(t)}}var X=()=>{let d=[];return {on:e=>(d.push(e),()=>{let n=d.indexOf(e);n>-1&&d.splice(n,1);}),call:e=>d.forEach(n=>n(e))}};function pe(){let{on:d,call:e}=X();return {onError:d,callOnError:e}}function ye(){let{on:d,call:e}=X();return {onSuccess:d,callOnSuccess:e}}var he=()=>{let d=new Set,e=new Map,n=(r,o)=>{e.get(r)?.forEach(a=>a(o));},t=(r,o)=>(e.has(r)||e.set(r,new Set),e.get(r).add(o),()=>e.get(r).delete(o));return {usePlugin:r=>{let o=new Set,a=(s,i)=>{let c=t(s,i);return o.add(c),c};return r.apply({on:a}),d.add(r),()=>{o.forEach(s=>s()),o.clear(),d.delete(r);}},emit:n}};function me(){let{on:d,call:e}=X();return {onStart:d,callOnStart:e}}function ge(d,e){let n=false,t=false,l=new Map,r=new Map,o=new Map,a=new Map,s=[],i=new Map,{GetNextDependency:u,GetPrevDependency:h,GetAllPrevDependency:S,GetAllNextDependency:p,rebuildDirectDependencyMaps:T}=ue(()=>l,()=>r,()=>a,()=>o),N={},M={};if(e.modules.useHistory){let{Undo:I,Redo:j,PushIntoHistory:Q,CreateHistoryAction:Z,initCanUndo:Pe,initCanRedo:Se}=e.modules.useHistory();N.pushIntoHistory=Q,N.createHistoryAction=Z,M={Undo:I,Redo:j,initCanUndo:Pe,initCanRedo:Se};}let{onError:D,callOnError:F}=pe(),{onSuccess:V,callOnSuccess:Y}=ye(),{onStart:_,callOnStart:H}=me(),{emit:q,usePlugin:z}=he(),{SetTrace:W,useTrace:$}=de(),g=$();z(g);let{schema:w,GetFormData:G,GetRenderSchemaByPath:y,GetGroupByPath:U,notifyAll:R,convertToRenderSchema:k}=se(d,{useGreedy:e.config.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:p,GetNextDependency:u,GetPrevDependency:h,GetAllPrevDependency:S,GetPathToLevelMap:()=>i},N,{callOnError:F,callOnSuccess:Y,callOnStart:H,emit:q},e.UITrigger),C=(I,j)=>{let Q=U(I),Z=k(j,I);return Q.children.push(Z),Q.dirtySignal.value++,Z},{SetRule:E,SetRules:P}=ie(y,l,r),{SetStrategy:b}=le(y),{SetValidators:A}=ce(y),x=fe(l),f=()=>{let I=x();s=I.steps,i=I.levelMap;},m=()=>{t||(t=true,Promise.resolve().then(()=>{if(f(),n){let{directNextMap:I,directPrevMap:j}=T(s.flat());o=I,a=j;}}).finally(()=>{t=false,n=false;}));};return {schema:w,SetRule:(...I)=>{E.apply(null,I),n=true,m();},SetRules:(...I)=>{P.apply(null,I),n=true,m();},SetStrategy:b,SetValidators:A,SetTrace:W,usePlugin:z,SetValue:(I,j)=>{y(I).dependOn(()=>j);},GetValue:(I,j="value")=>y(I)[j],GetFormData:G,GetGroupByPath:U,notifyAll:async()=>{f(),await R();},AddNewSchema:C,GetAllDependency:()=>l,GetDependencyOrder:()=>s,historyExports:M,onError:D,onSuccess:V,onStart:_}}var te=new Map,ve=(d,e,n)=>{try{if(typeof n.UITrigger.signalCreateor!="function"||typeof n.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(te.has(d))throw Error("engineID repeated");let t=ge(e,{config:n.config||{useGreedy:!1},UITrigger:n.UITrigger,modules:n.modules||{},plugins:{}}),{schema:l,GetFormData:r,SetRule:o,SetRules:a,SetStrategy:s,SetValidators:i,SetValue:c,GetValue:u,usePlugin:h,GetGroupByPath:S,notifyAll:p,SetTrace:T,GetAllDependency:N,GetDependencyOrder:M,AddNewSchema:D,historyExports:F,onError:V,onSuccess:Y,onStart:_}=t,q={...{config:{SetRule:o,SetRules:a,SetStrategy:s,SetValidators:i,notifyAll:p,SetTrace:T,usePlugin:h},data:{schema:l,GetFormData:r,AddNewSchema:D,SetValue:c,GetValue:u,GetGroupByPath:S},dependency:{GetAllDependency:N,GetDependencyOrder:M},hooks:{onError:V,onSuccess:Y,onStart:_}}},z=n.modules;return z&&Object.keys(z).forEach(W=>{let $=W;if($.startsWith("use")){let g=$.slice(3);$=g.charAt(0).toLowerCase()+g.slice(1);}W==="useHistory"&&F&&Object.keys(F).length>0&&(q[$]=F);}),te.set(d,q),q}catch(t){throw Error(t)}},dt=()=>(d,e,n)=>Fe(d,e,n),ut=d=>{let e=te.get(d);if(e)return e;throw Error("[MeshFlow] Engine ID not found.")},ft=d=>{te.delete(d);},Fe=ve;exports.deleteEngine=ft;exports.useEngine=ut;exports.useEngineManager=ve;exports.useMeshFlow=Fe;exports.useMeshFlowDefiner=dt;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- var ne=class{computedRules=[];store={OR:(e,r)=>{let t,l,n=this.computedRules;for(let i=0;i<n.length;i++){let a=n[i],o=a.logic(e);if(o instanceof Promise)return (async()=>{let d=await o;if(a.entityId==="__base__"?l=d:d&&(t=a.value),typeof t>"u")for(let u=i+1;u<n.length;u++){let y=n[u],S=y.logic(e),f=S instanceof Promise?await S:S;if(y.entityId==="__base__"){l=f;continue}if(f){t=y.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}})();let s=o;if(a.entityId==="__base__"){l=s;continue}if(s){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}},PRIORITY:(e,r)=>{let t=null,l=this.computedRules;for(let n=0;n<l.length;n++){let a=l[n].logic(e);if(a instanceof Promise)return (async()=>{let o=await a;if(o!==void 0)return {res:o,version:r};for(let s=n+1;s<l.length;s++){let d=l[s].logic(e),u=d instanceof Promise?await d:d;if(u!==void 0)return {res:u,version:r}}return {res:void 0,version:r}})();if(a!==void 0)return {res:a,version:r}}return {res:t,version:r}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat().sort((r,t)=>t.priority-r.priority):this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,r){return this.CurrentStrategy(e,r)}},$=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,r,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=r==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}updateInputValueRule(e){this.isValue&&this.setRule({priority:1,entityId:"__input_value__",logic:()=>e});}setDefaultRule(e){let r=new Set;r.add(e),this.rules.set(e.id,r);}setRules(e,r){r&&this.updateDeps(r);let t=++this.id,l={...e,entityId:t};for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[r,t]of e)this.deps.set(r,t);}setRule(e,r){if(r&&this.updateDeps(r),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let r=null;if(e.GetToken&&(r=e.GetToken()),this.pendingPromise&&this.promiseToken!==r&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let i=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof i=="object"||typeof a=="object")t=false;else {let o=Array.from(this.deps.keys());for(let s of o){let d=this.deps.get(s),u=e.GetValueByPath(s);if(d!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=r;let l=++this.version,n=this.strategy.evaluate(e,l);if(!(n instanceof Promise)){let{res:i,version:a}=n;return this.finalizeSync(i,a,e,r)}return this.pendingPromise=(async()=>{try{let{res:i,version:a}=await n;return this.finalizeSync(i,a,e,r)}catch(i){throw {path:this.path,error:i}}finally{this.promiseToken===r&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,r,t,l){return l!==this.promiseToken||r<this.version?this.cache:(this.cache=e,this.deps.forEach((n,i)=>{this.deps.set(i,t.GetValueByPath(i));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},te=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},r={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(r.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(r);}evaluate(e,r){let t=true,l=[...this.defaultValidators,...this.validators];for(let n of l){if(!n.condition(r))continue;let a=n.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var ae=(c={frameQuota:12})=>{let e=performance.now(),r=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),r=0,t=true;},shouldYield(){let n=performance.now();return r++,!!((r>=5||t)&&(r=0,n-e>c.frameQuota||l()))},async yieldToMain(){return new Promise(n=>{Pe(()=>{e=performance.now(),r=0,t&&(t=false),n();});})}}},Pe=c=>{let{port1:e,port2:r}=new MessageChannel;e.onmessage=c,r.postMessage(null);};function oe(c,e,r,t,l){let n=new Map,i=c.useGreedy,a=ae();return async(s,d)=>{let y=Symbol("token");n.set(s,y);let S=false;a.reset();let f=new Set,R=new Set,C=new Set(e.GetAllNextDependency(s));C.add(s);let E=new Map,N=new Map,w=new Set,V=e.GetPathToLevelMap(),_=V.get(s)??0,z=0,q=T=>{e.GetAllNextDependency(T).forEach(F=>{let A=V.get(F)||0;A>z&&(z=A);});};q(s),d.forEach(T=>{w.add(T);}),f.add(s);let W=performance.now();t.emit("flow:start",{path:s}),t.callOnStart({path:s});let H=false,J=30,Z=T=>{let{target:x,trigger:F}=T,A=false,O=false,k=r.GetRenderSchemaByPath(x),M=[],m=(p,g)=>{if(f.has(p)||R.has(p)||w.has(p))return;let D=0,v=V.get(p)??0;if(E.has(p))D=E.get(p)-1;else {if(v>_&&E.size>J){N.has(v)||N.set(v,new Set),N.get(v).add(p),t.emit("node:intercept",{path:p,type:7});return}let I=e.GetPrevDependency(p),B=0;for(let U of I){if(f.has(U))continue;(V.get(U)??0)>_&&B++;}D=B;}if(D<=0){let I=w.has(p),B=R.has(p);if(I||B){t.emit("node:intercept",{path:p,type:B?3:3.1});return}E.delete(p),w.add(p),t.emit("node:release",{path:p,type:g,detail:{path:x}});}else E.set(p,D);},b=(p=[])=>{if(n.get(s)!==y)return;if(p.length){let v={};for(let I of p){let B=(I.args||[]).reduce((U,ee)=>(U[ee]=k[ee],U),{});try{let U=I.fn(B);U&&typeof U=="object"&&Object.assign(v,U);}catch(U){}}for(let I in v)k[I]=v[I];A=true;}A&&l.flushPathSet.add(x),t.emit("node:success",{path:x}),f.add(x);let g=e.GetNextDependency(x);(A||O)&&e.GetAllNextDependency(x).forEach(I=>C.add(I));for(let v of g){if(f.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(R.has(v)||w.has(v)){t.emit("node:intercept",{path:v,type:R.has(v)?3:3.1});continue}if(A||O)m(v,1);else if(E.has(v))m(v,2);else {let B=V.get(v);N.has(B)||N.set(B,new Set);let U=N.get(B);U.has(v)||(U.add(v),t.emit("node:stagnate",{path:v,type:1}));}}R.delete(x),(async()=>{if(!S){let v=R.size,I=w.size;t.emit("flow:fire",{path:x,type:1,detail:{active:v,pending:I,blocked:E.size}}),h();}})();},G=p=>{t.emit("node:error",{path:x,error:p});let g=Symbol("abort");n.set(s,g),w.clear(),E.clear(),R.clear(),t.callOnError(p);},P=(p,g)=>{let D=false;p!==k[g]&&(k[g]=p,A=true,t.emit("node:bucket:success",{path:x,key:g,value:p}),g==="value"&&(D=true)),k.nodeBucket[g].isForceNotify()&&(O=true),(D||O)&&q(x);};t.emit("node:start",{path:x});try{let p=[];for(let g in k.nodeBucket){let D=k.nodeBucket[g];p.push(...D.getSideEffect());let v=D.evaluate({affectKey:g,triggerPath:F,GetRenderSchemaByPath:r.GetRenderSchemaByPath,GetValueByPath:I=>r.GetRenderSchemaByPath(I).value,GetToken:()=>y});if(v instanceof Promise){let I=v.then(B=>{n.get(s)===y&&P(B,g);});M.push(I);}else P(v,g);}if(M.length>0)return Promise.all(M).then(()=>{b(p);}).catch(G);b(p);return}catch(p){G(p);}},h=async()=>{if(n.get(s)!==y){S=false;return}S=true;let T=a.getIsFirstFrame(),x=0,F=()=>i&&T?30:1/0,A=0,O=F();try{for(;n.get(s)===y;){let k=A>=O,M=a.shouldYield();if(k||M){if(A>0&&(x++,(T||x%2===0)&&l.requestUpdate()),await a.yieldToMain(),n.get(s)!==y)break;A=0,T=a.getIsFirstFrame();}if(w.size>0&&R.size<5){for(let m of w){if(R.size>=5||A>=O)break;let b=V.get(m)??0,G=e.GetPrevDependency(m),P=G.length>1;if((!i||P)&&b>_){w.delete(m);let g=G.filter(D=>C.has(D)&&!f.has(D)).length;E.set(m,g||0),t.emit("node:intercept",{path:m,type:g>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:g}});continue}if(w.delete(m),R.add(m),t.emit("node:processing",{path:m}),Z({target:m,trigger:s}),A++,A>=O||a.shouldYield())break}if(w.size>0)continue}if(A<O&&i&&E.size>0&&R.size<5){let m=!1,b=0;for(let[G,P]of E)if(P<=0){let p=V.get(G)??0,g=e.GetPrevDependency(G);if(p>_&&g.length>1)continue;if(E.delete(G),w.add(G),b++,m=!0,t.emit("node:release",{path:G,type:4}),b>=O)break}if(b>0)continue;if(m){if(a.shouldYield()&&(await a.yieldToMain(),n.get(s)!==y))break;continue}}if(R.size===0&&w.size===0){let m=new Set;for(let P of N.keys())m.add(P);for(let[P]of E){let p=V.get(P)??0;p>_&&m.add(p);}let b=Array.from(m).sort((P,p)=>P-p),G=b[0];if(b.length>0&&G<=z){let P=b[0];if(P<=z){_=P;let p=N.get(P);p&&(p.forEach(g=>w.add(g)),N.delete(P));for(let[g]of E)(V.get(g)??0)===P&&(E.delete(g),w.add(g),t.emit("node:release",{path:g,type:3,detail:{level:P}}));continue}}else {N.forEach((P,p)=>{P.forEach(g=>{f.add(g),t.emit("node:intercept",{path:g,type:6});});}),N.clear();for(let[P]of E)f.add(P),t.emit("node:intercept",{path:P,type:6});E.clear();break}}w.size>0&&R.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,R.size+E.size+w.size===0){if(n.get(s)===y&&!H){H=true,t.emit("flow:end",{type:1}),l.requestUpdate();let M=performance.now();t.emit("flow:success",{duration:(M-W).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:R.size}});}};h();}}function se(c,e,r,t,l,n){let i=ve(c),a=0,o=new Map,s=new Map,d=new Map,u=false,y=new Set,S=false,f=true,R=null,C=h=>{let T=o.get(h);return s.get(T)},E=h=>d.get(h),N=async()=>{let h=Array.from(y);y.clear();for(let T of h){let x=C(T);n.signalTrigger(x.dirtySignal);}},w=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;y.size>0;)N();}finally{u=false;}}));},V=()=>{let h=(T,x,F)=>{if(typeof T!="object"||T===null||Array.isArray(T)){if(F.length>0){let O=F[F.length-1];x[O]=C(F.join(".")).value;}return}let A=Object.getOwnPropertyNames(T);for(let O of A)F.push(O),h(T[O],T,F),F.pop();};return h(i,null,[]),i},Y=oe({useGreedy:e.useGreedy},r,{GetRenderSchemaByPath:C},l,{requestUpdate:w,flushPathSet:y}),_=async()=>(S&&R||(S=true,R=(async()=>{let h=r.GetDependencyOrder(),T=performance.now(),x=performance.now();try{for(let A=0;A<h.length;A++){let O=h[A];await Promise.all(O.map(async k=>{let M=C(k),m=!1;for(let b in M.nodeBucket){let G=await M.nodeBucket[b].evaluate({affectKey:b,triggerPath:void 0,GetRenderSchemaByPath:C,GetValueByPath:P=>C(P).value,isSameToken:()=>!0});if(b==="options"){let P=!1,p=M.value;for(let g of G)if(g.value==p){P=!0;break}P||(M.value=void 0,m=!0);}G!==M[b]&&(M[b]=G,m=!0);}m&&y.add(k);})),performance.now()-x>12&&(await new Promise(k=>requestAnimationFrame(k)),x=performance.now());}y.size>0&&w(),f=!1;let F=performance.now();l.emit("flow:success",{duration:(F-T).toFixed(2)+"ms"}),l.callOnSuccess();}catch(F){throw l.emit("node:error",{path:F.path,error:F.error}),l.callOnError(F),F}finally{S=false,R=null,f=false;}})()),R),z=h=>{if(f)return;if(!C(h))throw Error("Node undefined");performance.now();y.add(h),w();let F=r.GetNextDependency(h);q(F,h);};function q(h,T){Y(T,h);}let W=h=>{if(!h)throw Error("path error");let T=C(h);T.nodeBucket.value&&T.nodeBucket.value.updateInputValueRule(T.value);},H=(h,T="")=>{let x="name"in h?h.name:void 0,F=x?T===""?x:`${T}.${x}`:T,A=n.signalCreateor(),O=a++,k={getRenderSchema:b=>C(b)},M=(b,G)=>{let P=b(G),p=C(G.path),g=t.createHistoryAction([{path:G.path,value:p.value},{path:G.path,value:P}],D=>{let v=C(D.path);v.value=D.value,W(D.path),z(D.path);});p.value=P,t.pushIntoHistory(g),W(G.path),z(G.path);},m={...h,disabled:!!h.disabled,hidden:"hidden"in h?h.hidden:false,readonly:"readonly"in h?h.readonly:false,required:"required"in h?h.required:false,path:F,dirtySignal:A,uid:O,nodeBucket:{},validators:new te(F),theme:"secondary",dependOn:b=>M(b,{...k,path:F})};return h.type==="group"&&(delete m.nodeBucket,delete m.validators,m.children=h.children.map(b=>H(b,F)),d.set(m.path,m)),o.set(m.path,m.uid),s.set(m.uid,m),m};return {schema:H(c),GetFormData:()=>V(),GetRenderSchemaByPath:C,GetGroupByPath:E,notifyAll:_,convertToRenderSchema:H}}function ve(c,e={}){let r=n=>{if(n.type=="group")return {key:n.name||"",isGroup:true,val:n.children.reduce((i,a)=>[...i,r(a)],[])};if(n.type=="input"||n.type=="number"||n.type=="select"||n.type=="checkbox")return {key:n.name,isGroup:false,val:n.value};throw Error(`undefined type:${n.type}`)},t=(n,i)=>{if(i.isGroup){let a={};i.key===""?a=n:n[i.key]=a,i.val.forEach(o=>{t(a,o);});}else n[i.key]=i.val;},l=r(c);return t(e,l),e}var ie=(c,e,r)=>{let l=n=>{let i=r.triggerPaths.map(s=>n.GetValueByPath(s)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>i}),Object.defineProperty(a,"affectedTatget",{get:()=>n.GetRenderSchemaByPath(c)[e]}),r.logic({slot:a})};return {value:r.value,targetPath:c,triggerPaths:r.triggerPaths,priority:r.priority??10,logic:l}},le=(c,e,r)=>{if(!c)throw Error("");let t=c,l=(a,o)=>{e.has(a)||e.set(a,new Set),e.get(a).add(o),r.has(o)||r.set(o,new Set),r.get(o).add(a);};return {SetRule:(a,o,s,d={logic:u=>{}})=>{let u=t(o),y=ie(o,s,{...d,triggerPaths:[a]}),S=[a].map(f=>[f,t(f).value]);if(l(a,o),u.nodeBucket[s])u.nodeBucket[s].setRule(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRule(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();},SetRules:(a,o,s,d={logic:u=>{}})=>{let u=t(o);for(let f of a)l(f,o);let y=ie(o,s,{...d,triggerPaths:a}),S=a.map(f=>[f,t(f).value]);if(u.nodeBucket[s])u.nodeBucket[s].setRules(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRules(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();}}},ce=c=>{let e=c||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,n)=>{e(t).nodeBucket[l].setStrategy(n);}}};var ue=c=>{let e=c||void 0;return {SetValidators:(t,l)=>{let n=e(t),i=(o,s,d)=>o(s,d),a=(o,s,d)=>o(s,d);if(!n.validators)throw Error("validator init error");n.validators.setValidators({logic:o=>i(l.logic,o,e),condition:typeof l.condition=="function"?o=>a(l.condition,o,e):()=>true});}}};function de(){let c=new Map,e=new Map,r=new Set,t=(i,a)=>{c.set(i,a);let o=e.get(i);o&&o(a);};return {SetTrace:(i,a)=>{e.set(i,a);let o=c.get(i)||"idle";return a(o),()=>{e.delete(i);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{r.forEach(o=>t(o,"idle")),r.clear(),c.clear();}),a.on("node:release",({path:o,type:s})=>{(s==1||s==2)&&(r.add(o),t(o,"pending"));}),a.on("node:pending",({path:o})=>{r.add(o),(!c.has(o)||c.get(o)==="idle")&&t(o,"pending");}),a.on("node:start",({path:o})=>{r.add(o),t(o,"calculating");}),a.on("node:success",({path:o})=>{t(o,"calculated");}),a.on("node:intercept",({path:o,type:s})=>{s==3&&t(o,"calculating"),s==6&&t(o,"idle");}),a.on("node:stagnate",({path:o})=>{t(o,"pending");}),a.on("node:error",({path:o})=>t(o,"error"));}})}}function pe(c,e,r,t){let l=d=>{let u=c(),y=e(),S=new Set;return u.get(d)?.forEach(f=>S.add(f)),S.size===0?[]:Array.from(S).filter(f=>{let R=y.get(f)||new Set;return !Array.from(R).some(N=>S.has(N))})};return {GetNextDependency:d=>{let u=t();return Array.from(u.get(d)||[])},GetPrevDependency:d=>{let u=r();return Array.from(u.get(d)||[])},GetAllPrevDependency:d=>{let u=e();return Array.from(u.get(d)||[])},GetAllNextDependency:d=>{let u=c();return Array.from(u.get(d)||[])},rebuildDirectDependencyMaps:d=>{let u=new Map,y=new Map;for(let S of d){let f=l(S);u.set(S,new Set(f));for(let R of f)y.has(R)||y.set(R,new Set),y.get(R).add(S);}return {directNextMap:u,directPrevMap:y}}}}function fe(c){let e=t=>{let l=[],n=[],i=new Map,a=t.size,o=0,s=0;for(let[d,u]of t)u===0&&n.push(d);if(n.length===0&&a>0)throw Error("Circular dependency detected");for(;n.length>0;){l.push([...n]);let d=[];for(let u of n){o++,i.set(u,s);let y=c.get(u);if(y)for(let S of y){let f=t.get(S)-1;t.set(S,f),f===0&&d.push(S);}}n=d,s++;}if(o<a)throw Error("Circular dependency detected");return {steps:l,levelMap:i}};return ()=>{let t=new Map;for(let l of c.keys()){let n=Array.from(c.get(l)||[]);t.has(l)||t.set(l,0);for(let i of n){let a=t.get(i)||0;t.set(i,++a);}}return e(t)}}function ye(){let c=[],e=[],t={canRedo:()=>{},canUndo:()=>{}},l=()=>{if(!c.length)return;let u=c.pop();u?.undoAction(),o(u);},n=()=>{if(!e.length)return;let u=e.pop();u?.redoAction(),s(u,false);},i=u=>{t.canUndo=()=>u(c.length);},a=u=>{t.canRedo=()=>u(e.length);},o=u=>{e.push(u),e.length>100&&e.shift(),t.canRedo(),t.canUndo();},s=(u,y=true)=>{y&&(e.length=0),c.push(u),c.length>100&&c.shift(),t.canUndo(),t.canRedo();};return {Undo:l,Redo:n,PushIntoHistory:s,CreateHistoryAction:(u,y)=>{let[S,f]=u;return {undoAction:()=>y(S),redoAction:()=>y(f)}},initCanUndo:i,initCanRedo:a}}var Q=()=>{let c=[];return {on:e=>(c.push(e),()=>{let r=c.indexOf(e);r>-1&&c.splice(r,1);}),call:e=>c.forEach(r=>r(e))}};function he(){let{on:c,call:e}=Q();return {onError:c,callOnError:e}}function me(){let{on:c,call:e}=Q();return {onSuccess:c,callOnSuccess:e}}var ge=()=>{let c=new Set,e=new Map,r=(n,i)=>{e.get(n)?.forEach(a=>a(i));},t=(n,i)=>(e.has(n)||e.set(n,new Set),e.get(n).add(i),()=>e.get(n).delete(i));return {usePlugin:n=>{let i=new Set,a=(o,s)=>{let d=t(o,s);return i.add(d),d};return n.apply({on:a}),c.add(n),()=>{i.forEach(o=>o()),i.clear(),c.delete(n);}},emit:r}};function Se(){let{on:c,call:e}=Q();return {onStart:c,callOnStart:e}}function Te(c,e,r){let t=false,l=false,n=new Map,i=new Map,a=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetAllNextDependency:R,rebuildDirectDependencyMaps:C}=pe(()=>n,()=>i,()=>o,()=>a),{Undo:E,Redo:N,PushIntoHistory:w,CreateHistoryAction:V,initCanUndo:Y,initCanRedo:_}=ye(),{onError:z,callOnError:q}=he(),{onSuccess:W,callOnSuccess:H}=me(),{onStart:J,callOnStart:Z}=Se(),{emit:h,usePlugin:T}=ge(),{SetTrace:x,useTrace:F}=de(),A=F();T(A);let{schema:O,GetFormData:k,GetRenderSchemaByPath:M,GetGroupByPath:m,notifyAll:b,convertToRenderSchema:G}=se(c,{useGreedy:e.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:R,GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetPathToLevelMap:()=>d},{pushIntoHistory:w,createHistoryAction:V},{callOnError:q,callOnSuccess:H,callOnStart:Z,emit:h},r),P=(K,L)=>{let j=m(K),re=G(L,K);return j.children.push(re),j.dirtySignal.value++,re},{SetRule:p,SetRules:g}=le(M,n,i),{SetStrategy:D}=ce(M),{SetValidators:v}=ue(M),I=fe(n),B=()=>{let K=I();s=K.steps,d=K.levelMap;};return {schema:O,SetRule:(...K)=>{p.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetRules:(...K)=>{g.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetStrategy:D,SetValidators:v,SetTrace:x,usePlugin:T,SetValue:(K,L)=>{M(K).dependOn(()=>L);},GetValue:(K,L="value")=>M(K)[L],GetFormData:k,GetGroupByPath:m,notifyAll:async()=>{B(),await b();},AddNewSchema:P,GetAllDependency:()=>n,GetDependencyOrder:()=>s,Undo:E,Redo:N,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:W,onStart:J}}var X=new Map,Re=(c,e,r)=>{try{if(typeof r.UITrigger.signalCreateor!="function"||typeof r.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(X.has(c))throw Error("engineID repeated");let t=Te(e,r.config||{useGreedy:!1},r.UITrigger),{schema:l,GetFormData:n,SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,SetValue:d,GetValue:u,usePlugin:y,GetGroupByPath:S,notifyAll:f,SetTrace:R,GetAllDependency:C,GetDependencyOrder:E,AddNewSchema:N,Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:q,onStart:W}=t,H={config:{SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,notifyAll:f,SetTrace:R,usePlugin:y},data:{schema:l,GetFormData:n,AddNewSchema:N,SetValue:d,GetValue:u,GetGroupByPath:S},history:{Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_},dependency:{GetAllDependency:C,GetDependencyOrder:E},hooks:{onError:z,onSuccess:q,onStart:W}};return X.set(c,H),H}catch(t){throw Error(t)}},pt=c=>{if(X.has(c))return X.get(c);throw Error("id undefined")},ft=c=>{X.delete(c);},yt=Re;export{ft as deleteEngine,pt as useEngine,Re as useEngineManager,yt as useMeshFlow};
1
+ var ne=class{computedRules=[];store={OR:(e,n)=>{let t,l,r=this.computedRules;for(let o=0;o<r.length;o++){let a=r[o],s=a.logic(e);if(s instanceof Promise)return (async()=>{let c=await s;if(a.entityId==="__base__"?l=c:c&&(t=c),typeof t>"u")for(let u=o+1;u<r.length;u++){let h=r[u],S=h.logic(e),p=S instanceof Promise?await S:S;if(h.entityId==="__base__"){l=p;continue}if(p){t=h.value;break}}return typeof t>"u"&&(t=l),{res:t,version:n}})();let i=s;if(a.entityId==="__base__"){l=i;continue}if(i){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:n}},PRIORITY:(e,n)=>{let t,l=this.computedRules;for(let r=0;r<l.length;r++){let a=l[r].logic(e);if(a instanceof Promise)return (async()=>{let s=await a;if(s!==void 0)return {res:s,version:n};for(let i=r+1;i<l.length;i++){let c=l[i].logic(e),u=c instanceof Promise?await c:c;if(u!==void 0)return {res:u,version:n}}return {res:void 0,version:n}})();if(a!==void 0)return {res:a,version:n}}return {res:t,version:n}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat().sort((n,t)=>t.priority-n.priority):this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,n){return this.CurrentStrategy(e,n)}},J=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,n,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=n==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}setDefaultRule(e){let n=new Set;n.add(e),this.rules.set(e.id,n);}setRules(e,n){n&&this.updateDeps(n);let t=++this.id,l={...e,entityId:t};for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(l);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let o=this.rules.get(r);o&&(o.delete(l),o.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[n,t]of e)this.deps.set(n,t);}setRule(e,n){if(n&&this.updateDeps(n),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(l);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let o=this.rules.get(r);o&&(o.delete(l),o.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let n=null;if(e.GetToken&&(n=e.GetToken()),this.pendingPromise&&this.promiseToken!==n&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let o=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof o=="object"||typeof a=="object")t=false;else {let s=Array.from(this.deps.keys());for(let i of s){let c=this.deps.get(i),u=e.GetValueByPath(i);if(c!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=n;let l=++this.version,r=this.strategy.evaluate(e,l);if(!(r instanceof Promise)){let{res:o,version:a}=r;return this.finalizeSync(o,a,e,n)}return this.pendingPromise=(async()=>{try{let{res:o,version:a}=await r;return this.finalizeSync(o,a,e,n)}catch(o){throw {path:this.path,error:o}}finally{this.promiseToken===n&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,n,t,l){return l!==this.promiseToken||n<this.version?this.cache:(this.cache=e,this.deps.forEach((r,o)=>{this.deps.set(o,t.GetValueByPath(o));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},ee=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},n={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(n.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(n);}evaluate(e,n){let t=true,l=[...this.defaultValidators,...this.validators];for(let r of l){if(!r.condition(n))continue;let a=r.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var re=(d={frameQuota:12})=>{let e=performance.now(),n=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),n=0,t=true;},shouldYield(){let r=performance.now();return n++,!!((n>=5||t)&&(n=0,r-e>d.frameQuota||l()))},async yieldToMain(){return new Promise(r=>{Te(()=>{e=performance.now(),n=0,t&&(t=false),r();});})}}},Te=d=>{let{port1:e,port2:n}=new MessageChannel;e.onmessage=d,n.postMessage(null);};function ae(d,e,n,t,l){let r=new Map,o=d.useGreedy,a=re();return async(i,c)=>{let h=Symbol("token");r.set(i,h);let S=false;a.reset();let p=new Set,T=new Set,N=new Set(e.GetAllNextDependency(i));N.add(i);let M=new Map,D=new Map,F=new Set,V=e.GetPathToLevelMap(),_=V.get(i)??0,H=0,q=G=>{e.GetAllNextDependency(G).forEach(U=>{let R=V.get(U)||0;R>H&&(H=R);});};q(i),c.forEach(G=>{F.add(G);}),p.add(i);let z=performance.now();t.emit("flow:start",{path:i}),t.callOnStart({path:i});let W=false,$=30,g=G=>{let{target:y,trigger:U}=G,R=false,k=false,C=n.GetNodeByPath(y),E=[],P=(f,m)=>{if(p.has(f)||T.has(f)||F.has(f))return;let B=0,v=V.get(f)??0;if(M.has(f))B=M.get(f)-1;else {if(v>_&&M.size>$){D.has(v)||D.set(v,new Set),D.get(v).add(f),t.emit("node:intercept",{path:f,type:7});return}let O=e.GetPrevDependency(f),L=0;for(let K of O){if(p.has(K))continue;(V.get(K)??0)>_&&L++;}B=L;}if(B<=0){let O=F.has(f),L=T.has(f);if(O||L){t.emit("node:intercept",{path:f,type:L?3:3.1});return}M.delete(f),F.add(f),t.emit("node:release",{path:f,type:m,detail:{path:y}});}else M.set(f,B);},b=(f=[])=>{if(r.get(i)!==h)return;if(f.length){let v={};for(let O of f){let L=(O.args||[]).reduce((K,I)=>(K[I]=C[I],K),{});try{let K=O.fn(L);K&&typeof K=="object"&&Object.assign(v,K);}catch(K){}}for(let O in v)C[O]=v[O];R=true;}R&&l.flushPathSet.add(y),t.emit("node:success",{path:y}),p.add(y);let m=e.GetNextDependency(y);(R||k)&&e.GetAllNextDependency(y).forEach(O=>N.add(O));for(let v of m){if(p.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(T.has(v)||F.has(v)){t.emit("node:intercept",{path:v,type:T.has(v)?3:3.1});continue}if(R||k)P(v,1);else if(M.has(v))P(v,2);else {let L=V.get(v);D.has(L)||D.set(L,new Set);let K=D.get(L);K.has(v)||(K.add(v),t.emit("node:stagnate",{path:v,type:1}));}}T.delete(y),(async()=>{if(!S){let v=T.size,O=F.size;t.emit("flow:fire",{path:y,type:1,detail:{active:v,pending:O,blocked:M.size}}),w();}})();},A=f=>{t.emit("node:error",{path:y,error:f});let m=Symbol("abort");r.set(i,m),F.clear(),M.clear(),T.clear(),t.callOnError(f);},x=(f,m)=>{let B=false;f!==C[m]&&(C[m]=f,R=true,t.emit("node:bucket:success",{path:y,key:m,value:f}),m==="value"&&(B=true)),C.nodeBucket[m].isForceNotify()&&(k=true),(B||k)&&q(y);};t.emit("node:start",{path:y});try{let f=[];for(let m in C.nodeBucket){let B=C.nodeBucket[m];f.push(...B.getSideEffect());let v=B.evaluate({affectKey:m,triggerPath:U,GetRenderSchemaByPath:n.GetNodeByPath,GetValueByPath:O=>n.GetNodeByPath(O).value,GetToken:()=>h});if(v instanceof Promise){let O=v.then(L=>{r.get(i)===h&&x(L,m);});E.push(O);}else x(v,m);}if(E.length>0)return Promise.all(E).then(()=>{b(f);}).catch(A);b(f);return}catch(f){A(f);}},w=async()=>{if(r.get(i)!==h){S=false;return}S=true;let G=a.getIsFirstFrame(),y=0,U=()=>o&&G?30:1/0,R=0,k=U();try{for(;r.get(i)===h;){let C=R>=k,E=a.shouldYield();if(C||E){if(R>0&&(y++,(G||y%2===0)&&l.requestUpdate()),await a.yieldToMain(),r.get(i)!==h)break;R=0,G=a.getIsFirstFrame();}if(F.size>0&&T.size<5){for(let P of F){if(T.size>=5||R>=k)break;let b=V.get(P)??0,A=e.GetPrevDependency(P),x=A.length>1;if((!o||x)&&b>_){F.delete(P);let m=A.filter(B=>N.has(B)&&!p.has(B)).length;M.set(P,m||0),t.emit("node:intercept",{path:P,type:m>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:m}});continue}if(F.delete(P),T.add(P),t.emit("node:processing",{path:P}),g({target:P,trigger:i}),R++,R>=k||a.shouldYield())break}if(F.size>0)continue}if(R<k&&o&&M.size>0&&T.size<5){let P=!1,b=0;for(let[A,x]of M)if(x<=0){let f=V.get(A)??0,m=e.GetPrevDependency(A);if(f>_&&m.length>1)continue;if(M.delete(A),F.add(A),b++,P=!0,t.emit("node:release",{path:A,type:4}),b>=k)break}if(b>0)continue;if(P){if(a.shouldYield()&&(await a.yieldToMain(),r.get(i)!==h))break;continue}}if(T.size===0&&F.size===0){let P=new Set;for(let x of D.keys())P.add(x);for(let[x]of M){let f=V.get(x)??0;f>_&&P.add(f);}let b=Array.from(P).sort((x,f)=>x-f),A=b[0];if(b.length>0&&A<=H){let x=b[0];if(x<=H){_=x;let f=D.get(x);f&&(f.forEach(m=>F.add(m)),D.delete(x));for(let[m]of M)(V.get(m)??0)===x&&(M.delete(m),F.add(m),t.emit("node:release",{path:m,type:3,detail:{level:x}}));continue}}else {D.forEach((x,f)=>{x.forEach(m=>{p.add(m),t.emit("node:intercept",{path:m,type:6});});}),D.clear();for(let[x]of M)p.add(x),t.emit("node:intercept",{path:x,type:6});M.clear();break}}F.size>0&&T.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,T.size+M.size+F.size===0){if(r.get(i)===h&&!W){W=true,t.emit("flow:end",{type:1}),l.requestUpdate();let E=performance.now();t.emit("flow:success",{duration:(E-z).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:T.size}});}};w();}}function se(d,e,n,t,l,r){let o=xe(d),a=0,s=new Map,i=new Map,c=new Map,u=false,h=new Set,S=false,p=true,T=null,N=g=>{let w=s.get(g);return i.get(w)},M=g=>c.get(g),D=async()=>{let g=Array.from(h);h.clear();for(let w of g){let G=N(w);r.signalTrigger(G.dirtySignal);}},F=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;h.size>0;)D();}finally{u=false;}}));},V=()=>{let g=(w,G,y)=>{if(typeof w!="object"||w===null||Array.isArray(w)){if(y.length>0){let R=y[y.length-1];G[R]=N(y.join(".")).value;}return}let U=Object.getOwnPropertyNames(w);for(let R of U)y.push(R),g(w[R],w,y),y.pop();};return g(o,null,[]),o},Y=ae({useGreedy:e.useGreedy},n,{GetNodeByPath:N},l,{requestUpdate:F,flushPathSet:h}),_=async()=>(S&&T||(S=true,T=(async()=>{let g=n.GetDependencyOrder(),w=performance.now(),G=performance.now();try{for(let U=0;U<g.length;U++){let R=g[U];await Promise.all(R.map(async k=>{let C=N(k),E=!1;for(let P in C.nodeBucket){let b=await C.nodeBucket[P].evaluate({affectKey:P,triggerPath:void 0,GetRenderSchemaByPath:N,GetValueByPath:A=>N(A).value,isSameToken:()=>!0});if(P==="options"){let A=!1,x=C.value;for(let f of b)if(f.value==x){A=!0;break}A||(C.value=void 0,E=!0);}b!==C[P]&&(C[P]=b,E=!0);}E&&h.add(k);})),performance.now()-G>12&&(await new Promise(k=>requestAnimationFrame(k)),G=performance.now());}h.size>0&&F(),p=!1;let y=performance.now();l.emit("flow:success",{duration:(y-w).toFixed(2)+"ms"}),l.callOnSuccess();}catch(y){throw l.emit("node:error",{path:y.path,error:y.error}),l.callOnError(y),y}finally{S=false,T=null,p=false;}})()),T),H=g=>{if(p)return;if(!N(g))throw Error("Node undefined");performance.now();h.add(g),F();let y=n.GetNextDependency(g);q(y,g);};function q(g,w){Y(w,g);}let z=(g,w="")=>{let G="name"in g?g.name:void 0,y=G?w===""?G:`${w}.${G}`:w,U=r.signalCreateor(),R=a++,k={getRenderSchema:P=>N(P)},C=(P,b)=>{let A=P(b),x=N(b.path),f=t.createHistoryAction([{path:b.path,value:x.value},{path:b.path,value:A}],m=>{let B=N(m.path);B.value=m.value,H(m.path);});x.value=A,t.pushIntoHistory(f),H(b.path);},E={...g,disabled:!!g.disabled,hidden:"hidden"in g?g.hidden:false,readonly:"readonly"in g?g.readonly:false,required:"required"in g?g.required:false,path:y,dirtySignal:U,uid:R,nodeBucket:{},validators:new ee(y),theme:"secondary",dependOn:P=>C(P,{...k,path:y})};return g.type==="group"&&(delete E.nodeBucket,delete E.validators,E.children=g.children.map(P=>z(P,y)),c.set(E.path,E)),s.set(E.path,E.uid),i.set(E.uid,E),E};return {schema:z(d),GetFormData:()=>V(),GetRenderSchemaByPath:N,GetGroupByPath:M,notifyAll:_,convertToRenderSchema:z}}function xe(d,e={}){let n=r=>{if(r.type=="group")return {key:r.name||"",isGroup:true,val:r.children.reduce((o,a)=>[...o,n(a)],[])};if(r.type=="input"||r.type=="number"||r.type=="select"||r.type=="checkbox")return {key:r.name,isGroup:false,val:r.value};throw Error(`undefined type:${r.type}`)},t=(r,o)=>{if(o.isGroup){let a={};o.key===""?a=r:r[o.key]=a,o.val.forEach(s=>{t(a,s);});}else r[o.key]=o.val;},l=n(d);return t(e,l),e}var oe=(d,e,n)=>{let l=r=>{let o=n.triggerPaths.map(i=>r.GetValueByPath(i)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>o}),Object.defineProperty(a,"affectedTatget",{get:()=>r.GetRenderSchemaByPath(d)[e]}),n.logic({slot:a})};return {value:n.value,targetPath:d,triggerPaths:n.triggerPaths,priority:n.priority??10,logic:l}},ie=(d,e,n)=>{if(!d)throw Error("");let t=d,l=(a,s)=>{e.has(a)||e.set(a,new Set),e.get(a).add(s),n.has(s)||n.set(s,new Set),n.get(s).add(a);};return {SetRule:(a,s,i,c={logic:u=>{}})=>{let u=t(s),h=oe(s,i,{...c,triggerPaths:[a]}),S=[a].map(p=>[p,t(p).value]);if(l(a,s),u.nodeBucket[i])u.nodeBucket[i].setRule(h,S),c.effect&&u.nodeBucket[i].setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]});else {let p=new J(u[i],i,s);p.setRule(h,S),c.effect&&p.setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]}),u.nodeBucket[i]=p;}c.forceNotify&&u.nodeBucket[i].forceNotify();},SetRules:(a,s,i,c={logic:u=>{}})=>{let u=t(s);for(let p of a)l(p,s);let h=oe(s,i,{...c,triggerPaths:a}),S=a.map(p=>[p,t(p).value]);if(u.nodeBucket[i])u.nodeBucket[i].setRules(h,S),c.effect&&u.nodeBucket[i].setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]});else {let p=new J(u[i],i,s);p.setRules(h,S),c.effect&&p.setSideEffect({fn:c.effect,args:c.effectArgs?c.effectArgs:[i]}),u.nodeBucket[i]=p;}c.forceNotify&&u.nodeBucket[i].forceNotify();}}};var le=d=>{let e=d||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,r)=>{e(t).nodeBucket[l].setStrategy(r);}}};var ce=d=>{let e=d||void 0;return {SetValidators:(t,l)=>{let r=e(t),o=(s,i,c)=>s(i,c),a=(s,i,c)=>s(i,c);if(!r.validators)throw Error("validator init error");r.validators.setValidators({logic:s=>o(l.logic,s,e),condition:typeof l.condition=="function"?s=>a(l.condition,s,e):()=>true});}}};function de(){let d=new Map,e=new Map,n=new Set,t=(o,a)=>{d.set(o,a);let s=e.get(o);s&&s(a);};return {SetTrace:(o,a)=>{e.set(o,a);let s=d.get(o)||"idle";return a(s),()=>{e.delete(o);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{n.forEach(s=>t(s,"idle")),n.clear(),d.clear();}),a.on("node:release",({path:s,type:i})=>{(i==1||i==2)&&(n.add(s),t(s,"pending"));}),a.on("node:pending",({path:s})=>{n.add(s),(!d.has(s)||d.get(s)==="idle")&&t(s,"pending");}),a.on("node:start",({path:s})=>{n.add(s),t(s,"calculating");}),a.on("node:success",({path:s})=>{t(s,"calculated");}),a.on("node:intercept",({path:s,type:i})=>{i==3&&t(s,"calculating"),i==6&&t(s,"idle");}),a.on("node:stagnate",({path:s})=>{t(s,"pending");}),a.on("node:error",({path:s})=>t(s,"error"));}})}}function ue(d,e,n,t){let l=c=>{let u=d(),h=e(),S=new Set;return u.get(c)?.forEach(p=>S.add(p)),S.size===0?[]:Array.from(S).filter(p=>{let T=h.get(p)||new Set;return !Array.from(T).some(D=>S.has(D))})};return {GetNextDependency:c=>{let u=t();return Array.from(u.get(c)||[])},GetPrevDependency:c=>{let u=n();return Array.from(u.get(c)||[])},GetAllPrevDependency:c=>{let u=e();return Array.from(u.get(c)||[])},GetAllNextDependency:c=>{let u=d();return Array.from(u.get(c)||[])},rebuildDirectDependencyMaps:c=>{let u=new Map,h=new Map;for(let S of c){let p=l(S);u.set(S,new Set(p));for(let T of p)h.has(T)||h.set(T,new Set),h.get(T).add(S);}return {directNextMap:u,directPrevMap:h}}}}function fe(d){let e=t=>{let l=[],r=[],o=new Map,a=t.size,s=0,i=0;for(let[c,u]of t)u===0&&r.push(c);if(r.length===0&&a>0)throw Error("Circular dependency detected");for(;r.length>0;){l.push([...r]);let c=[];for(let u of r){s++,o.set(u,i);let h=d.get(u);if(h)for(let S of h){let p=t.get(S)-1;t.set(S,p),p===0&&c.push(S);}}r=c,i++;}if(s<a)throw Error("Circular dependency detected");return {steps:l,levelMap:o}};return ()=>{let t=new Map;for(let l of d.keys()){let r=Array.from(d.get(l)||[]);t.has(l)||t.set(l,0);for(let o of r){let a=t.get(o)||0;t.set(o,++a);}}return e(t)}}var X=()=>{let d=[];return {on:e=>(d.push(e),()=>{let n=d.indexOf(e);n>-1&&d.splice(n,1);}),call:e=>d.forEach(n=>n(e))}};function pe(){let{on:d,call:e}=X();return {onError:d,callOnError:e}}function ye(){let{on:d,call:e}=X();return {onSuccess:d,callOnSuccess:e}}var he=()=>{let d=new Set,e=new Map,n=(r,o)=>{e.get(r)?.forEach(a=>a(o));},t=(r,o)=>(e.has(r)||e.set(r,new Set),e.get(r).add(o),()=>e.get(r).delete(o));return {usePlugin:r=>{let o=new Set,a=(s,i)=>{let c=t(s,i);return o.add(c),c};return r.apply({on:a}),d.add(r),()=>{o.forEach(s=>s()),o.clear(),d.delete(r);}},emit:n}};function me(){let{on:d,call:e}=X();return {onStart:d,callOnStart:e}}function ge(d,e){let n=false,t=false,l=new Map,r=new Map,o=new Map,a=new Map,s=[],i=new Map,{GetNextDependency:u,GetPrevDependency:h,GetAllPrevDependency:S,GetAllNextDependency:p,rebuildDirectDependencyMaps:T}=ue(()=>l,()=>r,()=>a,()=>o),N={},M={};if(e.modules.useHistory){let{Undo:I,Redo:j,PushIntoHistory:Q,CreateHistoryAction:Z,initCanUndo:Pe,initCanRedo:Se}=e.modules.useHistory();N.pushIntoHistory=Q,N.createHistoryAction=Z,M={Undo:I,Redo:j,initCanUndo:Pe,initCanRedo:Se};}let{onError:D,callOnError:F}=pe(),{onSuccess:V,callOnSuccess:Y}=ye(),{onStart:_,callOnStart:H}=me(),{emit:q,usePlugin:z}=he(),{SetTrace:W,useTrace:$}=de(),g=$();z(g);let{schema:w,GetFormData:G,GetRenderSchemaByPath:y,GetGroupByPath:U,notifyAll:R,convertToRenderSchema:k}=se(d,{useGreedy:e.config.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:p,GetNextDependency:u,GetPrevDependency:h,GetAllPrevDependency:S,GetPathToLevelMap:()=>i},N,{callOnError:F,callOnSuccess:Y,callOnStart:H,emit:q},e.UITrigger),C=(I,j)=>{let Q=U(I),Z=k(j,I);return Q.children.push(Z),Q.dirtySignal.value++,Z},{SetRule:E,SetRules:P}=ie(y,l,r),{SetStrategy:b}=le(y),{SetValidators:A}=ce(y),x=fe(l),f=()=>{let I=x();s=I.steps,i=I.levelMap;},m=()=>{t||(t=true,Promise.resolve().then(()=>{if(f(),n){let{directNextMap:I,directPrevMap:j}=T(s.flat());o=I,a=j;}}).finally(()=>{t=false,n=false;}));};return {schema:w,SetRule:(...I)=>{E.apply(null,I),n=true,m();},SetRules:(...I)=>{P.apply(null,I),n=true,m();},SetStrategy:b,SetValidators:A,SetTrace:W,usePlugin:z,SetValue:(I,j)=>{y(I).dependOn(()=>j);},GetValue:(I,j="value")=>y(I)[j],GetFormData:G,GetGroupByPath:U,notifyAll:async()=>{f(),await R();},AddNewSchema:C,GetAllDependency:()=>l,GetDependencyOrder:()=>s,historyExports:M,onError:D,onSuccess:V,onStart:_}}var te=new Map,ve=(d,e,n)=>{try{if(typeof n.UITrigger.signalCreateor!="function"||typeof n.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(te.has(d))throw Error("engineID repeated");let t=ge(e,{config:n.config||{useGreedy:!1},UITrigger:n.UITrigger,modules:n.modules||{},plugins:{}}),{schema:l,GetFormData:r,SetRule:o,SetRules:a,SetStrategy:s,SetValidators:i,SetValue:c,GetValue:u,usePlugin:h,GetGroupByPath:S,notifyAll:p,SetTrace:T,GetAllDependency:N,GetDependencyOrder:M,AddNewSchema:D,historyExports:F,onError:V,onSuccess:Y,onStart:_}=t,q={...{config:{SetRule:o,SetRules:a,SetStrategy:s,SetValidators:i,notifyAll:p,SetTrace:T,usePlugin:h},data:{schema:l,GetFormData:r,AddNewSchema:D,SetValue:c,GetValue:u,GetGroupByPath:S},dependency:{GetAllDependency:N,GetDependencyOrder:M},hooks:{onError:V,onSuccess:Y,onStart:_}}},z=n.modules;return z&&Object.keys(z).forEach(W=>{let $=W;if($.startsWith("use")){let g=$.slice(3);$=g.charAt(0).toLowerCase()+g.slice(1);}W==="useHistory"&&F&&Object.keys(F).length>0&&(q[$]=F);}),te.set(d,q),q}catch(t){throw Error(t)}},dt=()=>(d,e,n)=>Fe(d,e,n),ut=d=>{let e=te.get(d);if(e)return e;throw Error("[MeshFlow] Engine ID not found.")},ft=d=>{te.delete(d);},Fe=ve;export{ft as deleteEngine,ut as useEngine,ve as useEngineManager,Fe as useMeshFlow,dt as useMeshFlowDefiner};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshflow/core",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A logic orchestration engine utilizing topological scheduling and watermark control to resolve asynchronous race conditions in complex dependency linkages.”",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",