mobx-state-tree 5.0.3 → 5.1.2

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.
@@ -576,7 +576,7 @@ function resolveIdentifier(type, target, identifier) {
576
576
  assertIsStateTreeNode(target, 2);
577
577
  assertIsValidIdentifier(identifier, 3);
578
578
  var node = getStateTreeNode(target).root.identifierCache.resolve(type, normalizeIdentifier(identifier));
579
- return node ? node.value : undefined;
579
+ return node === null || node === void 0 ? void 0 : node.value;
580
580
  }
581
581
  /**
582
582
  * Returns the identifier of the target node.
@@ -1126,6 +1126,14 @@ var BaseNode = /** @class */ (function () {
1126
1126
  enumerable: false,
1127
1127
  configurable: true
1128
1128
  });
1129
+ Object.defineProperty(BaseNode.prototype, "getReconciliationType", {
1130
+ enumerable: false,
1131
+ configurable: true,
1132
+ writable: true,
1133
+ value: function () {
1134
+ return this.type;
1135
+ }
1136
+ });
1129
1137
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1130
1138
  enumerable: false,
1131
1139
  configurable: true,
@@ -3918,14 +3926,12 @@ function isMutable(value) {
3918
3926
  */
3919
3927
  function isPrimitive(value, includeDate) {
3920
3928
  if (includeDate === void 0) { includeDate = true; }
3921
- if (value === null || value === undefined)
3922
- return true;
3923
- if (typeof value === "string" ||
3929
+ return (value === null ||
3930
+ value === undefined ||
3931
+ typeof value === "string" ||
3924
3932
  typeof value === "number" ||
3925
3933
  typeof value === "boolean" ||
3926
- (includeDate && value instanceof Date))
3927
- return true;
3928
- return false;
3934
+ (includeDate && value instanceof Date));
3929
3935
  }
3930
3936
  /**
3931
3937
  * @internal
@@ -4582,6 +4588,8 @@ function splitJsonPath(path) {
4582
4588
  return parts;
4583
4589
  }
4584
4590
 
4591
+ /** @hidden */
4592
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4585
4593
  var SnapshotProcessor = /** @class */ (function (_super) {
4586
4594
  __extends(SnapshotProcessor, _super);
4587
4595
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4626,6 +4634,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4626
4634
  return sn;
4627
4635
  }
4628
4636
  });
4637
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4638
+ enumerable: false,
4639
+ configurable: true,
4640
+ writable: true,
4641
+ value: function (sn) {
4642
+ try {
4643
+ return this.preProcessSnapshot(sn);
4644
+ }
4645
+ catch (e) {
4646
+ return $preProcessorFailed;
4647
+ }
4648
+ }
4649
+ });
4629
4650
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4630
4651
  enumerable: false,
4631
4652
  configurable: true,
@@ -4644,11 +4665,14 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4644
4665
  value: function (node) {
4645
4666
  var _this = this;
4646
4667
  // the node has to use these methods rather than the original type ones
4647
- proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
4668
+ proxyNodeTypeMethods(node.type, this, "create");
4648
4669
  var oldGetSnapshot = node.getSnapshot;
4649
4670
  node.getSnapshot = function () {
4650
4671
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4651
4672
  };
4673
+ node.getReconciliationType = function () {
4674
+ return _this;
4675
+ };
4652
4676
  }
4653
4677
  });
4654
4678
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4691,7 +4715,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4691
4715
  configurable: true,
4692
4716
  writable: true,
4693
4717
  value: function (value, context) {
4694
- var processedSn = this.preProcessSnapshot(value);
4718
+ var processedSn = this.preProcessSnapshotSafe(value);
4719
+ if (processedSn === $preProcessorFailed) {
4720
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4721
+ }
4695
4722
  return this._subtype.validate(processedSn, context);
4696
4723
  }
4697
4724
  });
@@ -4712,7 +4739,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4712
4739
  ? this._subtype
4713
4740
  : isStateTreeNode(thing)
4714
4741
  ? getSnapshot(thing, false)
4715
- : this.preProcessSnapshot(thing);
4742
+ : this.preProcessSnapshotSafe(thing);
4743
+ if (value === $preProcessorFailed) {
4744
+ return false;
4745
+ }
4716
4746
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4717
4747
  }
4718
4748
  });
@@ -4733,7 +4763,7 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4733
4763
  return false;
4734
4764
  }
4735
4765
  var processedSn = this.preProcessSnapshot(snapshot);
4736
- return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
4766
+ return this._subtype.isMatchingSnapshotId(current, processedSn);
4737
4767
  }
4738
4768
  });
4739
4769
  return SnapshotProcessor;
