mobx-state-tree 5.0.0 → 5.0.4

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.
@@ -1,4 +1,4 @@
1
- import { isComputedProp, isObservableProp, createAtom, makeObservable, action, _allowStateChangesInsideComputed, reaction, computed, runInAction, observable, values, entries, _getGlobalState, isObservableArray, getAtom, ObservableMap, _interceptReads, intercept, observe, _getAdministration, set } from 'mobx';
1
+ import { isComputedProp, isObservableProp, createAtom, action, computed, _allowStateChangesInsideComputed, reaction, runInAction, observable, values, entries, _getGlobalState, isObservableArray, isObservableObject, defineProperty as defineProperty$1, ObservableMap, _interceptReads, intercept, observe, _getAdministration, set, makeObservable, getAtom } from 'mobx';
2
2
 
3
3
  var livelinessChecking = "warn";
4
4
  /**
@@ -64,11 +64,13 @@ PERFORMANCE OF THIS SOFTWARE.
64
64
  var extendStatics = function(d, b) {
65
65
  extendStatics = Object.setPrototypeOf ||
66
66
  ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
67
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
67
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
68
68
  return extendStatics(d, b);
69
69
  };
70
70
 
71
71
  function __extends(d, b) {
72
+ if (typeof b !== "function" && b !== null)
73
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
72
74
  extendStatics(d, b);
73
75
  function __() { this.constructor = d; }
74
76
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
@@ -97,13 +99,6 @@ function __rest(s, e) {
97
99
  return t;
98
100
  }
99
101
 
100
- function __decorate(decorators, target, key, desc) {
101
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
102
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
103
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
104
- return c > 3 && r && Object.defineProperty(target, key, r), r;
105
- }
106
-
107
102
  function __generator(thisArg, body) {
108
103
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
109
104
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -161,6 +156,7 @@ function __read(o, n) {
161
156
  return ar;
162
157
  }
163
158
 
159
+ /** @deprecated */
164
160
  function __spread() {
165
161
  for (var ar = [], i = 0; i < arguments.length; i++)
166
162
  ar = ar.concat(__read(arguments[i]));
@@ -1130,6 +1126,14 @@ var BaseNode = /** @class */ (function () {
1130
1126
  enumerable: false,
1131
1127
  configurable: true
1132
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
+ });
1133
1137
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1134
1138
  enumerable: false,
1135
1139
  configurable: true,
@@ -1265,7 +1269,6 @@ var ScalarNode = /** @class */ (function (_super) {
1265
1269
  __extends(ScalarNode, _super);
1266
1270
  function ScalarNode(simpleType, parent, subpath, environment, initialSnapshot) {
1267
1271
  var _this = _super.call(this, simpleType, parent, subpath, environment) || this;
1268
- makeObservable(_this);
1269
1272
  try {
1270
1273
  _this.storedValue = simpleType.createNewInstance(initialSnapshot);
1271
1274
  }
@@ -1386,11 +1389,9 @@ var ScalarNode = /** @class */ (function (_super) {
1386
1389
  this.fireInternalHook(name);
1387
1390
  }
1388
1391
  });
1389
- __decorate([
1390
- action
1391
- ], ScalarNode.prototype, "die", null);
1392
1392
  return ScalarNode;
1393
1393
  }(BaseNode));
1394
+ ScalarNode.prototype.die = action(ScalarNode.prototype.die);
1394
1395
 
1395
1396
  var nextNodeId = 1;
1396
1397
  var snapshotReactionOptions = {
@@ -1508,6 +1509,12 @@ var ObjectNode = /** @class */ (function (_super) {
1508
1509
  writable: true,
1509
1510
  value: false
1510
1511
  });
1512
+ Object.defineProperty(_this, "_snapshotComputed", {
1513
+ enumerable: true,
1514
+ configurable: true,
1515
+ writable: true,
1516
+ value: void 0
1517
+ });
1511
1518
  Object.defineProperty(_this, "_snapshotUponDeath", {
1512
1519
  enumerable: true,
1513
1520
  configurable: true,
@@ -1521,7 +1528,7 @@ var ObjectNode = /** @class */ (function (_super) {
1521
1528
  writable: true,
1522
1529
  value: void 0
1523
1530
  });
1524
- makeObservable(_this);
1531
+ _this._snapshotComputed = computed(function () { return freeze(_this.getSnapshot()); });
1525
1532
  _this.unbox = _this.unbox.bind(_this);
1526
1533
  _this._initialSnapshot = freeze(initialValue);
1527
1534
  _this.identifierAttribute = complexType.identifierAttribute;
@@ -1641,9 +1648,7 @@ var ObjectNode = /** @class */ (function (_super) {
1641
1648
  this._isRunningAction = false;
1642
1649
  }
1643
1650
  this._observableInstanceState = 2 /* CREATED */;
1644
- // NOTE: we need to touch snapshot, because non-observable
1645
- // "_observableInstanceState" field was touched
1646
- invalidateComputed(this, "snapshot");
1651
+ this._snapshotComputed.trackAndCompute();
1647
1652
  if (this.isRoot)
1648
1653
  this._addSnapshotReaction();
1649
1654
  this._childNodes = EMPTY_OBJECT;
@@ -1755,7 +1760,7 @@ var ObjectNode = /** @class */ (function (_super) {
1755
1760
  Object.defineProperty(ObjectNode.prototype, "snapshot", {
1756
1761
  // advantage of using computed for a snapshot is that nicely respects transactions etc.
1757
1762
  get: function () {
1758
- return freeze(this.getSnapshot());
1763
+ return this._snapshotComputed.get();
1759
1764
  },
1760
1765
  enumerable: false,
1761
1766
  configurable: true
@@ -2251,21 +2256,13 @@ var ObjectNode = /** @class */ (function (_super) {
2251
2256
  }
2252
2257
  }
2253
2258
  });
2254
- __decorate([
2255
- action
2256
- ], ObjectNode.prototype, "createObservableInstance", null);
2257
- __decorate([
2258
- computed
2259
- ], ObjectNode.prototype, "snapshot", null);
2260
- __decorate([
2261
- action
2262
- ], ObjectNode.prototype, "detach", null);
2263
- __decorate([
2264
- action
2265
- ], ObjectNode.prototype, "die", null);
2266
2259
  return ObjectNode;
2267
2260
  }(BaseNode));
2261
+ ObjectNode.prototype.createObservableInstance = action(ObjectNode.prototype.createObservableInstance);
2262
+ ObjectNode.prototype.detach = action(ObjectNode.prototype.detach);
2263
+ ObjectNode.prototype.die = action(ObjectNode.prototype.die);
2268
2264
 
2265
+ var _a;
2269
2266
  /**
2270
2267
  * @internal
2271
2268
  * @hidden
@@ -2351,7 +2348,6 @@ var BaseType = /** @class */ (function () {
2351
2348
  writable: true,
2352
2349
  value: void 0
2353
2350
  });
2354
- makeObservable(this);
2355
2351
  this.name = name;
2356
2352
  }
2357
2353
  Object.defineProperty(BaseType.prototype, "create", {
@@ -2436,13 +2432,10 @@ var BaseType = /** @class */ (function () {
2436
2432
  enumerable: false,
2437
2433
  configurable: true
2438
2434
  });
2439
- var _a;
2440
- _a = $type;
2441
- __decorate([
2442
- action
2443
- ], BaseType.prototype, "create", null);
2444
2435
  return BaseType;
2445
2436
  }());
2437
+ _a = $type;
2438
+ BaseType.prototype.create = action(BaseType.prototype.create);
2446
2439
  /**
2447
2440
  * A complex type produces a MST node (Node in the state tree)
2448
2441
  *
@@ -2479,6 +2472,16 @@ var ComplexType = /** @class */ (function (_super) {
2479
2472
  return node.storedValue;
2480
2473
  }
2481
2474
  });
2475
+ Object.defineProperty(ComplexType.prototype, "isMatchingSnapshotId", {
2476
+ enumerable: false,
2477
+ configurable: true,
2478
+ writable: true,
2479
+ value: function (current, snapshot) {
2480
+ return (!current.identifierAttribute ||
2481
+ current.identifier ===
2482
+ normalizeIdentifier(snapshot[current.identifierAttribute]));
2483
+ }
2484
+ });
2482
2485
  Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
2483
2486
  enumerable: false,
2484
2487
  configurable: true,
@@ -2497,9 +2500,7 @@ var ComplexType = /** @class */ (function (_super) {
2497
2500
  if (current.type === this &&
2498
2501
  isMutable(newValue) &&
2499
2502
  !isStateTreeNode(newValue) &&
2500
- (!current.identifierAttribute ||
2501
- current.identifier ===
2502
- normalizeIdentifier(newValue[current.identifierAttribute]))) {
2503
+ this.isMatchingSnapshotId(current, newValue)) {
2503
2504
  // the newValue has no node, so can be treated like a snapshot
2504
2505
  // we can reconcile
2505
2506
  current.applySnapshot(newValue);
@@ -2539,11 +2540,9 @@ var ComplexType = /** @class */ (function (_super) {
2539
2540
  return null;
2540
2541
  }
2541
2542
  });
2542
- __decorate([
2543
- action
2544
- ], ComplexType.prototype, "create", null);
2545
2543
  return ComplexType;
2546
2544
  }(BaseType));
2545
+ ComplexType.prototype.create = action(ComplexType.prototype.create);
2547
2546
  /**
2548
2547
  * @internal
2549
2548
  * @hidden
@@ -3972,12 +3971,21 @@ function deepFreeze(value) {
3972
3971
  function isSerializable(value) {
3973
3972
  return typeof value !== "function";
3974
3973
  }
3974
+ /**
3975
+ * @internal
3976
+ * @hidden
3977
+ */
3978
+ function defineProperty(object, key, descriptor) {
3979
+ isObservableObject(object)
3980
+ ? defineProperty$1(object, key, descriptor)
3981
+ : Object.defineProperty(object, key, descriptor);
3982
+ }
3975
3983
  /**
3976
3984
  * @internal
3977
3985
  * @hidden
3978
3986
  */
3979
3987
  function addHiddenFinalProp(object, propName, value) {
3980
- Object.defineProperty(object, propName, {
3988
+ defineProperty(object, propName, {
3981
3989
  enumerable: false,
3982
3990
  writable: false,
3983
3991
  configurable: true,
@@ -3989,7 +3997,7 @@ function addHiddenFinalProp(object, propName, value) {
3989
3997
  * @hidden
3990
3998
  */
3991
3999
  function addHiddenWritableProp(object, propName, value) {
3992
- Object.defineProperty(object, propName, {
4000
+ defineProperty(object, propName, {
3993
4001
  enumerable: false,
3994
4002
  writable: true,
3995
4003
  configurable: true,
@@ -4181,14 +4189,6 @@ function argsToArray(args) {
4181
4189
  res[i] = args[i];
4182
4190
  return res;
4183
4191
  }
4184
- /**
4185
- * @internal
4186
- * @hidden
4187
- */
4188
- function invalidateComputed(target, propName) {
4189
- var atom = getAtom(target, propName);
4190
- atom.trackAndCompute();
4191
- }
4192
4192
  /**
4193
4193
  * @internal
4194
4194
  * @hidden
@@ -4590,6 +4590,8 @@ function splitJsonPath(path) {
4590
4590
  return parts;
4591
4591
  }
4592
4592
 
4593
+ /** @hidden */
4594
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4593
4595
  var SnapshotProcessor = /** @class */ (function (_super) {
4594
4596
  __extends(SnapshotProcessor, _super);
4595
4597
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4634,6 +4636,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4634
4636
  return sn;
4635
4637
  }
4636
4638
  });
4639
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4640
+ enumerable: false,
4641
+ configurable: true,
4642
+ writable: true,
4643
+ value: function (sn) {
4644
+ try {
4645
+ return this.preProcessSnapshot(sn);
4646
+ }
4647
+ catch (e) {
4648
+ return $preProcessorFailed;
4649
+ }
4650
+ }
4651
+ });
4637
4652
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4638
4653
  enumerable: false,
4639
4654
  configurable: true,
@@ -4652,11 +4667,14 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4652
4667
  value: function (node) {
4653
4668
  var _this = this;
4654
4669
  // the node has to use these methods rather than the original type ones
4655
- proxyNodeTypeMethods(node.type, this, "create");
4670
+ proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
4656
4671
  var oldGetSnapshot = node.getSnapshot;
4657
4672
  node.getSnapshot = function () {
4658
4673
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4659
4674
  };
4675
+ node.getReconciliationType = function () {
4676
+ return _this;
4677
+ };
4660
4678
  }
4661
4679
  });
4662
4680
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4699,7 +4717,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4699
4717
  configurable: true,
4700
4718
  writable: true,
4701
4719
  value: function (value, context) {
4702
- var processedSn = this.preProcessSnapshot(value);
4720
+ var processedSn = this.preProcessSnapshotSafe(value);
4721
+ if (processedSn === $preProcessorFailed) {
4722
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4723
+ }
4703
4724
  return this._subtype.validate(processedSn, context);
4704
4725
  }
4705
4726
  });
@@ -4720,7 +4741,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4720
4741
  ? this._subtype
4721
4742
  : isStateTreeNode(thing)
4722
4743
  ? getSnapshot(thing, false)
4723
- : this.preProcessSnapshot(thing);
4744
+ : this.preProcessSnapshotSafe(thing);
4745
+ if (value === $preProcessorFailed) {
4746
+ return false;
4747
+ }
4724
4748
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4725
4749
  }
4726
4750
  });
@@ -4732,6 +4756,18 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4732
4756
  return this._subtype.isAssignableFrom(type);
4733
4757
  }
4734
4758
  });
4759
+ Object.defineProperty(SnapshotProcessor.prototype, "isMatchingSnapshotId", {
4760
+ enumerable: false,
4761
+ configurable: true,
4762
+ writable: true,
4763
+ value: function (current, snapshot) {
4764
+ if (!(this._subtype instanceof ComplexType)) {
4765
+ return false;
4766
+ }
4767
+ var processedSn = this.preProcessSnapshot(snapshot);
4768
+ return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
4769
+ }
4770
+ });
4735
4771
  return SnapshotProcessor;
4736
4772
  }(BaseType));
