@flowgram.ai/variable-core 0.1.23 → 0.1.25

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/esm/index.js CHANGED
@@ -16,7 +16,7 @@ import { ContainerModule } from "inversify";
16
16
  // src/variable-engine.ts
17
17
  import { Subject as Subject5 } from "rxjs";
18
18
  import { inject as inject2, injectable as injectable3, preDestroy } from "inversify";
19
- import { Disposable as Disposable6, DisposableCollection as DisposableCollection5 } from "@flowgram.ai/utils";
19
+ import { Disposable as Disposable4, DisposableCollection as DisposableCollection5 } from "@flowgram.ai/utils";
20
20
  import { Emitter as Emitter3 } from "@flowgram.ai/utils";
21
21
 
22
22
  // src/utils/toDisposable.tsx
@@ -47,9 +47,117 @@ var createMemo = () => {
47
47
  return memo;
48
48
  };
49
49
 
50
+ // src/scope/variable-table.ts
51
+ import { Subject, merge, share, skip, switchMap } from "rxjs";
52
+ import { DisposableCollection, Emitter } from "@flowgram.ai/utils";
53
+ var VariableTable = class {
54
+ constructor(parentTable) {
55
+ this.parentTable = parentTable;
56
+ this.table = /* @__PURE__ */ new Map();
57
+ this.onDataChangeEmitter = new Emitter();
58
+ this.variables$ = new Subject();
59
+ // 监听变量列表中的单个变量变化
60
+ this.anyVariableChange$ = this.variables$.pipe(
61
+ switchMap(
62
+ (_variables) => merge(
63
+ ..._variables.map(
64
+ (_v) => _v.value$.pipe(
65
+ // 跳过 BehaviorSubject 第一个
66
+ skip(1)
67
+ )
68
+ )
69
+ )
70
+ ),
71
+ share()
72
+ );
73
+ this.onDataChange = this.onDataChangeEmitter.event;
74
+ this._version = 0;
75
+ }
76
+ /**
77
+ * 监听任意变量变化
78
+ * @param observer 监听器,变量变化时会吐出值
79
+ * @returns
80
+ */
81
+ onAnyVariableChange(observer) {
82
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
83
+ }
84
+ /**
85
+ * 列表或者任意变量变化
86
+ * @param observer
87
+ */
88
+ onAnyChange(observer) {
89
+ const disposables = new DisposableCollection();
90
+ disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
91
+ return disposables;
92
+ }
93
+ fireChange() {
94
+ this._version++;
95
+ this.onDataChangeEmitter.fire();
96
+ this.parentTable?.fireChange();
97
+ }
98
+ get version() {
99
+ return this._version;
100
+ }
101
+ get variables() {
102
+ return Array.from(this.table.values());
103
+ }
104
+ get variableKeys() {
105
+ return Array.from(this.table.keys());
106
+ }
107
+ /**
108
+ * 根据 keyPath 找到对应的变量,或 Property 节点
109
+ * @param keyPath
110
+ * @returns
111
+ */
112
+ getByKeyPath(keyPath) {
113
+ const [variableKey, ...propertyKeys] = keyPath || [];
114
+ if (!variableKey) {
115
+ return;
116
+ }
117
+ const variable = this.getVariableByKey(variableKey);
118
+ return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
119
+ }
120
+ /**
121
+ * 根据 key 值找到相应的变量
122
+ * @param key
123
+ * @returns
124
+ */
125
+ getVariableByKey(key) {
126
+ return this.table.get(key);
127
+ }
128
+ /**
129
+ * 往 variableTable 添加输出变量
130
+ * @param variable
131
+ */
132
+ addVariableToTable(variable) {
133
+ this.table.set(variable.key, variable);
134
+ if (this.parentTable) {
135
+ this.parentTable.addVariableToTable(variable);
136
+ }
137
+ this.variables$.next(this.variables);
138
+ }
139
+ /**
140
+ * 从 variableTable 中移除变量
141
+ * @param key
142
+ */
143
+ removeVariableFromTable(key) {
144
+ this.table.delete(key);
145
+ if (this.parentTable) {
146
+ this.parentTable.removeVariableFromTable(key);
147
+ }
148
+ this.variables$.next(this.variables);
149
+ }
150
+ dispose() {
151
+ this.variableKeys.forEach(
152
+ (_key) => this.parentTable?.removeVariableFromTable(_key)
153
+ );
154
+ this.onDataChangeEmitter.dispose();
155
+ }
156
+ };
157
+
50
158
  // src/scope/scope-chain.ts
