mobx-state-tree 5.3.1-alpha.1 → 5.4.0

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.
@@ -1458,6 +1458,12 @@ var ObjectNode = /** @class */ (function (_super) {
1458
1458
  writable: true,
1459
1459
  value: void 0
1460
1460
  });
1461
+ Object.defineProperty(_this, "hasSnapshotPostProcessor", {
1462
+ enumerable: true,
1463
+ configurable: true,
1464
+ writable: true,
1465
+ value: false
1466
+ });
1461
1467
  Object.defineProperty(_this, "_applyPatches", {
1462
1468
  enumerable: true,
1463
1469
  configurable: true,
@@ -1788,6 +1794,9 @@ var ObjectNode = /** @class */ (function (_super) {
1788
1794
  Object.defineProperty(ObjectNode.prototype, "snapshot", {
1789
1795
  // advantage of using computed for a snapshot is that nicely respects transactions etc.
1790
1796
  get: function () {
1797
+ if (this.hasSnapshotPostProcessor) {
1798
+ this.createObservableInstanceIfNeeded();
1799
+ }
1791
1800
  return this._snapshotComputed.get();
1792
1801
  },
1793
1802
  enumerable: false,
@@ -4700,9 +4709,9 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4700
4709
  enumerable: false,
4701
4710
  configurable: true,
4702
4711
  writable: true,
4703
- value: function (sn) {
4712
+ value: function (sn, node) {
4704
4713
  if (this._processors.postProcessor) {
4705
- return this._processors.postProcessor.call(null, sn);
4714
+ return this._processors.postProcessor.call(null, sn, node.storedValue);
4706
4715
  }
4707
4716
  return sn;
4708
4717
  }
@@ -4715,10 +4724,11 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4715
4724
  var _this = this;
4716
4725
  // the node has to use these methods rather than the original type ones
4717
4726
  proxyNodeTypeMethods(node.type, this, "create");
4727
+ if (node instanceof ObjectNode) {
4728
+ node.hasSnapshotPostProcessor = !!this._processors.postProcessor;
4729
+ }
4718
4730
  var oldGetSnapshot = node.getSnapshot;
4719
- node.getSnapshot = function () {
4720
- return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4721
- };
4731
+ node.getSnapshot = function () { return _this.postProcessSnapshot(oldGetSnapshot.call(node), node); };
4722
4732
  if (!isUnionType(this._subtype)) {
4723
4733
  node.getReconciliationType = function () {
4724
4734
  return _this;
@@ -4758,7 +4768,7 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4758
4768
  value: function (node, applyPostProcess) {
4759
4769
  if (applyPostProcess === void 0) { applyPostProcess = true; }
4760
4770
  var sn = this._subtype.getSnapshot(node);
4761
- return applyPostProcess ? this.postProcessSnapshot(sn) : sn;
4771
+ return applyPostProcess ? this.postProcessSnapshot(sn, node) : sn;
4762
4772
  }
4763
4773
  });
4764
4774
  Object.defineProperty(SnapshotProcessor.prototype, "isValidSnapshot", {
@@ -4849,15 +4859,17 @@ function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
4849
4859
  * interface BackendTodo {
4850
4860
  * text: string | null
4851
4861
  * }
4862
+ *
4852
4863
  * const Todo2 = types.snapshotProcessor(Todo1, {
4853
4864
  * // from snapshot to instance
4854
- * preProcessor(sn: BackendTodo) {
4865
+ * preProcessor(snapshot: BackendTodo) {
4855
4866
  * return {
4856
4867
  * text: sn.text || "";
4857
4868
  * }
4858
4869
  * },
4870
+ *
4859
4871
  * // from instance to snapshot
4860
- * postProcessor(sn): BackendTodo {
4872
+ * postProcessor(snapshot, node): BackendTodo {
4861
4873
  * return {
4862
4874
  * text: !sn.text ? null : sn.text
4863
4875
  * }
@@ -5855,27 +5867,34 @@ function toPropertiesObject(declaredProps) {
5855
5867
  if (key in Hook) {
5856
5868
  throw fail$1("Hook '" + key + "' was defined as property. Hooks should be defined as part of the actions");
5857
5869
  }
5870
+ // the user intended to use a view
5858
5871
  var descriptor = Object.getOwnPropertyDescriptor(declaredProps, key);
5859
5872
  if ("get" in descriptor) {
5860
5873
  throw fail$1("Getters are not supported as properties. Please use views instead");
5861
5874
  }
5875
+ // undefined and null are not valid
5862
5876
  var value = descriptor.value;
5863
5877
  if (value === null || value === undefined) {
5864
5878
  throw fail$1("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
5865
5879
  }
5880
+ // its a primitive, convert to its type
5866
5881
  else if (isPrimitive(value)) {
5867
5882
  props[key] = optional(getPrimitiveFactoryFromValue(value), value);
5868
5883
  }
5884
+ // map defaults to empty object automatically for models
5869
5885
  else if (value instanceof MapType) {
5870
5886
  props[key] = optional(value, {});
5871
5887
  }
5872
5888
  else if (value instanceof ArrayType) {
5873
5889
  props[key] = optional(value, []);
5874
5890
  }
5891
+ // its already a type
5875
5892
  else if (isType(value)) ;
5893
+ // its a function, maybe the user wanted a view?
5876
5894
  else if (devMode() && typeof value === "function") {
5877
5895
  throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?");
5878
5896
  }
5897
+ // no other complex values
5879
5898
  else if (devMode() && typeof value === "object") {
5880
5899
  throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed an object. Try passing another model type or a types.frozen.");
5881
5900
  }
@@ -8524,4 +8543,4 @@ var types = {
8524
8543
  snapshotProcessor: snapshotProcessor
8525
8544
  };
8526
8545
 
8527
- export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setLivelinessChecking, setLivelynessChecking, splitJsonPath, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
8546
+ export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setLivelinessChecking, setLivelynessChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
@@ -1462,6 +1462,12 @@
1462
1462
  writable: true,
1463
1463
  value: void 0
1464
1464
  });
1465
+ Object.defineProperty(_this, "hasSnapshotPostProcessor", {
1466
+ enumerable: true,
1467
+ configurable: true,
1468
+ writable: true,
1469
+ value: false
1470
+ });
1465
1471
  Object.defineProperty(_this, "_applyPatches", {
1466
1472
  enumerable: true,
1467
1473
  configurable: true,
@@ -1792,6 +1798,9 @@
1792
1798
  Object.defineProperty(ObjectNode.prototype, "snapshot", {
1793
1799
  // advantage of using computed for a snapshot is that nicely respects transactions etc.
1794
1800
  get: function () {
1801
+ if (this.hasSnapshotPostProcessor) {
1802
+ this.createObservableInstanceIfNeeded();
1803
+ }
1795
1804
  return this._snapshotComputed.get();
1796
1805
  },
1797
1806
  enumerable: false,
@@ -4682,9 +4691,9 @@
4682
4691
  enumerable: false,
4683
4692
  configurable: true,
4684
4693
  writable: true,
4685
- value: function (sn) {
4694
+ value: function (sn, node) {
4686
4695
  if (this._processors.postProcessor) {
4687
- return this._processors.postProcessor.call(null, sn);
4696
+ return this._processors.postProcessor.call(null, sn, node.storedValue);
4688
4697
  }
4689
4698
  return sn;
4690
4699
  }
@@ -4697,10 +4706,11 @@
4697
4706
  var _this = this;
4698
4707
  // the node has to use these methods rather than the original type ones
4699
4708
  proxyNodeTypeMethods(node.type, this, "create");
4709
+ if (node instanceof ObjectNode) {
4710
+ node.hasSnapshotPostProcessor = !!this._processors.postProcessor;
4711
+ }
4700
4712
  var oldGetSnapshot = node.getSnapshot;
4701
- node.getSnapshot = function () {
4702
- return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4703
- };
4713
+ node.getSnapshot = function () { return _this.postProcessSnapshot(oldGetSnapshot.call(node), node); };
4704
4714
  if (!isUnionType(this._subtype)) {
4705
4715
  node.getReconciliationType = function () {
4706
4716
  return _this;
@@ -4740,7 +4750,7 @@
4740
4750
  value: function (node, applyPostProcess) {
4741
4751
  if (applyPostProcess === void 0) { applyPostProcess = true; }
4742
4752
  var sn = this._subtype.getSnapshot(node);
4743
- return applyPostProcess ? this.postProcessSnapshot(sn) : sn;
4753
+ return applyPostProcess ? this.postProcessSnapshot(sn, node) : sn;
4744
4754
  }
4745
4755
  });
4746
4756
  Object.defineProperty(SnapshotProcessor.prototype, "isValidSnapshot", {
@@ -4831,15 +4841,17 @@
4831
4841
  * interface BackendTodo {
4832
4842
  * text: string | null
4833
4843
  * }
4844
+ *
4834
4845
  * const Todo2 = types.snapshotProcessor(Todo1, {
4835
4846
  * // from snapshot to instance
4836
- * preProcessor(sn: BackendTodo) {
4847
+ * preProcessor(snapshot: BackendTodo) {
4837
4848
  * return {
4838
4849
  * text: sn.text || "";
4839
4850
  * }
4840
4851
  * },
4852
+ *
4841
4853
  * // from instance to snapshot
4842
- * postProcessor(sn): BackendTodo {
4854
+ * postProcessor(snapshot, node): BackendTodo {
4843
4855
  * return {
4844
4856
  * text: !sn.text ? null : sn.text
4845
4857
  * }
@@ -5837,27 +5849,34 @@
5837
5849
  if (key in Hook) {
5838
5850
  throw fail$1("Hook '" + key + "' was defined as property. Hooks should be defined as part of the actions");
5839
5851
  }
5852
+ // the user intended to use a view
5840
5853
  var descriptor = Object.getOwnPropertyDescriptor(declaredProps, key);
5841
5854
  if ("get" in descriptor) {
5842
5855
  throw fail$1("Getters are not supported as properties. Please use views instead");
5843
5856
  }
5857
+ // undefined and null are not valid
5844
5858
  var value = descriptor.value;
5845
5859
  if (value === null || value === undefined) {
5846
5860
  throw fail$1("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
5847
5861
  }
5862
+ // its a primitive, convert to its type
5848
5863
  else if (isPrimitive(value)) {
5849
5864
  props[key] = optional(getPrimitiveFactoryFromValue(value), value);
5850
5865
  }
5866
+ // map defaults to empty object automatically for models
5851
5867
  else if (value instanceof MapType) {
5852
5868
  props[key] = optional(value, {});
5853
5869
  }
5854
5870
  else if (value instanceof ArrayType) {
5855
5871
  props[key] = optional(value, []);
5856
5872
  }
5873
+ // its already a type
5857
5874
  else if (isType(value)) ;
5875
+ // its a function, maybe the user wanted a view?
5858
5876
  else if (typeof value === "function") {
5859
5877
  throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?");
5860
5878
  }
5879
+ // no other complex values
5861
5880
  else if (typeof value === "object") {
5862
5881
  throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed an object. Try passing another model type or a types.frozen.");
5863
5882
  }
@@ -8574,6 +8593,7 @@
8574
8593
  exports.setLivelinessChecking = setLivelinessChecking;
8575
8594
  exports.setLivelynessChecking = setLivelynessChecking;
8576
8595
  exports.splitJsonPath = splitJsonPath;
8596
+ exports.t = types;
8577
8597
  exports.toGenerator = toGenerator;
8578
8598
  exports.toGeneratorFunction = toGeneratorFunction;
8579
8599
  exports.tryReference = tryReference;