mobx-state-tree 5.0.2 → 5.1.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.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![npm version](https://badge.fury.io/js/mobx-state-tree.svg)](https://badge.fury.io/js/mobx-state-tree)
6
6
  [![CircleCI](https://circleci.com/gh/mobxjs/mobx-state-tree.svg?style=svg)](https://circleci.com/gh/mobxjs/mobx-state-tree)
7
- [![Coverage Status](https://coveralls.io/repos/github/mobxjs/mobx-state-tree/badge.svg?branch=master)](https://coveralls.io/github/mobxjs/mobx-state-tree?branch=master)
7
+ [![Codecov](https://codecov.io/gh/mobxjs/mobx-state-tree/branch/master/graph/badge.svg?token=BFkuCB6CtL)](https://codecov.io/gh/mobxjs/mobx-state-tree)
8
8
  [![Have a question? Ask on GitHub Discussions!](https://img.shields.io/badge/Have%20a%20question%3F-Ask%20on%20GitHub%20Discussions!-blue)](https://github.com/mobxjs/mobx-state-tree/discussions)
9
9
 
10
10
  ## What is mobx-state-tree?
@@ -68,11 +68,13 @@ PERFORMANCE OF THIS SOFTWARE.
68
68
  var extendStatics = function(d, b) {
69
69
  extendStatics = Object.setPrototypeOf ||
70
70
  ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
71
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
71
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
72
72
  return extendStatics(d, b);
73
73
  };
74
74
 
75
75
  function __extends(d, b) {
76
+ if (typeof b !== "function" && b !== null)
77
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
76
78
  extendStatics(d, b);
77
79
  function __() { this.constructor = d; }
78
80
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
@@ -158,6 +160,7 @@ function __read(o, n) {
158
160
  return ar;
159
161
  }
160
162
 
163
+ /** @deprecated */
161
164
  function __spread() {
162
165
  for (var ar = [], i = 0; i < arguments.length; i++)
163
166
  ar = ar.concat(__read(arguments[i]));
@@ -577,7 +580,7 @@ function resolveIdentifier(type, target, identifier) {
577
580
  assertIsStateTreeNode(target, 2);
578
581
  assertIsValidIdentifier(identifier, 3);
579
582
  var node = getStateTreeNode(target).root.identifierCache.resolve(type, normalizeIdentifier(identifier));
580
- return node ? node.value : undefined;
583
+ return node === null || node === void 0 ? void 0 : node.value;
581
584
  }
582
585
  /**
583
586
  * Returns the identifier of the target node.
@@ -1127,6 +1130,14 @@ var BaseNode = /** @class */ (function () {
1127
1130
  enumerable: false,
1128
1131
  configurable: true
1129
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
+ });
1130
1141
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1131
1142
  enumerable: false,
1132
1143
  configurable: true,
@@ -2465,6 +2476,16 @@ var ComplexType = /** @class */ (function (_super) {
2465
2476
  return node.storedValue;
2466
2477
  }
2467
2478
  });
2479
+ Object.defineProperty(ComplexType.prototype, "isMatchingSnapshotId", {
2480
+ enumerable: false,
2481
+ configurable: true,
2482
+ writable: true,
2483
+ value: function (current, snapshot) {
2484
+ return (!current.identifierAttribute ||
2485
+ current.identifier ===
2486
+ normalizeIdentifier(snapshot[current.identifierAttribute]));
2487
+ }
2488
+ });
2468
2489
  Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
2469
2490
  enumerable: false,
2470
2491
  configurable: true,
@@ -2483,9 +2504,7 @@ var ComplexType = /** @class */ (function (_super) {
2483
2504
  if (current.type === this &&
2484
2505
  isMutable(newValue) &&
2485
2506
  !isStateTreeNode(newValue) &&
2486
- (!current.identifierAttribute ||
2487
- current.identifier ===
2488
- normalizeIdentifier(newValue[current.identifierAttribute]))) {
2507
+ this.isMatchingSnapshotId(current, newValue)) {
2489
2508
  // the newValue has no node, so can be treated like a snapshot
2490
2509
  // we can reconcile
2491
2510
  current.applySnapshot(newValue);
@@ -3911,14 +3930,12 @@ function isMutable(value) {
3911
3930
  */
3912
3931
  function isPrimitive(value, includeDate) {
3913
3932
  if (includeDate === void 0) { includeDate = true; }
3914
- if (value === null || value === undefined)
3915
- return true;
3916
- if (typeof value === "string" ||
3933
+ return (value === null ||
3934
+ value === undefined ||
3935
+ typeof value === "string" ||
3917
3936
  typeof value === "number" ||
3918
3937
  typeof value === "boolean" ||
3919
- (includeDate && value instanceof Date))
3920
- return true;
3921
- return false;
3938
+ (includeDate && value instanceof Date));
3922
3939
  }
3923
3940
  /**
3924
3941
  * @internal
@@ -4575,6 +4592,8 @@ function splitJsonPath(path) {
4575
4592
  return parts;
4576
4593
  }
4577
4594
 
4595
+ /** @hidden */
4596
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4578
4597
  var SnapshotProcessor = /** @class */ (function (_super) {
4579
4598
  __extends(SnapshotProcessor, _super);
4580
4599
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4619,6 +4638,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4619
4638
  return sn;
4620
4639
  }
4621
4640
  });
4641
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4642
+ enumerable: false,
4643
+ configurable: true,
4644
+ writable: true,
4645
+ value: function (sn) {
4646
+ try {
4647
+ return this.preProcessSnapshot(sn);
4648
+ }
4649
+ catch (e) {
4650
+ return $preProcessorFailed;
4651
+ }
4652
+ }
4653
+ });
4622
4654
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4623
4655
  enumerable: false,
4624
4656
  configurable: true,
@@ -4642,6 +4674,9 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4642
4674
  node.getSnapshot = function () {
4643
4675
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4644
4676
  };
4677
+ node.getReconciliationType = function () {
4678
+ return _this;
4679
+ };
4645
4680
  }
4646
4681
  });
4647
4682
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4684,7 +4719,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4684
4719
  configurable: true,
4685
4720
  writable: true,
4686
4721
  value: function (value, context) {
4687
- var processedSn = this.preProcessSnapshot(value);
4722
+ var processedSn = this.preProcessSnapshotSafe(value);
4723
+ if (processedSn === $preProcessorFailed) {
4724
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4725
+ }
4688
4726
  return this._subtype.validate(processedSn, context);
4689
4727
  }
4690
4728
  });
@@ -4705,7 +4743,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4705
4743
  ? this._subtype
4706
4744
  : isStateTreeNode(thing)
4707
4745
  ? getSnapshot(thing, false)
4708
- : this.preProcessSnapshot(thing);
4746
+ : this.preProcessSnapshotSafe(thing);
4747
+ if (value === $preProcessorFailed) {
4748
+ return false;
4749
+ }
4709
4750
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4710
4751
  }
4711
4752
  });
@@ -4717,6 +4758,18 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4717
4758
  return this._subtype.isAssignableFrom(type);
4718
4759
  }
4719
4760
  });
