@flowgram.ai/variable-core 0.1.0-alpha.7 → 0.1.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -43,7 +43,6 @@ __export(src_exports, {
43
43
  CustomType: () => CustomType,
44
44
  DataNode: () => DataNode,
45
45
  EnumerateExpression: () => EnumerateExpression,
46
- ExpressionList: () => ExpressionList,
47
46
  IntegerType: () => IntegerType,
48
47
  KeyPathExpression: () => KeyPathExpression,
49
48
  KeyPathExpressionV2: () => KeyPathExpressionV2,
@@ -64,7 +63,7 @@ __export(src_exports, {
64
63
  VariableEngine: () => VariableEngine,
65
64
  VariableEngineProvider: () => VariableEngineProvider,
66
65
  VariableFieldKeyRenameService: () => VariableFieldKeyRenameService,
67
- VariableTable: () => VariableTable,
66
+ WrapArrayExpression: () => WrapArrayExpression,
68
67
  injectToAST: () => injectToAST,
69
68
  isMatchAST: () => isMatchAST,
70
69
  postConstructAST: () => postConstructAST,
@@ -76,13 +75,13 @@ __export(src_exports, {
76
75
  module.exports = __toCommonJS(src_exports);
77
76
 
78
77
  // src/variable-container-module.ts
79
- var import_inversify7 = require("inversify");
78
+ var import_inversify8 = require("inversify");
80
79
 
81
80
  // src/variable-engine.ts
82
81
  var import_rxjs6 = require("rxjs");
83
- var import_inversify5 = require("inversify");
84
- var import_utils10 = require("@flowgram.ai/utils");
85
- var import_utils11 = require("@flowgram.ai/utils");
82
+ var import_inversify6 = require("inversify");
83
+ var import_utils8 = require("@flowgram.ai/utils");
84
+ var import_utils9 = require("@flowgram.ai/utils");
86
85
 
87
86
  // src/utils/toDisposable.tsx
88
87
  var import_utils = require("@flowgram.ai/utils");
@@ -112,9 +111,117 @@ var createMemo = () => {
112
111
  return memo;
113
112
  };
114
113
 
114
+ // src/scope/variable-table.ts
115
+ var import_rxjs = require("rxjs");
116
+ var import_utils2 = require("@flowgram.ai/utils");
117
+ var VariableTable = class {
118
+ constructor(parentTable) {
119
+ this.parentTable = parentTable;
120
+ this.table = /* @__PURE__ */ new Map();
121
+ this.onDataChangeEmitter = new import_utils2.Emitter();
122
+ this.variables$ = new import_rxjs.Subject();
123
+ // 监听变量列表中的单个变量变化
124
+ this.anyVariableChange$ = this.variables$.pipe(
125
+ (0, import_rxjs.switchMap)(
126
+ (_variables) => (0, import_rxjs.merge)(
127
+ ..._variables.map(
128
+ (_v) => _v.value$.pipe(
129
+ // 跳过 BehaviorSubject 第一个
130
+ (0, import_rxjs.skip)(1)
131
+ )
132
+ )
133
+ )
134
+ ),
135
+ (0, import_rxjs.share)()
136
+ );
137
+ this.onDataChange = this.onDataChangeEmitter.event;
138
+ this._version = 0;
139
+ }
140
+ /**
141
+ * 监听任意变量变化
142
+ * @param observer 监听器,变量变化时会吐出值
143
+ * @returns
144
+ */
145
+ onAnyVariableChange(observer) {
146
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
147
+ }
148
+ /**
149
+ * 列表或者任意变量变化
150
+ * @param observer
151
+ */
152
+ onAnyChange(observer) {
153
+ const disposables = new import_utils2.DisposableCollection();
154
+ disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
155
+ return disposables;
156
+ }
157
+ fireChange() {
158
+ this._version++;
159
+ this.onDataChangeEmitter.fire();
160
+ this.parentTable?.fireChange();
161
+ }
162
+ get version() {
163
+ return this._version;
164
+ }
165
+ get variables() {
166
+ return Array.from(this.table.values());
167
+ }
168
+ get variableKeys() {
169
+ return Array.from(this.table.keys());
170
+ }
171
+ /**
172
+ * 根据 keyPath 找到对应的变量,或 Property 节点
173
+ * @param keyPath
174
+ * @returns
175
+ */
176
+ getByKeyPath(keyPath) {
177
+ const [variableKey, ...propertyKeys] = keyPath || [];
178
+ if (!variableKey) {
179
+ return;
180
+ }
181
+ const variable = this.getVariableByKey(variableKey);
182
+ return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
183
+ }
184
+ /**
185
+ * 根据 key 值找到相应的变量
186
+ * @param key
187
+ * @returns
188
+ */
189
+ getVariableByKey(key) {
190
+ return this.table.get(key);
191
+ }
192
+ /**
193
+ * 往 variableTable 添加输出变量
194
+ * @param variable
195
+ */
196
+ addVariableToTable(variable) {
197
+ this.table.set(variable.key, variable);
198
+ if (this.parentTable) {
199
+ this.parentTable.addVariableToTable(variable);
200
+ }
201
+ this.variables$.next(this.variables);
202
+ }
203
+ /**
204
+ * 从 variableTable 中移除变量
205
+ * @param key
206
+ */
207
+ removeVariableFromTable(key) {
208
+ this.table.delete(key);
209
+ if (this.parentTable) {
210
+ this.parentTable.removeVariableFromTable(key);
211
+ }
212
+ this.variables$.next(this.variables);
213
+ }
214
+ dispose() {
215
+ this.variableKeys.forEach(
216
+ (_key) => this.parentTable?.removeVariableFromTable(_key)
217
+ );
218
+ this.onDataChangeEmitter.dispose();
219
+ }
220
+ };
221
+
115
222
  // src/scope/scope-chain.ts
116
223
  var import_inversify = require("inversify");
117
- var import_utils2 = require("@flowgram.ai/utils");
224
+ var import_utils3 = require("@flowgram.ai/utils");
118
225
 
119
226
  // src/providers.ts
120
227
  var VariableEngineProvider = Symbol("DynamicVariableEngine");
@@ -123,7 +230,7 @@ var ContainerProvider = Symbol("ContainerProvider");
123
230
  // src/scope/scope-chain.ts
124
231
  var ScopeChain = class {
125
232
  constructor() {
126
- this.toDispose = new import_utils2.DisposableCollection();
233
+ this.toDispose = new import_utils3.DisposableCollection();
127
234
  }
128
235
  get variableEngine() {
129
236
  return this.variableEngineProvider();
@@ -155,7 +262,7 @@ ScopeChain = __decorateClass([
155
262
  ], ScopeChain);
156
263
 
157
264
  // src/scope/scope.ts
158
- var import_utils9 = require("@flowgram.ai/utils");
265
+ var import_utils7 = require("@flowgram.ai/utils");
159
266
 
160
267
  // src/ast/types.ts
161
268
  var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
@@ -174,7 +281,7 @@ var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
174
281
  ASTKind2["VariableDeclarationList"] = "VariableDeclarationList";
175
282
  ASTKind2["KeyPathExpression"] = "KeyPathExpression";
176
283
  ASTKind2["EnumerateExpression"] = "EnumerateExpression";
177
- ASTKind2["ExpressionList"] = "ExpressionList";
284
+ ASTKind2["WrapArrayExpression"] = "WrapArrayExpression";
178
285
  ASTKind2["ListNode"] = "ListNode";
179
286
  ASTKind2["DataNode"] = "DataNode";
180
287
  ASTKind2["MapNode"] = "MapNode";
@@ -183,7 +290,7 @@ var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
183
290
 
184
291
  // src/ast/ast-registers.ts
185
292
  var import_lodash3 = require("lodash");
186
- var import_inversify2 = require("inversify");
293
+ var import_inversify3 = require("inversify");
187
294
 
188
295
  // src/ast/utils/inversify.ts
189
296
  var injectToAST = (serviceIdentifier) => function(target, propertyKey) {
@@ -287,10 +394,10 @@ var ASTNodeFlags = /* @__PURE__ */ ((ASTNodeFlags2) => {
287
394
  })(ASTNodeFlags || {});
288
395
 
289
396
  // src/ast/ast-node.ts
290
- var import_rxjs = require("rxjs");
397
+ var import_rxjs2 = require("rxjs");
291
398
  var import_nanoid = require("nanoid");
292
399
  var import_fast_equals = require("fast-equals");
293
- var import_utils3 = require("@flowgram.ai/utils");
400
+ var import_utils4 = require("@flowgram.ai/utils");
294
401
  var ASTNode = class _ASTNode {
295
402
  /**
296
403
  * 构造函数
@@ -321,7 +428,7 @@ var ASTNode = class _ASTNode {
321
428
  * AST 节点变化事件,基于 Rxjs 实现
322
429
  * - 使用了 BehaviorSubject, 在订阅时会自动触发一次事件,事件为当前值
323
430
  */
324
- this.value$ = new import_rxjs.BehaviorSubject(this);
431
+ this.value$ = new import_rxjs2.BehaviorSubject(this);
325
432
  /**
326
433
  * 子节点
327
434
  */
@@ -329,8 +436,8 @@ var ASTNode = class _ASTNode {
329
436
  /**
330
437
  * 删除节点处理事件列表
331
438
  */
332
- this.toDispose = new import_utils3.DisposableCollection(
333
- import_utils3.Disposable.create(() => {
439
+ this.toDispose = new import_utils4.DisposableCollection(
440
+ import_utils4.Disposable.create(() => {
334
441
  this.parent?.fireChange();
335
442
  this.children.forEach((child) => child.dispose());
336
443
  })
@@ -384,7 +491,7 @@ var ASTNode = class _ASTNode {
384
491
  });
385
492
  this._children.add(child);
386
493
  child.toDispose.push(
387
- import_utils3.Disposable.create(() => {
494
+ import_utils4.Disposable.create(() => {
388
495
  this._children.delete(child);
389
496
  })
390
497
  );
@@ -461,8 +568,8 @@ var ASTNode = class _ASTNode {
461
568
  subscribe(observer, { selector, debounceAnimation, triggerOnInit } = {}) {
462
569
  return subsToDisposable(
463
570
  this.value$.pipe(
464
- (0, import_rxjs.map)(() => selector ? selector(this) : this),
465
- (0, import_rxjs.distinctUntilChanged)(
571
+ (0, import_rxjs2.map)(() => selector ? selector(this) : this),
572
+ (0, import_rxjs2.distinctUntilChanged)(
466
573
  (a, b) => (0, import_fast_equals.shallowEqual)(a, b),
467
574
  (value) => {
468
575
  if (value instanceof _ASTNode) {
@@ -472,9 +579,9 @@ var ASTNode = class _ASTNode {
472
579
  }
473
580
  ),
474
581
  // 默认跳过 BehaviorSubject 第一次触发
475
- triggerOnInit ? (0, import_rxjs.tap)(() => null) : (0, import_rxjs.skip)(1),
582
+ triggerOnInit ? (0, import_rxjs2.tap)(() => null) : (0, import_rxjs2.skip)(1),
476
583
  // 每个 animationFrame 内所有更新合并成一个
477
- debounceAnimation ? (0, import_rxjs.debounceTime)(0, import_rxjs.animationFrameScheduler) : (0, import_rxjs.tap)(() => null)
584
+ debounceAnimation ? (0, import_rxjs2.debounceTime)(0, import_rxjs2.animationFrameScheduler) : (0, import_rxjs2.tap)(() => null)
478
585
  ).subscribe(observer)
479
586
  );
480
587
  }
@@ -508,7 +615,7 @@ var BaseType = class extends ASTNode {
508
615
  this.flags = 8 /* BasicType */;
509
616
  }
510
617
  /**
511
- * 类型是否一致,节点有额外信息判断,请参考 extraTypeInfoEqual
618
+ * 类型是否一致
512
619
  * @param targetTypeJSON
513
620
  */
514
621
  isTypeEqual(targetTypeJSONOrKind) {
@@ -527,6 +634,10 @@ var BaseType = class extends ASTNode {
527
634
  getByKeyPath(keyPath = []) {
528
635
  throw new Error(`Get By Key Path is not implemented for Type: ${this.kind}`);
529
636
  }
637
+ /**
638
+ * Get AST JSON for current base type
639
+ * @returns
640
+ */
530
641
  toJSON() {
531
642
  return {
532
643
  kind: this.kind
@@ -780,7 +891,7 @@ var CustomType = class extends BaseType {
780
891
  CustomType.kind = "CustomType" /* CustomType */;
781
892
 
782
893
  // src/ast/expression/base-expression.ts
783
- var import_rxjs2 = require("rxjs");
894
+ var import_rxjs3 = require("rxjs");
784
895
  var import_fast_equals2 = require("fast-equals");
785
896
 
786
897
  // src/ast/utils/variable-field.ts
@@ -805,22 +916,22 @@ var BaseExpression = class extends ASTNode {
805
916
  * 引用变量
806
917
  */
807
918
  this._refs = [];
808
- this.refreshRefs$ = new import_rxjs2.Subject();
919
+ this.refreshRefs$ = new import_rxjs3.Subject();
809
920
  /**
810
921
  * 监听引用变量变化
811
922
  * 监听 [a.b.c] -> [a.b]
812
923
  */
813
924
  this.refs$ = this.refreshRefs$.pipe(
814
- (0, import_rxjs2.map)(() => this.getRefFields()),
815
- (0, import_rxjs2.distinctUntilChanged)(import_fast_equals2.shallowEqual),
816
- (0, import_rxjs2.switchMap)(
817
- (refs) => !refs?.length ? (0, import_rxjs2.of)([]) : (0, import_rxjs2.combineLatest)(
925
+ (0, import_rxjs3.map)(() => this.getRefFields()),
926
+ (0, import_rxjs3.distinctUntilChanged)(import_fast_equals2.shallowEqual),
927
+ (0, import_rxjs3.switchMap)(
928
+ (refs) => !refs?.length ? (0, import_rxjs3.of)([]) : (0, import_rxjs3.combineLatest)(
818
929
  refs.map(
819
- (ref) => ref ? ref.value$ : (0, import_rxjs2.of)(void 0)
930
+ (ref) => ref ? ref.value$ : (0, import_rxjs3.of)(void 0)
820
931
  )
821
932
  )
822
933
  ),
823
- (0, import_rxjs2.share)()
934
+ (0, import_rxjs3.share)()
824
935
  );
825
936
  this.toDispose.push(
826
937
  subsToDisposable(
@@ -854,29 +965,6 @@ var BaseExpression = class extends ASTNode {
854
965
  }
855
966
  };
856
967
 
857
- // src/ast/expression/expression-list.ts
858
- var ExpressionList = class extends ASTNode {
859
- fromJSON({ expressions }) {
860
- this.expressions = expressions.map((_expression, idx) => {
861
- const prevExpression = this.expressions[idx];
862
- if (prevExpression.kind !== _expression.kind) {
863
- prevExpression.dispose();
864
- this.fireChange();
865
- return this.createChildNode(_expression);
866
- }
867
- prevExpression.fromJSON(_expression);
868
- return prevExpression;
869
- });
870
- }
871
- toJSON() {
872
- return {
873
- kind: "ExpressionList" /* ExpressionList */,
874
- properties: this.expressions.map((_expression) => _expression.toJSON())
875
- };
876
- }
877
- };
878
- ExpressionList.kind = "ExpressionList" /* ExpressionList */;
879
-
880
968
  // src/ast/expression/keypath-expression.ts
881
969
  var import_fast_equals3 = require("fast-equals");
882
970
  var KeyPathExpression = class extends BaseExpression {
@@ -1058,8 +1146,45 @@ var KeyPathExpressionV2 = class extends BaseExpression {
1058
1146
  };
1059
1147
  KeyPathExpressionV2.kind = "KeyPathExpression" /* KeyPathExpression */;
1060
1148
 
1061
- // src/ast/declaration/variable-declaration.ts
1062
- var import_utils4 = require("@flowgram.ai/utils");
1149
+ // src/ast/expression/wrap-array-expression.ts
1150
+ var WrapArrayExpression = class extends BaseExpression {
1151
+ get wrapFor() {
1152
+ return this._wrapFor;
1153
+ }
1154
+ get returnType() {
1155
+ return this._returnType;
1156
+ }
1157
+ refreshReturnType() {
1158
+ const childReturnTypeJSON = this.wrapFor?.returnType?.toJSON();
1159
+ this.updateChildNodeByKey("_returnType", {
1160
+ kind: "Array" /* Array */,
1161
+ items: childReturnTypeJSON
1162
+ });
1163
+ }
1164
+ getRefFields() {
1165
+ return [];
1166
+ }
1167
+ fromJSON({ wrapFor: expression }) {
1168
+ this.updateChildNodeByKey("_wrapFor", expression);
1169
+ }
1170
+ toJSON() {
1171
+ return {
1172
+ kind: "WrapArrayExpression" /* WrapArrayExpression */,
1173
+ wrapFor: this.wrapFor?.toJSON()
1174
+ };
1175
+ }
1176
+ init() {
1177
+ this.toDispose.push(
1178
+ this.subscribe(this.refreshReturnType, {
1179
+ selector: (curr) => curr.wrapFor?.returnType
1180
+ })
1181
+ );
1182
+ }
1183
+ };
1184
+ WrapArrayExpression.kind = "WrapArrayExpression" /* WrapArrayExpression */;
1185
+ __decorateClass([
1186
+ postConstructAST()
1187
+ ], WrapArrayExpression.prototype, "init", 1);
1063
1188
 
1064
1189
  // src/ast/declaration/base-variable-field.ts
1065
1190
  var import_fast_equals5 = require("fast-equals");
@@ -1144,13 +1269,6 @@ var VariableDeclaration = class extends BaseVariableField {
1144
1269
  constructor(params) {
1145
1270
  super(params);
1146
1271
  this._order = 0;
1147
- this.scope.output.addVariableToTable(this);
1148
- this.toDispose.push(
1149
- import_utils4.Disposable.create(() => {
1150
- this.scope.output.setHasChanges();
1151
- this.scope.output.removeVariableFromTable(this.key);
1152
- })
1153
- );
1154
1272
  }
1155
1273
  get order() {
1156
1274
  return this._order;
@@ -1165,7 +1283,9 @@ var VariableDeclaration = class extends BaseVariableField {
1165
1283
  updateOrder(order = 0) {
1166
1284
  if (order !== this._order) {
1167
1285
  this._order = order;
1168
- this.scope.output.setHasChanges();
1286
+ this.dispatchGlobalEvent({
1287
+ type: "ReSortVariableDeclarations"
1288
+ });
1169
1289
  this.fireChange();
1170
1290
  }
1171
1291
  }
@@ -1370,9 +1490,9 @@ var ASTRegisters = class {
1370
1490
  this.registerAST(Property);
1371
1491
  this.registerAST(VariableDeclaration);
1372
1492
  this.registerAST(VariableDeclarationList);
1373
- this.registerAST(KeyPathExpression);
1493
+ this.registerAST(KeyPathExpressionV2);
1374
1494
  this.registerAST(EnumerateExpression);
1375
- this.registerAST(ExpressionList);
1495
+ this.registerAST(WrapArrayExpression);
1376
1496
  this.registerAST(MapNode);
1377
1497
  this.registerAST(DataNode);
1378
1498
  }
@@ -1425,7 +1545,7 @@ var ASTRegisters = class {
1425
1545
  }
1426
1546
  };
1427
1547
  ASTRegisters = __decorateClass([
1428
- (0, import_inversify2.injectable)()
1548
+ (0, import_inversify3.injectable)()
1429
1549
  ], ASTRegisters);
1430
1550
 
1431
1551
  // src/ast/factory.ts
@@ -1475,116 +1595,13 @@ var ASTFactory;
1475
1595
  kind: "KeyPathExpression" /* KeyPathExpression */,
1476
1596
  ...json
1477
1597
  });
1598
+ ASTFactory2.createWrapArrayExpression = (json) => ({
1599
+ kind: "WrapArrayExpression" /* WrapArrayExpression */,
1600
+ ...json
1601
+ });
1478
1602
  ASTFactory2.create = (targetType, json) => ({ kind: targetType.kind, ...json });
1479
1603
  })(ASTFactory || (ASTFactory = {}));
1480
1604
 
1481
- // src/scope/variable-table.ts
1482
- var import_rxjs3 = require("rxjs");
1483
- var import_utils5 = require("@flowgram.ai/utils");
1484
- var import_utils6 = require("@flowgram.ai/utils");
1485
- var VariableTable = class {
1486
- constructor(parentTable) {
1487
- this.parentTable = parentTable;
1488
- this.table = /* @__PURE__ */ new Map();
1489
- this.onDataChangeEmitter = new import_utils5.Emitter();
1490
- this.variables$ = new import_rxjs3.Subject();
1491
- // 监听变量列表中的单个变量变化
1492
- this.anyVariableChange$ = this.variables$.pipe(
1493
- (0, import_rxjs3.switchMap)(
1494
- (_variables) => (0, import_rxjs3.merge)(
1495
- ..._variables.map(
1496
- (_v) => _v.value$.pipe(
1497
- // 跳过 BehaviorSubject 第一个
1498
- (0, import_rxjs3.skip)(1)
1499
- )
1500
- )
1501
- )
1502
- ),
1503
- (0, import_rxjs3.share)()
1504
- );
1505
- this.onDataChange = this.onDataChangeEmitter.event;
1506
- this._version = 0;
1507
- }
1508
- /**
1509
- * 监听任意变量变化
1510
- * @param observer 监听器,变量变化时会吐出值
1511
- * @returns
1512
- */
1513
- onAnyVariableChange(observer) {
1514
- return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1515
- }
1516
- /**
1517
- * 列表或者任意变量变化
1518
- * @param observer
1519
- */
1520
- onAnyChange(observer) {
1521
- const disposables = new import_utils6.DisposableCollection();
1522
- disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
1523
- return disposables;
1524
- }
1525
- fireChange() {
1526
- this._version++;
1527
- this.onDataChangeEmitter.fire();
1528
- this.parentTable?.fireChange();
1529
- }
1530
- get version() {
1531
- return this._version;
1532
- }
1533
- get variables() {
1534
- return Array.from(this.table.values());
1535
- }
1536
- get variableKeys() {
1537
- return Array.from(this.table.keys());
1538
- }
1539
- /**
1540
- * 根据 keyPath 找到对应的变量,或 Property 节点
1541
- * @param keyPath
1542
- * @returns
1543
- */
1544
- getByKeyPath(keyPath) {
1545
- const [variableKey, ...propertyKeys] = keyPath || [];
1546
- if (!variableKey) {
1547
- return;
1548
- }
1549
- const variable = this.getVariableByKey(variableKey);
1550
- return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
1551
- }
1552
- /**
1553
- * 根据 key 值找到相应的变量
1554
- * @param key
1555
- * @returns
1556
- */
1557
- getVariableByKey(key) {
1558
- return this.table.get(key);
1559
- }
1560
- /**
1561
- * 往 variableTable 添加输出变量
1562
- * @param variable
1563
- */
1564
- addVariableToTable(variable) {
1565
- this.table.set(variable.key, variable);
1566
- if (this.parentTable) {
1567
- this.parentTable.addVariableToTable(variable);
1568
- }
1569
- this.variables$.next(this.variables);
1570
- }
1571
- /**
1572
- * 从 variableTable 中移除变量
1573
- * @param key
1574
- */
1575
- removeVariableFromTable(key) {
1576
- this.table.delete(key);
1577
- if (this.parentTable) {
1578
- this.parentTable.removeVariableFromTable(key);
1579
- }
1580
- this.variables$.next(this.variables);
1581
- }
1582
- dispose() {
1583
- this.variableKeys.forEach((_key) => this.parentTable?.removeVariableFromTable(_key));
1584
- this.onDataChangeEmitter.dispose();
1585
- }
1586
- };
1587
-
1588
1605
  // src/scope/datas/scope-output-data.ts
1589
1606
  var ScopeOutputData = class {
1590
1607
  constructor(scope) {
@@ -1593,7 +1610,7 @@ var ScopeOutputData = class {
1593
1610
  this._hasChanges = false;
1594
1611
  this.variableTable = new VariableTable(scope.variableEngine.globalVariableTable);
1595
1612
  this.scope.toDispose.pushAll([
1596
- // AST 根节点更新时,检查在这次 AST 变化期间节点是否有变化
1613
+ // When root AST node is updated, check if there are any changes during this AST change
1597
1614
  this.scope.ast.subscribe(() => {
1598
1615
  if (this._hasChanges) {
1599
1616
  this.memo.clear();
@@ -1602,6 +1619,19 @@ var ScopeOutputData = class {
1602
1619
  this._hasChanges = false;
1603
1620
  }
1604
1621
  }),
1622
+ this.scope.event.on("DisposeAST", (_action) => {
1623
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1624
+ this.removeVariableFromTable(_action.ast.key);
1625
+ }
1626
+ }),
1627
+ this.scope.event.on("NewAST", (_action) => {
1628
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1629
+ this.addVariableToTable(_action.ast);
1630
+ }
1631
+ }),
1632
+ this.scope.event.on("ReSortVariableDeclarations", () => {
1633
+ this._hasChanges = true;
1634
+ }),
1605
1635
  this.variableTable
1606
1636
  ]);
1607
1637
  }
@@ -1618,7 +1648,7 @@ var ScopeOutputData = class {
1618
1648
  return this.variableTable.onAnyVariableChange.bind(this.variableTable);
1619
1649
  }
1620
1650
  /**
1621
- * 作用域输出变量
1651
+ * Scope Output Variable Declarations
1622
1652
  */
1623
1653
  get variables() {
1624
1654
  return this.memo(
@@ -1627,7 +1657,7 @@ var ScopeOutputData = class {
1627
1657
  );
1628
1658
  }
1629
1659
  /**
1630
- * 输出的变量 keys
1660
+ * Output Variable Keys
1631
1661
  */
1632
1662
  get variableKeys() {
1633
1663
  return this.memo("variableKeys", () => this.variableTable.variableKeys);
@@ -1639,10 +1669,6 @@ var ScopeOutputData = class {
1639
1669
  this.variableTable.addVariableToTable(variable);
1640
1670
  this._hasChanges = true;
1641
1671
  }
1642
- // 标记为发生了变化,用于变量排序变化的场景
1643
- setHasChanges() {
1644
- this._hasChanges = true;
1645
- }
1646
1672
  removeVariableFromTable(key) {
1647
1673
  this.variableTable.removeVariableFromTable(key);
1648
1674
  this._hasChanges = true;
@@ -1650,7 +1676,9 @@ var ScopeOutputData = class {
1650
1676
  getVariableByKey(key) {
1651
1677
  return this.variableTable.getVariableByKey(key);
1652
1678
  }
1653
- // 通知覆盖作用域更新可用变量
1679
+ /**
1680
+ *
1681
+ */
1654
1682
  notifyCoversChange() {
1655
1683
  this.scope.coverScopes.forEach((scope) => scope.available.refresh());
1656
1684
  }
@@ -1660,8 +1688,8 @@ var ScopeOutputData = class {
1660
1688
  var import_rxjs4 = require("rxjs");
1661
1689
  var import_lodash4 = require("lodash");
1662
1690
  var import_fast_equals7 = require("fast-equals");
1663
- var import_utils7 = require("@flowgram.ai/utils");
1664
- var import_utils8 = require("@flowgram.ai/utils");
1691
+ var import_utils5 = require("@flowgram.ai/utils");
1692
+ var import_utils6 = require("@flowgram.ai/utils");
1665
1693
  var ScopeAvailableData = class {
1666
1694
  constructor(scope) {
1667
1695
  this.scope = scope;
@@ -1692,7 +1720,7 @@ var ScopeAvailableData = class {
1692
1720
  ),
1693
1721
  (0, import_rxjs4.share)()
1694
1722
  );
1695
- this.onDataChangeEmitter = new import_utils8.Emitter();
1723
+ this.onDataChangeEmitter = new import_utils6.Emitter();
1696
1724
  /**
1697
1725
  * 监听变量列表变化 + 任意子变量变化
1698
1726
  */
@@ -1706,7 +1734,7 @@ var ScopeAvailableData = class {
1706
1734
  this.onAnyVariableChange(() => {
1707
1735
  this.onDataChangeEmitter.fire(this._variables);
1708
1736
  }),
1709
- import_utils7.Disposable.create(() => {
1737
+ import_utils5.Disposable.create(() => {
1710
1738
  this.refresh$.complete();
1711
1739
  this.refresh$.unsubscribe();
1712
1740
  })
@@ -1804,7 +1832,7 @@ var Scope = class {
1804
1832
  * 数据缓存
1805
1833
  */
1806
1834
  this.memo = createMemo();
1807
- this.toDispose = new import_utils9.DisposableCollection();
1835
+ this.toDispose = new import_utils7.DisposableCollection();
1808
1836
  this.onDispose = this.toDispose.onDispose;
1809
1837
  this.id = options.id;
1810
1838
  this.meta = options.meta || {};
@@ -1813,7 +1841,7 @@ var Scope = class {
1813
1841
  this.ast = this.variableEngine.astRegisters.createAST(
1814
1842
  {
1815
1843
  kind: "MapNode" /* MapNode */,
1816
- key: this.id
1844
+ key: String(this.id)
1817
1845
  },
1818
1846
  {
1819
1847
  scope: this
@@ -1857,16 +1885,16 @@ var VariableEngine = class {
1857
1885
  constructor(chain, astRegisters) {
1858
1886
  this.chain = chain;
1859
1887
  this.astRegisters = astRegisters;
1860
- this.toDispose = new import_utils10.DisposableCollection();
1888
+ this.toDispose = new import_utils8.DisposableCollection();
1861
1889
  this.memo = createMemo();
1862
1890
  this.scopeMap = /* @__PURE__ */ new Map();
1863
1891
  this.globalEvent$ = new import_rxjs6.Subject();
1864
- this.onScopeChangeEmitter = new import_utils11.Emitter();
1892
+ this.onScopeChangeEmitter = new import_utils9.Emitter();
1865
1893
  this.globalVariableTable = new VariableTable();
1866
1894
  this.onScopeChange = this.onScopeChangeEmitter.event;
1867
1895
  this.toDispose.pushAll([
1868
1896
  chain,
1869
- import_utils10.Disposable.create(() => {
1897
+ import_utils8.Disposable.create(() => {
1870
1898
  this.getAllScopes().forEach((scope) => scope.dispose());
1871
1899
  this.globalVariableTable.dispose();
1872
1900
  })
@@ -1886,11 +1914,18 @@ var VariableEngine = class {
1886
1914
  removeScopeById(scopeId) {
1887
1915
  this.getScopeById(scopeId)?.dispose();
1888
1916
  }
1889
- // 获取 Scope,如果 Scope 存在且类型相同,则会直接使用
1890
- createScope(id, meta) {
1917
+ /**
1918
+ * Get Scope, if Scope exists and type is same, will use it directly
1919
+ * @param id scope id
1920
+ * @param meta scope meta, defined by user
1921
+ * @param ScopeConstructor scope constructor, default is Scope. you can extends Scope to create your own scope
1922
+ * @returns
1923
+ */
1924
+ createScope(id, meta, options = {}) {
1925
+ const { ScopeConstructor = Scope } = options;
1891
1926
  let scope = this.getScopeById(id);
1892
1927
  if (!scope) {
1893
- scope = new Scope({ variableEngine: this, meta, id });
1928
+ scope = new ScopeConstructor({ variableEngine: this, meta, id });
1894
1929
  this.scopeMap.set(id, scope);
1895
1930
  this.onScopeChangeEmitter.fire({ type: "add", scope });
1896
1931
  scope.toDispose.pushAll([
@@ -1936,27 +1971,27 @@ var VariableEngine = class {
1936
1971
  }
1937
1972
  };
1938
1973
  __decorateClass([
1939
- (0, import_inversify5.inject)(ContainerProvider)
1974
+ (0, import_inversify6.inject)(ContainerProvider)
1940
1975
  ], VariableEngine.prototype, "containerProvider", 2);
1941
1976
  __decorateClass([
1942
- (0, import_inversify5.preDestroy)()
1977
+ (0, import_inversify6.preDestroy)()
1943
1978
  ], VariableEngine.prototype, "dispose", 1);
1944
1979
  VariableEngine = __decorateClass([
1945
- (0, import_inversify5.injectable)(),
1946
- __decorateParam(0, (0, import_inversify5.inject)(ScopeChain)),
1947
- __decorateParam(1, (0, import_inversify5.inject)(ASTRegisters))
1980
+ (0, import_inversify6.injectable)(),
1981
+ __decorateParam(0, (0, import_inversify6.inject)(ScopeChain)),
1982
+ __decorateParam(1, (0, import_inversify6.inject)(ASTRegisters))
1948
1983
  ], VariableEngine);
1949
1984
 
1950
1985
  // src/services/variable-field-key-rename-service.ts
1951
1986
  var import_lodash5 = require("lodash");
1952
- var import_inversify6 = require("inversify");
1953
- var import_utils12 = require("@flowgram.ai/utils");
1987
+ var import_inversify7 = require("inversify");
1988
+ var import_utils10 = require("@flowgram.ai/utils");
1954
1989
  var VariableFieldKeyRenameService = class {
1955
1990
  constructor() {
1956
- this.toDispose = new import_utils12.DisposableCollection();
1957
- this.renameEmitter = new import_utils12.Emitter();
1991
+ this.toDispose = new import_utils10.DisposableCollection();
1992
+ this.renameEmitter = new import_utils10.Emitter();
1958
1993
  // 没有被 rename 的字段通过 disposeInList 透出,让业务区分变量是 rename 删除的,还是真正从列表中删除的
1959
- this.disposeInListEmitter = new import_utils12.Emitter();
1994
+ this.disposeInListEmitter = new import_utils10.Emitter();
1960
1995
  this.onRename = this.renameEmitter.event;
1961
1996
  this.onDisposeInList = this.disposeInListEmitter.event;
1962
1997
  }
@@ -2015,20 +2050,20 @@ var VariableFieldKeyRenameService = class {
2015
2050
  }
2016
2051
  };
2017
2052
  __decorateClass([
2018
- (0, import_inversify6.inject)(VariableEngine)
2053
+ (0, import_inversify7.inject)(VariableEngine)
2019
2054
  ], VariableFieldKeyRenameService.prototype, "variableEngine", 2);
2020
2055
  __decorateClass([
2021
- (0, import_inversify6.postConstruct)()
2056
+ (0, import_inversify7.postConstruct)()
2022
2057
  ], VariableFieldKeyRenameService.prototype, "init", 1);
2023
2058
  __decorateClass([
2024
- (0, import_inversify6.preDestroy)()
2059
+ (0, import_inversify7.preDestroy)()
2025
2060
  ], VariableFieldKeyRenameService.prototype, "dispose", 1);
2026
2061
  VariableFieldKeyRenameService = __decorateClass([
2027
- (0, import_inversify6.injectable)()
2062
+ (0, import_inversify7.injectable)()
2028
2063
  ], VariableFieldKeyRenameService);
2029
2064
 
2030
2065
  // src/variable-container-module.ts
2031
- var VariableContainerModule = new import_inversify7.ContainerModule((bind) => {
2066
+ var VariableContainerModule = new import_inversify8.ContainerModule((bind) => {
2032
2067
  bind(VariableEngine).toSelf().inSingletonScope();
2033
2068
  bind(ASTRegisters).toSelf().inSingletonScope();
2034
2069
  bind(VariableFieldKeyRenameService).toSelf().inSingletonScope();
@@ -2095,7 +2130,6 @@ function useAvailableVariables() {
2095
2130
  CustomType,
2096
2131
  DataNode,
2097
2132
  EnumerateExpression,
2098
- ExpressionList,
2099
2133
  IntegerType,
2100
2134
  KeyPathExpression,
2101
2135
  KeyPathExpressionV2,
@@ -2116,7 +2150,7 @@ function useAvailableVariables() {
2116
2150
  VariableEngine,
2117
2151
  VariableEngineProvider,
2118
2152
  VariableFieldKeyRenameService,
2119
- VariableTable,
2153
+ WrapArrayExpression,
2120
2154
  injectToAST,
2121
2155
  isMatchAST,
2122
2156
  postConstructAST,