4737
4773
  function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
@@ -4963,7 +4999,6 @@ var MapType = /** @class */ (function (_super) {
4963
4999
  });
4964
5000
  _this._determineIdentifierMode();
4965
5001
  _this.hookInitializers = hookInitializers;
4966
- makeObservable(_this);
4967
5002
  return _this;
4968
5003
  }
4969
5004
  Object.defineProperty(MapType.prototype, "hooks", {
@@ -5264,16 +5299,14 @@ var MapType = /** @class */ (function (_super) {
5264
5299
  node.storedValue.delete(subpath);
5265
5300
  }
5266
5301
  });
5267
- __decorate([
5268
- action
5269
- ], MapType.prototype, "applySnapshot", null);
5270
5302
  return MapType;
5271
5303
  }(ComplexType));
5304
+ MapType.prototype.applySnapshot = action(MapType.prototype.applySnapshot);
5272
5305
  /**
5273
5306
  * `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
5274
5307
  * If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
5275
5308
  *
5276
- * This type will always produce [observable maps](https://mobx.js.org/refguide/map.html)
5309
+ * This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
5277
5310
  *
5278
5311
  * Example:
5279
5312
  * ```ts
@@ -5336,7 +5369,6 @@ var ArrayType = /** @class */ (function (_super) {
5336
5369
  writable: true,
5337
5370
  value: []
5338
5371
  });
