@flowgram.ai/variable-core 0.5.7 → 1.0.1

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
@@ -88,7 +88,7 @@ module.exports = __toCommonJS(index_exports);
88
88
  var import_inversify8 = require("inversify");
89
89
 
90
90
  // src/variable-engine.ts
91
- var import_rxjs6 = require("rxjs");
91
+ var import_rxjs7 = require("rxjs");
92
92
  var import_inversify6 = require("inversify");
93
93
  var import_utils8 = require("@flowgram.ai/utils");
94
94
  var import_utils9 = require("@flowgram.ai/utils");
@@ -352,7 +352,7 @@ var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
352
352
  })(ASTKind || {});
353
353
 
354
354
  // src/ast/ast-registers.ts
355
- var import_lodash_es3 = require("lodash-es");
355
+ var import_lodash_es4 = require("lodash-es");
356
356
  var import_inversify3 = require("inversify");
357
357
 
358
358
  // src/ast/utils/inversify.ts
@@ -461,6 +461,7 @@ function isMatchAST(node, targetType) {
461
461
  // src/ast/ast-node.ts
462
462
  var import_rxjs2 = require("rxjs");
463
463
  var import_nanoid = require("nanoid");
464
+ var import_lodash_es = require("lodash-es");
464
465
  var import_fast_equals = require("fast-equals");
465
466
  var import_utils4 = require("@flowgram.ai/utils");
466
467
  var ASTNode = class _ASTNode {
@@ -519,6 +520,16 @@ var ASTNode = class _ASTNode {
519
520
  this.opts = opts;
520
521
  this.key = key || (0, import_nanoid.nanoid)();
521
522
  this.fromJSON = this.withBatchUpdate(this.fromJSON.bind(this));
523
+ const rawToJSON = this.toJSON?.bind(this);
524
+ this.toJSON = () => (0, import_lodash_es.omitBy)(
525
+ {
526
+ // always include kind
527
+ kind: this.kind,
528
+ ...rawToJSON?.() || {}
529
+ },
530
+ // remove undefined fields
531
+ import_lodash_es.isNil
532
+ );
522
533
  }
523
534
  /**
524
535
  * The type of the ASTNode.
@@ -535,16 +546,6 @@ var ASTNode = class _ASTNode {
535
546
  get children() {
536
547
  return Array.from(this._children);
537
548
  }
538
- /**
539
- * Serializes the current ASTNode to ASTNodeJSON.
540
- * @returns
541
- */
542
- toJSON() {
543
- console.warn("[VariableEngine] Please Implement toJSON method for " + this.kind);
544
- return {
545
- kind: this.kind
546
- };
547
- }
548
549
  /**
549
550
  * Creates a child ASTNode.
550
551
  * @param json The AST JSON of the child ASTNode.
@@ -711,15 +712,6 @@ var BaseType = class extends ASTNode {
711
712
  getByKeyPath(keyPath = []) {
712
713
  throw new Error(`Get By Key Path is not implemented for Type: ${this.kind}`);
713
714
  }
714
- /**
715
- * Serialize the node to a JSON object.
716
- * @returns The JSON representation of the node.
717
- */
718
- toJSON() {
719
- return {
720
- kind: this.kind
721
- };
722
- }
723
715
  };
724
716
 
725
717
  // src/ast/type/array.ts
@@ -814,6 +806,15 @@ var StringType = class extends BaseType {
814
806
  this.fireChange();
815
807
  }
816
808
  }
809
+ /**
810
+ * Serialize the `StringType` to `StringJSON`.
811
+ * @returns The JSON representation of `StringType`.
812
+ */
813
+ toJSON() {
814
+ return {
815
+ format: this._format
816
+ };
817
+ }
817
818
  };
818
819
  StringType.kind = "String" /* String */;
819
820
 
@@ -829,6 +830,9 @@ var IntegerType = class extends BaseType {
829
830
  */
830
831
  fromJSON() {
831
832
  }
833
+ toJSON() {
834
+ return {};
835
+ }
832
836
  };
833
837
  IntegerType.kind = "Integer" /* Integer */;
834
838
 
@@ -840,6 +844,9 @@ var BooleanType = class extends BaseType {
840
844
  */
841
845
  fromJSON() {
842
846
  }
847
+ toJSON() {
848
+ return {};
849
+ }
843
850
  };
844
851
  BooleanType.kind = "Boolean" /* Boolean */;
