mobx-state-tree 5.0.1 → 5.0.5

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
@@ -5,7 +5,7 @@
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
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)
8
- [![Have a question? Ask on Spectrum!](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/mobx-state-tree)
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?
11
11
 
@@ -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]));
@@ -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);
@@ -3956,12 +3975,21 @@ function deepFreeze(value) {
3956
3975
  function isSerializable(value) {
3957
3976
  return typeof value !== "function";
3958
3977
  }
3978
+ /**
3979
+ * @internal
3980
+ * @hidden
3981
+ */
3982
+ function defineProperty(object, key, descriptor) {
3983
+ mobx.isObservableObject(object)
3984
+ ? mobx.defineProperty(object, key, descriptor)
3985
+ : Object.defineProperty(object, key, descriptor);
3986
+ }
3959
3987
  /**
3960
3988
  * @internal
3961
3989
  * @hidden
3962
3990
  */
3963
3991
  function addHiddenFinalProp(object, propName, value) {
3964
- Object.defineProperty(object, propName, {
3992
+ defineProperty(object, propName, {
3965
3993
  enumerable: false,
3966
3994
  writable: false,
3967
3995
  configurable: true,
@@ -3973,7 +4001,7 @@ function addHiddenFinalProp(object, propName, value) {
3973
4001
  * @hidden
3974
4002
  */
3975
4003
  function addHiddenWritableProp(object, propName, value) {
3976
- Object.defineProperty(object, propName, {
4004
+ defineProperty(object, propName, {
3977
4005
  enumerable: false,
3978
4006
  writable: true,
3979
4007
  configurable: true,
@@ -4566,6 +4594,8 @@ function splitJsonPath(path) {
4566
4594
  return parts;
4567
4595
  }
4568
4596
 
4597
+ /** @hidden */
4598
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4569
4599
  var SnapshotProcessor = /** @class */ (function (_super) {
4570
4600
  __extends(SnapshotProcessor, _super);
4571
4601
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4610,6 +4640,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4610
4640
  return sn;
4611
4641
  }
4612
4642
  });
4643
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4644
+ enumerable: false,
4645
+ configurable: true,
4646
+ writable: true,
4647
+ value: function (sn) {
4648
+ try {
4649
+ return this.preProcessSnapshot(sn);
4650
+ }
4651
+ catch (e) {
4652
+ return $preProcessorFailed;
4653
+ }
4654
+ }
4655
+ });
4613
4656
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4614
4657
  enumerable: false,
4615
4658
  configurable: true,
@@ -4633,6 +4676,9 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4633
4676
  node.getSnapshot = function () {
4634
4677
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4635
4678
  };
4679
+ node.getReconciliationType = function () {
4680
+ return _this;
4681
+ };
4636
4682
  }
4637
4683
  });
4638
4684
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4675,7 +4721,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4675
4721
  configurable: true,
4676
4722
  writable: true,
4677
4723
  value: function (value, context) {
4678
- var processedSn = this.preProcessSnapshot(value);
4724
+ var processedSn = this.preProcessSnapshotSafe(value);
4725
+ if (processedSn === $preProcessorFailed) {
4726
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4727
+ }
4679
4728
  return this._subtype.validate(processedSn, context);
4680
4729
  }
4681
4730
  });
@@ -4696,7 +4745,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4696
4745
  ? this._subtype
4697
4746
  : isStateTreeNode(thing)
4698
4747
  ? getSnapshot(thing, false)
4699
- : this.preProcessSnapshot(thing);
4748
+ : this.preProcessSnapshotSafe(thing);
4749
+ if (value === $preProcessorFailed) {
4750
+ return false;
4751
+ }
4700
4752
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4701
4753
  }
4702
4754
  });
@@ -4708,6 +4760,18 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4708
4760
  return this._subtype.isAssignableFrom(type);
4709
4761
  }
4710
4762
  });
