@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/esm/index.js CHANGED
@@ -395,6 +395,7 @@ import {
395
395
  tap
396
396
  } from "rxjs";
397
397
  import { nanoid } from "nanoid";
398
+ import { isNil, omitBy } from "lodash-es";
398
399
  import { shallowEqual } from "fast-equals";
399
400
  import { Disposable as Disposable2, DisposableCollection as DisposableCollection3 } from "@flowgram.ai/utils";
400
401
  var ASTNode = class _ASTNode {
@@ -453,6 +454,16 @@ var ASTNode = class _ASTNode {
453
454
  this.opts = opts;
454
455
  this.key = key || nanoid();
455
456
  this.fromJSON = this.withBatchUpdate(this.fromJSON.bind(this));
457
+ const rawToJSON = this.toJSON?.bind(this);
458
+ this.toJSON = () => omitBy(
459
+ {
460
+ // always include kind
461
+ kind: this.kind,
462
+ ...rawToJSON?.() || {}
463
+ },
464
+ // remove undefined fields
465
+ isNil
466
+ );
456
467
  }
457
468
  /**
458
469
  * The type of the ASTNode.
@@ -469,16 +480,6 @@ var ASTNode = class _ASTNode {
469
480
  get children() {
470
481
  return Array.from(this._children);
471
482
  }
472
- /**
473
- * Serializes the current ASTNode to ASTNodeJSON.
474
- * @returns
475
- */
476
- toJSON() {
477
- console.warn("[VariableEngine] Please Implement toJSON method for " + this.kind);
478
- return {
479
- kind: this.kind
480
- };
481
- }
482
483
  /**
483
484
  * Creates a child ASTNode.
484
485
  * @param json The AST JSON of the child ASTNode.
@@ -645,15 +646,6 @@ var BaseType = class extends ASTNode {
645
646
  getByKeyPath(keyPath = []) {
646
647
  throw new Error(`Get By Key Path is not implemented for Type: ${this.kind}`);
647
648
  }
648
- /**
649
- * Serialize the node to a JSON object.
650
- * @returns The JSON representation of the node.
651
- */
652
- toJSON() {
653
- return {
654
- kind: this.kind
655
- };
656
- }
657
649
  };
658
650
 
659
651
  // src/ast/type/array.ts
@@ -748,6 +740,15 @@ var StringType = class extends BaseType {
748
740
  this.fireChange();
749
741
  }
750
742
  }
743
+ /**
744
+ * Serialize the `StringType` to `StringJSON`.
745
+ * @returns The JSON representation of `StringType`.
746
+ */
747
+ toJSON() {
748
+ return {
749
+ format: this._format
750
+ };
751
+ }
751
752
  };
752
753
  StringType.kind = "String" /* String */;
753
754
 
@@ -763,6 +764,9 @@ var IntegerType = class extends BaseType {
763
764
  */
764
765
  fromJSON() {
765
766
  }
767
+ toJSON() {
768
+ return {};
769
+ }
766
770
  };
767
771
  IntegerType.kind = "Integer" /* Integer */;
768
772
 
@@ -774,6 +778,9 @@ var BooleanType = class extends BaseType {
774
778
  */
775
779
  fromJSON() {
776
780
  }
781
+ toJSON() {
782
+ return {};
783
+ }
777
784
  };
778
785
  BooleanType.kind = "Boolean" /* Boolean */;
779
786
 
@@ -785,6 +792,9 @@ var NumberType = class extends BaseType {
785
792
  */
786
793
  fromJSON() {
787
794
  }
795
+ toJSON() {
796
+ return {};
797
+ }
788
798
  };
789
799
  NumberType.kind = "Number" /* Number */;
790
800
 
@@ -890,7 +900,6 @@ var ObjectType = class extends BaseType {
890
900
  */
891
901
  toJSON() {
892
902
  return {
893
- kind: "Object" /* Object */,
894
903
  properties: this.properties.map((_property) => _property.toJSON())
895
904
  };
896
905
  }
@@ -974,6 +983,11 @@ var CustomType = class extends BaseType {
974
983
  }
975
984
  return targetTypeJSON?.kind === this.kind && targetTypeJSON?.typeName === this.typeName;
976
985
  }
986
+ toJSON() {
987
+ return {
988
+ typeName: this.typeName
989
+ };
990
+ }
977
991
  };
978
992
  CustomType.kind = "CustomType" /* CustomType */;
979
993
 
@@ -1108,6 +1122,7 @@ var EnumerateExpression = class extends BaseExpression {
1108
1122
  EnumerateExpression.kind = "EnumerateExpression" /* EnumerateExpression */;
1109
1123
 
1110
1124
  // src/ast/expression/keypath-expression.ts
1125
+ import { distinctUntilChanged as distinctUntilChanged3 } from "rxjs";
1111
1126
  import { shallowEqual as shallowEqual3 } from "fast-equals";
1112
1127
 
1113
1128
  // src/ast/utils/expression.ts
@@ -1148,12 +1163,14 @@ var KeyPathExpression = class extends BaseExpression {
1148
1163
  }
1149
1164
  }),
1150
1165
  subsToDisposable(
1151
- this.refs$.subscribe((_type) => {
1166
+ this.refs$.pipe(
1167
+ distinctUntilChanged3(
1168
+ (prev, next) => prev === next,
1169
+ (_refs) => _refs?.[0]?.type?.hash
1170
+ )
1171
+ ).subscribe((_type) => {
1152
1172
  const [ref] = this._refs;
1153
- if (this.prevRefTypeHash !== ref?.type?.hash) {
1154
- this.prevRefTypeHash = ref?.type?.hash;
1155
- this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
1156
- }
1173
+ this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
1157
1174
  })
1158
1175
  )
1159
1176
  ]);
