@meshflow/core 0.4.9 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.mts CHANGED
@@ -1,9 +1,18 @@
1
+ /**
2
+ * @internal
3
+ * */
1
4
  type Unwrap<T> = T extends ReadonlyArray<infer U> ? U : T;
5
+ /**
6
+ * @internal
7
+ * */
2
8
  type InferLeafType<T> = Unwrap<T> extends infer Node ? Node extends {
3
9
  readonly name: any;
4
10
  } ? Node extends {
5
11
  readonly children: infer C;
6
12
  } ? InferLeafType<C> : Node : never : never;
13
+ /**
14
+ * @internal
15
+ * */
7
16
  type InferLeafPath<T, Prefix extends string = ""> = Unwrap<T> extends infer Node ? Node extends {
8
17
  readonly name: infer N;
9
18
  } ? N extends string ? N extends "" ? Node extends {
@@ -16,8 +25,9 @@ type InferLeafPath<T, Prefix extends string = ""> = Unwrap<T> extends infer Node
16
25
  type KeysOfUnion<T> = T extends any ? keyof T : never;
17
26
 
18
27
  /**
19
- * 🚀 核心内部事件名枚举
20
- * 使用 const enum 确保编译后直接内联为字符串,零运行时开销
28
+ * @description 核心内部事件名枚举
29
+ * @group 类型管理
30
+ * @category 事件类型
21
31
  */
22
32
  declare const enum MeshFlowEventsName {
23
33
  FlowStart = 0,
@@ -40,7 +50,8 @@ declare const enum MeshFlowEventsName {
40
50
  EntangleBlocked = 17
41
51
  }
42
52
  /**
43
- * 🧱 基础事件负载定义
53
+ * @description 基础事件负载定义
54
+ * @internal
44
55
  */
45
56
  interface BaseMeshEvents {
46
57
  [MeshFlowEventsName.FlowStart]: {
@@ -106,8 +117,9 @@ interface BaseMeshEvents {
106
117
  };
107
118
  }
108
119
  /**
109
- * 🌟 完整事件扩展定义
110
- * 包含流程控制及“逆转未来”等特殊场景
120
+ * @description 完整事件扩展定义
121
+ * @group 类型管理
122
+ * @category 事件类型
111
123
  */
112
124
  interface MeshEvents extends BaseMeshEvents {
113
125
  [MeshFlowEventsName.FlowSuccess]: {
@@ -125,12 +137,28 @@ interface MeshEvents extends BaseMeshEvents {
125
137
  triggerPath: MeshPath;
126
138
  };
127
139
  }
140
+ /**
141
+ * @internal
142
+ */
128
143
  type MeshEventName = keyof MeshEvents;
144
+ /**
145
+ * @internal
146
+ */
129
147
  type MeshEmit = <K extends MeshEventName>(event: K, data: MeshEvents[K]) => void;
148
+ /**
149
+ * @group 类型管理
150
+ * @category 历史类型
151
+ *
152
+ */
130
153
  type HistoryActionItem = {
131
154
  undoAction: () => void;
132
155
  redoAction: () => void;
133
156
  };
157
+ /**
158
+ * @group 类型管理
159
+ * @category 历史类型
160
+ *
161
+ */
134
162
  type MeshFlowHistory = {
135
163
  Undo: () => void;
136
164
  Redo: () => void;
@@ -148,12 +176,37 @@ type MeshFlowHistory = {
148
176
  redoAction: () => any;
149
177
  };
150
178
  };
179
+ /**
180
+ * @group 类型管理
181
+ * @category 错误类型
182
+ */
151
183
  interface MeshErrorContext {
152
184
  path: string;
153
185
  error: any;
154
186
  }
187
+ /**
188
+ * MeshPath:多模态路径标识符
189
+ * @description
190
+ * 定义 MeshFlow 节点的唯一寻址路径。支持多种原始类型以适配不同的业务场景:
191
+ * - **string**: 💡 推荐。语义化最强,支持深度路径嵌套(如 `user.profile.id`)。
192
+ * - **number**: ✅ 稳定。常用于位操作、枚举 ID 或高性能数组索引节点,与引擎内部 UID 逻辑契合度高。
193
+ * - **symbol**: ⚠️ **实验性**。用于创建绝对私有的节点,防止意外覆盖。
194
+ * @note **关于 Symbol 的约束**:
195
+ * 目前版本下,Symbol 路径无法被标准 JSON 序列化,可能会出现意料之外的bug。
196
+ * @group 类型管理
197
+ * @category 路径类型
198
+ */
155
199
  type MeshPath = string | number | symbol;
200
+ /**
201
+ * @internal
202
+ */
156
203
  type MeshNodeProxy<Node, V, NM, Extra = {}> = Extra & V & Node & NM;
204
+ /**
205
+ *
206
+ * @description task节点类型
207
+ * @group 类型管理
208
+ * @category 节点类型
209
+ */
157
210
  interface MeshFlowTaskNode<P extends MeshPath = MeshPath, V = any, NM = any> {
158
211
  path: P;
159
212
  uid: number;
@@ -168,6 +221,12 @@ interface MeshFlowTaskNode<P extends MeshPath = MeshPath, V = any, NM = any> {
168
221
  dependOn: (cb: (val: V) => V, key?: keyof NM) => void;
169
222
  createView: <E extends Record<string, any> = {}>(extraProps?: E) => MeshNodeProxy<MeshFlowTaskNode<P, V, NM>, V, NM, E>;
170
223
  }
224
+ /**
225
+ *
226
+ * @description group节点类型
227
+ * @group 类型管理
228
+ * @category 节点类型
229
+ */
171
230
  interface MeshFlowGroupNode<P extends MeshPath = MeshPath> {
172
231
  path: P;
173
232
  uid: number;
@@ -177,33 +236,167 @@ interface MeshFlowGroupNode<P extends MeshPath = MeshPath> {
177
236
  meta: Record<string, any>;
178
237
  createView: (extraProps?: Record<string, any>) => any;
179
238
  }
239
+ /**
240
+ * @internal
241
+ * */
180
242
  interface StandardUITrigger<T> {
181
243
  signalCreator: () => T;
182
244
  signalTrigger: (signal: T) => void;
183
245
  }
246
+ /**
247
+ * @group 参数类型
248
+ * @category 依赖设置
249
+ * @description 桶计算的逻辑块入参类型
250
+ */
184
251
  interface logicApi<TKeys extends MeshPath> {
185
252
  slot: {
186
253
  triggerTargets: Array<Record<TKeys | InternalKeys, any>>;
187
254
  affectedTatget: any;
188
255
  };
189
256
  }
257
+ /**
258
+ *
259
+ * @description 节点开放的一些内部键值
260
+ * @group 类型管理
261
+ * @category 节点类型
262
+ */
190
263
  type InternalKeys = 'path' | 'uid' | 'type' | 'meta' | 'state';
264
+ /**
265
+ * 节点规则配置接口
266
+ * @group 参数类型
267
+ * @category 依赖设置
268
+ * @typeParam NM - 状态大盘的类型定义
269
+ * @typeParam TKeys - 当前节点关联的键集合
270
+ * @params logic - 桶计算的逻辑块,一个桶里面可以装多个逻辑块,根据策略进行计算,逻辑块入参参考{}
271
+ */
191
272
  interface SetRuleOptions<NM, TKeys extends KeysOfUnion<NM>> {
273
+ /**
274
+ * 结果覆盖值 (静态产出)
275
+ * * @description
276
+ * 规则命中后的确定性结果。其行为在不同策略下表现如下:
277
+ * * - **【必备】OR 策略**:作为 If-Then 模型的成果。当 logic 返回真值时,此字段提供节点最终产出的数据。
278
+ * - **【可选】PRIORITY 策略**:作为静态覆盖。若配置此值,将无条件替换 logic 的计算结果;若不配置,则直接采用 logic 的返回值。
279
+ * - **【可选】MERGE 策略**:作为结构化补丁。用于在逻辑运算之外,额外合并一份静态的配置增量。
280
+ * * @note 💡 **最佳实践**:
281
+ * 在 `OR` 模式下,建议永远配合 `value` 使用以获得明确的业务状态。
282
+ */
192
283
  value?: any;
284
+ /**
285
+ * 逻辑优先级 (仅在 PRIORITY 策略下生效)
286
+ */
193
287
  priority?: number;
194
288
  forceNotify?: boolean;
289
+ /**
290
+ * 核心逻辑片段 (Logic Fragment)
291
+ * * @description
292
+ * 节点规则的执行体。它是碎片化的,允许针对同一节点注册多个逻辑片段。
293
+ * * **策略影响 (Strategy Impact):**
294
+ * - **OR (逻辑或)**: 只要有一个逻辑片段返回真值,即终止计算并输出该值。
295
+ * - **PRIORITY (优先级)**: 按 `priority` 顺序执行,取第一个非 `undefined` 的返回值。
296
+ * - **MERGE (增量聚合)**: 执行所有逻辑片段,并将结果进行深度合并 (Object/Array)。
297
+ * * @param api 注入的运行上下文 {@link logicApi}
298
+ */
195
299
  logic: (api: logicApi<TKeys>) => any;
300
+ /**
301
+ * 后置副作用 (Post-Settlement Effect)
302
+ * * @description
303
+ * 节点计算完成后的回调钩子。
304
+ *
305
+ * * @param args 由 {@link effectArgs} 指定的实时数据快照。
306
+ */
196
307
  effect?: (args: any) => any;
308
+ /**
309
+ * 📥 副作用参数声明
310
+ * * @description
311
+ * 显式定义需要注入给 `effect` 函数的参数。
312
+ * 引擎会从全局状态大盘 (NM) 中摘取这些字段的最新值,打包传递给副作用函数。
313
+ */
197
314
  effectArgs?: Array<KeysOfUnion<NM>>;
315
+ /**
316
+ * 桶的缓存策略
317
+ * * @description
318
+ * 控制计算结果的记忆化 (Memoization) 行为。
319
+ * - `shallow` (默认): 基于依赖项进行浅比较,未变则直接复用缓存。
320
+ * - `none`: 彻底禁用缓存,每次唤醒必执行。
321
+ */
198
322
  cacheStrategy?: "none" | "shallow";
323
+ /**
324
+ * 触发键定义 (精准点火开关)
325
+ * * @description
326
+ * 定义该“法条”对源节点中哪些字段的变更敏感。
327
+ * * **触发行为:**
328
+ * - **已定义**:仅当列表中的 Key 发生变更时,才执行 `logic`。实现精准的按需计算。
329
+ * - **未定义 (Default)**:源节点(TriggerPath)内的**任意**字段变更都会唤醒本条逻辑。
330
+ */
199
331
  triggerKeys?: Array<TKeys | Exclude<InternalKeys, 'state'>>;
200
332
  }
333
+ /**
334
+ * @group 参数类型
335
+ * @category 纠缠设置
336
+ * @description 提案的update可选的参数,参考{@link GhostProposalApi}
337
+ */
201
338
  type EntangleOp = "add" | "intersect" | "union" | "merge" | "remove";
339
+ /**
340
+ * 幽灵提案 API (Ghost Proposal API)
341
+ * * ### 架构思想:延迟决议 (Deferred Resolution)
342
+ * 在复杂的 DAG (有向无环图) 状态机中,如果在副作用函数中直接修改目标状态(如 `tgt.price = 100`),
343
+ * 极易引发不可控的竞态条件 (Race Condition)、级联重绘或死循环。
344
+ * 为了系统性地规避上述风险,MeshFlow 设计了 **“幽灵提案”** 机制。其核心交互模式借鉴了 **Git 的 Pull Request**:
345
+ * 1. **📝 提交提案 (Propose)**:引擎限制了对状态的直接修改。所有通过此 API 发起的操作(`set` / `update` / `patch`)
346
+ * 都不会立即生效,而是转化为数据对象并暂存于引擎的缓冲池 (`_ghostBuffer`) 中。
347
+ * 2. **🛡️ 统一清算 (Resolve)**:当当前批次的所有计算流执行完毕后,引擎会作为调度中心,统一收集并合并这些提案。
348
+ * 3. **⚖️ 权重裁决 (Weight)**:面对多源并发修改,引擎严格按照提案的**权重 (`weight`)** 和预设策略进行确定性计算,而非依赖执行的先后顺序。
349
+ * > **💡 总结**:幽灵提案机制将不可控的“时间依赖”转化为了安全的“逻辑依赖”,从而保证了每次状态计算的原子性与确定性。
350
+ * @example
351
+ * // 场景:多个规则并发更新购物车总价
352
+ * engine.config.useEntangle({
353
+ * // ...
354
+ * emit: (src, tgt, propose) => {
355
+ * // 提交增量修改提案,而非直接操作 tgt.totalPrice
356
+ * propose.update('totalPrice', src.price, 'add');
357
+ * }
358
+ * });
359
+ * @group 参数类型
360
+ * @category 纠缠设置
361
+ */
202
362
  interface GhostProposalApi<T> {
363
+ /**
364
+ * 提交【绝对值覆盖】提案
365
+ * @description 直接用新值覆盖目标节点的指定状态。
366
+ * @param key 目标节点的状态属性名
367
+ * @param value 期望设置的新值
368
+ * @param weight 提案权重 (默认: 1)。当同一批次内有多个规则试图 `set` 同一个 key 时,权重最高者获胜。
369
+ */
203
370
  set: (key: string, value: any, weight?: number) => void;
371
+ /**
372
+ * 提交【增量运算】提案
373
+ * @description 提交一个增量操作,引擎会在清算时将其与目标节点的旧值进行合并计算。
374
+ * @param key 目标节点的状态属性名
375
+ * @param delta 增量数据 (如累加的数值、需追加的数组元素)
376
+ * @param op 运算策略 (默认: 'add')。支持:累加(add)、移除(remove)、交集(intersect)、并集(union)、深度合并(merge)。
377
+ */
204
378
  update: (key: string, delta: any, op?: EntangleOp) => void;
379
+ /**
380
+ * 提交【函数式补丁】提案
381
+ * @description 基于目标节点的当前状态进行纯函数推导,适用于高度依赖旧值的复杂状态计算。
382
+ * @param key 目标节点的状态属性名
383
+ * @param patchFn 状态计算回调。接收该 key 的当前旧值 (`oldState`),需返回计算后的新值。
384
+ ** @note ⚠️ **性能预警**:
385
+ * `patch` 模式虽然具备最高的自由度,在常规业务逻辑(如表单联动、状态切换)中可放心使用。
386
+ * 但由于其返回对象通常会触发堆内存分配,在高频纠缠的情况下会显著增加 GC压力。
387
+ * 为了追求极致的内存性能并减少 GC 压力,请优先考虑性能更优的update方法。
388
+ */
205
389
  patch: (key: string, patchFn: (oldState: T) => T) => void;
206
390
  }
391
+ /**
392
+ * 幽灵提案数据载体 (Internal Ghost Payload)
393
+ * * @description
394
+ * 这是 {@link GhostProposalApi} 调用的内部物化结构。
395
+ * 当用户在 `emit` 中调用 `propose` 的相关方法时,引擎会实例化此对象并推入 `_ghostBuffer` 缓冲池,等待当前批次调度结束时由 `resolveGhosts` 统一清算。
396
+ * * @internal 引擎内部流转类型,普通开发者无需手动构造。
397
+ * @group Core Api
398
+ * @category Entanglement
399
+ */
207
400
  type EntangleGhost<T = any> = {
208
401
  key: string;
209
402
  value?: any;
@@ -212,29 +405,110 @@ type EntangleGhost<T = any> = {
212
405
  op?: EntangleOp;
213
406
  patch?: (oldState: T) => T;
214
407
  };
408
+ /**
409
+ * 量子纠缠机制的配置选项
410
+ * @typeParam P - 路径标识类型
411
+ * @group 参数类型
412
+ * @category 纠缠设置
413
+ */
215
414
  type EntangleArgType<P extends MeshPath, IsProxy extends boolean = boolean> = {
216
415
  cause: P;
217
416
  impact: P;
218
417
  via: string[];
219
418
  isProxy?: IsProxy;
220
419
  filter?: (cause: IsProxy extends true ? any : MeshFlowTaskNode<P>, impact: IsProxy extends true ? any : MeshFlowTaskNode<P>) => boolean;
420
+ /**
421
+ * @params propose 提案调用参考{@link GhostProposalApi}
422
+ */
221
423
  emit: <T>(cause: IsProxy extends true ? any : MeshFlowTaskNode<P>, impact: IsProxy extends true ? any : MeshFlowTaskNode<P>, propose: GhostProposalApi<T>) => void | EntangleGhost<T> | undefined | Promise<void | EntangleGhost<T> | undefined>;
222
424
  };
425
+ /**
426
+ * 引擎点火溯源标识 (Trigger Cause)
427
+ * * @description
428
+ * 用于标识当前计算任务是被何种机制唤醒的。
429
+ * 在复杂的 DAG 图与量子纠缠网络中,精准的溯源不仅有助于 DevTools 的可视化链路追踪,
430
+ * 更是引擎底层防范“无间断递归”和“环路死锁”的核心判定依据。
431
+ * @group 类型管理
432
+ * @category 内部任务触发类型
433
+ */
223
434
  declare const enum TriggerCause {
435
+ /**
436
+ * **正向因果推导 (CAUSALITY)**
437
+ * @description 顺向的拓扑传播。即:上游依赖节点的值发生变更,导致当前节点按照标准的 DAG 边方向被触发重算。
438
+ * @note 这是引擎最基础、最常规的自然点火原因。
439
+ */
224
440
  CAUSALITY = 0,// 因果推导(正常)
441
+ /**
442
+ * **纠缠源头 (INVERSION)**
443
+ * @description 当前节点作为量子纠缠 (`useEntangle`) 的**“直接标的”**被唤醒。
444
+ * 即:它是被幽灵提案直接修改的那个节点。它打破了原有的触发链,成为了新一轮拓扑计算的“新源头”。
445
+ */
225
446
  INVERSION = 1,// 逆转回跳(纠缠)
447
+ /**
448
+ * **纠缠连锁余波 (REPERCUSSION)**
449
+ * @description 由 `INVERSION` 节点引发的下游连带更新。
450
+ * 即:当前节点本身并没有被纠缠直接修改,但因为它的上游节点是被纠缠修改的,它顺着 DAG 拓扑被“余波”唤醒。
451
+ */
226
452
  REPERCUSSION = 2
227
453
  }
228
-
229
- type ContractType = "boolean" | "scalar" | "array" | "object";
454
+ /**
455
+ * 引擎预设的桶计算策略
456
+ * @description 决定了当一个节点绑定了多个规则时,引擎如何处理冲突、优先级以及最终值的推导。
457
+ * 所有策略均原生支持异步逻辑,并严格保障异步链条中的按序执行原则。
458
+ * @group 类型管理
459
+ * @category 桶计算策略类型
460
+ */
230
461
  declare enum DefaultStrategy {
462
+ /**
463
+ * **逻辑或 / 短路回退策略 (OR)**
464
+ * * @description
465
+ * 按序执行规则。当找到第一个满足条件(逻辑返回真值)的规则时,立即中断后续规则执行(短路机制)。
466
+ * * **核心行为:**
467
+ * 1. **短路匹配**:若 `rule.logic` 返回 truthy,将提取该规则的 `value` 作为最终结果并中断。
468
+ * 2. **异步原子性**:严格按照声明顺序 `await`,保证高优先级规则先被检验。
469
+ * 3. **底色回退**:若所有普通规则均未匹配(或返回 falsy),则回退使用 `__base__` 规则的值作为兜底保障。
470
+ */
231
471
  OR = "OR",
472
+ /**
473
+ * **绝对优先级策略 (PRIORITY)**
474
+ * * @description
475
+ * 严格遵循规则顺序的“首中制”策略,适用于互斥型逻辑判断。
476
+ * * **核心行为:**
477
+ * 1. **非空即中**:按序执行,首个返回 **非 `undefined`** 的规则直接获胜,立即中断后续计算。
478
+ * 2. **无视布尔值**:与 `OR` 策略不同,即使逻辑返回 `false`、`0` 或 `null`,只要不是 `undefined`,依然视为有效命中。
479
+ */
232
480
  PRIORITY = "PRIORITY",
481
+ /**
482
+ * **聚合策略 (MERGE)**
483
+ * * @description
484
+ * 收集桶内所有规则的产出,并按照规则定义的先后顺序进行**结构化合并**。
485
+ * * **核心特性:**
486
+ * 1. **有序覆盖**:后执行的规则结果具有更高优先级。
487
+ * - **对象**:执行浅层合并 `{ ...old, ...new }`,同名键值由后者覆盖。
488
+ * - **数组**:执行末尾追加 `[...old, ...new]`。
489
+ * 2. **异步原子性**:原生支持异步规则。即使存在 Promise,引擎也会通过异步链条严格保证合并顺序与规则声明顺序一致。
490
+ * 3. **底色机制 (__base__)**:支持 `entityId` 为 `__base__` 的特殊规则。其产出作为节点的“底色数据”,会被普通规则的非空产出所覆盖。
491
+ * * @example
492
+ * // 场景:多个规则共同定义一个配置对象
493
+ * // Rule A: { a: 1 }
494
+ * // Rule B (async): 返回 { b: 2 }
495
+ * // 最终结果: { a: 1, b: 2 }
496
+ */
233
497
  MERGE = "MERGE"
234
498
  }
499
+
500
+ type ContractType = "boolean" | "scalar" | "array" | "object";
501
+ /**
502
+ * @group Core Api
503
+ * @category 内部实现
504
+ *
505
+ */
235
506
  declare class SchemaBucket<P> {
236
507
  private path;
237
508
  private strategy;
509
+ /**
510
+ * @internal
511
+ * */
238
512
  contract: ContractType;
239
513
  private rules;
240
514
  private isValue;
@@ -248,11 +522,23 @@ declare class SchemaBucket<P> {
248
522
  useCache: boolean;
249
523
  private effectArray;
250
524
  constructor(baseValue: any, key: string | number | symbol, path: P);
525
+ /**
526
+ * @internal
527
+ * */
251
528
  setUseCache(val: boolean): void;
252
529
  forceNotify(): void;
530
+ /**
531
+ * @internal
532
+ * */
253
533
  isForceNotify(): boolean;
254
534
  setStrategy(type: DefaultStrategy): void;
535
+ /**
536
+ * @internal
537
+ * */
255
538
  setDefaultRule(value: any): void;
539
+ /**
540
+ * @internal
541
+ * */
256
542
  setRules<TKeys = any>(value: {
257
543
  value: any;
258
544
  targetUid: number;
@@ -265,11 +551,17 @@ declare class SchemaBucket<P> {
265
551
  Array<TKeys | Exclude<InternalKeys, "state">>,
266
552
  any
267
553
  ]>): () => void;
554
+ /**
555
+ * @internal
556
+ * */
268
557
  updateDeps<TKeys>(DepsArray: Array<[
269
558
  number,
270
559
  Array<TKeys | Exclude<InternalKeys, "state">>,
271
560
  any
272
561
  ]>): void;
562
+ /**
563
+ * @internal
564
+ * */
273
565
  setRule<TKeys = any>(value: {
274
566
  value: any;
275
567
  targetUid: number;
@@ -282,16 +574,28 @@ declare class SchemaBucket<P> {
282
574
  Array<TKeys | Exclude<InternalKeys, "state">>,
283
575
  any
284
576
  ]>): (() => void) | undefined;
577
+ /**
578
+ * @internal
579
+ * */
285
580
  setSideEffect(data: {
286
581
  fn: (args: any[]) => any;
287
582
  args: any[];
288
583
  }): void;
584
+ /**
585
+ * @internal
586
+ * */
289
587
  getSideEffect(): {
290
588
  fn: (args: any) => any;
291
589
  args: any[];
292
590
  }[];
293
591
  evaluate(api: any): any;
592
+ /**
593
+ * @internal
594
+ * */
294
595
  private finalizeSync;
596
+ /**
597
+ * @internal
598
+ * */
295
599
  private inferType;
296
600
  }
297
601
 
@@ -394,6 +698,11 @@ declare function useEngineInstance<T, P extends MeshPath, S = any, M extends Rec
394
698
  CancelTask: () => void;
395
699
  };
396
700
 
701
+ /**
702
+ * @group Core Api
703
+ * @category 内部实现
704
+ *
705
+ */
397
706
  declare function useScheduler<T, //ui trigger中定义的类型
398
707
  P extends MeshPath, // 路径类型
399
708
  B extends Record<string, any> = StandardUITrigger<T>, NM = any>(config: {
@@ -435,7 +744,13 @@ B extends Record<string, any> = StandardUITrigger<T>, NM = any>(config: {
435
744
  UidToNodeMap: MeshFlowTaskNode<P, any, NM>[];
436
745
  };
437
746
 
747
+ /**
748
+ * @internal
749
+ */
438
750
  type SchedulerType<T, P extends MeshPath, S, M extends Record<string, any>, NM> = ReturnType<typeof useEngineInstance<T, P, S, M, NM>>;
751
+ /**
752
+ * @internal
753
+ */
439
754
  type BaseEngine<T> = {
440
755
  data: {
441
756
  SetValue: T extends {
@@ -497,21 +812,43 @@ type BaseEngine<T> = {
497
812
  } ? F : never;
498
813
  };
499
814
  };
815
+ /**
816
+ * @internal
817
+ */
500
818
  type TransformModuleKey<T> = T extends "useMeshRenderGate" ? "render" : T extends `use${infer Rest}` ? Uncapitalize<Rest> : T;
819
+ /**
820
+ * @internal
821
+ */
501
822
  type MapModuleToReturn<K, F, P extends MeshPath> = K extends "useSchemaValidators" | "schemaValidators" ? {
502
823
  SetValidators: (path: P, options: {
503
824
  logic: (val: any, GetByPath: (path: P) => any) => any;
504
825
  condition: (data: any) => boolean;
505
826
  }) => void;
506
827
  } : K extends "useHistory" | "history" | "useMeshRenderGate" | "meshRenderGate" ? F extends (...args: any) => infer R ? R extends (...args: any) => infer R2 ? R2 : R : any : F extends (...args: any) => infer R ? R : any;
828
+ /**
829
+ * @internal
830
+ */
507
831
  type EngineModules<M extends Record<string, any>, P extends MeshPath> = {
508
832
  [K in keyof M as TransformModuleKey<string & K>]: M[K] extends (...args: any) => any ? MapModuleToReturn<K, M[K], P> : M[K] extends Record<string, any> ? EngineModules<M[K], P> : M[K];
509
833
  };
834
+ /**
835
+ *@internal
836
+ */
510
837
  type Engine<T, M extends Record<string, any>, P extends MeshPath> = BaseEngine<T> & {
511
838
  modules: EngineModules<M, P>;
512
839
  };
513
- /** @deprecated 请使用新的 useMeshFlow 别名 */
514
- declare const useEngineManager: <const S extends Record<string, any> | any[], T, //UITrigger的类型
840
+ /**
841
+ * 初始化并获取 MeshFlow 引擎实例
842
+ * * @description
843
+ *
844
+ * * **查看完整的引擎实例 API 文档,请点击这里:** {@link EngineCoreAPI}
845
+ * @group Core Api
846
+ * @category 入口函数
847
+ * @param id 引擎实例的唯一 ID
848
+ * @param Schema 类型定义模板(仅用于 TS 类型约束,不参与运行逻辑),注册节点通过模块的方式进行
849
+ * @param options 引擎配置项与扩展模块 {@link MeshFlowOptions}
850
+ */
851
+ declare const useMeshFlow: <const S extends Record<string, any> | any[], T, //UITrigger的类型
515
852
  M extends Record<string, any>, NM extends Record<string, any> = InferLeafType<S>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : InferLeafPath<S> | (string & {})>(id: MeshPath, Schema: S, options: {
516
853
  metaType?: NM;
517
854
  config?: {
@@ -587,6 +924,20 @@ M extends Record<string, any>, NM extends Record<string, any> = InferLeafType<S>
587
924
  destroyPlugin: () => void;
588
925
  CancelTask: () => void;
589
926
  }, M, P>;
927
+ /**
928
+ * 类型工厂:锁定全局路径与元数据类型,生成定制化的实例化函数。
929
+ * @description
930
+ * 这是一个高阶函数(Currying),旨在解决泛型冗余。通过预先注入 `MeshPath` (P) 和 `MetaType` (NM),
931
+ * 你会得到一个“专属”的实例化工具,从而避免在业务代码中反复书写冗长的泛型尖括号。
932
+ * **工作流:**
933
+ * 1. 在项目初始化/配置文件中定义:`const defineMesh = useMeshFlowDefiner<MyPaths, MyMeta>();`
934
+ * 2. 在业务逻辑中实例化:`const engine = defineMesh('app-engine', schema, { ... });`
935
+ * @template P - 当前项目定义的路径字面量类型 (MeshPath)
936
+ * @template S - 初始 Schema 的结构定义
937
+ * @template NM - 节点元数据 (Node Metadata) 的类型定义
938
+ * @group Core Api
939
+ * @category 入口函数
940
+ */
590
941
  declare const useMeshFlowDefiner: <P extends MeshPath, S extends Record<string, any> | any[] = any, NM extends Record<string, any> = any>() => <T, M extends Record<string, any>>(id: MeshPath, schema: S, options: {
591
942
  metaType?: NM;
592
943
  UITrigger?: {
@@ -597,86 +948,26 @@ declare const useMeshFlowDefiner: <P extends MeshPath, S extends Record<string,
597
948
  config?: any;
598
949
  }) => Engine<SchedulerType<T, P, S, M, NM>, M, P>;
599
950
  /**
600
- * 获取 Engine 实例
601
- * @template M 手动注入的模块映射 (例如 { useHistory: typeof useHistory })
602
- * @template P ID 类型 (支持 string | number | symbol)
951
+ * 实例检索:跨文件/组件获取已激活的 Engine 实例
952
+ * @description
953
+ * 只要引擎通过 `useMeshFlow` `defineMesh` 初始化过,你就可以在任何地方通过 ID 直接拿到它。
954
+ * 无需 Prop Drilling,它是跨组件通讯的核心桥梁。
955
+ * @template M - 动态插件类型 (可选)
956
+ * @template P - 路径类型标识 (可选)
957
+ * @param id - 引擎实例的唯一 ID
958
+ * @throws {MeshError.EngineNotFound} 如果 ID 对应实例不存在则抛错
959
+ * @group Core Api
960
+ * @category 实例管理
603
961
  */
604
962
  declare const useEngine: <M extends Record<string, any> = {}, P extends MeshPath = any, NM extends Record<string, any> = Record<string, any>, S = any, T = any>(id: MeshPath) => Engine<SchedulerType<T, P, S, M, NM>, M, P>;
963
+ /**
964
+ * 🗑️ 实例销毁:从全局池中注销并释放引擎资源。
965
+ * * @description
966
+ * 彻底切断引擎与其所有插件、异步任务的联系,并从内存中移除引用。
967
+ * * @param id - 待销毁引擎的唯一标识符
968
+ * @group Core Api
969
+ * @category 实例管理
970
+ */
605
971
  declare const deleteEngine: (id: MeshPath) => void;
606
- declare const useMeshFlow: <const S extends Record<string, any> | any[], T, M extends Record<string, any>, NM extends Record<string, any> = InferLeafType<S>, P extends MeshPath = [InferLeafPath<S>] extends [never] ? MeshPath : (string & {}) | InferLeafPath<S>>(id: MeshPath, Schema: S, options: {
607
- metaType?: NM;
608
- config?: {
609
- useGreedy: boolean;
610
- useEntangleStep?: number;
611
- };
612
- modules?: M;
613
- UITrigger?: {
614
- signalCreator: () => T;
615
- signalTrigger: (signal: T) => void;
616
- };
617
- }) => Engine<{
618
- SetRule: <TKeys extends KeysOfUnion<NM>>(outDegreePath: P, inDegreePath: P, key: (string & {}) | KeysOfUnion<NM>, options: SetRuleOptions<NM, TKeys>) => void;
619
- SetRules: <TKeys extends KeysOfUnion<NM>>(outDegreePaths: P[], inDegreePath: P, key: (string & {}) | KeysOfUnion<NM>, options: SetRuleOptions<NM, TKeys>) => void;
620
- SetStrategy: (path: P, key: KeysOfUnion<NM>, strategy: DefaultStrategy) => void;
621
- useEntangle: (config: EntangleArgType<P>) => void;
622
- SetTrace: (myPath: P, onUpdate: (newStatus: "idle" | "pending" | "calculating" | "calculated" | "error" | "canceled") => void) => {
623
- cancel: () => void;
624
- };
625
- usePlugin: (plugin: {
626
- apply: (api: {
627
- on: (event: MeshEventName, cb: Function) => () => boolean | undefined;
628
- }) => void;
629
- }) => () => void;
630
- SetValue: (path: P, key: (string & {}) | KeysOfUnion<NM>, value: any) => void;
631
- GetValue: (path: P, key?: string) => any;
632
- SetValues: (updates: {
633
- path: P;
634
- key: (string & {}) | KeysOfUnion<NM>;
635
- value: any;
636
- }[]) => void;
637
- GetGroupByPath: (path: MeshPath) => MeshFlowGroupNode<MeshPath>;
638
- notifyAll: () => Promise<void>;
639
- GetAllDependency: () => number[][];
640
- GetDependencyOrder: () => number[][];
641
- historyExports: Partial<MeshFlowHistory>;
642
- formExports: {};
643
- validatorExports: {
644
- SetValidators?: ((path: P, options: {
645
- logic: (val: any, GetByPath: any) => any;
646
- condition: (data: any) => boolean;
647
- }) => void) | undefined;
648
- };
649
- batchRenderExport: {
650
- init: any;
651
- };
652
- hasRenderGate: () => boolean;
653
- onError: (cb: (error: MeshErrorContext) => void) => () => void;
654
- onSuccess: (cb: (data: unknown) => void) => () => void;
655
- onStart: (cb: (data: {
656
- path: P;
657
- }) => void) => () => void;
658
- scheduler: {
659
- registerNode: (nodeMeta: Omit<MeshFlowTaskNode<P, any, any>, "createView" | "proxy" | "dependOn" | "calledBy" | "uid" | "dirtySignal" | "nodeBucket">) => MeshFlowTaskNode<P, any, NM>;
660
- registerGroupNode: (groupMeta: Omit<MeshFlowGroupNode<P>, "createView" | "calledBy" | "uid" | "dirtySignal">) => MeshFlowGroupNode<P>;
661
- GetNodeByPath: (path: P) => MeshFlowTaskNode<P, any, NM>;
662
- GetGroupByPath: (path: MeshPath) => MeshFlowGroupNode<MeshPath>;
663
- notify: (path: P) => void;
664
- notifyAll: () => Promise<void>;
665
- batchNotify: (updates: {
666
- path: P;
667
- key: (string & {}) | KeysOfUnion<NM>;
668
- value: any;
669
- }[]) => void;
670
- useEntangle: (config: EntangleArgType<P>) => void;
671
- updateEntangleLevel: () => void;
672
- SetBucket: (newBucket: SchemaBucket<P>) => number;
673
- GetBucket: (bucketId: number) => SchemaBucket<P>;
674
- CancelTask: () => void;
675
- UITrigger: any;
676
- UidToNodeMap: MeshFlowTaskNode<P, any, NM>[];
677
- };
678
- destroyPlugin: () => void;
679
- CancelTask: () => void;
680
- }, M, P>;
681
972
 
682
- export { type BaseEngine, DefaultStrategy, type Engine, type EngineModules, type GhostProposalApi, type InferLeafPath, type InferLeafType, type MapModuleToReturn, type MeshEmit, type MeshErrorContext, type MeshEvents, MeshFlowEventsName, type MeshFlowGroupNode, type MeshFlowTaskNode, type MeshNodeProxy, type MeshPath, type SchedulerType, SchemaBucket, type SetRuleOptions, type TransformModuleKey, TriggerCause, deleteEngine, useEngine, useEngineManager, useMeshFlow, useMeshFlowDefiner, useScheduler };
973
+ export { type BaseEngine, DefaultStrategy, type Engine, type EngineModules, type GhostProposalApi, type InferLeafPath, type InferLeafType, type MapModuleToReturn, type MeshEmit, type MeshErrorContext, type MeshEvents, MeshFlowEventsName, type MeshFlowGroupNode, type MeshFlowTaskNode, type MeshNodeProxy, type MeshPath, type SchedulerType, SchemaBucket, type SetRuleOptions, type TransformModuleKey, TriggerCause, deleteEngine, useEngine, useMeshFlow, useMeshFlowDefiner, useScheduler };