@flowgram.ai/variable-core 0.2.16 → 0.2.18

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
@@ -70,29 +70,41 @@ var VariableTable = class {
70
70
  ),
71
71
  share()
72
72
  );
73
+ /**
74
+ * @deprecated use onListOrAnyVarChange instead
75
+ */
73
76
  this.onDataChange = this.onDataChangeEmitter.event;
74
77
  this._version = 0;
75
78
  }
76
79
  /**
77
- * 监听任意变量变化
78
- * @param observer 监听器,变量变化时会吐出值
80
+ * listen to any variable update in list
81
+ * @param observer
79
82
  * @returns
80
83
  */
81
84
  onAnyVariableChange(observer) {
82
85
  return subsToDisposable(this.anyVariableChange$.subscribe(observer));
83
86
  }
84
87
  /**
85
- * 列表或者任意变量变化
88
+ * listen to variable list change
86
89
  * @param observer
90
+ * @returns
87
91
  */
88
- onAnyChange(observer) {
92
+ onVariableListChange(observer) {
93
+ return subsToDisposable(this.variables$.subscribe(observer));
94
+ }
95
+ /**
96
+ * listen to variable list change + any variable update in list
97
+ * @param observer
98
+ */
99
+ onListOrAnyVarChange(observer) {
89
100
  const disposables = new DisposableCollection();
90
- disposables.pushAll([this.onDataChange(observer), this.onAnyVariableChange(observer)]);
101
+ disposables.pushAll([this.onVariableListChange(observer), this.onAnyVariableChange(observer)]);
91
102
  return disposables;
92
103
  }
93
104
  fireChange() {
94
105
  this._version++;
95
106
  this.onDataChangeEmitter.fire();
107
+ this.variables$.next(this.variables);
96
108
  this.parentTable?.fireChange();
97
109
  }
98
110
  get version() {
@@ -134,7 +146,6 @@ var VariableTable = class {
134
146
  if (this.parentTable) {
135
147
  this.parentTable.addVariableToTable(variable);
136
148
  }
137
- this.variables$.next(this.variables);
138
149
  }
139
150
  /**
140
151
  * 从 variableTable 中移除变量
@@ -145,12 +156,14 @@ var VariableTable = class {
145
156
  if (this.parentTable) {
146
157
  this.parentTable.removeVariableFromTable(key);
147
158
  }
148
- this.variables$.next(this.variables);
149
159
  }
150
160
  dispose() {
151
161
  this.variableKeys.forEach(
152
162
  (_key) => this.parentTable?.removeVariableFromTable(_key)
153
163
  );
164
+ this.parentTable?.fireChange();
165
+ this.variables$.complete();
166
+ this.variables$.unsubscribe();
154
167
  this.onDataChangeEmitter.dispose();
155
168
  }
156
169
  };
@@ -1126,9 +1139,11 @@ var WrapArrayExpression = class extends BaseExpression {
1126
1139
  };
1127
1140
  }
1128
1141
  init() {
1142
+ this.refreshReturnType = this.refreshReturnType.bind(this);
1129
1143
  this.toDispose.push(
1130
1144
  this.subscribe(this.refreshReturnType, {
1131
- selector: (curr) => curr.wrapFor?.returnType
1145
+ selector: (curr) => curr.wrapFor?.returnType,
1146
+ triggerOnInit: true
1132
1147
  })
1133
1148
  );
1134
1149
  }
@@ -1152,6 +1167,9 @@ var BaseVariableField = class extends ASTNode {
1152
1167
  get parentFields() {
1153
1168
  return getParentFields(this);
1154
1169
  }
1170
+ get keyPath() {
1171
+ return this.parentFields.reverse().map((_field) => _field.key);
1172
+ }
1155
1173
  get meta() {
1156
1174
  return this._meta;
1157
1175
  }
@@ -1161,6 +1179,9 @@ var BaseVariableField = class extends ASTNode {
1161
1179
  get initializer() {
1162
1180
  return this._initializer;
1163
1181
  }
1182
+ get hash() {
1183
+ return `[${this._version}]${this.keyPath.join(".")}`;
1184
+ }
1164
1185
  /**
1165
1186
  * 解析 VariableDeclarationJSON 从而生成变量声明节点
1166
1187
  */
@@ -1593,12 +1614,30 @@ var ScopeOutputData = class {
1593
1614
  get globalVariableTable() {
1594
1615
  return this.scope.variableEngine.globalVariableTable;
1595
1616
  }
1617
+ /**
1618
+ * @deprecated use onListOrAnyVarChange instead
1619
+ */
1596
1620
  get onDataChange() {
1597
1621
  return this.variableTable.onDataChange.bind(this.variableTable);
1598
1622
  }
1623
+ /**
1624
+ * listen to variable list change
1625
+ */
1626
+ get onVariableListChange() {
1627
+ return this.variableTable.onVariableListChange.bind(this.variableTable);
1628
+ }
1629
+ /**
1630
+ * listen to any variable update in list
1631
+ */
1599
1632
  get onAnyVariableChange() {
1600
1633
  return this.variableTable.onAnyVariableChange.bind(this.variableTable);
1601
1634
  }
1635
+ /**
1636
+ * listen to variable list change + any variable update in list
1637
+ */
1638
+ get onListOrAnyVarChange() {
1639
+ return this.variableTable.onListOrAnyVarChange.bind(this.variableTable);
1640
+ }
1602
1641
  /**
1603
1642
  * Scope Output Variable Declarations
1604
1643
  */
@@ -1644,7 +1683,9 @@ import {
1644
1683
  merge as merge2,
1645
1684
  share as share3,
1646
1685
  skip as skip3,
1647
- switchMap as switchMap3
1686
+ startWith,
1687
+ switchMap as switchMap3,
1688
+ tap as tap2
1648
1689
  } from "rxjs";
1649
1690
  import { flatten } from "lodash";
1650
1691
  import { shallowEqual as shallowEqual7 } from "fast-equals";
@@ -1680,19 +1721,29 @@ var ScopeAvailableData = class {
1680
1721
  ),
1681
1722
  share3()
1682
1723
  );
1724
+ /**
1725
+ * @deprecated
1726
+ */
1683
1727
  this.onDataChangeEmitter = new Emitter2();
1728
+ this.onListOrAnyVarChangeEmitter = new Emitter2();
1684
1729
  /**
1685
- * 监听变量列表变化 + 任意子变量变化
1730
+ * @deprecated use available.onListOrAnyVarChange instead
1686
1731
  */
1687
1732
  this.onDataChange = this.onDataChangeEmitter.event;
1733
+ /**
1734
+ * listen to variable list change + any variable drilldown change
1735
+ */
1736
+ this.onListOrAnyVarChange = this.onListOrAnyVarChangeEmitter.event;
1688
1737
  this.scope.toDispose.pushAll([
1689
1738
  this.onVariableListChange((_variables) => {
1690
1739
  this._variables = _variables;
1691
1740
  this.memo.clear();
1692
1741
  this.onDataChangeEmitter.fire(this._variables);
1742
+ this.onListOrAnyVarChangeEmitter.fire(this._variables);
1693
1743
  }),
1694
1744
  this.onAnyVariableChange(() => {
1695
1745
  this.onDataChangeEmitter.fire(this._variables);
1746
+ this.onListOrAnyVarChangeEmitter.fire(this._variables);
1696
1747
  }),
1697
1748
  Disposable3.create(() => {
1698
1749
  this.refresh$.complete();
@@ -1711,15 +1762,15 @@ var ScopeAvailableData = class {
1711
1762
  this.refresh$.next();
1712
1763
  }
1713
1764
  /**
1714
- * 监听任意变量变化
1715
- * @param observer 监听器,变量变化时会吐出值
1765
+ * listen to any variable update in list
1766
+ * @param observer
1716
1767
  * @returns
1717
1768
  */
1718
1769
  onAnyVariableChange(observer) {
1719
1770
  return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1720
1771
  }
1721
1772
  /**
1722
- * 监听变量列表变化
1773
+ * listen to variable list change
1723
1774
  * @param observer
1724
1775
  * @returns
1725
1776
  */
@@ -1755,6 +1806,20 @@ var ScopeAvailableData = class {
1755
1806
  }
1756
1807
  return this.globalVariableTable.getByKeyPath(keyPath);
1757
1808
  }
1809
+ /**
1810
+ * Track Variable Change (Includes type update and children update) By KeyPath
1811
+ * @returns
1812
+ */
1813
+ trackByKeyPath(keyPath = [], cb, opts) {
1814
+ const { triggerOnInit = true } = opts || {};
1815
+ return subsToDisposable(
1816
+ merge2(this.anyVariableChange$, this.variables$).pipe(
1817
+ triggerOnInit ? startWith() : tap2(() => null),
1818
+ map3(() => this.getByKeyPath(keyPath)),
1819
+ distinctUntilChanged3((_prevNode, _node) => _prevNode?.hash !== _node?.hash)
1820
+ ).subscribe(cb)
1821
+ );
1822
+ }
1758
1823
  };
1759
1824
 
1760
1825
  // src/scope/datas/scope-event-data.ts
@@ -1838,6 +1903,33 @@ var Scope = class {
1838
1903
  get disposed() {
1839
1904
  return this.toDispose.disposed;
1840
1905
  }
1906
+ setVar(arg1, arg2) {
1907
+ if (typeof arg1 === "string" && arg2 !== void 0) {
1908
+ return this.ast.set(arg1, arg2);
1909
+ }
1910
+ if (typeof arg1 === "object" && arg2 === void 0) {
1911
+ return this.ast.set("outputs", arg1);
1912
+ }
1913
+ throw new Error("Invalid arguments");
1914
+ }
1915
+ /**
1916
+ * Retrieves a variable from the Scope by key.
1917
+ *
1918
+ * @param key - The key of the variable to retrieve. Defaults to 'outputs'.
1919
+ * @returns The value of the variable, or undefined if not found.
1920
+ */
1921
+ getVar(key = "outputs") {
1922
+ return this.ast.get(key);
1923
+ }
1924
+ /**
1925
+ * Clears a variable from the Scope by key.
1926
+ *
1927
+ * @param key - The key of the variable to clear. Defaults to 'outputs'.
1928
+ * @returns The updated AST node.
1929
+ */
1930
+ clearVar(key = "outputs") {
1931
+ return this.ast.remove(key);
1932
+ }
1841
1933
  };
1842
1934
 
1843
1935
  // src/variable-engine.ts
@@ -2062,7 +2154,7 @@ function useAvailableVariables() {
2062
2154
  const refresh = useRefresh2();
2063
2155
  useEffect2(() => {
2064
2156
  if (!scope) {
2065
- const disposable2 = variableEngine.globalVariableTable.onAnyChange(() => {
2157
+ const disposable2 = variableEngine.globalVariableTable.onListOrAnyVarChange(() => {
2066
2158
  refresh();
2067
2159
  });
2068
2160
  return () => disposable2.dispose();