@flowgram.ai/variable-core 0.1.0-alpha.2

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,2032 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result) __defProp(target, key, result);
9
+ return result;
10
+ };
11
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
12
+
13
+ // src/variable-container-module.ts
14
+ import { ContainerModule } from "inversify";
15
+
16
+ // src/variable-engine.ts
17
+ import { Subject as Subject5 } from "rxjs";
18
+ import { inject as inject2, injectable as injectable3, preDestroy } from "inversify";
19
+ import { Disposable as Disposable6, DisposableCollection as DisposableCollection5 } from "@flowgram.ai/utils";
20
+ import { Emitter as Emitter3 } from "@flowgram.ai/utils";
21
+
22
+ // src/utils/toDisposable.tsx
23
+ import { Disposable } from "@flowgram.ai/utils";
24
+ function subsToDisposable(subscription) {
25
+ return Disposable.create(() => subscription.unsubscribe());
26
+ }
27
+
28
+ // src/utils/memo.ts
29
+ var createMemo = () => {
30
+ const _memoCache = /* @__PURE__ */ new Map();
31
+ const memo = (key, fn) => {
32
+ if (_memoCache.has(key)) {
33
+ return _memoCache.get(key);
34
+ }
35
+ const data = fn();
36
+ _memoCache.set(key, data);
37
+ return data;
38
+ };
39
+ const clear = (key) => {
40
+ if (key) {
41
+ _memoCache.delete(key);
42
+ } else {
43
+ _memoCache.clear();
44
+ }
45
+ };
46
+ memo.clear = clear;
47
+ return memo;
48
+ };
49
+
50
+ // src/scope/scope-chain.ts
51
+ import { inject, injectable } from "inversify";
52
+ import { DisposableCollection } from "@flowgram.ai/utils";
53
+
54
+ // src/providers.ts
55
+ var VariableEngineProvider = Symbol("DynamicVariableEngine");
56
+ var ContainerProvider = Symbol("ContainerProvider");
57
+
58
+ // src/scope/scope-chain.ts
59
+ var ScopeChain = class {
60
+ constructor() {
61
+ this.toDispose = new DisposableCollection();
62
+ }
63
+ get variableEngine() {
64
+ return this.variableEngineProvider();
65
+ }
66
+ /**
67
+ * 所有作用域依赖关系刷新
68
+ */
69
+ refreshAllChange() {
70
+ this.variableEngine.getAllScopes().forEach((_scope) => {
71
+ _scope.refreshCovers();
72
+ _scope.refreshDeps();
73
+ });
74
+ }
75
+ dispose() {
76
+ this.toDispose.dispose();
77
+ }
78
+ get disposed() {
79
+ return this.toDispose.disposed;
80
+ }
81
+ get onDispose() {
82
+ return this.toDispose.onDispose;
83
+ }
84
+ };
85
+ __decorateClass([
86
+ inject(VariableEngineProvider)
87
+ ], ScopeChain.prototype, "variableEngineProvider", 2);
88
+ ScopeChain = __decorateClass([
89
+ injectable()
90
+ ], ScopeChain);
91
+
92
+ // src/scope/scope.ts
93
+ import { DisposableCollection as DisposableCollection4 } from "@flowgram.ai/utils";
94
+
95
+ // src/ast/types.ts
96
+ var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
97
+ ASTKind2["String"] = "String";
98
+ ASTKind2["Number"] = "Number";
99
+ ASTKind2["Integer"] = "Integer";
100
+ ASTKind2["Boolean"] = "Boolean";
101
+ ASTKind2["Object"] = "Object";
102
+ ASTKind2["Array"] = "Array";
103
+ ASTKind2["Map"] = "Map";
104
+ ASTKind2["Union"] = "Union";
105
+ ASTKind2["Any"] = "Any";
106
+ ASTKind2["Property"] = "Property";
107
+ ASTKind2["VariableDeclaration"] = "VariableDeclaration";
108
+ ASTKind2["VariableDeclarationList"] = "VariableDeclarationList";
109
+ ASTKind2["KeyPathExpression"] = "KeyPathExpression";
110
+ ASTKind2["EnumerateExpression"] = "EnumerateExpression";
111
+ ASTKind2["ExpressionList"] = "ExpressionList";
112
+ ASTKind2["ListNode"] = "ListNode";
113
+ ASTKind2["DataNode"] = "DataNode";
114
+ ASTKind2["MapNode"] = "MapNode";
115
+ return ASTKind2;
116
+ })(ASTKind || {});
117
+
118
+ // src/ast/ast-registers.ts
119
+ import { omit } from "lodash";
120
+ import { injectable as injectable2 } from "inversify";
121
+
122
+ // src/ast/utils/inversify.ts
123
+ var injectToAST = (serviceIdentifier) => function(target, propertyKey) {
124
+ if (!serviceIdentifier) {
125
+ throw new Error(
126
+ `ServiceIdentifier ${serviceIdentifier} in @lazyInject is Empty, it might be caused by file circular dependency, please check it.`
127
+ );
128
+ }
129
+ const descriptor = {
130
+ get() {
131
+ const container = this.scope.variableEngine.container;
132
+ return container.get(serviceIdentifier);
133
+ },
134
+ set() {
135
+ },
136
+ configurable: true,
137
+ enumerable: true
138
+ };
139
+ return descriptor;
140
+ };
141
+ var POST_CONSTRUCT_AST_SYMBOL = Symbol("post_construct_ast");
142
+ var postConstructAST = () => (target, propertyKey) => {
143
+ if (!Reflect.hasMetadata(POST_CONSTRUCT_AST_SYMBOL, target)) {
144
+ Reflect.defineMetadata(POST_CONSTRUCT_AST_SYMBOL, propertyKey, target);
145
+ } else {
146
+ throw Error("Duplication Post Construct AST");
147
+ }
148
+ };
149
+
150
+ // src/ast/utils/helpers.ts
151
+ function updateChildNodeHelper({
152
+ getChildNode,
153
+ updateChildNode,
154
+ removeChildNode,
155
+ nextJSON
156
+ }) {
157
+ const currNode = getChildNode();
158
+ const isNewKind = currNode?.kind !== nextJSON?.kind;
159
+ const isNewKey = nextJSON?.key && nextJSON?.key !== currNode?.key;
160
+ if (isNewKind || isNewKey) {
161
+ if (currNode) {
162
+ currNode.dispose();
163
+ removeChildNode();
164
+ }
165
+ if (nextJSON) {
166
+ const newNode = this.createChildNode(nextJSON);
167
+ updateChildNode(newNode);
168
+ this.fireChange();
169
+ return newNode;
170
+ } else {
171
+ this.fireChange();
172
+ }
173
+ } else if (nextJSON) {
174
+ currNode?.fromJSON(nextJSON);
175
+ }
176
+ return currNode;
177
+ }
178
+ function parseTypeJsonOrKind(typeJSONOrKind) {
179
+ return typeof typeJSONOrKind === "string" ? { kind: typeJSONOrKind } : typeJSONOrKind;
180
+ }
181
+ function getAllChildren(ast) {
182
+ return [...ast.children, ...ast.children.map((_child) => getAllChildren(_child)).flat()];
183
+ }
184
+ function isMatchAST(node, targetType) {
185
+ return node?.kind === targetType?.kind;
186
+ }
187
+
188
+ // src/ast/flags.ts
189
+ var ASTNodeFlags = /* @__PURE__ */ ((ASTNodeFlags2) => {
190
+ ASTNodeFlags2[ASTNodeFlags2["None"] = 0] = "None";
191
+ ASTNodeFlags2[ASTNodeFlags2["VariableField"] = 1] = "VariableField";
192
+ ASTNodeFlags2[ASTNodeFlags2["Expression"] = 4] = "Expression";
193
+ ASTNodeFlags2[ASTNodeFlags2["BasicType"] = 8] = "BasicType";
194
+ ASTNodeFlags2[ASTNodeFlags2["DrilldownType"] = 16] = "DrilldownType";
195
+ ASTNodeFlags2[ASTNodeFlags2["EnumerateType"] = 32] = "EnumerateType";
196
+ ASTNodeFlags2[ASTNodeFlags2["UnionType"] = 64] = "UnionType";
197
+ ASTNodeFlags2[ASTNodeFlags2["VariableType"] = 120] = "VariableType";
198
+ return ASTNodeFlags2;
199
+ })(ASTNodeFlags || {});
200
+
201
+ // src/ast/ast-node.ts
202
+ import {
203
+ BehaviorSubject,
204
+ animationFrameScheduler,
205
+ debounceTime,
206
+ distinctUntilChanged,
207
+ map,
208
+ skip,
209
+ tap
210
+ } from "rxjs";
211
+ import { nanoid } from "nanoid";
212
+ import { shallowEqual } from "fast-equals";
213
+ import { Disposable as Disposable2, DisposableCollection as DisposableCollection2 } from "@flowgram.ai/utils";
214
+ var ASTNode = class _ASTNode {
215
+ /**
216
+ * 构造函数
217
+ * @param createParams 创建 ASTNode 的必要参数
218
+ * @param injectOptions 依赖注入各种模块
219
+ */
220
+ constructor({ key, parent, scope }, opts) {
221
+ /**
222
+ * 节点 Flags,记录一些 Flag 信息
223
+ */
224
+ this.flags = 0 /* None */;
225
+ /**
226
+ * 节点的版本号,每 fireChange 一次 version + 1
227
+ */
228
+ this._version = 0;
229
+ /**
230
+ * 更新锁
231
+ */
232
+ this.changeLocked = false;
233
+ /**
234
+ * Batch Update 相关参数
235
+ */
236
+ this._batch = {
237
+ batching: false,
238
+ hasChangesInBatch: false
239
+ };
240
+ /**
241
+ * AST 节点变化事件,基于 Rxjs 实现
242
+ * - 使用了 BehaviorSubject, 在订阅时会自动触发一次事件,事件为当前值
243
+ */
244
+ this.value$ = new BehaviorSubject(this);
245
+ /**
246
+ * 子节点
247
+ */
248
+ this._children = /* @__PURE__ */ new Set();
249
+ /**
250
+ * 删除节点处理事件列表
251
+ */
252
+ this.toDispose = new DisposableCollection2(
253
+ Disposable2.create(() => {
254
+ this.parent?.fireChange();
255
+ this.children.forEach((child) => child.dispose());
256
+ })
257
+ );
258
+ /**
259
+ * 销毁时触发的回调
260
+ */
261
+ this.onDispose = this.toDispose.onDispose;
262
+ this.scope = scope;
263
+ this.parent = parent;
264
+ this.opts = opts;
265
+ this.key = key || nanoid();
266
+ this.fromJSON = this.withBatchUpdate(this.fromJSON.bind(this));
267
+ this.dispatchGlobalEvent({ type: "NewAST" });
268
+ }
269
+ /**
270
+ * AST 节点的类型
271
+ */
272
+ get kind() {
273
+ if (!this.constructor.kind) {
274
+ throw new Error(`ASTNode Registry need a kind: ${this.constructor.name}`);
275
+ }
276
+ return this.constructor.kind;
277
+ }
278
+ /**
279
+ * 获取当前节点所有子节点
280
+ */
281
+ get children() {
282
+ return Array.from(this._children);
283
+ }
284
+ /**
285
+ * 转化为 ASTNodeJSON
286
+ * @returns
287
+ */
288
+ toJSON() {
289
+ console.warn("[VariableEngine] Please Implement toJSON method for " + this.kind);
290
+ return {
291
+ kind: this.kind
292
+ };
293
+ }
294
+ /**
295
+ * 创建子节点
296
+ * @param json 子节点的 AST JSON
297
+ * @returns
298
+ */
299
+ createChildNode(json) {
300
+ const astRegisters = this.scope.variableEngine.astRegisters;
301
+ const child = astRegisters.createAST(json, {
302
+ parent: this,
303
+ scope: this.scope
304
+ });
305
+ this._children.add(child);
306
+ child.toDispose.push(
307
+ Disposable2.create(() => {
308
+ this._children.delete(child);
309
+ })
310
+ );
311
+ return child;
312
+ }
313
+ /**
314
+ * 更新子节点,快速实现子节点更新消费逻辑
315
+ * @param keyInThis 当前对象上的指定 key
316
+ */
317
+ updateChildNodeByKey(keyInThis, nextJSON) {
318
+ this.withBatchUpdate(updateChildNodeHelper).call(this, {
319
+ getChildNode: () => this[keyInThis],
320
+ updateChildNode: (_node) => this[keyInThis] = _node,
321
+ removeChildNode: () => this[keyInThis] = void 0,
322
+ nextJSON
323
+ });
324
+ }
325
+ /**
326
+ * 批处理更新,批处理函数内所有的 fireChange 都合并成一个
327
+ * @param updater 批处理函数
328
+ * @returns
329
+ */
330
+ withBatchUpdate(updater) {
331
+ return (...args) => {
332
+ if (this._batch.batching) {
333
+ return updater.call(this, ...args);
334
+ }
335
+ this._batch.hasChangesInBatch = false;
336
+ this._batch.batching = true;
337
+ const res = updater.call(this, ...args);
338
+ this._batch.batching = false;
339
+ if (this._batch.hasChangesInBatch) {
340
+ this.fireChange();
341
+ }
342
+ this._batch.hasChangesInBatch = false;
343
+ return res;
344
+ };
345
+ }
346
+ /**
347
+ * 触发当前节点更新
348
+ */
349
+ fireChange() {
350
+ if (this.changeLocked || this.disposed) {
351
+ return;
352
+ }
353
+ if (this._batch.batching) {
354
+ this._batch.hasChangesInBatch = true;
355
+ return;
356
+ }
357
+ this._version++;
358
+ this.value$.next(this);
359
+ this.dispatchGlobalEvent({ type: "UpdateAST" });
360
+ this.parent?.fireChange();
361
+ }
362
+ /**
363
+ * 节点的版本值
364
+ * - 通过 NodeA === NodeB && versionA === versionB 可以比较两者是否相等
365
+ */
366
+ get version() {
367
+ return this._version;
368
+ }
369
+ /**
370
+ * 节点唯一 hash 值
371
+ */
372
+ get hash() {
373
+ return `${this._version}${this.kind}${this.key}`;
374
+ }
375
+ /**
376
+ * 监听 AST 节点的变化
377
+ * @param observer 监听回调
378
+ * @param selector 监听指定数据
379
+ * @returns
380
+ */
381
+ subscribe(observer, { selector, debounceAnimation, triggerOnInit } = {}) {
382
+ return subsToDisposable(
383
+ this.value$.pipe(
384
+ map(() => selector ? selector(this) : this),
385
+ distinctUntilChanged(
386
+ (a, b) => shallowEqual(a, b),
387
+ (value) => {
388
+ if (value instanceof _ASTNode) {
389
+ return value.hash;
390
+ }
391
+ return value;
392
+ }
393
+ ),
394
+ // 默认跳过 BehaviorSubject 第一次触发
395
+ triggerOnInit ? tap(() => null) : skip(1),
396
+ // 每个 animationFrame 内所有更新合并成一个
397
+ debounceAnimation ? debounceTime(0, animationFrameScheduler) : tap(() => null)
398
+ ).subscribe(observer)
399
+ );
400
+ }
401
+ dispatchGlobalEvent(event) {
402
+ this.scope.event.dispatch({
403
+ ...event,
404
+ ast: this
405
+ });
406
+ }
407
+ /**
408
+ * 销毁
409
+ */
410
+ dispose() {
411
+ if (this.toDispose.disposed) {
412
+ return;
413
+ }
414
+ this.toDispose.dispose();
415
+ this.dispatchGlobalEvent({ type: "DisposeAST" });
416
+ this.value$.complete();
417
+ this.value$.unsubscribe();
418
+ }
419
+ get disposed() {
420
+ return this.toDispose.disposed;
421
+ }
422
+ };
423
+
424
+ // src/ast/type/base-type.ts
425
+ var BaseType = class extends ASTNode {
426
+ constructor() {
427
+ super(...arguments);
428
+ this.flags = 8 /* BasicType */;
429
+ }
430
+ /**
431
+ * 类型是否一致,节点有额外信息判断,请参考 extraTypeInfoEqual
432
+ * @param targetTypeJSON
433
+ */
434
+ isTypeEqual(targetTypeJSONOrKind) {
435
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
436
+ if (targetTypeJSON?.kind === "Union" /* Union */) {
437
+ return (targetTypeJSON?.types || [])?.some(
438
+ (_subType) => this.isTypeEqual(_subType)
439
+ );
440
+ }
441
+ return this.kind === targetTypeJSON?.kind;
442
+ }
443
+ /**
444
+ * 可下钻类型需实现
445
+ * @param keyPath
446
+ */
447
+ getByKeyPath(keyPath = []) {
448
+ throw new Error(`Get By Key Path is not implemented for Type: ${this.kind}`);
449
+ }
450
+ toJSON() {
451
+ return {
452
+ kind: this.kind
453
+ };
454
+ }
455
+ };
456
+
457
+ // src/ast/type/array.ts
458
+ var ArrayType = class extends BaseType {
459
+ constructor() {
460
+ super(...arguments);
461
+ this.flags = 16 /* DrilldownType */ | 32 /* EnumerateType */;
462
+ }
463
+ fromJSON({ items }) {
464
+ this.updateChildNodeByKey("items", parseTypeJsonOrKind(items));
465
+ }
466
+ // items 类型是否可下钻
467
+ get canDrilldownItems() {
468
+ return !!(this.items?.flags & 16 /* DrilldownType */);
469
+ }
470
+ getByKeyPath(keyPath) {
471
+ const [curr, ...rest] = keyPath || [];
472
+ if (curr === "0" && this.canDrilldownItems) {
473
+ return this.items.getByKeyPath(rest);
474
+ }
475
+ return void 0;
476
+ }
477
+ isTypeEqual(targetTypeJSONOrKind) {
478
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
479
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
480
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
481
+ return isSuperEqual;
482
+ }
483
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
484
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
485
+ }
486
+ /**
487
+ * Array 强比较
488
+ * @param targetTypeJSON
489
+ * @returns
490
+ */
491
+ customStrongEqual(targetTypeJSON) {
492
+ if (!this.items) {
493
+ return !targetTypeJSON?.items;
494
+ }
495
+ return this.items?.isTypeEqual(targetTypeJSON.items);
496
+ }
497
+ toJSON() {
498
+ return {
499
+ kind: "Array" /* Array */,
500
+ items: this.items?.toJSON()
501
+ };
502
+ }
503
+ };
504
+ ArrayType.kind = "Array" /* Array */;
505
+
506
+ // src/ast/type/string.ts
507
+ var StringType = class extends BaseType {
508
+ constructor() {
509
+ super(...arguments);
510
+ this.flags = 8 /* BasicType */;
511
+ }
512
+ fromJSON() {
513
+ }
514
+ };
515
+ StringType.kind = "String" /* String */;
516
+
517
+ // src/ast/type/integer.ts
518
+ var IntegerType = class extends BaseType {
519
+ constructor() {
520
+ super(...arguments);
521
+ this.flags = 8 /* BasicType */;
522
+ }
523
+ fromJSON() {
524
+ }
525
+ };
526
+ IntegerType.kind = "Integer" /* Integer */;
527
+
528
+ // src/ast/type/boolean.ts
529
+ var BooleanType = class extends BaseType {
530
+ fromJSON() {
531
+ }
532
+ };
533
+ BooleanType.kind = "Boolean" /* Boolean */;
534
+
535
+ // src/ast/type/number.ts
536
+ var NumberType = class extends BaseType {
537
+ fromJSON() {
538
+ }
539
+ };
540
+ NumberType.kind = "Number" /* Number */;
541
+
542
+ // src/ast/type/map.ts
543
+ var MapType = class extends BaseType {
544
+ fromJSON({ keyType = "String" /* String */, valueType }) {
545
+ this.updateChildNodeByKey("keyType", parseTypeJsonOrKind(keyType));
546
+ this.updateChildNodeByKey("valueType", parseTypeJsonOrKind(valueType));
547
+ }
548
+ // Value 类型是否可下钻,后续实现
549
+ // get canDrilldownValue(): boolean {
550
+ // return !!(this.valueType.flags & ASTNodeFlags.DrilldownType);
551
+ // }
552
+ // getByKeyPath(keyPath: string[]): BaseVariableField | undefined {
553
+ // const [curr, ...rest] = keyPath || [];
554
+ // if (curr === '*' && this.canDrilldownValue) {
555
+ // return this.valueType.getByKeyPath(rest);
556
+ // }
557
+ // return undefined;
558
+ // }
559
+ isTypeEqual(targetTypeJSONOrKind) {
560
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
561
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
562
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
563
+ return isSuperEqual;
564
+ }
565
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
566
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
567
+ }
568
+ /**
569
+ * Map 强比较
570
+ * @param targetTypeJSON
571
+ * @returns
572
+ */
573
+ customStrongEqual(targetTypeJSON) {
574
+ const { keyType = "String" /* String */, valueType } = targetTypeJSON;
575
+ const isValueTypeEqual = !valueType && !this.valueType || this.valueType?.isTypeEqual(valueType);
576
+ return isValueTypeEqual && this.keyType?.isTypeEqual(keyType);
577
+ }
578
+ toJSON() {
579
+ return {
580
+ kind: "Map" /* Map */,
581
+ keyType: this.keyType?.toJSON(),
582
+ valueType: this.valueType?.toJSON()
583
+ };
584
+ }
585
+ };
586
+ // public flags: ASTNodeFlags = ASTNodeFlags.DrilldownType | ASTNodeFlags.EnumerateType;
587
+ MapType.kind = "Map" /* Map */;
588
+
589
+ // src/ast/type/object.ts
590
+ import { xor } from "lodash";
591
+ var ObjectType = class extends BaseType {
592
+ constructor() {
593
+ super(...arguments);
594
+ this.flags = 16 /* DrilldownType */;
595
+ this.propertyTable = /* @__PURE__ */ new Map();
596
+ }
597
+ fromJSON({ properties }) {
598
+ const removedKeys = new Set(this.propertyTable.keys());
599
+ const prev = [...this.properties || []];
600
+ this.properties = (properties || []).map((property) => {
601
+ const existProperty = this.propertyTable.get(property.key);
602
+ removedKeys.delete(property.key);
603
+ if (existProperty) {
604
+ existProperty.fromJSON(property);
605
+ return existProperty;
606
+ } else {
607
+ const newProperty = this.createChildNode({
608
+ ...property,
609
+ kind: "Property" /* Property */
610
+ });
611
+ this.fireChange();
612
+ this.propertyTable.set(property.key, newProperty);
613
+ return newProperty;
614
+ }
615
+ });
616
+ removedKeys.forEach((key) => {
617
+ const property = this.propertyTable.get(key);
618
+ property?.dispose();
619
+ this.propertyTable.delete(key);
620
+ this.fireChange();
621
+ });
622
+ this.dispatchGlobalEvent({
623
+ type: "ObjectPropertiesChange",
624
+ payload: {
625
+ prev,
626
+ next: [...this.properties]
627
+ }
628
+ });
629
+ }
630
+ toJSON() {
631
+ return {
632
+ kind: "Object" /* Object */,
633
+ properties: this.properties.map((_property) => _property.toJSON())
634
+ };
635
+ }
636
+ /**
637
+ * 根据 KeyPath 找到对应的变量
638
+ * @param keyPath 变量路径
639
+ * @returns
640
+ */
641
+ getByKeyPath(keyPath) {
642
+ const [curr, ...restKeyPath] = keyPath;
643
+ const property = this.propertyTable.get(curr);
644
+ if (!restKeyPath.length) {
645
+ return property;
646
+ }
647
+ if (property?.type && property?.type?.flags & 16 /* DrilldownType */) {
648
+ return property.type.getByKeyPath(restKeyPath);
649
+ }
650
+ return void 0;
651
+ }
652
+ isTypeEqual(targetTypeJSONOrKind) {
653
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
654
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
655
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
656
+ return isSuperEqual;
657
+ }
658
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
659
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
660
+ }
661
+ /**
662
+ * Object 类型强比较
663
+ * @param targetTypeJSON
664
+ * @returns
665
+ */
666
+ customStrongEqual(targetTypeJSON) {
667
+ const targetProperties = targetTypeJSON.properties || [];
668
+ const sourcePropertyKeys = Array.from(this.propertyTable.keys());
669
+ const targetPropertyKeys = targetProperties.map((_target) => _target.key);
670
+ const isKeyStrongEqual = !xor(sourcePropertyKeys, targetPropertyKeys).length;
671
+ return isKeyStrongEqual && targetProperties.every((targetProperty) => {
672
+ const sourceProperty = this.propertyTable.get(targetProperty.key);
673
+ return sourceProperty && sourceProperty.key === targetProperty.key && sourceProperty.type?.isTypeEqual(targetProperty?.type);
674
+ });
675
+ }
676
+ };
677
+ ObjectType.kind = "Object" /* Object */;
678
+
679
+ // src/ast/expression/base-expression.ts
680
+ import {
681
+ distinctUntilChanged as distinctUntilChanged2,
682
+ map as map2,
683
+ switchMap,
684
+ combineLatest,
685
+ of,
686
+ Subject,
687
+ share
688
+ } from "rxjs";
689
+ import { shallowEqual as shallowEqual2 } from "fast-equals";
690
+
691
+ // src/ast/utils/variable-field.ts
692
+ function getParentFields(ast) {
693
+ let curr = ast.parent;
694
+ const res = [];
695
+ while (curr) {
696
+ if (curr.flags & 1 /* VariableField */) {
697
+ res.push(curr);
698
+ }
699
+ curr = curr.parent;
700
+ }
701
+ return res;
702
+ }
703
+
704
+ // src/ast/expression/base-expression.ts
705
+ var BaseExpression = class extends ASTNode {
706
+ constructor(params, opts) {
707
+ super(params, opts);
708
+ this.flags = 4 /* Expression */;
709
+ /**
710
+ * 引用变量
711
+ */
712
+ this._refs = [];
713
+ this.refreshRefs$ = new Subject();
714
+ /**
715
+ * 监听引用变量变化
716
+ * 监听 [a.b.c] -> [a.b]
717
+ */
718
+ this.refs$ = this.refreshRefs$.pipe(
719
+ map2(() => this.getRefFields()),
720
+ distinctUntilChanged2(shallowEqual2),
721
+ switchMap(
722
+ (refs) => !refs?.length ? of([]) : combineLatest(
723
+ refs.map(
724
+ (ref) => ref ? ref.value$ : of(void 0)
725
+ )
726
+ )
727
+ ),
728
+ share()
729
+ );
730
+ this.toDispose.push(
731
+ subsToDisposable(
732
+ this.refs$.subscribe((_refs) => {
733
+ this._refs = _refs;
734
+ this.fireChange();
735
+ })
736
+ )
737
+ );
738
+ }
739
+ /**
740
+ * 获取全局变量表,方便表达式获取引用变量
741
+ */
742
+ get globalVariableTable() {
743
+ return this.scope.variableEngine.globalVariableTable;
744
+ }
745
+ /**
746
+ * 父变量字段,通过由近而远的方式进行排序
747
+ */
748
+ get parentFields() {
749
+ return getParentFields(this);
750
+ }
751
+ get refs() {
752
+ return this._refs;
753
+ }
754
+ /**
755
+ * 刷新变量引用
756
+ */
757
+ refreshRefs() {
758
+ this.refreshRefs$.next();
759
+ }
760
+ };
761
+
762
+ // src/ast/expression/expression-list.ts
763
+ var ExpressionList = class extends ASTNode {
764
+ fromJSON({ expressions }) {
765
+ this.expressions = expressions.map((_expression, idx) => {
766
+ const prevExpression = this.expressions[idx];
767
+ if (prevExpression.kind !== _expression.kind) {
768
+ prevExpression.dispose();
769
+ this.fireChange();
770
+ return this.createChildNode(_expression);
771
+ }
772
+ prevExpression.fromJSON(_expression);
773
+ return prevExpression;
774
+ });
775
+ }
776
+ toJSON() {
777
+ return {
778
+ kind: "ExpressionList" /* ExpressionList */,
779
+ properties: this.expressions.map((_expression) => _expression.toJSON())
780
+ };
781
+ }
782
+ };
783
+ ExpressionList.kind = "ExpressionList" /* ExpressionList */;
784
+
785
+ // src/ast/expression/keypath-expression.ts
786
+ import { shallowEqual as shallowEqual3 } from "fast-equals";
787
+ var KeyPathExpression = class extends BaseExpression {
788
+ constructor(params, opts) {
789
+ super(params, opts);
790
+ this._keyPath = [];
791
+ this.toDispose.pushAll([
792
+ // 可以用变量列表变化时候 (有新增或者删除时)
793
+ this.scope.available.onVariableListChange(() => {
794
+ this.refreshRefs();
795
+ }),
796
+ // this._keyPath 指向的可引用变量发生变化时,刷新引用数据
797
+ this.scope.available.onAnyVariableChange((_v) => {
798
+ if (_v.key === this._keyPath[0]) {
799
+ this.refreshRefs();
800
+ }
801
+ })
802
+ ]);
803
+ }
804
+ get keyPath() {
805
+ return this._keyPath;
806
+ }
807
+ getRefFields() {
808
+ const ref = this.scope.available.getByKeyPath(this._keyPath);
809
+ return ref ? [ref] : [];
810
+ }
811
+ get returnType() {
812
+ const [refNode] = this._refs || [];
813
+ if (refNode && refNode.flags & 1 /* VariableField */) {
814
+ return refNode.type;
815
+ }
816
+ return;
817
+ }
818
+ /**
819
+ * 业务重改该方法可快速定制自己的 Path 表达式
820
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
821
+ * @param json 业务定义的 Path 表达式
822
+ * @returns
823
+ */
824
+ parseToKeyPath(json) {
825
+ return json.keyPath;
826
+ }
827
+ fromJSON(json) {
828
+ const keyPath = this.parseToKeyPath(json);
829
+ if (!shallowEqual3(keyPath, this._keyPath)) {
830
+ this._keyPath = keyPath;
831
+ this.refreshRefs();
832
+ }
833
+ }
834
+ toJSON() {
835
+ return {
836
+ kind: "KeyPathExpression" /* KeyPathExpression */,
837
+ keyPath: this._keyPath
838
+ };
839
+ }
840
+ };
841
+ KeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
842
+
843
+ // src/ast/expression/enumerate-expression.ts
844
+ var EnumerateExpression = class extends BaseExpression {
845
+ get enumerateFor() {
846
+ return this._enumerateFor;
847
+ }
848
+ get returnType() {
849
+ const childReturnType = this.enumerateFor?.returnType;
850
+ if (childReturnType?.kind === "Array" /* Array */) {
851
+ return childReturnType.items;
852
+ }
853
+ return void 0;
854
+ }
855
+ getRefFields() {
856
+ return [];
857
+ }
858
+ fromJSON({ enumerateFor: expression }) {
859
+ this.updateChildNodeByKey("_enumerateFor", expression);
860
+ }
861
+ toJSON() {
862
+ return {
863
+ kind: "EnumerateExpression" /* EnumerateExpression */,
864
+ enumerateFor: this.enumerateFor?.toJSON()
865
+ };
866
+ }
867
+ };
868
+ EnumerateExpression.kind = "EnumerateExpression" /* EnumerateExpression */;
869
+
870
+ // src/ast/expression/keypath-expression-v2.ts
871
+ import { shallowEqual as shallowEqual4 } from "fast-equals";
872
+
873
+ // src/ast/utils/expression.ts
874
+ import { intersection } from "lodash";
875
+ function getAllRefs(ast) {
876
+ return getAllChildren(ast).filter((_child) => _child.flags & 4 /* Expression */).map((_child) => _child.refs).flat().filter(Boolean);
877
+ }
878
+ function checkRefCycle(curr, refNodes) {
879
+ if (intersection(curr.scope.coverScopes, refNodes.map((_ref) => _ref?.scope).filter(Boolean)).length === 0) {
880
+ return false;
881
+ }
882
+ const visited = /* @__PURE__ */ new Set();
883
+ const queue = [...refNodes];
884
+ while (queue.length) {
885
+ const currNode = queue.shift();
886
+ visited.add(currNode);
887
+ for (const ref of getAllRefs(currNode).filter((_ref) => !visited.has(_ref))) {
888
+ queue.push(ref);
889
+ }
890
+ }
891
+ return intersection(Array.from(visited), getParentFields(curr)).length > 0;
892
+ }
893
+
894
+ // src/ast/expression/keypath-expression-v2.ts
895
+ var KeyPathExpressionV2 = class extends BaseExpression {
896
+ constructor(params, opts) {
897
+ super(params, opts);
898
+ this._keyPath = [];
899
+ this.toDispose.pushAll([
900
+ // 可以用变量列表变化时候 (有新增或者删除时)
901
+ this.scope.available.onVariableListChange(() => {
902
+ this.refreshRefs();
903
+ }),
904
+ // this._keyPath 指向的可引用变量发生变化时,刷新引用数据
905
+ this.scope.available.onAnyVariableChange((_v) => {
906
+ if (_v.key === this._keyPath[0]) {
907
+ this.refreshRefs();
908
+ }
909
+ }),
910
+ subsToDisposable(
911
+ this.refs$.subscribe((_type) => {
912
+ const [ref] = this._refs;
913
+ if (this.prevRefTypeHash !== ref?.type?.hash) {
914
+ this.prevRefTypeHash = ref?.type?.hash;
915
+ this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
916
+ }
917
+ })
918
+ )
919
+ ]);
920
+ }
921
+ get keyPath() {
922
+ return this._keyPath;
923
+ }
924
+ getRefFields() {
925
+ const ref = this.scope.available.getByKeyPath(this._keyPath);
926
+ if (checkRefCycle(this, [ref])) {
927
+ console.warn(
928
+ "[CustomKeyPathExpression] checkRefCycle: Reference Cycle Existed",
929
+ this.parentFields.map((_field) => _field.key).reverse()
930
+ );
931
+ return [];
932
+ }
933
+ return ref ? [ref] : [];
934
+ }
935
+ get returnType() {
936
+ return this._returnType;
937
+ }
938
+ /**
939
+ * 业务重改该方法可快速定制自己的 Path 表达式
940
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
941
+ * @param json 业务定义的 Path 表达式
942
+ * @returns
943
+ */
944
+ parseToKeyPath(json) {
945
+ return json.keyPath;
946
+ }
947
+ fromJSON(json) {
948
+ const keyPath = this.parseToKeyPath(json);
949
+ if (!shallowEqual4(keyPath, this._keyPath)) {
950
+ this._keyPath = keyPath;
951
+ this.refreshRefs();
952
+ }
953
+ }
954
+ getReturnTypeJSONByRef(_ref) {
955
+ return _ref?.type?.toJSON();
956
+ }
957
+ toJSON() {
958
+ return {
959
+ kind: "KeyPathExpression" /* KeyPathExpression */,
960
+ keyPath: this._keyPath
961
+ };
962
+ }
963
+ };
964
+ KeyPathExpressionV2.kind = "KeyPathExpression" /* KeyPathExpression */;
965
+
966
+ // src/ast/declaration/variable-declaration.ts
967
+ import { Disposable as Disposable3 } from "@flowgram.ai/utils";
968
+
969
+ // src/ast/declaration/base-variable-field.ts
970
+ import { shallowEqual as shallowEqual5 } from "fast-equals";
971
+ var BaseVariableField = class extends ASTNode {
972
+ constructor() {
973
+ super(...arguments);
974
+ this.flags = 1 /* VariableField */;
975
+ this._meta = {};
976
+ }
977
+ /**
978
+ * 父变量字段,通过由近而远的方式进行排序
979
+ */
980
+ get parentFields() {
981
+ return getParentFields(this);
982
+ }
983
+ get meta() {
984
+ return this._meta;
985
+ }
986
+ get type() {
987
+ return this._initializer?.returnType || this._type;
988
+ }
989
+ get initializer() {
990
+ return this._initializer;
991
+ }
992
+ /**
993
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
994
+ */
995
+ fromJSON({ type, initializer, meta }) {
996
+ this.updateType(type);
997
+ this.updateInitializer(initializer);
998
+ this.updateMeta(meta);
999
+ }
1000
+ updateType(type) {
1001
+ const nextTypeJson = typeof type === "string" ? { kind: type } : type;
1002
+ this.updateChildNodeByKey("_type", nextTypeJson);
1003
+ }
1004
+ updateInitializer(nextInitializer) {
1005
+ this.updateChildNodeByKey("_initializer", nextInitializer);
1006
+ }
1007
+ updateMeta(nextMeta) {
1008
+ if (!shallowEqual5(nextMeta, this._meta)) {
1009
+ this._meta = nextMeta;
1010
+ this.fireChange();
1011
+ }
1012
+ }
1013
+ /**
1014
+ * 根据 keyPath 去找下钻的变量字段
1015
+ * @param keyPath
1016
+ * @returns
1017
+ */
1018
+ getByKeyPath(keyPath) {
1019
+ if (this.type?.flags & 16 /* DrilldownType */) {
1020
+ return this.type.getByKeyPath(keyPath);
1021
+ }
1022
+ return void 0;
1023
+ }
1024
+ /**
1025
+ * 监听类型变化
1026
+ * @param observer
1027
+ * @returns
1028
+ */
1029
+ onTypeChange(observer) {
1030
+ return this.subscribe(observer, { selector: (curr) => curr.type });
1031
+ }
1032
+ /**
1033
+ * 转换为 JSON
1034
+ * @returns
1035
+ */
1036
+ toJSON() {
1037
+ return {
1038
+ kind: this.kind,
1039
+ key: this.key,
1040
+ type: this.type?.toJSON(),
1041
+ initializer: this.initializer?.toJSON(),
1042
+ meta: this._meta
1043
+ };
1044
+ }
1045
+ };
1046
+
1047
+ // src/ast/declaration/variable-declaration.ts
1048
+ var VariableDeclaration = class extends BaseVariableField {
1049
+ constructor(params) {
1050
+ super(params);
1051
+ this._order = 0;
1052
+ this.scope.output.addVariableToTable(this);
1053
+ this.toDispose.push(
1054
+ Disposable3.create(() => {
1055
+ this.scope.output.setHasChanges();
1056
+ this.scope.output.removeVariableFromTable(this.key);
1057
+ })
1058
+ );
1059
+ }
1060
+ get order() {
1061
+ return this._order;
1062
+ }
1063
+ /**
1064
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
1065
+ */
1066
+ fromJSON({ order, ...rest }) {
1067
+ this.updateOrder(order);
1068
+ super.fromJSON(rest);
1069
+ }
1070
+ updateOrder(order = 0) {
1071
+ if (order !== this._order) {
1072
+ this._order = order;
1073
+ this.scope.output.setHasChanges();
1074
+ this.fireChange();
1075
+ }
1076
+ }
1077
+ // 监听类型变化
1078
+ onTypeChange(observer) {
1079
+ return this.subscribe(observer, { selector: (curr) => curr.type });
1080
+ }
1081
+ };
1082
+ VariableDeclaration.kind = "VariableDeclaration" /* VariableDeclaration */;
1083
+
1084
+ // src/ast/declaration/variable-declaration-list.ts
1085
+ var VariableDeclarationList = class extends ASTNode {
1086
+ constructor() {
1087
+ super(...arguments);
1088
+ this.declarationTable = /* @__PURE__ */ new Map();
1089
+ }
1090
+ fromJSON({ declarations, startOrder }) {
1091
+ const removedKeys = new Set(this.declarationTable.keys());
1092
+ const prev = [...this.declarations || []];
1093
+ this.declarations = (declarations || []).map(
1094
+ (declaration, idx) => {
1095
+ const order = (startOrder || 0) + idx;
1096
+ const declarationKey = declaration.key || this.declarations?.[idx]?.key;
1097
+ const existDeclaration = this.declarationTable.get(declarationKey);
1098
+ if (declarationKey) {
1099
+ removedKeys.delete(declarationKey);
1100
+ }
1101
+ if (existDeclaration) {
1102
+ existDeclaration.fromJSON({ order, ...declaration });
1103
+ return existDeclaration;
1104
+ } else {
1105
+ const newDeclaration = this.createChildNode({
1106
+ order,
1107
+ ...declaration,
1108
+ kind: "VariableDeclaration" /* VariableDeclaration */
1109
+ });
1110
+ this.fireChange();
1111
+ this.declarationTable.set(newDeclaration.key, newDeclaration);
1112
+ return newDeclaration;
1113
+ }
1114
+ }
1115
+ );
1116
+ removedKeys.forEach((key) => {
1117
+ const declaration = this.declarationTable.get(key);
1118
+ declaration?.dispose();
1119
+ this.declarationTable.delete(key);
1120
+ });
1121
+ this.dispatchGlobalEvent({
1122
+ type: "VariableListChange",
1123
+ payload: {
1124
+ prev,
1125
+ next: [...this.declarations]
1126
+ }
1127
+ });
1128
+ }
1129
+ toJSON() {
1130
+ return {
1131
+ kind: "VariableDeclarationList" /* VariableDeclarationList */,
1132
+ properties: this.declarations.map((_declaration) => _declaration.toJSON())
1133
+ };
1134
+ }
1135
+ };
1136
+ VariableDeclarationList.kind = "VariableDeclarationList" /* VariableDeclarationList */;
1137
+
1138
+ // src/ast/declaration/property.ts
1139
+ var Property = class extends BaseVariableField {
1140
+ };
1141
+ Property.kind = "Property" /* Property */;
1142
+
1143
+ // src/ast/common/data-node.ts
1144
+ import { shallowEqual as shallowEqual6 } from "fast-equals";
1145
+ var DataNode = class extends ASTNode {
1146
+ get data() {
1147
+ return this._data;
1148
+ }
1149
+ fromJSON(json) {
1150
+ const { kind, ...restData } = json;
1151
+ if (!shallowEqual6(restData, this._data)) {
1152
+ this._data = restData;
1153
+ this.fireChange();
1154
+ }
1155
+ }
1156
+ toJSON() {
1157
+ return {
1158
+ kind: "DataNode" /* DataNode */,
1159
+ ...this._data
1160
+ };
1161
+ }
1162
+ partialUpdate(nextData) {
1163
+ if (!shallowEqual6(nextData, this._data)) {
1164
+ this._data = {
1165
+ ...this._data,
1166
+ ...nextData
1167
+ };
1168
+ this.fireChange();
1169
+ }
1170
+ }
1171
+ };
1172
+ DataNode.kind = "DataNode" /* DataNode */;
1173
+
1174
+ // src/ast/common/list-node.ts
1175
+ var ListNode = class extends ASTNode {
1176
+ get list() {
1177
+ return this._list;
1178
+ }
1179
+ fromJSON({ list }) {
1180
+ this._list.slice(list.length).forEach((_item) => {
1181
+ _item.dispose();
1182
+ this.fireChange();
1183
+ });
1184
+ this._list = list.map((_item, idx) => {
1185
+ const prevItem = this._list[idx];
1186
+ if (prevItem.kind !== _item.kind) {
1187
+ prevItem.dispose();
1188
+ this.fireChange();
1189
+ return this.createChildNode(_item);
1190
+ }
1191
+ prevItem.fromJSON(_item);
1192
+ return prevItem;
1193
+ });
1194
+ }
1195
+ toJSON() {
1196
+ return {
1197
+ kind: "ListNode" /* ListNode */,
1198
+ list: this._list.map((item) => item.toJSON())
1199
+ };
1200
+ }
1201
+ };
1202
+ ListNode.kind = "ListNode" /* ListNode */;
1203
+
1204
+ // src/ast/common/map-node.ts
1205
+ var MapNode = class extends ASTNode {
1206
+ constructor() {
1207
+ super(...arguments);
1208
+ this.map = /* @__PURE__ */ new Map();
1209
+ }
1210
+ fromJSON({ map: map4 }) {
1211
+ const removedKeys = new Set(this.map.keys());
1212
+ for (const [key, item] of map4 || []) {
1213
+ removedKeys.delete(key);
1214
+ this.set(key, item);
1215
+ }
1216
+ for (const removeKey of Array.from(removedKeys)) {
1217
+ this.remove(removeKey);
1218
+ }
1219
+ }
1220
+ toJSON() {
1221
+ return {
1222
+ kind: "MapNode" /* MapNode */,
1223
+ map: Array.from(this.map.entries())
1224
+ };
1225
+ }
1226
+ /**
1227
+ * 往 Map 中设置 ASTNode
1228
+ * @param key ASTNode 的索引,
1229
+ * @param json
1230
+ */
1231
+ set(key, nextJSON) {
1232
+ return this.withBatchUpdate(updateChildNodeHelper).call(this, {
1233
+ getChildNode: () => this.get(key),
1234
+ removeChildNode: () => this.map.delete(key),
1235
+ updateChildNode: (nextNode) => this.map.set(key, nextNode),
1236
+ nextJSON
1237
+ });
1238
+ }
1239
+ /**
1240
+ * 移除指定 ASTNode
1241
+ * @param key
1242
+ */
1243
+ remove(key) {
1244
+ this.get(key)?.dispose();
1245
+ this.map.delete(key);
1246
+ this.fireChange();
1247
+ }
1248
+ /**
1249
+ * 获取 ASTNode
1250
+ * @param key
1251
+ * @returns
1252
+ */
1253
+ get(key) {
1254
+ return this.map.get(key);
1255
+ }
1256
+ };
1257
+ MapNode.kind = "MapNode" /* MapNode */;
1258
+
1259
+ // src/ast/ast-registers.ts
1260
+ var ASTRegisters = class {
1261
+ /**
1262
+ * 核心 AST 节点注册
1263
+ */
1264
+ constructor() {
1265
+ this.injectors = /* @__PURE__ */ new Map();
1266
+ this.astMap = /* @__PURE__ */ new Map();
1267
+ this.registerAST(StringType);
1268
+ this.registerAST(NumberType);
1269
+ this.registerAST(BooleanType);
1270
+ this.registerAST(IntegerType);
1271
+ this.registerAST(ObjectType);
1272
+ this.registerAST(ArrayType);
1273
+ this.registerAST(MapType);
1274
+ this.registerAST(Property);
1275
+ this.registerAST(VariableDeclaration);
1276
+ this.registerAST(VariableDeclarationList);
1277
+ this.registerAST(KeyPathExpression);
1278
+ this.registerAST(EnumerateExpression);
1279
+ this.registerAST(ExpressionList);
1280
+ this.registerAST(MapNode);
1281
+ this.registerAST(DataNode);
1282
+ }
1283
+ /**
1284
+ * 创建 AST 节点
1285
+ * @param param 创建参数
1286
+ * @returns
1287
+ */
1288
+ createAST(json, { parent, scope }) {
1289
+ const Registry = this.astMap.get(json.kind);
1290
+ if (!Registry) {
1291
+ throw Error(`ASTKind: ${String(json.kind)} can not find its ASTNode Registry`);
1292
+ }
1293
+ const injector = this.injectors.get(json.kind);
1294
+ const node = new Registry(
1295
+ {
1296
+ key: json.key,
1297
+ scope,
1298
+ parent
1299
+ },
1300
+ injector?.() || {}
1301
+ );
1302
+ node.changeLocked = true;
1303
+ node.fromJSON(omit(json, ["key", "kind"]));
1304
+ node.changeLocked = false;
1305
+ if (Reflect.hasMetadata(POST_CONSTRUCT_AST_SYMBOL, node)) {
1306
+ const postConstructKey = Reflect.getMetadata(POST_CONSTRUCT_AST_SYMBOL, node);
1307
+ node[postConstructKey]?.();
1308
+ }
1309
+ return node;
1310
+ }
1311
+ /**
1312
+ * 根据 AST 节点类型获取节点 Registry
1313
+ * @param kind
1314
+ * @returns
1315
+ */
1316
+ getASTRegistryByKind(kind) {
1317
+ return this.astMap.get(kind);
1318
+ }
1319
+ /**
1320
+ * 注册 AST 节点
1321
+ * @param ASTNode
1322
+ * @param injector
1323
+ */
1324
+ registerAST(ASTNode2, injector) {
1325
+ this.astMap.set(ASTNode2.kind, ASTNode2);
1326
+ if (injector) {
1327
+ this.injectors.set(ASTNode2.kind, injector);
1328
+ }
1329
+ }
1330
+ };
1331
+ ASTRegisters = __decorateClass([
1332
+ injectable2()
1333
+ ], ASTRegisters);
1334
+
1335
+ // src/ast/factory.ts
1336
+ var ASTFactory;
1337
+ ((ASTFactory2) => {
1338
+ ASTFactory2.createString = () => ({ kind: "String" /* String */ });
1339
+ ASTFactory2.createNumber = () => ({ kind: "Number" /* Number */ });
1340
+ ASTFactory2.createBoolean = () => ({ kind: "Boolean" /* Boolean */ });
1341
+ ASTFactory2.createInteger = () => ({ kind: "Integer" /* Integer */ });
1342
+ ASTFactory2.createObject = (json) => ({
1343
+ kind: "Object" /* Object */,
1344
+ ...json
1345
+ });
1346
+ ASTFactory2.createArray = (json) => ({
1347
+ kind: "Array" /* Array */,
1348
+ ...json
1349
+ });
1350
+ ASTFactory2.createMap = (json) => ({
1351
+ kind: "Map" /* Map */,
1352
+ ...json
1353
+ });
1354
+ ASTFactory2.createUnion = (json) => ({
1355
+ kind: "Union" /* Union */,
1356
+ ...json
1357
+ });
1358
+ ASTFactory2.createVariableDeclaration = (json) => ({
1359
+ kind: "VariableDeclaration" /* VariableDeclaration */,
1360
+ ...json
1361
+ });
1362
+ ASTFactory2.createProperty = (json) => ({
1363
+ kind: "Property" /* Property */,
1364
+ ...json
1365
+ });
1366
+ ASTFactory2.createVariableDeclarationList = (json) => ({
1367
+ kind: "VariableDeclarationList" /* VariableDeclarationList */,
1368
+ ...json
1369
+ });
1370
+ ASTFactory2.createEnumerateExpression = (json) => ({
1371
+ kind: "EnumerateExpression" /* EnumerateExpression */,
1372
+ ...json
1373
+ });
1374
+ ASTFactory2.createKeyPathExpression = (json) => ({
1375
+ kind: "KeyPathExpression" /* KeyPathExpression */,
1376
+ ...json
1377
+ });
1378
+ })(ASTFactory || (ASTFactory = {}));
1379
+
1380
+ // src/scope/variable-table.ts
1381
+ import { Subject as Subject2, merge, share as share2, skip as skip2, switchMap as switchMap2 } from "rxjs";
1382
+ import { Emitter } from "@flowgram.ai/utils";
1383
+ import { DisposableCollection as DisposableCollection3 } from "@flowgram.ai/utils";
1384
+ var VariableTable = class {
1385
+ constructor(parentTable) {
1386
+ this.parentTable = parentTable;
1387
+ this.table = /* @__PURE__ */ new Map();
1388
+ this.onDataChangeEmitter = new Emitter();
1389
+ this.variables$ = new Subject2();
1390
+ // 监听变量列表中的单个变量变化
1391
+ this.anyVariableChange$ = this.variables$.pipe(
1392
+ switchMap2(
1393
+ (_variables) => merge(
1394
+ ..._variables.map(
1395
+ (_v) => _v.value$.pipe(
1396
+ // 跳过 BehaviorSubject 第一个
1397
+ skip2(1)
1398
+ )
1399
+ )
1400
+ )
1401
+ ),
1402
+ share2()
1403
+ );
1404
+ this.onDataChange = this.onDataChangeEmitter.event;
1405
+ this._version = 0;
1406
+ }
1407
+ /**
1408
+ * 监听任意变量变化
1409
+ * @param observer 监听器,变量变化时会吐出值
1410
+ * @returns
1411
+ */
1412
+ onAnyVariableChange(observer) {
1413
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1414
+ }
1415
+ /**
1416
+ * 列表或者任意变量变化
1417
+ * @param observer
1418
+ */
1419
+ onAnyChange(observer) {
1420
+ const disposables = new DisposableCollection3();
1421
+ disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
1422
+ return disposables;
1423
+ }
1424
+ fireChange() {
1425
+ this._version++;
1426
+ this.onDataChangeEmitter.fire();
1427
+ this.parentTable?.fireChange();
1428
+ }
1429
+ get version() {
1430
+ return this._version;
1431
+ }
1432
+ get variables() {
1433
+ return Array.from(this.table.values());
1434
+ }
1435
+ get variableKeys() {
1436
+ return Array.from(this.table.keys());
1437
+ }
1438
+ /**
1439
+ * 根据 keyPath 找到对应的变量,或 Property 节点
1440
+ * @param keyPath
1441
+ * @returns
1442
+ */
1443
+ getByKeyPath(keyPath) {
1444
+ const [variableKey, ...propertyKeys] = keyPath || [];
1445
+ if (!variableKey) {
1446
+ return;
1447
+ }
1448
+ const variable = this.getVariableByKey(variableKey);
1449
+ return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
1450
+ }
1451
+ /**
1452
+ * 根据 key 值找到相应的变量
1453
+ * @param key
1454
+ * @returns
1455
+ */
1456
+ getVariableByKey(key) {
1457
+ return this.table.get(key);
1458
+ }
1459
+ /**
1460
+ * 往 variableTable 添加输出变量
1461
+ * @param variable
1462
+ */
1463
+ addVariableToTable(variable) {
1464
+ this.table.set(variable.key, variable);
1465
+ if (this.parentTable) {
1466
+ this.parentTable.addVariableToTable(variable);
1467
+ }
1468
+ this.variables$.next(this.variables);
1469
+ }
1470
+ /**
1471
+ * 从 variableTable 中移除变量
1472
+ * @param key
1473
+ */
1474
+ removeVariableFromTable(key) {
1475
+ this.table.delete(key);
1476
+ if (this.parentTable) {
1477
+ this.parentTable.removeVariableFromTable(key);
1478
+ }
1479
+ this.variables$.next(this.variables);
1480
+ }
1481
+ dispose() {
1482
+ this.variableKeys.forEach((_key) => this.parentTable?.removeVariableFromTable(_key));
1483
+ this.onDataChangeEmitter.dispose();
1484
+ }
1485
+ };
1486
+
1487
+ // src/scope/datas/scope-output-data.ts
1488
+ var ScopeOutputData = class {
1489
+ constructor(scope) {
1490
+ this.scope = scope;
1491
+ this.memo = createMemo();
1492
+ this._hasChanges = false;
1493
+ this.variableTable = new VariableTable(scope.variableEngine.globalVariableTable);
1494
+ this.scope.toDispose.pushAll([
1495
+ // AST 根节点更新时,检查在这次 AST 变化期间节点是否有变化
1496
+ this.scope.ast.subscribe(() => {
1497
+ if (this._hasChanges) {
1498
+ this.memo.clear();
1499
+ this.notifyCoversChange();
1500
+ this.variableTable.fireChange();
1501
+ this._hasChanges = false;
1502
+ }
1503
+ }),
1504
+ this.variableTable
1505
+ ]);
1506
+ }
1507
+ get variableEngine() {
1508
+ return this.scope.variableEngine;
1509
+ }
1510
+ get globalVariableTable() {
1511
+ return this.scope.variableEngine.globalVariableTable;
1512
+ }
1513
+ get onDataChange() {
1514
+ return this.variableTable.onDataChange.bind(this.variableTable);
1515
+ }
1516
+ get onAnyVariableChange() {
1517
+ return this.variableTable.onAnyVariableChange.bind(this.variableTable);
1518
+ }
1519
+ /**
1520
+ * 作用域输出变量
1521
+ */
1522
+ get variables() {
1523
+ return this.memo(
1524
+ "variables",
1525
+ () => this.variableTable.variables.sort((a, b) => a.order - b.order)
1526
+ );
1527
+ }
1528
+ /**
1529
+ * 输出的变量 keys
1530
+ */
1531
+ get variableKeys() {
1532
+ return this.memo("variableKeys", () => this.variableTable.variableKeys);
1533
+ }
1534
+ addVariableToTable(variable) {
1535
+ if (variable.scope !== this.scope) {
1536
+ throw Error("VariableDeclaration must be a ast node in scope");
1537
+ }
1538
+ this.variableTable.addVariableToTable(variable);
1539
+ this._hasChanges = true;
1540
+ }
1541
+ // 标记为发生了变化,用于变量排序变化的场景
1542
+ setHasChanges() {
1543
+ this._hasChanges = true;
1544
+ }
1545
+ removeVariableFromTable(key) {
1546
+ this.variableTable.removeVariableFromTable(key);
1547
+ this._hasChanges = true;
1548
+ }
1549
+ getVariableByKey(key) {
1550
+ return this.variableTable.getVariableByKey(key);
1551
+ }
1552
+ // 通知覆盖作用域更新可用变量
1553
+ notifyCoversChange() {
1554
+ this.scope.coverScopes.forEach((scope) => scope.available.refresh());
1555
+ }
1556
+ };
1557
+
1558
+ // src/scope/datas/scope-available-data.ts
1559
+ import {
1560
+ Subject as Subject3,
1561
+ distinctUntilChanged as distinctUntilChanged3,
1562
+ map as map3,
1563
+ merge as merge2,
1564
+ share as share3,
1565
+ skip as skip3,
1566
+ switchMap as switchMap3
1567
+ } from "rxjs";
1568
+ import { flatten } from "lodash";
1569
+ import { shallowEqual as shallowEqual7 } from "fast-equals";
1570
+ import { Disposable as Disposable5 } from "@flowgram.ai/utils";
1571
+ import { Emitter as Emitter2 } from "@flowgram.ai/utils";
1572
+ var ScopeAvailableData = class {
1573
+ constructor(scope) {
1574
+ this.scope = scope;
1575
+ this.memo = createMemo();
1576
+ this.refresh$ = new Subject3();
1577
+ this._variables = [];
1578
+ /**
1579
+ * 监听
1580
+ */
1581
+ this.variables$ = this.refresh$.pipe(
1582
+ // 输出变量是否 version 发生变化
1583
+ map3(() => flatten(this.depScopes.map((scope) => scope.output.variables || []))),
1584
+ // 变量列表浅比较
1585
+ distinctUntilChanged3(shallowEqual7),
1586
+ share3()
1587
+ );
1588
+ // 监听变量列表中的单个变量变化
1589
+ this.anyVariableChange$ = this.variables$.pipe(
1590
+ switchMap3(
1591
+ (_variables) => merge2(
1592
+ ..._variables.map(
1593
+ (_v) => _v.value$.pipe(
1594
+ // 跳过 BehaviorSubject 第一个
1595
+ skip3(1)
1596
+ )
1597
+ )
1598
+ )
1599
+ ),
1600
+ share3()
1601
+ );
1602
+ this.onDataChangeEmitter = new Emitter2();
1603
+ /**
1604
+ * 监听变量列表变化 + 任意子变量变化
1605
+ */
1606
+ this.onDataChange = this.onDataChangeEmitter.event;
1607
+ this.scope.toDispose.pushAll([
1608
+ this.onVariableListChange((_variables) => {
1609
+ this._variables = _variables;
1610
+ this.memo.clear();
1611
+ this.onDataChangeEmitter.fire(this._variables);
1612
+ }),
1613
+ this.onAnyVariableChange(() => {
1614
+ this.onDataChangeEmitter.fire(this._variables);
1615
+ }),
1616
+ Disposable5.create(() => {
1617
+ this.refresh$.complete();
1618
+ this.refresh$.unsubscribe();
1619
+ })
1620
+ ]);
1621
+ }
1622
+ get globalVariableTable() {
1623
+ return this.scope.variableEngine.globalVariableTable;
1624
+ }
1625
+ // 刷新可访问变量列表
1626
+ refresh() {
1627
+ if (this.scope.disposed) {
1628
+ return;
1629
+ }
1630
+ this.refresh$.next();
1631
+ }
1632
+ /**
1633
+ * 监听任意变量变化
1634
+ * @param observer 监听器,变量变化时会吐出值
1635
+ * @returns
1636
+ */
1637
+ onAnyVariableChange(observer) {
1638
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1639
+ }
1640
+ /**
1641
+ * 监听变量列表变化
1642
+ * @param observer
1643
+ * @returns
1644
+ */
1645
+ onVariableListChange(observer) {
1646
+ return subsToDisposable(this.variables$.subscribe(observer));
1647
+ }
1648
+ /**
1649
+ * 获取可消费变量
1650
+ */
1651
+ get variables() {
1652
+ return this._variables;
1653
+ }
1654
+ /**
1655
+ * 获取可访问的变量 keys
1656
+ */
1657
+ get variableKeys() {
1658
+ return this.memo("availableKeys", () => this._variables.map((_v) => _v.key));
1659
+ }
1660
+ /**
1661
+ * 返回依赖的作用域
1662
+ */
1663
+ get depScopes() {
1664
+ return this.scope.depScopes;
1665
+ }
1666
+ /**
1667
+ * 通过 keyPath 找到可用变量
1668
+ * @param keyPath
1669
+ * @returns
1670
+ */
1671
+ getByKeyPath(keyPath = []) {
1672
+ if (!this.variableKeys.includes(keyPath[0])) {
1673
+ return;
1674
+ }
1675
+ return this.globalVariableTable.getByKeyPath(keyPath);
1676
+ }
1677
+ };
1678
+
1679
+ // src/scope/datas/scope-event-data.ts
1680
+ import { Subject as Subject4, filter } from "rxjs";
1681
+ var ScopeEventData = class {
1682
+ constructor(scope) {
1683
+ this.scope = scope;
1684
+ this.event$ = new Subject4();
1685
+ scope.toDispose.pushAll([
1686
+ this.subscribe((_action) => {
1687
+ scope.variableEngine.fireGlobalEvent(_action);
1688
+ })
1689
+ ]);
1690
+ }
1691
+ dispatch(action) {
1692
+ if (this.scope.disposed) {
1693
+ return;
1694
+ }
1695
+ this.event$.next(action);
1696
+ }
1697
+ subscribe(observer) {
1698
+ return subsToDisposable(this.event$.subscribe(observer));
1699
+ }
1700
+ on(type, observer) {
1701
+ return subsToDisposable(
1702
+ this.event$.pipe(filter((_action) => _action.type === type)).subscribe(observer)
1703
+ );
1704
+ }
1705
+ };
1706
+
1707
+ // src/scope/scope.ts
1708
+ var Scope = class {
1709
+ constructor(options) {
1710
+ /**
1711
+ * 数据缓存
1712
+ */
1713
+ this.memo = createMemo();
1714
+ this.toDispose = new DisposableCollection4();
1715
+ this.onDispose = this.toDispose.onDispose;
1716
+ this.id = options.id;
1717
+ this.meta = options.meta || {};
1718
+ this.variableEngine = options.variableEngine;
1719
+ this.event = new ScopeEventData(this);
1720
+ this.ast = this.variableEngine.astRegisters.createAST(
1721
+ {
1722
+ kind: "MapNode" /* MapNode */,
1723
+ key: this.id
1724
+ },
1725
+ {
1726
+ scope: this
1727
+ }
1728
+ );
1729
+ this.output = new ScopeOutputData(this);
1730
+ this.available = new ScopeAvailableData(this);
1731
+ }
1732
+ refreshCovers() {
1733
+ this.memo.clear("covers");
1734
+ }
1735
+ refreshDeps() {
1736
+ this.memo.clear("deps");
1737
+ this.available.refresh();
1738
+ }
1739
+ get depScopes() {
1740
+ return this.memo(
1741
+ "deps",
1742
+ () => this.variableEngine.chain.getDeps(this).filter((_scope) => Boolean(_scope) && !_scope?.disposed)
1743
+ );
1744
+ }
1745
+ get coverScopes() {
1746
+ return this.memo(
1747
+ "covers",
1748
+ () => this.variableEngine.chain.getCovers(this).filter((_scope) => Boolean(_scope) && !_scope?.disposed)
1749
+ );
1750
+ }
1751
+ dispose() {
1752
+ this.ast.dispose();
1753
+ this.toDispose.dispose();
1754
+ this.coverScopes.forEach((_scope) => _scope.refreshDeps());
1755
+ this.depScopes.forEach((_scope) => _scope.refreshCovers());
1756
+ }
1757
+ get disposed() {
1758
+ return this.toDispose.disposed;
1759
+ }
1760
+ };
1761
+
1762
+ // src/variable-engine.ts
1763
+ var VariableEngine = class {
1764
+ constructor(chain, astRegisters) {
1765
+ this.chain = chain;
1766
+ this.astRegisters = astRegisters;
1767
+ this.toDispose = new DisposableCollection5();
1768
+ this.memo = createMemo();
1769
+ this.scopeMap = /* @__PURE__ */ new Map();
1770
+ this.globalEvent$ = new Subject5();
1771
+ this.onScopeChangeEmitter = new Emitter3();
1772
+ this.globalVariableTable = new VariableTable();
1773
+ this.onScopeChange = this.onScopeChangeEmitter.event;
1774
+ this.toDispose.pushAll([
1775
+ chain,
1776
+ Disposable6.create(() => {
1777
+ this.getAllScopes().forEach((scope) => scope.dispose());
1778
+ this.globalVariableTable.dispose();
1779
+ })
1780
+ ]);
1781
+ }
1782
+ get container() {
1783
+ return this.containerProvider();
1784
+ }
1785
+ dispose() {
1786
+ this.toDispose.dispose();
1787
+ }
1788
+ // 根据 scopeId 找到作用域
1789
+ getScopeById(scopeId) {
1790
+ return this.scopeMap.get(scopeId);
1791
+ }
1792
+ // 移除作用域
1793
+ removeScopeById(scopeId) {
1794
+ this.getScopeById(scopeId)?.dispose();
1795
+ }
1796
+ // 获取 Scope,如果 Scope 存在且类型相同,则会直接使用
1797
+ createScope(id, meta) {
1798
+ let scope = this.getScopeById(id);
1799
+ if (!scope) {
1800
+ scope = new Scope({ variableEngine: this, meta, id });
1801
+ this.scopeMap.set(id, scope);
1802
+ this.onScopeChangeEmitter.fire({ type: "add", scope });
1803
+ scope.toDispose.pushAll([
1804
+ scope.ast.subscribe(() => {
1805
+ this.onScopeChangeEmitter.fire({ type: "update", scope });
1806
+ }),
1807
+ // 可用变量发生变化
1808
+ scope.available.onDataChange(() => {
1809
+ this.onScopeChangeEmitter.fire({ type: "available", scope });
1810
+ })
1811
+ ]);
1812
+ scope.onDispose(() => {
1813
+ this.scopeMap.delete(id);
1814
+ this.onScopeChangeEmitter.fire({ type: "delete", scope });
1815
+ });
1816
+ }
1817
+ return scope;
1818
+ }
1819
+ // 获取系统中所有的作用域
1820
+ getAllScopes({
1821
+ sort
1822
+ } = {}) {
1823
+ const allScopes = Array.from(this.scopeMap.values());
1824
+ if (sort) {
1825
+ const sortScopes = this.chain.sortAll();
1826
+ const remainScopes = new Set(allScopes);
1827
+ sortScopes.forEach((_scope) => remainScopes.delete(_scope));
1828
+ return [...sortScopes, ...Array.from(remainScopes)];
1829
+ }
1830
+ return [...allScopes];
1831
+ }
1832
+ fireGlobalEvent(event) {
1833
+ this.globalEvent$.next(event);
1834
+ }
1835
+ onGlobalEvent(type, observer) {
1836
+ return subsToDisposable(
1837
+ this.globalEvent$.subscribe((_action) => {
1838
+ if (_action.type === type) {
1839
+ observer(_action);
1840
+ }
1841
+ })
1842
+ );
1843
+ }
1844
+ };
1845
+ __decorateClass([
1846
+ inject2(ContainerProvider)
1847
+ ], VariableEngine.prototype, "containerProvider", 2);
1848
+ __decorateClass([
1849
+ preDestroy()
1850
+ ], VariableEngine.prototype, "dispose", 1);
1851
+ VariableEngine = __decorateClass([
1852
+ injectable3(),
1853
+ __decorateParam(0, inject2(ScopeChain)),
1854
+ __decorateParam(1, inject2(ASTRegisters))
1855
+ ], VariableEngine);
1856
+
1857
+ // src/services/variable-field-key-rename-service.ts
1858
+ import { difference } from "lodash";
1859
+ import { inject as inject3, injectable as injectable4, postConstruct, preDestroy as preDestroy2 } from "inversify";
1860
+ import { DisposableCollection as DisposableCollection6, Emitter as Emitter4 } from "@flowgram.ai/utils";
1861
+ var VariableFieldKeyRenameService = class {
1862
+ constructor() {
1863
+ this.toDispose = new DisposableCollection6();
1864
+ this.renameEmitter = new Emitter4();
1865
+ // 没有被 rename 的字段通过 disposeInList 透出,让业务区分变量是 rename 删除的,还是真正从列表中删除的
1866
+ this.disposeInListEmitter = new Emitter4();
1867
+ this.onRename = this.renameEmitter.event;
1868
+ this.onDisposeInList = this.disposeInListEmitter.event;
1869
+ }
1870
+ handleFieldListChange(ast, prev, next) {
1871
+ if (!ast || !prev?.length || !next?.length) {
1872
+ this.notifyFieldsDispose(prev, next);
1873
+ return;
1874
+ }
1875
+ if (prev.length !== next.length) {
1876
+ this.notifyFieldsDispose(prev, next);
1877
+ return;
1878
+ }
1879
+ let renameNodeInfo = null;
1880
+ let existFieldChanged = false;
1881
+ for (const [index, prevField] of prev.entries()) {
1882
+ const nextField = next[index];
1883
+ if (prevField.key !== nextField.key) {
1884
+ if (existFieldChanged) {
1885
+ this.notifyFieldsDispose(prev, next);
1886
+ return;
1887
+ }
1888
+ existFieldChanged = true;
1889
+ if (prevField.type?.kind === nextField.type?.kind) {
1890
+ renameNodeInfo = { before: prevField, after: nextField };
1891
+ }
1892
+ }
1893
+ }
1894
+ if (!renameNodeInfo) {
1895
+ this.notifyFieldsDispose(prev, next);
1896
+ return;
1897
+ }
1898
+ this.renameEmitter.fire(renameNodeInfo);
1899
+ }
1900
+ notifyFieldsDispose(prev, next) {
1901
+ const removedFields = difference(prev || [], next || []);
1902
+ removedFields.forEach((_field) => this.disposeInListEmitter.fire(_field));
1903
+ }
1904
+ init() {
1905
+ this.toDispose.pushAll([
1906
+ this.variableEngine.onGlobalEvent(
1907
+ "VariableListChange",
1908
+ (_action) => {
1909
+ this.handleFieldListChange(_action.ast, _action.payload?.prev, _action.payload?.next);
1910
+ }
1911
+ ),
1912
+ this.variableEngine.onGlobalEvent(
1913
+ "ObjectPropertiesChange",
1914
+ (_action) => {
1915
+ this.handleFieldListChange(_action.ast, _action.payload?.prev, _action.payload?.next);
1916
+ }
1917
+ )
1918
+ ]);
1919
+ }
1920
+ dispose() {
1921
+ this.toDispose.dispose();
1922
+ }
1923
+ };
1924
+ __decorateClass([
1925
+ inject3(VariableEngine)
1926
+ ], VariableFieldKeyRenameService.prototype, "variableEngine", 2);
1927
+ __decorateClass([
1928
+ postConstruct()
1929
+ ], VariableFieldKeyRenameService.prototype, "init", 1);
1930
+ __decorateClass([
1931
+ preDestroy2()
1932
+ ], VariableFieldKeyRenameService.prototype, "dispose", 1);
1933
+ VariableFieldKeyRenameService = __decorateClass([
1934
+ injectable4()
1935
+ ], VariableFieldKeyRenameService);
1936
+
1937
+ // src/variable-container-module.ts
1938
+ var VariableContainerModule = new ContainerModule((bind) => {
1939
+ bind(VariableEngine).toSelf().inSingletonScope();
1940
+ bind(ASTRegisters).toSelf().inSingletonScope();
1941
+ bind(VariableFieldKeyRenameService).toSelf().inSingletonScope();
1942
+ bind(VariableEngineProvider).toDynamicValue((ctx) => () => ctx.container.get(VariableEngine));
1943
+ bind(ContainerProvider).toDynamicValue((ctx) => () => ctx.container);
1944
+ });
1945
+
1946
+ // src/react/context.tsx
1947
+ import { createContext, useContext } from "react";
1948
+ var ScopeContext = createContext(null);
1949
+ var ScopeProvider = ScopeContext.Provider;
1950
+ var useScopeContext = () => useContext(ScopeContext);
1951
+ var useCurrentScope = () => useContext(ScopeContext)?.scope;
1952
+
1953
+ // src/react/hooks/useScopeAvailable.ts
1954
+ import { useEffect } from "react";
1955
+ import { useRefresh } from "@flowgram.ai/core";
1956
+ function useScopeAvailable() {
1957
+ const scope = useCurrentScope();
1958
+ const refresh = useRefresh();
1959
+ useEffect(() => {
1960
+ const disposable = scope.available.onDataChange(() => {
1961
+ refresh();
1962
+ });
1963
+ return () => disposable.dispose();
1964
+ }, []);
1965
+ return scope.available;
1966
+ }
1967
+
1968
+ // src/react/hooks/useAvailableVariables.ts
1969
+ import { useEffect as useEffect2 } from "react";
1970
+ import { useRefresh as useRefresh2, useService } from "@flowgram.ai/core";
1971
+ function useAvailableVariables() {
1972
+ const scope = useCurrentScope();
1973
+ const variableEngine = useService(VariableEngine);
1974
+ const refresh = useRefresh2();
1975
+ useEffect2(() => {
1976
+ if (!scope) {
1977
+ const disposable2 = variableEngine.globalVariableTable.onAnyChange(() => {
1978
+ refresh();
1979
+ });
1980
+ return () => disposable2.dispose();
1981
+ }
1982
+ const disposable = scope.available.onDataChange(() => {
1983
+ refresh();
1984
+ });
1985
+ return () => disposable.dispose();
1986
+ }, []);
1987
+ return scope ? scope.available.variables : variableEngine.globalVariableTable.variables;
1988
+ }
1989
+ export {
1990
+ ASTFactory,
1991
+ ASTKind,
1992
+ ASTNode,
1993
+ ASTNodeFlags,
1994
+ ASTRegisters,
1995
+ ArrayType,
1996
+ BaseExpression,
1997
+ BaseType,
1998
+ BaseVariableField,
1999
+ BooleanType,
2000
+ DataNode,
2001
+ EnumerateExpression,
2002
+ ExpressionList,
2003
+ IntegerType,
2004
+ KeyPathExpression,
2005
+ KeyPathExpressionV2,
2006
+ ListNode,
2007
+ MapNode,
2008
+ MapType,
2009
+ NumberType,
2010
+ ObjectType,
2011
+ Property,
2012
+ Scope,
2013
+ ScopeChain,
2014
+ ScopeOutputData,
2015
+ ScopeProvider,
2016
+ StringType,
2017
+ VariableContainerModule,
2018
+ VariableDeclaration,
2019
+ VariableDeclarationList,
2020
+ VariableEngine,
2021
+ VariableEngineProvider,
2022
+ VariableFieldKeyRenameService,
2023
+ VariableTable,
2024
+ injectToAST,
2025
+ isMatchAST,
2026
+ postConstructAST,
2027
+ useAvailableVariables,
2028
+ useCurrentScope,
2029
+ useScopeAvailable,
2030
+ useScopeContext
2031
+ };
2032
+ //# sourceMappingURL=index.js.map