5339
- makeObservable(_this);
5340
5372
  _this.hookInitializers = hookInitializers;
5341
5373
  return _this;
5342
5374
  }
@@ -5585,15 +5617,13 @@ var ArrayType = /** @class */ (function (_super) {
5585
5617
  node.storedValue.splice(Number(subpath), 1);
5586
5618
  }
5587
5619
  });
5588
- __decorate([
5589
- action
5590
- ], ArrayType.prototype, "applySnapshot", null);
5591
5620
  return ArrayType;
5592
5621
  }(ComplexType));
5622
+ ArrayType.prototype.applySnapshot = action(ArrayType.prototype.applySnapshot);
5593
5623
  /**
5594
5624
  * `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
5595
5625
  *
5596
- * This type will always produce [observable arrays](https://mobx.js.org/refguide/array.html)
5626
+ * This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
5597
5627
  *
5598
5628
  * Example:
5599
5629
  * ```ts
@@ -5734,8 +5764,8 @@ function areSame(oldNode, newValue) {
5734
5764
  oldNode.identifier !== null &&
5735
5765
  oldNode.identifierAttribute &&
5736
5766
  isPlainObject(newValue) &&
5737
- oldNode.identifier === normalizeIdentifier(newValue[oldNode.identifierAttribute]) &&
5738
- oldNode.type.is(newValue));
5767
+ oldNode.type.is(newValue) &&
5768
+ oldNode.type.isMatchingSnapshotId(oldNode, newValue));
5739
5769
  }
5740
5770
  /**
5741
5771
  * Returns if a given value represents an array type.
@@ -5898,7 +5928,6 @@ var ModelType = /** @class */ (function (_super) {
5898
5928
  });