4761
+ Object.defineProperty(SnapshotProcessor.prototype, "isMatchingSnapshotId", {
4762
+ enumerable: false,
4763
+ configurable: true,
4764
+ writable: true,
4765
+ value: function (current, snapshot) {
4766
+ if (!(this._subtype instanceof ComplexType)) {
4767
+ return false;
4768
+ }
4769
+ var processedSn = this.preProcessSnapshot(snapshot);
4770
+ return this._subtype.isMatchingSnapshotId(current, processedSn);
4771
+ }
4772
+ });
4720
4773
  return SnapshotProcessor;
4721
4774
  }(BaseType));
4722
4775
  function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
@@ -4978,18 +5031,17 @@ var MapType = /** @class */ (function (_super) {
4978
5031
  }
4979
5032
  var modelTypes = [];
4980
5033
  if (tryCollectModelTypes(this._subType, modelTypes)) {
4981
- var identifierAttribute_1 = undefined;
4982
- modelTypes.forEach(function (type) {
4983
- if (type.identifierAttribute) {
4984
- if (identifierAttribute_1 && identifierAttribute_1 !== type.identifierAttribute) {
4985
- 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");
4986
- }
4987
- identifierAttribute_1 = type.identifierAttribute;
5034
+ var identifierAttribute = modelTypes.reduce(function (current, type) {
5035
+ if (!type.identifierAttribute)
5036
+ return current;
5037
+ if (current && current !== type.identifierAttribute) {
5038
+ 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");
4988
5039
  }
4989
- });
4990
- if (identifierAttribute_1) {
5040
+ return type.identifierAttribute;
5041
+ }, undefined);
5042
+ if (identifierAttribute) {
4991
5043
  this.identifierMode = MapIdentifierMode.YES;
4992
- this.mapIdentifierAttribute = identifierAttribute_1;
5044
+ this.mapIdentifierAttribute = identifierAttribute;
4993
5045
  }
4994
5046
  else {
4995
5047
  this.identifierMode = MapIdentifierMode.NO;
@@ -5708,13 +5760,17 @@ function areSame(oldNode, newValue) {
5708
5760
  if (oldNode.snapshot === newValue) {
5709
5761
  return true;
5710
5762
  }
5763
+ // Non object nodes don't get reconciled
5764
+ if (!(oldNode instanceof ObjectNode)) {
5765
+ return false;
5766
+ }
5767
+ var oldNodeType = oldNode.getReconciliationType();
5711
5768
  // new value is a snapshot with the correct identifier
5712
- return (oldNode instanceof ObjectNode &&
5713
- oldNode.identifier !== null &&
5769
+ return (oldNode.identifier !== null &&
5714
5770
  oldNode.identifierAttribute &&
5715
5771
  isPlainObject(newValue) &&
5716
- oldNode.identifier === normalizeIdentifier(newValue[oldNode.identifierAttribute]) &&
5717
- oldNode.type.is(newValue));
5772
+ oldNodeType.is(newValue) &&
5773
+ oldNodeType.isMatchingSnapshotId(oldNode, newValue));
5718
5774
  }
5719
5775
  /**
5720
5776
  * Returns if a given value represents an array type.
@@ -6868,7 +6924,7 @@ var Union = /** @class */ (function (_super) {
6868
6924
  configurable: true,
6869
6925
  writable: true,
6870
6926
  value: function (current, newValue, parent, subpath) {
6871
- var type = this.determineType(newValue, current.type);
6927
+ var type = this.determineType(newValue, current.getReconciliationType());
6872
6928
  if (!type)
6873
6929
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6874
6930
  return type.reconcile(current, newValue, parent, subpath);