@@ -1203,6 +1220,7 @@ var KeyPathExpression = class extends BaseExpression {
1203
1220
  const keyPath = this.parseToKeyPath(json);
1204
1221
  if (!shallowEqual3(keyPath, this._keyPath)) {
1205
1222
  this._keyPath = keyPath;
1223
+ this._rawPathJson = json;
1206
1224
  this.refreshRefs();
1207
1225
  }
1208
1226
  }
@@ -1219,10 +1237,7 @@ var KeyPathExpression = class extends BaseExpression {
1219
1237
  * @returns The JSON representation of `KeyPathExpression`.
1220
1238
  */
1221
1239
  toJSON() {
1222
- return {
1223
- kind: "KeyPathExpression" /* KeyPathExpression */,
1224
- keyPath: this._keyPath
1225
- };
1240
+ return this._rawPathJson;
1226
1241
  }
1227
1242
  };
1228
1243
  KeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
@@ -1288,6 +1303,7 @@ var LegacyKeyPathExpression = class extends BaseExpression {
1288
1303
  const keyPath = this.parseToKeyPath(json);
1289
1304
  if (!shallowEqual4(keyPath, this._keyPath)) {
1290
1305
  this._keyPath = keyPath;
1306
+ this._rawPathJson = json;
1291
1307
  this.refreshRefs();
1292
1308
  }
1293
1309
  }
@@ -1296,10 +1312,7 @@ var LegacyKeyPathExpression = class extends BaseExpression {
1296
1312
  * @returns The JSON representation of `KeyPathExpression`.
1297
1313
  */
1298
1314
  toJSON() {
1299
- return {
1300
- kind: "KeyPathExpression" /* KeyPathExpression */,
1301
- keyPath: this._keyPath
1302
- };
1315
+ return this._rawPathJson;
1303
1316
  }
1304
1317
  };
1305
1318
  LegacyKeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
@@ -1475,7 +1488,6 @@ var BaseVariableField = class extends ASTNode {
1475
1488
  */
1476
1489
  toJSON() {
1477
1490
  return {
1478
- kind: this.kind,
1479
1491
  key: this.key,
1480
1492
  type: this.type?.toJSON(),
1481
1493
  initializer: this.initializer?.toJSON(),
@@ -1516,6 +1528,16 @@ var VariableDeclaration = class extends BaseVariableField {
1516
1528
  this.fireChange();
1517
1529
  }
1518
1530
  }
1531
+ /**
1532
+ * Serialize the `VariableDeclaration` to `VariableDeclarationJSON`.
1533
+ * @returns The JSON representation of `VariableDeclaration`.
1534
+ */
1535
+ toJSON() {
1536
+ return {
1537
+ ...super.toJSON(),
1538
+ order: this.order
1539
+ };
1540
+ }
1519
1541
  };
1520
1542
  VariableDeclaration.kind = "VariableDeclaration" /* VariableDeclaration */;
1521
1543
 
@@ -1749,6 +1771,9 @@ var ASTRegisters = class {
1749
1771
  * Core AST node registration.
1750
1772
  */
1751
1773
  constructor() {
1774
+ /**
1775
+ * @deprecated Please use `@injectToAst(XXXService) declare xxxService: XXXService` to achieve external dependency injection.
1776
+ */
1752
1777
  this.injectors = /* @__PURE__ */ new Map();
1753
1778
  this.astMap = /* @__PURE__ */ new Map();
1754
1779
  this.registerAST(StringType);
@@ -1808,7 +1833,6 @@ var ASTRegisters = class {
1808
1833
  /**
1809
1834
  * Registers an AST node.
1810
1835
  * @param ASTNode
1811
- * @param injector
1812
1836
  */
1813
1837
  registerAST(ASTNode2, injector) {
1814
1838
  this.astMap.set(ASTNode2.kind, ASTNode2);
@@ -2000,7 +2024,7 @@ import {
2000
2024
  Subject as Subject3,
2001
2025
  animationFrameScheduler as animationFrameScheduler2,
2002
2026
  debounceTime as debounceTime2,
2003
- distinctUntilChanged as distinctUntilChanged3,
2027
+ distinctUntilChanged as distinctUntilChanged4,
2004
2028
  map as map3,
2005
2029
  merge as merge2,
2006
2030
  share as share3,
@@ -2027,7 +2051,7 @@ var ScopeAvailableData = class {
2027
2051
  // Map to the flattened list of variables from all dependency scopes.
2028
2052
  map3(() => flatten(this.depScopes.map((scope) => scope.output.variables || []))),
2029
2053
  // Use shallow equality to check if the variable list has changed.
2030
- distinctUntilChanged3(shallowEqual7),
2054
+ distinctUntilChanged4(shallowEqual7),
2031
2055
  share3()
2032
2056
  );
2033
2057
  /**
@@ -2168,7 +2192,7 @@ var ScopeAvailableData = class {
2168
2192
  const v = this.getByKeyPath(keyPath);
2169
2193
  return selector ? selector(v) : v;
2170
2194
  }),
2171
- distinctUntilChanged3(
2195
+ distinctUntilChanged4(
2172
2196
  (a, b) => shallowEqual7(a, b),
2173
2197
  (value) => {
2174
2198
  if (value instanceof ASTNode) {