5899
5929
  }
5900
5930
  });
5901
- makeObservable(_this);
5902
5931
  Object.assign(_this, defaultObjectOptions, opts);
5903
5932
  // ensures that any default value gets converted to its related type
5904
5933
  _this.properties = toPropertiesObject(_this.properties);
@@ -6061,19 +6090,8 @@ var ModelType = /** @class */ (function (_super) {
6061
6090
  // is this a computed property?
6062
6091
  var descriptor = Object.getOwnPropertyDescriptor(views, key);
6063
6092
  if ("get" in descriptor) {
6064
- if (isComputedProp(self, key)) {
6065
- var computedValue = _getAdministration(self, key);
6066
- // TODO: mobx currently does not allow redefining computes yet, pending #1121
6067
- // FIXME: this binds to the internals of mobx!
6068
- computedValue.derivation = descriptor.get;
6069
- computedValue.scope = self;
6070
- if (descriptor.set)
6071
- computedValue.setter = action(computedValue.name + "-setter", descriptor.set);
6072
- }
6073
- else {
6074
- Object.defineProperty(self, key, descriptor);
6075
- makeObservable(self, (_a = {}, _a[key] = computed, _a));
6076
- }
6093
+ defineProperty$1(self, key, descriptor);
6094
+ makeObservable(self, (_a = {}, _a[key] = computed, _a));
6077
6095
  }
6078
6096
  else if (typeof descriptor.value === "function") {
6079
6097
  (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, key, descriptor.value);
@@ -6337,11 +6355,9 @@ var ModelType = /** @class */ (function (_super) {
6337
6355
  node.storedValue[subpath] = undefined;
6338
6356
  }
6339
6357
  });
6340
- __decorate([
6341
- action
6342
- ], ModelType.prototype, "applySnapshot", null);
6343
6358
  return ModelType;
6344
6359
  }(ComplexType));
6360
+ ModelType.prototype.applySnapshot = action(ModelType.prototype.applySnapshot);
6345
6361
  /**
6346
6362
  * `types.model` - Creates a new model type by providing a name, properties, volatile state and actions.
6347
6363
  *
@@ -6903,7 +6919,7 @@ var Union = /** @class */ (function (_super) {
6903
6919
  configurable: true,
6904
6920
  writable: true,
6905
6921
  value: function (current, newValue, parent, subpath) {
6906
- var type = this.determineType(newValue, current.type);
6922
+ var type = this.determineType(newValue, current.getReconciliationType());
6907
6923
  if (!type)
6908
6924
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6909
6925
  return type.reconcile(current, newValue, parent, subpath);