4763
+ Object.defineProperty(SnapshotProcessor.prototype, "isMatchingSnapshotId", {
4764
+ enumerable: false,
4765
+ configurable: true,
4766
+ writable: true,
4767
+ value: function (current, snapshot) {
4768
+ if (!(this._subtype instanceof ComplexType)) {
4769
+ return false;
4770
+ }
4771
+ var processedSn = this.preProcessSnapshot(snapshot);
4772
+ return this._subtype.isMatchingSnapshotId(current, processedSn);
4773
+ }
4774
+ });
4711
4775
  return SnapshotProcessor;
4712
4776
  }(BaseType));
4713
4777
  function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
@@ -5246,7 +5310,7 @@ MapType.prototype.applySnapshot = mobx.action(MapType.prototype.applySnapshot);
5246
5310
  * `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
5247
5311
  * If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
5248
5312
  *
5249
- * This type will always produce [observable maps](https://mobx.js.org/refguide/map.html)
5313
+ * This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
5250
5314
  *
5251
5315
  * Example:
5252
5316
  * ```ts
@@ -5563,7 +5627,7 @@ ArrayType.prototype.applySnapshot = mobx.action(ArrayType.prototype.applySnapsho
5563
5627
  /**
5564
5628
  * `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
5565
5629
  *
5566
- * This type will always produce [observable arrays](https://mobx.js.org/refguide/array.html)
5630
+ * This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
5567
5631
  *
5568
5632
  * Example:
5569
5633
  * ```ts
@@ -5699,13 +5763,17 @@ function areSame(oldNode, newValue) {
5699
5763
  if (oldNode.snapshot === newValue) {
5700
5764
  return true;
5701
5765
  }
5766
+ // Non object nodes don't get reconciled
5767
+ if (!(oldNode instanceof ObjectNode)) {
5768
+ return false;
5769
+ }
5770
+ var oldNodeType = oldNode.getReconciliationType();
5702
5771
  // new value is a snapshot with the correct identifier
5703
- return (oldNode instanceof ObjectNode &&
5704
- oldNode.identifier !== null &&
5772
+ return (oldNode.identifier !== null &&
5705
5773
  oldNode.identifierAttribute &&
5706
5774
  isPlainObject(newValue) &&
5707
- oldNode.identifier === normalizeIdentifier(newValue[oldNode.identifierAttribute]) &&
5708
- oldNode.type.is(newValue));
5775
+ oldNodeType.is(newValue) &&
5776
+ oldNodeType.isMatchingSnapshotId(oldNode, newValue));
5709
5777
  }
5710
5778
  /**
5711
5779
  * Returns if a given value represents an array type.
@@ -6030,19 +6098,8 @@ var ModelType = /** @class */ (function (_super) {
6030
6098
  // is this a computed property?
6031
6099
  var descriptor = Object.getOwnPropertyDescriptor(views, key);
6032
6100
  if ("get" in descriptor) {
6033
- if (mobx.isComputedProp(self, key)) {
6034
- var computedValue = mobx._getAdministration(self, key);
6035
- // TODO: mobx currently does not allow redefining computes yet, pending #1121
6036
- // FIXME: this binds to the internals of mobx!
6037
- computedValue.derivation = descriptor.get;
6038
- computedValue.scope = self;
6039
- if (descriptor.set)
6040
- computedValue.setter = mobx.action(computedValue.name + "-setter", descriptor.set);
6041
- }
6042
- else {
6043
- Object.defineProperty(self, key, descriptor);
6044
- mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
6045
- }
6101
+ mobx.defineProperty(self, key, descriptor);
6102
+ mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
6046
6103
  }
6047
6104
  else if (typeof descriptor.value === "function") {
6048
6105
  (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, key, descriptor.value);
@@ -6870,7 +6927,7 @@ var Union = /** @class */ (function (_super) {
6870
6927
  configurable: true,
6871
6928
  writable: true,
6872
6929
  value: function (current, newValue, parent, subpath) {
6873
- var type = this.determineType(newValue, current.type);
6930
+ var type = this.determineType(newValue, current.getReconciliationType());
6874
6931
  if (!type)
6875
6932
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6876
6933
  return type.reconcile(current, newValue, parent, subpath);