845
852
 
@@ -851,6 +858,9 @@ var NumberType = class extends BaseType {
851
858
  */
852
859
  fromJSON() {
853
860
  }
861
+ toJSON() {
862
+ return {};
863
+ }
854
864
  };
855
865
  NumberType.kind = "Number" /* Number */;
856
866
 
@@ -903,7 +913,7 @@ var MapType = class extends BaseType {
903
913
  MapType.kind = "Map" /* Map */;
904
914
 
905
915
  // src/ast/type/object.ts
906
- var import_lodash_es = require("lodash-es");
916
+ var import_lodash_es2 = require("lodash-es");
907
917
  var ObjectType = class extends BaseType {
908
918
  constructor() {
909
919
  super(...arguments);
@@ -956,7 +966,6 @@ var ObjectType = class extends BaseType {
956
966
  */
957
967
  toJSON() {
958
968
  return {
959
- kind: "Object" /* Object */,
960
969
  properties: this.properties.map((_property) => _property.toJSON())
961
970
  };
962
971
  }
@@ -999,7 +1008,7 @@ var ObjectType = class extends BaseType {
999
1008
  const targetProperties = targetTypeJSON.properties || [];
1000
1009
  const sourcePropertyKeys = Array.from(this.propertyTable.keys());
1001
1010
  const targetPropertyKeys = targetProperties.map((_target) => _target.key);
1002
- const isKeyStrongEqual = !(0, import_lodash_es.xor)(sourcePropertyKeys, targetPropertyKeys).length;
1011
+ const isKeyStrongEqual = !(0, import_lodash_es2.xor)(sourcePropertyKeys, targetPropertyKeys).length;
1003
1012
  return isKeyStrongEqual && targetProperties.every((targetProperty) => {
1004
1013
  const sourceProperty = this.propertyTable.get(targetProperty.key);
1005
1014
  return sourceProperty && sourceProperty.key === targetProperty.key && sourceProperty.type?.isTypeEqual(targetProperty?.type);
@@ -1040,6 +1049,11 @@ var CustomType = class extends BaseType {
1040
1049
  }
1041
1050
  return targetTypeJSON?.kind === this.kind && targetTypeJSON?.typeName === this.typeName;
1042
1051
  }
1052
+ toJSON() {
1053
+ return {
1054
+ typeName: this.typeName
1055
+ };
1056
+ }
1043
1057
  };
1044
1058
  CustomType.kind = "CustomType" /* CustomType */;
1045
1059
 
@@ -1166,15 +1180,16 @@ var EnumerateExpression = class extends BaseExpression {
1166
1180
  EnumerateExpression.kind = "EnumerateExpression" /* EnumerateExpression */;
1167
1181
 
1168
1182
  // src/ast/expression/keypath-expression.ts
1183
+ var import_rxjs4 = require("rxjs");
1169
1184
  var import_fast_equals3 = require("fast-equals");
1170
1185
 
1171
1186
  // src/ast/utils/expression.ts
1172
- var import_lodash_es2 = require("lodash-es");
1187
+ var import_lodash_es3 = require("lodash-es");
1173
1188
  function getAllRefs(ast) {
1174
1189
  return getAllChildren(ast).filter((_child) => _child.flags & 4 /* Expression */).map((_child) => _child.refs).flat().filter(Boolean);
1175
1190
  }
1176
1191
  function checkRefCycle(curr, refNodes) {
1177
- if ((0, import_lodash_es2.intersection)(curr.scope.coverScopes, refNodes.map((_ref) => _ref?.scope).filter(Boolean)).length === 0) {
1192
+ if ((0, import_lodash_es3.intersection)(curr.scope.coverScopes, refNodes.map((_ref) => _ref?.scope).filter(Boolean)).length === 0) {
1178
1193
  return false;
1179
1194
  }
1180
1195
  const visited = /* @__PURE__ */ new Set();
@@ -1186,7 +1201,7 @@ function checkRefCycle(curr, refNodes) {
1186
1201
  queue.push(ref);
1187
1202
  }
1188
1203
  }
1189
- return (0, import_lodash_es2.intersection)(Array.from(visited), getParentFields(curr)).length > 0;
1204
+ return (0, import_lodash_es3.intersection)(Array.from(visited), getParentFields(curr)).length > 0;
1190
1205
  }
1191
1206
 
1192
1207
  // src/ast/expression/keypath-expression.ts
@@ -1206,12 +1221,14 @@ var KeyPathExpression = class extends BaseExpression {
1206
1221
  }
1207
1222
  }),
1208
1223
  subsToDisposable(
1209
- this.refs$.subscribe((_type) => {
1224
+ this.refs$.pipe(
1225
+ (0, import_rxjs4.distinctUntilChanged)(
1226
+ (prev, next) => prev === next,
1227
+ (_refs) => _refs?.[0]?.type?.hash
1228
+ )
1229
+ ).subscribe((_type) => {
1210
1230
  const [ref] = this._refs;
1211
- if (this.prevRefTypeHash !== ref?.type?.hash) {
1212
- this.prevRefTypeHash = ref?.type?.hash;
1213
- this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
1214
- }
1231
+ this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
1215
1232
  })
1216
1233
  )
1217
1234
  ]);
@@ -1261,6 +1278,7 @@ var KeyPathExpression = class extends BaseExpression {
1261
1278
  const keyPath = this.parseToKeyPath(json);
1262
1279
  if (!(0, import_fast_equals3.shallowEqual)(keyPath, this._keyPath)) {
1263
1280
  this._keyPath = keyPath;
1281
+ this._rawPathJson = json;
1264
1282
  this.refreshRefs();
1265
1283
  }
1266
1284
  }
@@ -1277,10 +1295,7 @@ var KeyPathExpression = class extends BaseExpression {
1277
1295
  * @returns The JSON representation of `KeyPathExpression`.
1278
1296
  */
1279
1297
  toJSON() {
1280
- return {
1281
- kind: "KeyPathExpression" /* KeyPathExpression */,
1282
- keyPath: this._keyPath
1283
- };
1298
+ return this._rawPathJson;
1284
1299
  }
1285
1300
  };
1286
1301
  KeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
@@ -1346,6 +1361,7 @@ var LegacyKeyPathExpression = class extends BaseExpression {
1346
1361
  const keyPath = this.parseToKeyPath(json);
1347
1362
  if (!(0, import_fast_equals4.shallowEqual)(keyPath, this._keyPath)) {
1348
1363
  this._keyPath = keyPath;
1364
+ this._rawPathJson = json;
1349
1365
  this.refreshRefs();
1350
1366
  }
1351
1367
  }
@@ -1354,10 +1370,7 @@ var LegacyKeyPathExpression = class extends BaseExpression {
1354
1370
  * @returns The JSON representation of `KeyPathExpression`.
1355
1371
  */
1356
1372
  toJSON() {
1357
- return {
1358
- kind: "KeyPathExpression" /* KeyPathExpression */,
1359
- keyPath: this._keyPath
1360
- };
1373
+ return this._rawPathJson;
1361
1374
  }
1362
1375
  };
1363
1376
  LegacyKeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
@@ -1533,7 +1546,6 @@ var BaseVariableField = class extends ASTNode {
1533
1546
  */
1534
1547
  toJSON() {
1535
1548
  return {
1536
- kind: this.kind,
1537
1549
  key: this.key,
1538
1550
  type: this.type?.toJSON(),
1539
1551
  initializer: this.initializer?.toJSON(),
@@ -1574,6 +1586,16 @@ var VariableDeclaration = class extends BaseVariableField {
1574
1586
  this.fireChange();
1575
1587
  }
1576
1588
  }
1589
+ /**
1590
+ * Serialize the `VariableDeclaration` to `VariableDeclarationJSON`.
1591
+ * @returns The JSON representation of `VariableDeclaration`.
1592
+ */
1593
+ toJSON() {
1594
+ return {
1595
+ ...super.toJSON(),
1596
+ order: this.order
1597
+ };
1598
+ }
1577
1599
  };
1578
1600
  VariableDeclaration.kind = "VariableDeclaration" /* VariableDeclaration */;
1579
1601
 
@@ -1807,6 +1829,9 @@ var ASTRegisters = class {
1807
1829
  * Core AST node registration.
1808
1830
  */
1809
1831
  constructor() {
1832
+ /**
1833
+ * @deprecated Please use `@injectToAst(XXXService) declare xxxService: XXXService` to achieve external dependency injection.
1834
+ */
1810
1835
  this.injectors = /* @__PURE__ */ new Map();
1811
1836
  this.astMap = /* @__PURE__ */ new Map();
1812
1837
  this.registerAST(StringType);
@@ -1846,7 +1871,7 @@ var ASTRegisters = class {
1846
1871
  injector?.() || {}
1847
1872
  );
1848
1873
  node.changeLocked = true;
1849
- node.fromJSON((0, import_lodash_es3.omit)(json, ["key", "kind"]));
1874
+ node.fromJSON((0, import_lodash_es4.omit)(json, ["key", "kind"]));
1850
1875
  node.changeLocked = false;
1851
1876
  node.dispatchGlobalEvent({ type: "NewAST" });
1852
1877
  if (Reflect.hasMetadata(POST_CONSTRUCT_AST_SYMBOL, node)) {
@@ -1866,7 +1891,6 @@ var ASTRegisters = class {
1866
1891
  /**
1867
1892
  * Registers an AST node.
1868
1893
  * @param ASTNode
1869
- * @param injector
1870
1894
  */
1871
1895
  registerAST(ASTNode2, injector) {
1872
1896
  this.astMap.set(ASTNode2.kind, ASTNode2);
@@ -2054,8 +2078,8 @@ var ScopeOutputData = class {
2054
2078
  };
2055
2079
 
2056
2080
  // src/scope/datas/scope-available-data.ts
2057
- var import_rxjs4 = require("rxjs");
2058
- var import_lodash_es4 = require("lodash-es");
2081
+ var import_rxjs5 = require("rxjs");
2082
+ var import_lodash_es5 = require("lodash-es");
2059
2083
  var import_fast_equals7 = require("fast-equals");
2060
2084
  var import_utils5 = require("@flowgram.ai/utils");
2061
2085
  var import_utils6 = require("@flowgram.ai/utils");
@@ -2064,33 +2088,33 @@ var ScopeAvailableData = class {
2064
2088
  this.scope = scope;
2065
2089
  this.memo = createMemo();
2066
2090
  this._version = 0;
2067
- this.refresh$ = new import_rxjs4.Subject();
2091
+ this.refresh$ = new import_rxjs5.Subject();
2068
2092
  this._variables = [];
2069
2093
  /**
2070
2094
  * An observable that emits when the list of available variables changes.
2071
2095
  */
2072
2096
  this.variables$ = this.refresh$.pipe(
2073
2097
  // Map to the flattened list of variables from all dependency scopes.
2074
- (0, import_rxjs4.map)(() => (0, import_lodash_es4.flatten)(this.depScopes.map((scope) => scope.output.variables || []))),
2098
+ (0, import_rxjs5.map)(() => (0, import_lodash_es5.flatten)(this.depScopes.map((scope) => scope.output.variables || []))),
2075
2099
  // Use shallow equality to check if the variable list has changed.
2076
- (0, import_rxjs4.distinctUntilChanged)(import_fast_equals7.shallowEqual),
2077
- (0, import_rxjs4.share)()
2100
+ (0, import_rxjs5.distinctUntilChanged)(import_fast_equals7.shallowEqual),
2101
+ (0, import_rxjs5.share)()
2078
2102
  );
2079
2103
  /**
2080
2104
  * An observable that emits when any variable in the available list changes its value.
2081
2105
  */
2082
2106
  this.anyVariableChange$ = this.variables$.pipe(
2083
- (0, import_rxjs4.switchMap)(
2084
- (_variables) => (0, import_rxjs4.merge)(
2107
+ (0, import_rxjs5.switchMap)(
2108
+ (_variables) => (0, import_rxjs5.merge)(
2085
2109
  ..._variables.map(
2086
2110
  (_v) => _v.value$.pipe(
2087
2111
  // Skip the initial value of the BehaviorSubject.
2088
- (0, import_rxjs4.skip)(1)
2112
+ (0, import_rxjs5.skip)(1)
2089
2113
  )
2090
2114
  )
2091
2115
  )
2092
2116
  ),
2093
- (0, import_rxjs4.share)()
2117
+ (0, import_rxjs5.share)()
2094
2118
  );
2095
2119
  /**
2096
2120
  * @deprecated
@@ -2208,13 +2232,13 @@ var ScopeAvailableData = class {
2208
2232
  trackByKeyPath(keyPath = [], cb, opts) {
2209
2233
  const { triggerOnInit = true, debounceAnimation, selector } = opts || {};
2210
2234
  return subsToDisposable(
2211
- (0, import_rxjs4.merge)(this.anyVariableChange$, this.variables$).pipe(
2212
- triggerOnInit ? (0, import_rxjs4.startWith)() : (0, import_rxjs4.tap)(() => null),
2213
- (0, import_rxjs4.map)(() => {
2235
+ (0, import_rxjs5.merge)(this.anyVariableChange$, this.variables$).pipe(
2236
+ triggerOnInit ? (0, import_rxjs5.startWith)() : (0, import_rxjs5.tap)(() => null),
2237
+ (0, import_rxjs5.map)(() => {
2214
2238
  const v = this.getByKeyPath(keyPath);
2215
2239
  return selector ? selector(v) : v;
2216
2240
  }),
2217
- (0, import_rxjs4.distinctUntilChanged)(
2241
+ (0, import_rxjs5.distinctUntilChanged)(
2218
2242
  (a, b) => (0, import_fast_equals7.shallowEqual)(a, b),
2219
2243
  (value) => {
2220
2244
  if (value instanceof ASTNode) {
@@ -2224,18 +2248,18 @@ var ScopeAvailableData = class {
2224
2248
  }
2225
2249
  ),
2226
2250
  // Debounce updates to a single emission per animation frame.
2227
- debounceAnimation ? (0, import_rxjs4.debounceTime)(0, import_rxjs4.animationFrameScheduler) : (0, import_rxjs4.tap)(() => null)
2251
+ debounceAnimation ? (0, import_rxjs5.debounceTime)(0, import_rxjs5.animationFrameScheduler) : (0, import_rxjs5.tap)(() => null)
2228
2252
  ).subscribe(cb)
2229
2253
  );
2230
2254
  }
2231
2255
  };
2232
2256
 
2233
2257
  // src/scope/datas/scope-event-data.ts
2234
- var import_rxjs5 = require("rxjs");
2258
+ var import_rxjs6 = require("rxjs");
2235
2259
  var ScopeEventData = class {
2236
2260
  constructor(scope) {
2237
2261
  this.scope = scope;
2238
- this.event$ = new import_rxjs5.Subject();
2262
+ this.event$ = new import_rxjs6.Subject();
2239
2263
  scope.toDispose.pushAll([
2240
2264
  this.subscribe((_action) => {
2241
2265
  scope.variableEngine.fireGlobalEvent(_action);
@@ -2268,7 +2292,7 @@ var ScopeEventData = class {
2268
2292
  */
2269
2293
  on(type, observer) {
2270
2294
  return subsToDisposable(
2271
- this.event$.pipe((0, import_rxjs5.filter)((_action) => _action.type === type)).subscribe(observer)
2295
+ this.event$.pipe((0, import_rxjs6.filter)((_action) => _action.type === type)).subscribe(observer)
2272
2296
  );
2273
2297
  }
2274
2298
  };
@@ -2381,7 +2405,7 @@ var VariableEngine = class {
2381
2405
  /**
2382
2406
  * A rxjs subject that emits global events occurring within the variable engine.
2383
2407
  */
2384
- this.globalEvent$ = new import_rxjs6.Subject();
2408
+ this.globalEvent$ = new import_rxjs7.Subject();
2385
2409
  this.onScopeChangeEmitter = new import_utils9.Emitter();
2386
2410
  /**
2387
2411
  * A table containing all global variables.
@@ -2508,7 +2532,7 @@ VariableEngine = __decorateClass([
2508
2532
  ], VariableEngine);
2509
2533
 
2510
2534
  // src/services/variable-field-key-rename-service.ts
2511
- var import_lodash_es5 = require("lodash-es");
2535
+ var import_lodash_es6 = require("lodash-es");
2512
2536
  var import_inversify7 = require("inversify");
2513
2537
  var import_utils10 = require("@flowgram.ai/utils");
2514
2538
  var VariableFieldKeyRenameService = class {
@@ -2571,7 +2595,7 @@ var VariableFieldKeyRenameService = class {
2571
2595
  * @param next The list of fields after the change.
2572
2596
  */
2573
2597
  notifyFieldsDispose(prev, next) {
2574
- const removedFields = (0, import_lodash_es5.difference)(prev || [], next || []);
2598
+ const removedFields = (0, import_lodash_es6.difference)(prev || [], next || []);
2575
2599
  removedFields.forEach((_field) => this.disposeInListEmitter.fire(_field));
2576
2600
  }
2577
2601
  init() {