@flowgram.ai/variable-core 0.1.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.
@@ -0,0 +1,996 @@
1
+ import { ContainerModule, interfaces } from 'inversify';
2
+ import * as _flowgram_ai_utils from '@flowgram.ai/utils';
3
+ import { DisposableCollection, Event, Disposable, Emitter } from '@flowgram.ai/utils';
4
+ import { BehaviorSubject, Subject, Observable, Observer as Observer$1 } from 'rxjs';
5
+ export { Observer } from 'rxjs';
6
+ import * as react from 'react';
7
+
8
+ declare const VariableContainerModule: ContainerModule;
9
+
10
+ declare const VariableEngineProvider: unique symbol;
11
+ type VariableEngineProvider = () => VariableEngine;
12
+
13
+ /**
14
+ * 作用域依赖关系管理数据结构
15
+ * - ScopeOrder 可能存在多种实现方式,因此采取抽象类的方式,具体的实现由子类实现
16
+ */
17
+ declare abstract class ScopeChain {
18
+ readonly toDispose: DisposableCollection;
19
+ variableEngineProvider: VariableEngineProvider;
20
+ get variableEngine(): VariableEngine;
21
+ constructor();
22
+ /**
23
+ * 所有作用域依赖关系刷新
24
+ */
25
+ refreshAllChange(): void;
26
+ abstract getDeps(scope: Scope): Scope[];
27
+ abstract getCovers(scope: Scope): Scope[];
28
+ abstract sortAll(): Scope[];
29
+ dispose(): void;
30
+ get disposed(): boolean;
31
+ get onDispose(): Event<void>;
32
+ }
33
+
34
+ declare enum ASTNodeFlags {
35
+ None = 0,
36
+ /**
37
+ * 变量字段
38
+ */
39
+ VariableField = 1,
40
+ /**
41
+ * 表达式
42
+ */
43
+ Expression = 4,
44
+ /**
45
+ * 变量类型
46
+ */
47
+ BasicType = 8,
48
+ DrilldownType = 16,
49
+ EnumerateType = 32,
50
+ UnionType = 64,
51
+ VariableType = 120
52
+ }
53
+
54
+ interface ASTNodeRegistry<JSON extends ASTNodeJSON = any, InjectOpts = any> {
55
+ kind: string;
56
+ new (params: CreateASTParams, injectOpts: InjectOpts): ASTNode<JSON>;
57
+ }
58
+ declare abstract class ASTNode<JSON extends ASTNodeJSON = any, InjectOpts = any> implements Disposable {
59
+ /**
60
+ * @deprecated
61
+ * 获取 ASTNode 注入的 opts
62
+ *
63
+ * 请使用 @injectToAst(XXXService) declare xxxService: XXXService 实现外部依赖注入
64
+ */
65
+ readonly opts?: InjectOpts;
66
+ /**
67
+ * 节点的唯一标识符,节点不指定则默认由 nanoid 生成,不可更改
68
+ * - 如需要生成新 key,则销毁当前节点并生成新的节点
69
+ */
70
+ readonly key: Identifier;
71
+ /**
72
+ * 节点类型
73
+ */
74
+ static readonly kind: ASTKindType;
75
+ /**
76
+ * 节点 Flags,记录一些 Flag 信息
77
+ */
78
+ readonly flags: number;
79
+ /**
80
+ * 节点所处的作用域
81
+ */
82
+ readonly scope: Scope;
83
+ /**
84
+ * 父节点
85
+ */
86
+ readonly parent: ASTNode | undefined;
87
+ /**
88
+ * 节点的版本号,每 fireChange 一次 version + 1
89
+ */
90
+ private _version;
91
+ /**
92
+ * 更新锁
93
+ */
94
+ changeLocked: boolean;
95
+ /**
96
+ * Batch Update 相关参数
97
+ */
98
+ private _batch;
99
+ /**
100
+ * AST 节点变化事件,基于 Rxjs 实现
101
+ * - 使用了 BehaviorSubject, 在订阅时会自动触发一次事件,事件为当前值
102
+ */
103
+ readonly value$: BehaviorSubject<ASTNode>;
104
+ /**
105
+ * 子节点
106
+ */
107
+ protected _children: Set<ASTNode<any, any>>;
108
+ /**
109
+ * 删除节点处理事件列表
110
+ */
111
+ readonly toDispose: DisposableCollection;
112
+ /**
113
+ * 销毁时触发的回调
114
+ */
115
+ onDispose: _flowgram_ai_utils.Event<void>;
116
+ /**
117
+ * 构造函数
118
+ * @param createParams 创建 ASTNode 的必要参数
119
+ * @param injectOptions 依赖注入各种模块
120
+ */
121
+ constructor({ key, parent, scope }: CreateASTParams, opts?: InjectOpts);
122
+ /**
123
+ * AST 节点的类型
124
+ */
125
+ get kind(): string;
126
+ /**
127
+ * 解析 AST JSON 数据
128
+ * @param json AST JSON 数据
129
+ */
130
+ abstract fromJSON(json: JSON): void;
131
+ /**
132
+ * 获取当前节点所有子节点
133
+ */
134
+ get children(): ASTNode[];
135
+ /**
136
+ * 转化为 ASTNodeJSON
137
+ * @returns
138
+ */
139
+ toJSON(): ASTNodeJSON;
140
+ /**
141
+ * 创建子节点
142
+ * @param json 子节点的 AST JSON
143
+ * @returns
144
+ */
145
+ protected createChildNode<ChildNode extends ASTNode = ASTNode>(json: ASTNodeJSON): ChildNode;
146
+ /**
147
+ * 更新子节点,快速实现子节点更新消费逻辑
148
+ * @param keyInThis 当前对象上的指定 key
149
+ */
150
+ protected updateChildNodeByKey(keyInThis: keyof this, nextJSON?: ASTNodeJSON): void;
151
+ /**
152
+ * 批处理更新,批处理函数内所有的 fireChange 都合并成一个
153
+ * @param updater 批处理函数
154
+ * @returns
155
+ */
156
+ protected withBatchUpdate<ParamTypes extends any[], ReturnType>(updater: (...args: ParamTypes) => ReturnType): (...args: ParamTypes) => ReturnType;
157
+ /**
158
+ * 触发当前节点更新
159
+ */
160
+ fireChange(): void;
161
+ /**
162
+ * 节点的版本值
163
+ * - 通过 NodeA === NodeB && versionA === versionB 可以比较两者是否相等
164
+ */
165
+ get version(): number;
166
+ /**
167
+ * 节点唯一 hash 值
168
+ */
169
+ get hash(): string;
170
+ /**
171
+ * 监听 AST 节点的变化
172
+ * @param observer 监听回调
173
+ * @param selector 监听指定数据
174
+ * @returns
175
+ */
176
+ subscribe<Data = this>(observer: ObserverOrNext<Data>, { selector, debounceAnimation, triggerOnInit }?: SubscribeConfig<this, Data>): Disposable;
177
+ dispatchGlobalEvent<ActionType extends GlobalEventActionType = GlobalEventActionType>(event: Omit<ActionType, 'ast'>): void;
178
+ /**
179
+ * 销毁
180
+ */
181
+ dispose(): void;
182
+ get disposed(): boolean;
183
+ /**
184
+ * 节点扩展信息
185
+ */
186
+ [key: string]: unknown;
187
+ }
188
+
189
+ /**
190
+ * 声明类 AST 节点
191
+ */
192
+ type VariableDeclarationJSON<VariableMeta = any> = BaseVariableFieldJSON<VariableMeta> & {
193
+ order?: number;
194
+ };
195
+ declare class VariableDeclaration<VariableMeta = any> extends BaseVariableField<VariableMeta> {
196
+ static kind: string;
197
+ protected _order: number;
198
+ get order(): number;
199
+ constructor(params: CreateASTParams);
200
+ /**
201
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
202
+ */
203
+ fromJSON({ order, ...rest }: VariableDeclarationJSON<VariableMeta>): void;
204
+ updateOrder(order?: number): void;
205
+ onTypeChange(observer: (type: ASTNode | undefined) => void): Disposable;
206
+ }
207
+
208
+ interface VariableDeclarationListJSON<VariableMeta = any> {
209
+ /**
210
+ * declarations 一定是 VariableDeclaration 类型,因此业务可以不用填 kind
211
+ */
212
+ declarations?: VariableDeclarationJSON<VariableMeta>[];
213
+ startOrder?: number;
214
+ }
215
+ type VariableDeclarationListChangeAction = GlobalEventActionType<'VariableListChange', {
216
+ prev: VariableDeclaration[];
217
+ next: VariableDeclaration[];
218
+ }, VariableDeclarationList>;
219
+ declare class VariableDeclarationList extends ASTNode<VariableDeclarationListJSON> {
220
+ static kind: string;
221
+ declarationTable: Map<string, VariableDeclaration>;
222
+ declarations: VariableDeclaration[];
223
+ fromJSON({ declarations, startOrder }: VariableDeclarationListJSON): void;
224
+ toJSON(): ASTNodeJSON;
225
+ }
226
+
227
+ type PropertyJSON<VariableMeta = any> = BaseVariableFieldJSON<VariableMeta> & {
228
+ key: string;
229
+ };
230
+ declare class Property<VariableMeta = any> extends BaseVariableField<VariableMeta> {
231
+ static kind: string;
232
+ }
233
+
234
+ declare abstract class BaseType<JSON extends ASTNodeJSON = any, InjectOpts = any> extends ASTNode<JSON, InjectOpts> {
235
+ flags: number;
236
+ /**
237
+ * 类型是否一致,节点有额外信息判断,请参考 extraTypeInfoEqual
238
+ * @param targetTypeJSON
239
+ */
240
+ isTypeEqual(targetTypeJSONOrKind?: ASTNodeJSONOrKind): boolean;
241
+ /**
242
+ * 可下钻类型需实现
243
+ * @param keyPath
244
+ */
245
+ getByKeyPath(keyPath?: string[]): BaseVariableField | undefined;
246
+ toJSON(): ASTNodeJSON;
247
+ }
248
+
249
+ declare class StringType extends BaseType {
250
+ flags: ASTNodeFlags;
251
+ static kind: string;
252
+ fromJSON(): void;
253
+ }
254
+
255
+ declare class IntegerType extends BaseType {
256
+ flags: ASTNodeFlags;
257
+ static kind: string;
258
+ fromJSON(): void;
259
+ }
260
+
261
+ declare class BooleanType extends BaseType {
262
+ static kind: string;
263
+ fromJSON(): void;
264
+ }
265
+
266
+ declare class NumberType extends BaseType {
267
+ static kind: string;
268
+ fromJSON(): void;
269
+ }
270
+
271
+ interface ArrayJSON {
272
+ items?: ASTNodeJSONOrKind;
273
+ }
274
+ declare class ArrayType extends BaseType<ArrayJSON> {
275
+ flags: ASTNodeFlags;
276
+ static kind: string;
277
+ items: BaseType;
278
+ fromJSON({ items }: ArrayJSON): void;
279
+ get canDrilldownItems(): boolean;
280
+ getByKeyPath(keyPath: string[]): BaseVariableField | undefined;
281
+ isTypeEqual(targetTypeJSONOrKind?: ASTNodeJSONOrKind): boolean;
282
+ /**
283
+ * Array 强比较
284
+ * @param targetTypeJSON
285
+ * @returns
286
+ */
287
+ protected customStrongEqual(targetTypeJSON: ASTNodeJSON): boolean;
288
+ toJSON(): ASTNodeJSON;
289
+ }
290
+
291
+ interface MapJSON {
292
+ keyType?: ASTNodeJSONOrKind;
293
+ valueType?: ASTNodeJSONOrKind;
294
+ }
295
+ declare class MapType extends BaseType<MapJSON> {
296
+ static kind: string;
297
+ keyType: BaseType;
298
+ valueType: BaseType;
299
+ fromJSON({ keyType, valueType }: MapJSON): void;
300
+ isTypeEqual(targetTypeJSONOrKind?: ASTNodeJSONOrKind): boolean;
301
+ /**
302
+ * Map 强比较
303
+ * @param targetTypeJSON
304
+ * @returns
305
+ */
306
+ protected customStrongEqual(targetTypeJSON: ASTNodeJSON): boolean;
307
+ toJSON(): ASTNodeJSON;
308
+ }
309
+
310
+ interface ObjectJSON<VariableMeta = any> {
311
+ /**
312
+ * Object 的 properties 一定是 Property 类型,因此业务可以不用填 kind
313
+ */
314
+ properties?: PropertyJSON<VariableMeta>[];
315
+ }
316
+ type ObjectPropertiesChangeAction = GlobalEventActionType<'ObjectPropertiesChange', {
317
+ prev: Property[];
318
+ next: Property[];
319
+ }, ObjectType>;
320
+ declare class ObjectType extends BaseType<ObjectJSON> {
321
+ flags: ASTNodeFlags;
322
+ static kind: string;
323
+ propertyTable: Map<string, Property>;
324
+ properties: Property[];
325
+ fromJSON({ properties }: ObjectJSON): void;
326
+ toJSON(): ASTNodeJSON;
327
+ /**
328
+ * 根据 KeyPath 找到对应的变量
329
+ * @param keyPath 变量路径
330
+ * @returns
331
+ */
332
+ getByKeyPath(keyPath: string[]): Property | undefined;
333
+ isTypeEqual(targetTypeJSONOrKind?: ASTNodeJSONOrKind): boolean;
334
+ /**
335
+ * Object 类型强比较
336
+ * @param targetTypeJSON
337
+ * @returns
338
+ */
339
+ protected customStrongEqual(targetTypeJSON: ASTNodeJSON): boolean;
340
+ }
341
+
342
+ interface UnionJSON {
343
+ types?: ASTNodeJSONOrKind[];
344
+ }
345
+
346
+ type ExpressionRefs = (BaseVariableField | undefined)[];
347
+ declare abstract class BaseExpression<JSON extends ASTNodeJSON = any, InjectOpts = any> extends ASTNode<JSON, InjectOpts> {
348
+ flags: ASTNodeFlags;
349
+ /**
350
+ * 获取全局变量表,方便表达式获取引用变量
351
+ */
352
+ get globalVariableTable(): VariableTable;
353
+ /**
354
+ * 父变量字段,通过由近而远的方式进行排序
355
+ */
356
+ get parentFields(): BaseVariableField[];
357
+ /**
358
+ * 获取表达式引用的变量字段
359
+ * - 通常是 变量 VariableDeclaration,或者 属性 Property 节点
360
+ */
361
+ abstract getRefFields(): ExpressionRefs;
362
+ /**
363
+ * 表达式返回的数据类型
364
+ */
365
+ abstract returnType: BaseType | undefined;
366
+ /**
367
+ * 引用变量
368
+ */
369
+ protected _refs: ExpressionRefs;
370
+ get refs(): ExpressionRefs;
371
+ protected refreshRefs$: Subject<void>;
372
+ /**
373
+ * 刷新变量引用
374
+ */
375
+ refreshRefs(): void;
376
+ /**
377
+ * 监听引用变量变化
378
+ * 监听 [a.b.c] -> [a.b]
379
+ */
380
+ refs$: Observable<ExpressionRefs>;
381
+ constructor(params: CreateASTParams, opts?: InjectOpts);
382
+ }
383
+
384
+ interface ExpressionListJSON {
385
+ expressions: ASTNodeJSON[];
386
+ }
387
+ declare class ExpressionList extends ASTNode<ExpressionListJSON> {
388
+ static kind: string;
389
+ expressions: ASTNode[];
390
+ fromJSON({ expressions }: ExpressionListJSON): void;
391
+ toJSON(): ASTNodeJSON;
392
+ }
393
+
394
+ interface KeyPathExpressionJSON$1 {
395
+ keyPath: string[];
396
+ }
397
+ declare class KeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPathExpressionJSON$1> extends BaseExpression<CustomPathJSON> {
398
+ static kind: string;
399
+ protected _keyPath: string[];
400
+ get keyPath(): string[];
401
+ getRefFields(): BaseVariableField[];
402
+ get returnType(): BaseType | undefined;
403
+ /**
404
+ * 业务重改该方法可快速定制自己的 Path 表达式
405
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
406
+ * @param json 业务定义的 Path 表达式
407
+ * @returns
408
+ */
409
+ protected parseToKeyPath(json: CustomPathJSON): string[];
410
+ fromJSON(json: CustomPathJSON): void;
411
+ constructor(params: CreateASTParams, opts: any);
412
+ toJSON(): ASTNodeJSON;
413
+ }
414
+
415
+ interface EnumerateExpressionJSON {
416
+ enumerateFor: ASTNodeJSON;
417
+ }
418
+ /**
419
+ * 遍历表达式,对列表进行遍历,获取遍历后的变量类型
420
+ */
421
+ declare class EnumerateExpression extends BaseExpression<EnumerateExpressionJSON> {
422
+ static kind: string;
423
+ protected _enumerateFor: BaseExpression | undefined;
424
+ get enumerateFor(): BaseExpression<any, any> | undefined;
425
+ get returnType(): BaseType | undefined;
426
+ getRefFields(): [];
427
+ fromJSON({ enumerateFor: expression }: EnumerateExpressionJSON): void;
428
+ toJSON(): ASTNodeJSON;
429
+ }
430
+
431
+ interface KeyPathExpressionJSON {
432
+ keyPath: string[];
433
+ }
434
+ /**
435
+ * 新版 KeyPathExpressionV2,相比旧版:
436
+ * - returnType 拷贝新一份,避免引用问题
437
+ * - 引入成环检测
438
+ */
439
+ declare class KeyPathExpressionV2<CustomPathJSON extends ASTNodeJSON = KeyPathExpressionJSON> extends BaseExpression<CustomPathJSON> {
440
+ static kind: string;
441
+ protected _keyPath: string[];
442
+ get keyPath(): string[];
443
+ getRefFields(): BaseVariableField[];
444
+ _returnType: BaseType;
445
+ get returnType(): BaseType<any, any>;
446
+ /**
447
+ * 业务重改该方法可快速定制自己的 Path 表达式
448
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
449
+ * @param json 业务定义的 Path 表达式
450
+ * @returns
451
+ */
452
+ protected parseToKeyPath(json: CustomPathJSON): string[];
453
+ fromJSON(json: CustomPathJSON): void;
454
+ getReturnTypeJSONByRef(_ref: BaseVariableField | undefined): ASTNodeJSON | undefined;
455
+ protected prevRefTypeHash: string | undefined;
456
+ constructor(params: CreateASTParams, opts: any);
457
+ toJSON(): ASTNodeJSON;
458
+ }
459
+
460
+ /**
461
+ * 声明类 AST 节点
462
+ */
463
+ type BaseVariableFieldJSON<VariableMeta = any> = {
464
+ key?: Identifier;
465
+ type?: ASTNodeJSONOrKind;
466
+ initializer?: ASTNodeJSON;
467
+ meta?: VariableMeta;
468
+ };
469
+ declare abstract class BaseVariableField<VariableMeta = any> extends ASTNode<BaseVariableFieldJSON<VariableMeta>> {
470
+ flags: ASTNodeFlags;
471
+ protected _type?: BaseType;
472
+ protected _meta: VariableMeta;
473
+ protected _initializer?: BaseExpression;
474
+ /**
475
+ * 父变量字段,通过由近而远的方式进行排序
476
+ */
477
+ get parentFields(): BaseVariableField[];
478
+ get meta(): VariableMeta;
479
+ get type(): BaseType;
480
+ get initializer(): BaseExpression | undefined;
481
+ /**
482
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
483
+ */
484
+ fromJSON({ type, initializer, meta }: BaseVariableFieldJSON<VariableMeta>): void;
485
+ updateType(type: BaseVariableFieldJSON['type']): void;
486
+ updateInitializer(nextInitializer?: BaseVariableFieldJSON['initializer']): void;
487
+ updateMeta(nextMeta: VariableMeta): void;
488
+ /**
489
+ * 根据 keyPath 去找下钻的变量字段
490
+ * @param keyPath
491
+ * @returns
492
+ */
493
+ getByKeyPath(keyPath: string[]): BaseVariableField | undefined;
494
+ /**
495
+ * 监听类型变化
496
+ * @param observer
497
+ * @returns
498
+ */
499
+ onTypeChange(observer: (type: ASTNode | undefined) => void): _flowgram_ai_utils.Disposable;
500
+ /**
501
+ * 转换为 JSON
502
+ * @returns
503
+ */
504
+ toJSON(): BaseVariableFieldJSON<VariableMeta> & {
505
+ kind: string;
506
+ };
507
+ }
508
+
509
+ declare class VariableTable implements Disposable {
510
+ parentTable?: VariableTable | undefined;
511
+ protected table: Map<string, VariableDeclaration>;
512
+ protected onDataChangeEmitter: Emitter<void>;
513
+ protected variables$: Subject<VariableDeclaration[]>;
514
+ protected anyVariableChange$: Observable<VariableDeclaration>;
515
+ /**
516
+ * 监听任意变量变化
517
+ * @param observer 监听器,变量变化时会吐出值
518
+ * @returns
519
+ */
520
+ onAnyVariableChange(observer: (changedVariable: VariableDeclaration) => void): Disposable;
521
+ /**
522
+ * 列表或者任意变量变化
523
+ * @param observer
524
+ */
525
+ onAnyChange(observer: () => void): DisposableCollection;
526
+ onDataChange: _flowgram_ai_utils.Event<void>;
527
+ protected _version: number;
528
+ fireChange(): void;
529
+ get version(): number;
530
+ constructor(parentTable?: VariableTable | undefined);
531
+ get variables(): VariableDeclaration[];
532
+ get variableKeys(): string[];
533
+ /**
534
+ * 根据 keyPath 找到对应的变量,或 Property 节点
535
+ * @param keyPath
536
+ * @returns
537
+ */
538
+ getByKeyPath(keyPath: string[]): BaseVariableField | undefined;
539
+ /**
540
+ * 根据 key 值找到相应的变量
541
+ * @param key
542
+ * @returns
543
+ */
544
+ getVariableByKey(key: string): VariableDeclaration<any> | undefined;
545
+ /**
546
+ * 往 variableTable 添加输出变量
547
+ * @param variable
548
+ */
549
+ addVariableToTable(variable: VariableDeclaration): void;
550
+ /**
551
+ * 从 variableTable 中移除变量
552
+ * @param key
553
+ */
554
+ removeVariableFromTable(key: string): void;
555
+ dispose(): void;
556
+ }
557
+
558
+ /**
559
+ * 作用域输出
560
+ */
561
+ declare class ScopeOutputData {
562
+ readonly scope: Scope;
563
+ protected variableTable: VariableTable;
564
+ protected memo: {
565
+ <T>(key: string | symbol, fn: () => T): T;
566
+ clear: (key?: (string | symbol) | undefined) => void;
567
+ };
568
+ get variableEngine(): VariableEngine;
569
+ get globalVariableTable(): VariableTable;
570
+ get onDataChange(): _flowgram_ai_utils.Event<void>;
571
+ get onAnyVariableChange(): (observer: (changedVariable: VariableDeclaration<any>) => void) => _flowgram_ai_utils.Disposable;
572
+ protected _hasChanges: boolean;
573
+ constructor(scope: Scope);
574
+ /**
575
+ * 作用域输出变量
576
+ */
577
+ get variables(): VariableDeclaration[];
578
+ /**
579
+ * 输出的变量 keys
580
+ */
581
+ get variableKeys(): string[];
582
+ addVariableToTable(variable: VariableDeclaration): void;
583
+ setHasChanges(): void;
584
+ removeVariableFromTable(key: string): void;
585
+ getVariableByKey(key: string): VariableDeclaration<any> | undefined;
586
+ notifyCoversChange(): void;
587
+ }
588
+
589
+ /**
590
+ * 作用域可用变量
591
+ */
592
+ declare class ScopeAvailableData {
593
+ readonly scope: Scope;
594
+ protected memo: {
595
+ <T>(key: string | symbol, fn: () => T): T;
596
+ clear: (key?: (string | symbol) | undefined) => void;
597
+ };
598
+ get globalVariableTable(): VariableTable;
599
+ protected refresh$: Subject<void>;
600
+ protected _variables: VariableDeclaration[];
601
+ refresh(): void;
602
+ /**
603
+ * 监听
604
+ */
605
+ protected variables$: Observable<VariableDeclaration[]>;
606
+ protected anyVariableChange$: Observable<VariableDeclaration>;
607
+ /**
608
+ * 监听任意变量变化
609
+ * @param observer 监听器,变量变化时会吐出值
610
+ * @returns
611
+ */
612
+ onAnyVariableChange(observer: (changedVariable: VariableDeclaration) => void): Disposable;
613
+ /**
614
+ * 监听变量列表变化
615
+ * @param observer
616
+ * @returns
617
+ */
618
+ onVariableListChange(observer: (variables: VariableDeclaration[]) => void): Disposable;
619
+ protected onDataChangeEmitter: Emitter<VariableDeclaration<any>[]>;
620
+ /**
621
+ * 监听变量列表变化 + 任意子变量变化
622
+ */
623
+ onDataChange: _flowgram_ai_utils.Event<VariableDeclaration<any>[]>;
624
+ constructor(scope: Scope);
625
+ /**
626
+ * 获取可消费变量
627
+ */
628
+ get variables(): VariableDeclaration[];
629
+ /**
630
+ * 获取可访问的变量 keys
631
+ */
632
+ get variableKeys(): string[];
633
+ /**
634
+ * 返回依赖的作用域
635
+ */
636
+ get depScopes(): Scope[];
637
+ /**
638
+ * 通过 keyPath 找到可用变量
639
+ * @param keyPath
640
+ * @returns
641
+ */
642
+ getByKeyPath(keyPath?: string[]): VariableDeclaration | Property | undefined;
643
+ }
644
+
645
+ type Observer<ActionType extends GlobalEventActionType = GlobalEventActionType> = (action: ActionType) => void;
646
+ declare class ScopeEventData {
647
+ readonly scope: Scope;
648
+ event$: Subject<GlobalEventActionType>;
649
+ dispatch<ActionType extends GlobalEventActionType = GlobalEventActionType>(action: ActionType): void;
650
+ subscribe<ActionType extends GlobalEventActionType = GlobalEventActionType>(observer: Observer<ActionType>): Disposable;
651
+ on<ActionType extends GlobalEventActionType = GlobalEventActionType>(type: ActionType['type'], observer: Observer<ActionType>): Disposable;
652
+ constructor(scope: Scope);
653
+ }
654
+
655
+ type ASTKindType = string;
656
+ type Identifier = string;
657
+ interface ASTNodeJSON {
658
+ kind?: ASTKindType;
659
+ key?: Identifier;
660
+ [key: string]: any;
661
+ }
662
+ /**
663
+ * 核心 AST 节点类型
664
+ */
665
+ declare enum ASTKind {
666
+ /**
667
+ * 类型相关
668
+ * - 内部默认实现一套基于 JSON 类型的类型 AST 节点
669
+ */
670
+ String = "String",
671
+ Number = "Number",
672
+ Integer = "Integer",
673
+ Boolean = "Boolean",
674
+ Object = "Object",
675
+ Array = "Array",
676
+ Map = "Map",
677
+ Union = "Union",
678
+ Any = "Any",
679
+ /**
680
+ * 声明
681
+ */
682
+ Property = "Property",
683
+ VariableDeclaration = "VariableDeclaration",
684
+ VariableDeclarationList = "VariableDeclarationList",
685
+ /**
686
+ * 表达式
687
+ */
688
+ KeyPathExpression = "KeyPathExpression",
689
+ EnumerateExpression = "EnumerateExpression",
690
+ ExpressionList = "ExpressionList",
691
+ /**
692
+ * 通用 AST 节点
693
+ */
694
+ ListNode = "ListNode",
695
+ DataNode = "DataNode",
696
+ MapNode = "MapNode"
697
+ }
698
+ interface CreateASTParams {
699
+ scope: Scope;
700
+ key?: Identifier;
701
+ parent?: ASTNode;
702
+ }
703
+ type ASTNodeJSONOrKind = string | ASTNodeJSON;
704
+ type ObserverOrNext<T> = Partial<Observer$1<T>> | ((value: T) => void);
705
+ interface SubscribeConfig<This, Data> {
706
+ debounceAnimation?: boolean;
707
+ triggerOnInit?: boolean;
708
+ selector?: (curr: This) => Data;
709
+ }
710
+ type GetKindJSON<KindType extends string, JSON extends ASTNodeJSON> = {
711
+ kind: KindType;
712
+ key?: Identifier;
713
+ } & JSON;
714
+ type GetKindJSONOrKind<KindType extends string, JSON extends ASTNodeJSON> = ({
715
+ kind: KindType;
716
+ key?: Identifier;
717
+ } & JSON) | KindType;
718
+ interface GlobalEventActionType<Type = string, Payload = any, AST extends ASTNode = ASTNode> {
719
+ type: Type;
720
+ payload?: Payload;
721
+ ast?: AST;
722
+ }
723
+
724
+ type DataInjector = () => Record<string, any>;
725
+ declare class ASTRegisters {
726
+ protected injectors: Map<ASTKindType, DataInjector>;
727
+ protected astMap: Map<ASTKindType, ASTNodeRegistry>;
728
+ /**
729
+ * 核心 AST 节点注册
730
+ */
731
+ constructor();
732
+ /**
733
+ * 创建 AST 节点
734
+ * @param param 创建参数
735
+ * @returns
736
+ */
737
+ createAST<ReturnNode extends ASTNode = ASTNode>(json: ASTNodeJSON, { parent, scope }: CreateASTParams): ReturnNode;
738
+ /**
739
+ * 根据 AST 节点类型获取节点 Registry
740
+ * @param kind
741
+ * @returns
742
+ */
743
+ getASTRegistryByKind(kind: ASTKindType): ASTNodeRegistry<any, any> | undefined;
744
+ /**
745
+ * 注册 AST 节点
746
+ * @param ASTNode
747
+ * @param injector
748
+ */
749
+ registerAST(ASTNode: ASTNodeRegistry, injector?: DataInjector): void;
750
+ }
751
+
752
+ /**
753
+ * 通用数据 AST 节点,无子节点
754
+ */
755
+ declare class DataNode<Data = any> extends ASTNode {
756
+ static kind: string;
757
+ protected _data: Data;
758
+ get data(): Data;
759
+ fromJSON(json: Data): void;
760
+ toJSON(): {
761
+ kind: ASTKind;
762
+ } & Data;
763
+ partialUpdate(nextData: Data): void;
764
+ }
765
+
766
+ interface ListNodeJSON {
767
+ list: ASTNodeJSON[];
768
+ }
769
+ declare class ListNode extends ASTNode<ListNodeJSON> {
770
+ static kind: string;
771
+ protected _list: ASTNode[];
772
+ get list(): ASTNode[];
773
+ fromJSON({ list }: ListNodeJSON): void;
774
+ toJSON(): ASTNodeJSON;
775
+ }
776
+
777
+ interface MapNodeJSON {
778
+ map: [string, ASTNodeJSON][];
779
+ }
780
+ declare class MapNode extends ASTNode<MapNodeJSON> {
781
+ static kind: string;
782
+ protected map: Map<string, ASTNode>;
783
+ fromJSON({ map }: MapNodeJSON): void;
784
+ toJSON(): ASTNodeJSON;
785
+ /**
786
+ * 往 Map 中设置 ASTNode
787
+ * @param key ASTNode 的索引,
788
+ * @param json
789
+ */
790
+ set<Node extends ASTNode = ASTNode>(key: string, nextJSON: ASTNodeJSON): Node;
791
+ /**
792
+ * 移除指定 ASTNode
793
+ * @param key
794
+ */
795
+ remove(key: string): void;
796
+ /**
797
+ * 获取 ASTNode
798
+ * @param key
799
+ * @returns
800
+ */
801
+ get(key: string): ASTNode | undefined;
802
+ }
803
+
804
+ declare namespace ASTFactory {
805
+ /**
806
+ * 类型相关
807
+ * @returns
808
+ */
809
+ const createString: () => {
810
+ kind: ASTKind;
811
+ };
812
+ const createNumber: () => {
813
+ kind: ASTKind;
814
+ };
815
+ const createBoolean: () => {
816
+ kind: ASTKind;
817
+ };
818
+ const createInteger: () => {
819
+ kind: ASTKind;
820
+ };
821
+ const createObject: (json: ObjectJSON) => {
822
+ properties?: PropertyJSON<any>[] | undefined;
823
+ kind: ASTKind;
824
+ };
825
+ const createArray: (json: ArrayJSON) => {
826
+ items?: ASTNodeJSONOrKind | undefined;
827
+ kind: ASTKind;
828
+ };
829
+ const createMap: (json: MapJSON) => {
830
+ keyType?: ASTNodeJSONOrKind | undefined;
831
+ valueType?: ASTNodeJSONOrKind | undefined;
832
+ kind: ASTKind;
833
+ };
834
+ const createUnion: (json: UnionJSON) => {
835
+ types?: ASTNodeJSONOrKind[] | undefined;
836
+ kind: ASTKind;
837
+ };
838
+ /**
839
+ * 声明相关
840
+ */
841
+ const createVariableDeclaration: <VariableMeta = any>(json: VariableDeclarationJSON<VariableMeta>) => {
842
+ key?: string | undefined;
843
+ type?: ASTNodeJSONOrKind | undefined;
844
+ initializer?: ASTNodeJSON | undefined;
845
+ meta?: VariableMeta | undefined;
846
+ order?: number | undefined;
847
+ kind: ASTKind;
848
+ };
849
+ const createProperty: <VariableMeta = any>(json: PropertyJSON<VariableMeta>) => {
850
+ key: string;
851
+ type?: ASTNodeJSONOrKind | undefined;
852
+ initializer?: ASTNodeJSON | undefined;
853
+ meta?: VariableMeta | undefined;
854
+ kind: ASTKind;
855
+ };
856
+ const createVariableDeclarationList: (json: VariableDeclarationListJSON) => {
857
+ declarations?: VariableDeclarationJSON<any>[] | undefined;
858
+ startOrder?: number | undefined;
859
+ kind: ASTKind;
860
+ };
861
+ /**
862
+ * 表达式相关
863
+ */
864
+ const createEnumerateExpression: (json: EnumerateExpressionJSON) => {
865
+ enumerateFor: ASTNodeJSON;
866
+ kind: ASTKind;
867
+ };
868
+ const createKeyPathExpression: (json: KeyPathExpressionJSON$1) => {
869
+ keyPath: string[];
870
+ kind: ASTKind;
871
+ };
872
+ }
873
+
874
+ declare const injectToAST: (serviceIdentifier: interfaces.ServiceIdentifier) => (target: any, propertyKey: string) => any;
875
+ declare const postConstructAST: () => (target: any, propertyKey: string) => void;
876
+
877
+ declare class Scope<ScopeMeta extends Record<string, any> = Record<string, any>> {
878
+ /**
879
+ * Scope 唯一索引
880
+ */
881
+ readonly id: string;
882
+ /**
883
+ * Scope 依赖变量引擎
884
+ */
885
+ readonly variableEngine: VariableEngine;
886
+ /**
887
+ * 作用域的基本元信息,包括作用域所在节点及一些 flag 信息,上层业务可以额外扩展
888
+ */
889
+ readonly meta: ScopeMeta;
890
+ /**
891
+ * 作用域 AST 根节点
892
+ * - Map<formItemKey, formItemValue>
893
+ */
894
+ readonly ast: MapNode;
895
+ /**
896
+ * 可用变量数据管理
897
+ */
898
+ readonly available: ScopeAvailableData;
899
+ /**
900
+ * 输出变量数据管理
901
+ */
902
+ readonly output: ScopeOutputData;
903
+ /**
904
+ * 作用域事件管理
905
+ */
906
+ readonly event: ScopeEventData;
907
+ /**
908
+ * 数据缓存
909
+ */
910
+ protected memo: {
911
+ <T>(key: string | symbol, fn: () => T): T;
912
+ clear: (key?: (string | symbol) | undefined) => void;
913
+ };
914
+ toDispose: DisposableCollection;
915
+ constructor(options: {
916
+ id: string;
917
+ variableEngine: VariableEngine;
918
+ meta?: ScopeMeta;
919
+ });
920
+ refreshCovers(): void;
921
+ refreshDeps(): void;
922
+ get depScopes(): Scope[];
923
+ get coverScopes(): Scope[];
924
+ dispose(): void;
925
+ onDispose: _flowgram_ai_utils.Event<void>;
926
+ get disposed(): boolean;
927
+ }
928
+
929
+ interface ScopeChangeAction {
930
+ type: 'add' | 'delete' | 'update' | 'available';
931
+ scope: Scope;
932
+ }
933
+
934
+ declare class VariableEngine implements Disposable {
935
+ readonly chain: ScopeChain;
936
+ readonly astRegisters: ASTRegisters;
937
+ protected toDispose: DisposableCollection;
938
+ protected memo: {
939
+ <T>(key: string | symbol, fn: () => T): T;
940
+ clear: (key?: (string | symbol) | undefined) => void;
941
+ };
942
+ protected scopeMap: Map<string, Scope<Record<string, any>>>;
943
+ globalEvent$: Subject<GlobalEventActionType>;
944
+ protected onScopeChangeEmitter: Emitter<ScopeChangeAction>;
945
+ globalVariableTable: VariableTable;
946
+ onScopeChange: _flowgram_ai_utils.Event<ScopeChangeAction>;
947
+ private readonly containerProvider;
948
+ get container(): interfaces.Container;
949
+ constructor(chain: ScopeChain, // 作用域依赖关系偏序集
950
+ astRegisters: ASTRegisters);
951
+ dispose(): void;
952
+ getScopeById(scopeId: string): Scope | undefined;
953
+ removeScopeById(scopeId: string): void;
954
+ createScope(id: string, meta?: Record<string, any>): Scope;
955
+ getAllScopes({ sort, }?: {
956
+ sort?: boolean;
957
+ }): Scope[];
958
+ fireGlobalEvent(event: GlobalEventActionType): void;
959
+ onGlobalEvent<ActionType extends GlobalEventActionType = GlobalEventActionType>(type: ActionType['type'], observer: (action: ActionType) => void): Disposable;
960
+ }
961
+
962
+ interface ScopeContextProps {
963
+ scope: Scope;
964
+ }
965
+ declare const ScopeProvider: react.Provider<ScopeContextProps>;
966
+ declare const useScopeContext: () => ScopeContextProps | null;
967
+ declare const useCurrentScope: () => Scope<Record<string, any>>;
968
+
969
+ /**
970
+ * 获取作用域的可访问变量
971
+ */
972
+ declare function useScopeAvailable(): ScopeAvailableData;
973
+
974
+ /**
975
+ * 获取作用域的可访问变量
976
+ */
977
+ declare function useAvailableVariables(): VariableDeclaration[];
978
+
979
+ interface RenameInfo {
980
+ before: BaseVariableField;
981
+ after: BaseVariableField;
982
+ }
983
+ declare class VariableFieldKeyRenameService {
984
+ variableEngine: VariableEngine;
985
+ toDispose: DisposableCollection;
986
+ renameEmitter: Emitter<RenameInfo>;
987
+ disposeInListEmitter: Emitter<BaseVariableField<any>>;
988
+ onRename: _flowgram_ai_utils.Event<RenameInfo>;
989
+ onDisposeInList: _flowgram_ai_utils.Event<BaseVariableField<any>>;
990
+ handleFieldListChange(ast?: ASTNode, prev?: BaseVariableField[], next?: BaseVariableField[]): void;
991
+ notifyFieldsDispose(prev?: BaseVariableField[], next?: BaseVariableField[]): void;
992
+ init(): void;
993
+ dispose(): void;
994
+ }
995
+
996
+ export { ASTFactory, ASTKind, ASTNode, ASTNodeFlags, type ASTNodeJSON, type ASTNodeRegistry, ASTRegisters, ArrayType, BaseExpression, BaseType, BaseVariableField, BooleanType, type CreateASTParams, DataNode, EnumerateExpression, type EnumerateExpressionJSON, ExpressionList, type ExpressionListJSON, type GetKindJSON, type GetKindJSONOrKind, type GlobalEventActionType, IntegerType, KeyPathExpression, type KeyPathExpressionJSON$1 as KeyPathExpressionJSON, KeyPathExpressionV2, ListNode, type ListNodeJSON, MapNode, type MapNodeJSON, MapType, NumberType, type ObjectJSON, type ObjectPropertiesChangeAction, ObjectType, Property, type PropertyJSON, Scope, ScopeChain, ScopeOutputData, ScopeProvider, StringType, type UnionJSON, VariableContainerModule, VariableDeclaration, type VariableDeclarationJSON, VariableDeclarationList, type VariableDeclarationListChangeAction, type VariableDeclarationListJSON, VariableEngine, VariableEngineProvider, VariableFieldKeyRenameService, VariableTable, injectToAST, postConstructAST, useAvailableVariables, useCurrentScope, useScopeAvailable, useScopeContext };