@@ -4997,18 +5027,17 @@ var MapType = /** @class */ (function (_super) {
4997
5027
  }
4998
5028
  var modelTypes = [];
4999
5029
  if (tryCollectModelTypes(this._subType, modelTypes)) {
5000
- var identifierAttribute_1 = undefined;
5001
- modelTypes.forEach(function (type) {
5002
- if (type.identifierAttribute) {
5003
- if (identifierAttribute_1 && identifierAttribute_1 !== type.identifierAttribute) {
5004
- throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + identifierAttribute_1 + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
5005
- }
5006
- identifierAttribute_1 = type.identifierAttribute;
5030
+ var identifierAttribute = modelTypes.reduce(function (current, type) {
5031
+ if (!type.identifierAttribute)
5032
+ return current;
5033
+ if (current && current !== type.identifierAttribute) {
5034
+ throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + current + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
5007
5035
  }
5008
- });
5009
- if (identifierAttribute_1) {
5036
+ return type.identifierAttribute;
5037
+ }, undefined);
5038
+ if (identifierAttribute) {
5010
5039
  this.identifierMode = MapIdentifierMode.YES;
5011
- this.mapIdentifierAttribute = identifierAttribute_1;
5040
+ this.mapIdentifierAttribute = identifierAttribute;
5012
5041
  }
5013
5042
  else {
5014
5043
  this.identifierMode = MapIdentifierMode.NO;
@@ -5727,13 +5756,17 @@ function areSame(oldNode, newValue) {
5727
5756
  if (oldNode.snapshot === newValue) {
5728
5757
  return true;
5729
5758
  }
5759
+ // Non object nodes don't get reconciled
5760
+ if (!(oldNode instanceof ObjectNode)) {
5761
+ return false;
5762
+ }
5763
+ var oldNodeType = oldNode.getReconciliationType();
5730
5764
  // new value is a snapshot with the correct identifier
5731
- return (oldNode instanceof ObjectNode &&
5732
- oldNode.identifier !== null &&
5765
+ return (oldNode.identifier !== null &&
5733
5766
  oldNode.identifierAttribute &&
5734
5767
  isPlainObject(newValue) &&
5735
- oldNode.type.isMatchingSnapshotId(oldNode, newValue) &&
5736
- oldNode.type.is(newValue));
5768
+ oldNodeType.is(newValue) &&
5769
+ oldNodeType.isMatchingSnapshotId(oldNode, newValue));
5737
5770
  }
5738
5771
  /**
5739
5772
  * Returns if a given value represents an array type.
@@ -6887,7 +6920,7 @@ var Union = /** @class */ (function (_super) {
6887
6920
  configurable: true,
6888
6921
  writable: true,
6889
6922
  value: function (current, newValue, parent, subpath) {
6890
- var type = this.determineType(newValue, current.type);
6923
+ var type = this.determineType(newValue, current.getReconciliationType());
6891
6924
  if (!type)
6892
6925
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6893
6926
  return type.reconcile(current, newValue, parent, subpath);
@@ -580,7 +580,7 @@
580
580
  assertIsStateTreeNode(target, 2);
581
581
  assertIsValidIdentifier(identifier, 3);
582
582
  var node = getStateTreeNode(target).root.identifierCache.resolve(type, normalizeIdentifier(identifier));
583
- return node ? node.value : undefined;
583
+ return node === null || node === void 0 ? void 0 : node.value;
584
584
  }
585
585
  /**
586
586
  * Returns the identifier of the target node.
@@ -1130,6 +1130,14 @@
1130
1130
  enumerable: false,
1131
1131
  configurable: true
1132
1132
  });
1133
+ Object.defineProperty(BaseNode.prototype, "getReconciliationType", {
1134
+ enumerable: false,
1135
+ configurable: true,
1136
+ writable: true,
1137
+ value: function () {
1138
+ return this.type;
1139
+ }
1140
+ });
1133
1141
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1134
1142
  enumerable: false,
1135
1143
  configurable: true,
@@ -3922,14 +3930,12 @@
3922
3930
  */
3923
3931
  function isPrimitive(value, includeDate) {
3924
3932
  if (includeDate === void 0) { includeDate = true; }
3925
- if (value === null || value === undefined)
3926
- return true;
3927
- if (typeof value === "string" ||
3933
+ return (value === null ||
3934
+ value === undefined ||
3935
+ typeof value === "string" ||
3928
3936
  typeof value === "number" ||
3929
3937
  typeof value === "boolean" ||
3930
- (includeDate && value instanceof Date))
3931
- return true;
3932
- return false;
3938
+ (includeDate && value instanceof Date));
3933
3939
  }
3934
3940
  /**
3935
3941
  * @internal
@@ -4564,6 +4570,8 @@
4564
4570
  return parts;
4565
4571
  }
4566
4572
 
4573
+ /** @hidden */
4574
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4567
4575
  var SnapshotProcessor = /** @class */ (function (_super) {
4568
4576
  __extends(SnapshotProcessor, _super);
4569
4577
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4608,6 +4616,19 @@
4608
4616
  return sn;
4609
4617
  }
4610
4618
  });
4619
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4620
+ enumerable: false,
4621
+ configurable: true,
4622
+ writable: true,
4623
+ value: function (sn) {
4624
+ try {
4625
+ return this.preProcessSnapshot(sn);
4626
+ }
4627
+ catch (e) {
4628
+ return $preProcessorFailed;
4629
+ }
4630
+ }
4631
+ });
4611
4632
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4612
4633
  enumerable: false,
4613
4634
  configurable: true,
@@ -4626,11 +4647,14 @@
4626
4647
  value: function (node) {
4627
4648
  var _this = this;
4628
4649
  // the node has to use these methods rather than the original type ones
4629
- proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
4650
+ proxyNodeTypeMethods(node.type, this, "create");
4630
4651
  var oldGetSnapshot = node.getSnapshot;
4631
4652
  node.getSnapshot = function () {
4632
4653
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4633
4654
  };
4655
+ node.getReconciliationType = function () {
4656
+ return _this;
4657
+ };
4634
4658
  }
4635
4659
  });
4636
4660
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4673,7 +4697,10 @@
4673
4697
  configurable: true,
4674
4698
  writable: true,
4675
4699
  value: function (value, context) {
4676
- var processedSn = this.preProcessSnapshot(value);
4700
+ var processedSn = this.preProcessSnapshotSafe(value);
4701
+ if (processedSn === $preProcessorFailed) {
4702
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4703
+ }
4677
4704
  return this._subtype.validate(processedSn, context);
4678
4705
  }
4679
4706
  });