51
159
  import { inject, injectable } from "inversify";
52
- import { DisposableCollection } from "@flowgram.ai/utils";
160
+ import { DisposableCollection as DisposableCollection2 } from "@flowgram.ai/utils";
53
161
 
54
162
  // src/providers.ts
55
163
  var VariableEngineProvider = Symbol("DynamicVariableEngine");
@@ -58,7 +166,7 @@ var ContainerProvider = Symbol("ContainerProvider");
58
166
  // src/scope/scope-chain.ts
59
167
  var ScopeChain = class {
60
168
  constructor() {
61
- this.toDispose = new DisposableCollection();
169
+ this.toDispose = new DisposableCollection2();
62
170
  }
63
171
  get variableEngine() {
64
172
  return this.variableEngineProvider();
@@ -228,12 +336,12 @@ import {
228
336
  debounceTime,
229
337
  distinctUntilChanged,
230
338
  map,
231
- skip,
339
+ skip as skip2,
232
340
  tap
233
341
  } from "rxjs";
234
342
  import { nanoid } from "nanoid";
235
343
  import { shallowEqual } from "fast-equals";
236
- import { Disposable as Disposable2, DisposableCollection as DisposableCollection2 } from "@flowgram.ai/utils";
344
+ import { Disposable as Disposable2, DisposableCollection as DisposableCollection3 } from "@flowgram.ai/utils";
237
345
  var ASTNode = class _ASTNode {
238
346
  /**
239
347
  * 构造函数
@@ -272,7 +380,7 @@ var ASTNode = class _ASTNode {
272
380
  /**
273
381
  * 删除节点处理事件列表
274
382
  */
275
- this.toDispose = new DisposableCollection2(
383
+ this.toDispose = new DisposableCollection3(
276
384
  Disposable2.create(() => {
277
385
  this.parent?.fireChange();
278
386
  this.children.forEach((child) => child.dispose());
@@ -415,7 +523,7 @@ var ASTNode = class _ASTNode {
415
523
  }
416
524
  ),
417
525
  // 默认跳过 BehaviorSubject 第一次触发
418
- triggerOnInit ? tap(() => null) : skip(1),
526
+ triggerOnInit ? tap(() => null) : skip2(1),
419
527
  // 每个 animationFrame 内所有更新合并成一个
420
528
  debounceAnimation ? debounceTime(0, animationFrameScheduler) : tap(() => null)
421
529
  ).subscribe(observer)
@@ -726,11 +834,11 @@ CustomType.kind = "CustomType" /* CustomType */;
726
834
  import {
727
835
  distinctUntilChanged as distinctUntilChanged2,
728
836
  map as map2,
729
- switchMap,
837
+ switchMap as switchMap2,
730
838
  combineLatest,
731
839
  of,
732
- Subject,
733
- share
840
+ Subject as Subject2,
841
+ share as share2
734
842
  } from "rxjs";
735
843
  import { shallowEqual as shallowEqual2 } from "fast-equals";
736
844
 
@@ -756,7 +864,7 @@ var BaseExpression = class extends ASTNode {
756
864
  * 引用变量
757
865
  */
758
866
  this._refs = [];
759
- this.refreshRefs$ = new Subject();
867
+ this.refreshRefs$ = new Subject2();
760
868
  /**
761
869
  * 监听引用变量变化
762
870
  * 监听 [a.b.c] -> [a.b]
@@ -764,14 +872,14 @@ var BaseExpression = class extends ASTNode {
764
872
  this.refs$ = this.refreshRefs$.pipe(
765
873
  map2(() => this.getRefFields()),
766
874
  distinctUntilChanged2(shallowEqual2),
767
- switchMap(
875
+ switchMap2(
768
876
  (refs) => !refs?.length ? of([]) : combineLatest(
769
877
  refs.map(
770
878
  (ref) => ref ? ref.value$ : of(void 0)
771
879
  )
772
880
  )
773
881
  ),
774
- share()
882
+ share2()
775
883
  );
776
884
  this.toDispose.push(
777
885
  subsToDisposable(
@@ -1009,9 +1117,6 @@ var KeyPathExpressionV2 = class extends BaseExpression {
1009
1117
  };
1010
1118
  KeyPathExpressionV2.kind = "KeyPathExpression" /* KeyPathExpression */;
1011
1119
 
1012
- // src/ast/declaration/variable-declaration.ts
1013
- import { Disposable as Disposable3 } from "@flowgram.ai/utils";
1014
-
1015
1120
  // src/ast/declaration/base-variable-field.ts
1016
1121
  import { shallowEqual as shallowEqual5 } from "fast-equals";
1017
1122
  var BaseVariableField = class extends ASTNode {
@@ -1095,13 +1200,6 @@ var VariableDeclaration = class extends BaseVariableField {
1095
1200
  constructor(params) {
1096
1201
  super(params);
1097
1202
  this._order = 0;
1098
- this.scope.output.addVariableToTable(this);
1099
- this.toDispose.push(
1100
- Disposable3.create(() => {
1101
- this.scope.output.setHasChanges();
1102
- this.scope.output.removeVariableFromTable(this.key);
1103
- })
1104
- );
1105
1203
  }
1106
1204
  get order() {
1107
1205
  return this._order;
@@ -1116,7 +1214,9 @@ var VariableDeclaration = class extends BaseVariableField {
1116
1214
  updateOrder(order = 0) {
1117
1215
  if (order !== this._order) {
1118
1216
  this._order = order;
1119
- this.scope.output.setHasChanges();
1217
+ this.dispatchGlobalEvent({
1218
+ type: "ReSortVariableDeclarations"
1219
+ });
1120
1220
  this.fireChange();
1121
1221
  }
1122
1222
  }
@@ -1429,113 +1529,6 @@ var ASTFactory;
1429
1529
  ASTFactory2.create = (targetType, json) => ({ kind: targetType.kind, ...json });
1430
1530
  })(ASTFactory || (ASTFactory = {}));
1431
1531
 
1432
- // src/scope/variable-table.ts
1433
- import { Subject as Subject2, merge, share as share2, skip as skip2, switchMap as switchMap2 } from "rxjs";
1434
- import { Emitter } from "@flowgram.ai/utils";
1435
- import { DisposableCollection as DisposableCollection3 } from "@flowgram.ai/utils";
1436
- var VariableTable = class {
1437
- constructor(parentTable) {
1438
- this.parentTable = parentTable;
1439
- this.table = /* @__PURE__ */ new Map();
1440
- this.onDataChangeEmitter = new Emitter();
1441
- this.variables$ = new Subject2();
1442
- // 监听变量列表中的单个变量变化
1443
- this.anyVariableChange$ = this.variables$.pipe(
1444
- switchMap2(
1445
- (_variables) => merge(
1446
- ..._variables.map(
1447
- (_v) => _v.value$.pipe(
1448
- // 跳过 BehaviorSubject 第一个
1449
- skip2(1)
1450
- )
1451
- )
1452
- )
1453
- ),
1454
- share2()
1455
- );
1456
- this.onDataChange = this.onDataChangeEmitter.event;
1457
- this._version = 0;
1458
- }
1459
- /**
1460
- * 监听任意变量变化
1461
- * @param observer 监听器,变量变化时会吐出值
1462
- * @returns
1463
- */
1464
- onAnyVariableChange(observer) {
1465
- return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1466
- }
1467
- /**
1468
- * 列表或者任意变量变化
1469
- * @param observer
1470
- */
1471
- onAnyChange(observer) {
1472
- const disposables = new DisposableCollection3();
1473
- disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
1474
- return disposables;
1475
- }
1476
- fireChange() {
1477
- this._version++;
1478
- this.onDataChangeEmitter.fire();
1479
- this.parentTable?.fireChange();
1480
- }
1481
- get version() {
1482
- return this._version;
1483
- }
1484
- get variables() {
1485
- return Array.from(this.table.values());
1486
- }
1487
- get variableKeys() {
1488
- return Array.from(this.table.keys());
1489
- }
1490
- /**
1491
- * 根据 keyPath 找到对应的变量,或 Property 节点
1492
- * @param keyPath
1493
- * @returns
1494
- */
1495
- getByKeyPath(keyPath) {
1496
- const [variableKey, ...propertyKeys] = keyPath || [];
1497
- if (!variableKey) {
1498
- return;
1499
- }
1500
- const variable = this.getVariableByKey(variableKey);
1501
- return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
1502
- }
1503
- /**
1504
- * 根据 key 值找到相应的变量
1505
- * @param key
1506
- * @returns
1507
- */
1508
- getVariableByKey(key) {
1509
- return this.table.get(key);
1510
- }
1511
- /**
1512
- * 往 variableTable 添加输出变量
1513
- * @param variable
1514
- */
1515
- addVariableToTable(variable) {
1516
- this.table.set(variable.key, variable);
1517
- if (this.parentTable) {
1518
- this.parentTable.addVariableToTable(variable);
1519
- }
1520
- this.variables$.next(this.variables);
1521
- }
1522
- /**
1523
- * 从 variableTable 中移除变量
1524
- * @param key
1525
- */
1526
- removeVariableFromTable(key) {
1527
- this.table.delete(key);
1528
- if (this.parentTable) {
1529
- this.parentTable.removeVariableFromTable(key);
1530
- }
1531
- this.variables$.next(this.variables);
1532
- }
1533
- dispose() {
1534
- this.variableKeys.forEach((_key) => this.parentTable?.removeVariableFromTable(_key));
1535
- this.onDataChangeEmitter.dispose();
1536
- }
1537
- };
1538
-
1539
1532
  // src/scope/datas/scope-output-data.ts
1540
1533
  var ScopeOutputData = class {
1541
1534
  constructor(scope) {
@@ -1544,7 +1537,7 @@ var ScopeOutputData = class {
1544
1537
  this._hasChanges = false;
1545
1538
  this.variableTable = new VariableTable(scope.variableEngine.globalVariableTable);
1546
1539
  this.scope.toDispose.pushAll([
1547
- // AST 根节点更新时,检查在这次 AST 变化期间节点是否有变化
1540
+ // When root AST node is updated, check if there are any changes during this AST change
1548
1541
  this.scope.ast.subscribe(() => {
1549
1542
  if (this._hasChanges) {
1550
1543
  this.memo.clear();
@@ -1553,6 +1546,19 @@ var ScopeOutputData = class {
1553
1546
  this._hasChanges = false;
1554
1547
  }
1555
1548
  }),
1549
+ this.scope.event.on("DisposeAST", (_action) => {
1550
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1551
+ this.removeVariableFromTable(_action.ast.key);
1552
+ }
1553
+ }),
1554
+ this.scope.event.on("NewAST", (_action) => {
1555
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1556
+ this.addVariableToTable(_action.ast);
1557
+ }
1558
+ }),
1559
+ this.scope.event.on("ReSortVariableDeclarations", () => {
1560
+ this._hasChanges = true;
1561
+ }),
1556
1562
  this.variableTable
1557
1563
  ]);
1558
1564
  }
@@ -1569,7 +1575,7 @@ var ScopeOutputData = class {
1569
1575
  return this.variableTable.onAnyVariableChange.bind(this.variableTable);
1570
1576
  }
1571
1577
  /**
1572
- * 作用域输出变量
1578
+ * Scope Output Variable Declarations
1573
1579
  */
1574
1580
  get variables() {
1575
1581
  return this.memo(
@@ -1578,7 +1584,7 @@ var ScopeOutputData = class {
1578
1584
  );
1579
1585
  }
1580
1586
  /**
1581
- * 输出的变量 keys
1587
+ * Output Variable Keys
1582
1588
  */
1583
1589
  get variableKeys() {
1584
1590
  return this.memo("variableKeys", () => this.variableTable.variableKeys);
@@ -1590,10 +1596,6 @@ var ScopeOutputData = class {
1590
1596
  this.variableTable.addVariableToTable(variable);
1591
1597
  this._hasChanges = true;
1592
1598
  }
1593
- // 标记为发生了变化,用于变量排序变化的场景
1594
- setHasChanges() {
1595
- this._hasChanges = true;
1596
- }
1597
1599
  removeVariableFromTable(key) {
1598
1600
  this.variableTable.removeVariableFromTable(key);
1599
1601
  this._hasChanges = true;
@@ -1601,7 +1603,9 @@ var ScopeOutputData = class {
1601
1603
  getVariableByKey(key) {
1602
1604
  return this.variableTable.getVariableByKey(key);
1603
1605
  }
1604
- // 通知覆盖作用域更新可用变量
1606
+ /**
1607
+ *
1608
+ */
1605
1609
  notifyCoversChange() {
1606
1610
  this.scope.coverScopes.forEach((scope) => scope.available.refresh());
1607
1611
  }
@@ -1619,7 +1623,7 @@ import {
1619
1623
  } from "rxjs";
1620
1624
  import { flatten } from "lodash";
1621
1625
  import { shallowEqual as shallowEqual7 } from "fast-equals";
1622
- import { Disposable as Disposable5 } from "@flowgram.ai/utils";
1626
+ import { Disposable as Disposable3 } from "@flowgram.ai/utils";
1623
1627
  import { Emitter as Emitter2 } from "@flowgram.ai/utils";
1624
1628
  var ScopeAvailableData = class {
1625
1629
  constructor(scope) {
@@ -1665,7 +1669,7 @@ var ScopeAvailableData = class {
1665
1669
  this.onAnyVariableChange(() => {
1666
1670
  this.onDataChangeEmitter.fire(this._variables);
1667
1671
  }),
1668
- Disposable5.create(() => {
1672
+ Disposable3.create(() => {
1669
1673
  this.refresh$.complete();
1670
1674
  this.refresh$.unsubscribe();
1671
1675
  })
@@ -1772,7 +1776,7 @@ var Scope = class {
1772
1776
  this.ast = this.variableEngine.astRegisters.createAST(
1773
1777
  {
1774
1778
  kind: "MapNode" /* MapNode */,
1775
- key: this.id
1779
+ key: String(this.id)
1776
1780
  },
1777
1781
  {
1778
1782
  scope: this
@@ -1825,7 +1829,7 @@ var VariableEngine = class {
1825
1829
  this.onScopeChange = this.onScopeChangeEmitter.event;
1826
1830
  this.toDispose.pushAll([
1827
1831
  chain,
1828
- Disposable6.create(() => {
1832
+ Disposable4.create(() => {
1829
1833
  this.getAllScopes().forEach((scope) => scope.dispose());
1830
1834
  this.globalVariableTable.dispose();
1831
1835
  })
@@ -1845,11 +1849,18 @@ var VariableEngine = class {
1845
1849
  removeScopeById(scopeId) {
1846
1850
  this.getScopeById(scopeId)?.dispose();
1847
1851
  }
1848
- // 获取 Scope,如果 Scope 存在且类型相同,则会直接使用
1849
- createScope(id, meta) {
1852
+ /**
1853
+ * Get Scope, if Scope exists and type is same, will use it directly
1854
+ * @param id scope id
1855
+ * @param meta scope meta, defined by user
1856
+ * @param ScopeConstructor scope constructor, default is Scope. you can extends Scope to create your own scope
1857
+ * @returns
1858
+ */
1859
+ createScope(id, meta, options = {}) {
1860
+ const { ScopeConstructor = Scope } = options;
1850
1861
  let scope = this.getScopeById(id);
1851
1862
  if (!scope) {
1852
- scope = new Scope({ variableEngine: this, meta, id });
1863
+ scope = new ScopeConstructor({ variableEngine: this, meta, id });
1853
1864
  this.scopeMap.set(id, scope);
1854
1865
  this.onScopeChangeEmitter.fire({ type: "add", scope });
1855
1866
  scope.toDispose.pushAll([
@@ -2074,7 +2085,6 @@ export {
2074
2085
  VariableEngine,
2075
2086
  VariableEngineProvider,
2076
2087
  VariableFieldKeyRenameService,
2077
- VariableTable,
2078
2088
  injectToAST,
2079
2089
  isMatchAST,
2080
2090
  postConstructAST,