@@ -4694,7 +4721,10 @@
4694
4721
  ? this._subtype
4695
4722
  : isStateTreeNode(thing)
4696
4723
  ? getSnapshot(thing, false)
4697
- : this.preProcessSnapshot(thing);
4724
+ : this.preProcessSnapshotSafe(thing);
4725
+ if (value === $preProcessorFailed) {
4726
+ return false;
4727
+ }
4698
4728
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4699
4729
  }
4700
4730
  });
@@ -4715,7 +4745,7 @@
4715
4745
  return false;
4716
4746
  }
4717
4747
  var processedSn = this.preProcessSnapshot(snapshot);
4718
- return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
4748
+ return this._subtype.isMatchingSnapshotId(current, processedSn);
4719
4749
  }
4720
4750
  });
4721
4751
  return SnapshotProcessor;
@@ -4979,18 +5009,17 @@
4979
5009
  }
4980
5010
  var modelTypes = [];
4981
5011
  if (tryCollectModelTypes(this._subType, modelTypes)) {
4982
- var identifierAttribute_1 = undefined;
4983
- modelTypes.forEach(function (type) {
4984
- if (type.identifierAttribute) {
4985
- if (identifierAttribute_1 && identifierAttribute_1 !== type.identifierAttribute) {
4986
- throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + identifierAttribute_1 + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
4987
- }
4988
- identifierAttribute_1 = type.identifierAttribute;
5012
+ var identifierAttribute = modelTypes.reduce(function (current, type) {
5013
+ if (!type.identifierAttribute)
5014
+ return current;
5015
+ if (current && current !== type.identifierAttribute) {
5016
+ throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + current + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
4989
5017
  }
4990
- });
4991
- if (identifierAttribute_1) {
5018
+ return type.identifierAttribute;
5019
+ }, undefined);
5020
+ if (identifierAttribute) {
4992
5021
  this.identifierMode = MapIdentifierMode.YES;
4993
- this.mapIdentifierAttribute = identifierAttribute_1;
5022
+ this.mapIdentifierAttribute = identifierAttribute;
4994
5023
  }
4995
5024
  else {
4996
5025
  this.identifierMode = MapIdentifierMode.NO;
@@ -5709,13 +5738,17 @@
5709
5738
  if (oldNode.snapshot === newValue) {
5710
5739
  return true;
5711
5740
  }
5741
+ // Non object nodes don't get reconciled
5742
+ if (!(oldNode instanceof ObjectNode)) {
5743
+ return false;
5744
+ }
5745
+ var oldNodeType = oldNode.getReconciliationType();
5712
5746
  // new value is a snapshot with the correct identifier
5713
- return (oldNode instanceof ObjectNode &&
5714
- oldNode.identifier !== null &&
5747
+ return (oldNode.identifier !== null &&
5715
5748
  oldNode.identifierAttribute &&
5716
5749
  isPlainObject(newValue) &&
5717
- oldNode.type.isMatchingSnapshotId(oldNode, newValue) &&
5718
- oldNode.type.is(newValue));
5750
+ oldNodeType.is(newValue) &&
5751
+ oldNodeType.isMatchingSnapshotId(oldNode, newValue));
5719
5752
  }
5720
5753
  /**
5721
5754
  * Returns if a given value represents an array type.
@@ -6869,7 +6902,7 @@
6869
6902
  configurable: true,
6870
6903
  writable: true,
6871
6904
  value: function (current, newValue, parent, subpath) {
6872
- var type = this.determineType(newValue, current.type);
6905
+ var type = this.determineType(newValue, current.getReconciliationType());
6873
6906
  if (!type)
6874
6907
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6875
6908
  return type.reconcile(current, newValue, parent, subpath);