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,7 +1,7 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('mobx')) :
3
3
  typeof define === 'function' && define.amd ? define(['exports', 'mobx'], factory) :
4
- (global = global || self, factory(global.mobxStateTree = {}, global.mobx));
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.mobxStateTree = {}, global.mobx));
5
5
  }(this, (function (exports, mobx) { 'use strict';
6
6
 
7
7
  var livelinessChecking = "warn";
@@ -68,11 +68,13 @@
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 __());
@@ -101,13 +103,6 @@
101
103
  return t;
102
104
  }
103
105
 
104
- function __decorate(decorators, target, key, desc) {
105
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
106
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
107
- 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;
108
- return c > 3 && r && Object.defineProperty(target, key, r), r;
109
- }
110
-
111
106
  function __generator(thisArg, body) {
112
107
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
113
108
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -165,6 +160,7 @@
165
160
  return ar;
166
161
  }
167
162
 
163
+ /** @deprecated */
168
164
  function __spread() {
169
165
  for (var ar = [], i = 0; i < arguments.length; i++)
170
166
  ar = ar.concat(__read(arguments[i]));
@@ -1134,6 +1130,14 @@
1134
1130
  enumerable: false,
1135
1131
  configurable: true
1136
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
+ });
1137
1141
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1138
1142
  enumerable: false,
1139
1143
  configurable: true,
@@ -1269,7 +1273,6 @@
1269
1273
  __extends(ScalarNode, _super);
1270
1274
  function ScalarNode(simpleType, parent, subpath, environment, initialSnapshot) {
1271
1275
  var _this = _super.call(this, simpleType, parent, subpath, environment) || this;
1272
- mobx.makeObservable(_this);
1273
1276
  try {
1274
1277
  _this.storedValue = simpleType.createNewInstance(initialSnapshot);
1275
1278
  }
@@ -1390,11 +1393,9 @@
1390
1393
  this.fireInternalHook(name);
1391
1394
  }
1392
1395
  });
1393
- __decorate([
1394
- mobx.action
1395
- ], ScalarNode.prototype, "die", null);
1396
1396
  return ScalarNode;
1397
1397
  }(BaseNode));
1398
+ ScalarNode.prototype.die = mobx.action(ScalarNode.prototype.die);
1398
1399
 
1399
1400
  var nextNodeId = 1;
1400
1401
  var snapshotReactionOptions = {
@@ -1512,6 +1513,12 @@
1512
1513
  writable: true,
1513
1514
  value: false
1514
1515
  });
1516
+ Object.defineProperty(_this, "_snapshotComputed", {
1517
+ enumerable: true,
1518
+ configurable: true,
1519
+ writable: true,
1520
+ value: void 0
1521
+ });
1515
1522
  Object.defineProperty(_this, "_snapshotUponDeath", {
1516
1523
  enumerable: true,
1517
1524
  configurable: true,
@@ -1525,7 +1532,7 @@
1525
1532
  writable: true,
1526
1533
  value: void 0
1527
1534
  });
1528
- mobx.makeObservable(_this);
1535
+ _this._snapshotComputed = mobx.computed(function () { return freeze(_this.getSnapshot()); });
1529
1536
  _this.unbox = _this.unbox.bind(_this);
1530
1537
  _this._initialSnapshot = freeze(initialValue);
1531
1538
  _this.identifierAttribute = complexType.identifierAttribute;
@@ -1645,9 +1652,7 @@
1645
1652
  this._isRunningAction = false;
1646
1653
  }
1647
1654
  this._observableInstanceState = 2 /* CREATED */;
1648
- // NOTE: we need to touch snapshot, because non-observable
1649
- // "_observableInstanceState" field was touched
1650
- invalidateComputed(this, "snapshot");
1655
+ this._snapshotComputed.trackAndCompute();
1651
1656
  if (this.isRoot)
1652
1657
  this._addSnapshotReaction();
1653
1658
  this._childNodes = EMPTY_OBJECT;
@@ -1759,7 +1764,7 @@
1759
1764
  Object.defineProperty(ObjectNode.prototype, "snapshot", {
1760
1765
  // advantage of using computed for a snapshot is that nicely respects transactions etc.
1761
1766
  get: function () {
1762
- return freeze(this.getSnapshot());
1767
+ return this._snapshotComputed.get();
1763
1768
  },
1764
1769
  enumerable: false,
1765
1770
  configurable: true
@@ -2255,21 +2260,13 @@
2255
2260
  }
2256
2261
  }
2257
2262
  });
2258
- __decorate([
2259
- mobx.action
2260
- ], ObjectNode.prototype, "createObservableInstance", null);
2261
- __decorate([
2262
- mobx.computed
2263
- ], ObjectNode.prototype, "snapshot", null);
2264
- __decorate([
2265
- mobx.action
2266
- ], ObjectNode.prototype, "detach", null);
2267
- __decorate([
2268
- mobx.action
2269
- ], ObjectNode.prototype, "die", null);
2270
2263
  return ObjectNode;
2271
2264
  }(BaseNode));
2265
+ ObjectNode.prototype.createObservableInstance = mobx.action(ObjectNode.prototype.createObservableInstance);
2266
+ ObjectNode.prototype.detach = mobx.action(ObjectNode.prototype.detach);
2267
+ ObjectNode.prototype.die = mobx.action(ObjectNode.prototype.die);
2272
2268
 
2269
+ var _a;
2273
2270
  /**
2274
2271
  * @internal
2275
2272
  * @hidden
@@ -2355,7 +2352,6 @@
2355
2352
  writable: true,
2356
2353
  value: void 0
2357
2354
  });
2358
- mobx.makeObservable(this);
2359
2355
  this.name = name;
2360
2356
  }
2361
2357
  Object.defineProperty(BaseType.prototype, "create", {
@@ -2440,13 +2436,10 @@
2440
2436
  enumerable: false,
2441
2437
  configurable: true
2442
2438
  });
2443
- var _a;
2444
- _a = $type;
2445
- __decorate([
2446
- mobx.action
2447
- ], BaseType.prototype, "create", null);
2448
2439
  return BaseType;
2449
2440
  }());
2441
+ _a = $type;
2442
+ BaseType.prototype.create = mobx.action(BaseType.prototype.create);
2450
2443
  /**
2451
2444
  * A complex type produces a MST node (Node in the state tree)
2452
2445
  *
@@ -2483,6 +2476,16 @@
2483
2476
  return node.storedValue;
2484
2477
  }
2485
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
+ });
2486
2489
  Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
2487
2490
  enumerable: false,
2488
2491
  configurable: true,
@@ -2501,9 +2504,7 @@
2501
2504
  if (current.type === this &&
2502
2505
  isMutable(newValue) &&
2503
2506
  !isStateTreeNode(newValue) &&
2504
- (!current.identifierAttribute ||
2505
- current.identifier ===
2506
- normalizeIdentifier(newValue[current.identifierAttribute]))) {
2507
+ this.isMatchingSnapshotId(current, newValue)) {
2507
2508
  // the newValue has no node, so can be treated like a snapshot
2508
2509
  // we can reconcile
2509
2510
  current.applySnapshot(newValue);
@@ -2543,11 +2544,9 @@
2543
2544
  return null;
2544
2545
  }
2545
2546
  });
2546
- __decorate([
2547
- mobx.action
2548
- ], ComplexType.prototype, "create", null);
2549
2547
  return ComplexType;
2550
2548
  }(BaseType));
2549
+ ComplexType.prototype.create = mobx.action(ComplexType.prototype.create);
2551
2550
  /**
2552
2551
  * @internal
2553
2552
  * @hidden
@@ -3972,12 +3971,21 @@
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
+ mobx.isObservableObject(object)
3980
+ ? mobx.defineProperty(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 @@
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 @@
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 = mobx.getAtom(target, propName);
4190
- atom.trackAndCompute();
4191
- }
4192
4192
  /**
4193
4193
  * @internal
4194
4194
  * @hidden
@@ -4572,6 +4572,8 @@
4572
4572
  return parts;
4573
4573
  }
4574
4574
 
4575
+ /** @hidden */
4576
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4575
4577
  var SnapshotProcessor = /** @class */ (function (_super) {
4576
4578
  __extends(SnapshotProcessor, _super);
4577
4579
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4616,6 +4618,19 @@
4616
4618
  return sn;
4617
4619
  }
4618
4620
  });
4621
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4622
+ enumerable: false,
4623
+ configurable: true,
4624
+ writable: true,
4625
+ value: function (sn) {
4626
+ try {
4627
+ return this.preProcessSnapshot(sn);
4628
+ }
4629
+ catch (e) {
4630
+ return $preProcessorFailed;
4631
+ }
4632
+ }
4633
+ });
4619
4634
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4620
4635
  enumerable: false,
4621
4636
  configurable: true,
@@ -4634,11 +4649,14 @@
4634
4649
  value: function (node) {
4635
4650
  var _this = this;
4636
4651
  // the node has to use these methods rather than the original type ones
4637
- proxyNodeTypeMethods(node.type, this, "create");
4652
+ proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
4638
4653
  var oldGetSnapshot = node.getSnapshot;
4639
4654
  node.getSnapshot = function () {
4640
4655
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4641
4656
  };
4657
+ node.getReconciliationType = function () {
4658
+ return _this;
4659
+ };
4642
4660
  }
4643
4661
  });
4644
4662
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4681,7 +4699,10 @@
4681
4699
  configurable: true,
4682
4700
  writable: true,
4683
4701
  value: function (value, context) {
4684
- var processedSn = this.preProcessSnapshot(value);
4702
+ var processedSn = this.preProcessSnapshotSafe(value);
4703
+ if (processedSn === $preProcessorFailed) {
4704
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4705
+ }
4685
4706
  return this._subtype.validate(processedSn, context);
4686
4707
  }
4687
4708
  });
@@ -4702,7 +4723,10 @@
4702
4723
  ? this._subtype
4703
4724
  : isStateTreeNode(thing)
4704
4725
  ? getSnapshot(thing, false)
4705
- : this.preProcessSnapshot(thing);
4726
+ : this.preProcessSnapshotSafe(thing);
4727
+ if (value === $preProcessorFailed) {
4728
+ return false;
4729
+ }
4706
4730
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4707
4731
  }
4708
4732
  });
@@ -4714,6 +4738,18 @@
4714
4738
  return this._subtype.isAssignableFrom(type);
4715
4739
  }
4716
4740
  });
4741
+ Object.defineProperty(SnapshotProcessor.prototype, "isMatchingSnapshotId", {
4742
+ enumerable: false,
4743
+ configurable: true,
4744
+ writable: true,
4745
+ value: function (current, snapshot) {
4746
+ if (!(this._subtype instanceof ComplexType)) {
4747
+ return false;
4748
+ }
4749
+ var processedSn = this.preProcessSnapshot(snapshot);
4750
+ return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
4751
+ }
4752
+ });
4717
4753
  return SnapshotProcessor;
4718
4754
  }(BaseType));
4719
4755
  function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
@@ -4945,7 +4981,6 @@
4945
4981
  });
4946
4982
  _this._determineIdentifierMode();
4947
4983
  _this.hookInitializers = hookInitializers;
4948
- mobx.makeObservable(_this);
4949
4984
  return _this;
4950
4985
  }
4951
4986
  Object.defineProperty(MapType.prototype, "hooks", {
@@ -5029,7 +5064,7 @@
5029
5064
  Object.keys(hooks).forEach(function (name) {
5030
5065
  var hook = hooks[name];
5031
5066
  var actionInvoker = createActionInvoker(instance, name, hook);
5032
- ( addHiddenWritableProp)(instance, name, actionInvoker);
5067
+ (addHiddenWritableProp)(instance, name, actionInvoker);
5033
5068
  });
5034
5069
  });
5035
5070
  mobx.intercept(instance, this.willChange);
@@ -5246,16 +5281,14 @@
5246
5281
  node.storedValue.delete(subpath);
5247
5282
  }
5248
5283
  });
5249
- __decorate([
5250
- mobx.action
5251
- ], MapType.prototype, "applySnapshot", null);
5252
5284
  return MapType;
5253
5285
  }(ComplexType));
5286
+ MapType.prototype.applySnapshot = mobx.action(MapType.prototype.applySnapshot);
5254
5287
  /**
5255
5288
  * `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
5256
5289
  * If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
5257
5290
  *
5258
- * This type will always produce [observable maps](https://mobx.js.org/refguide/map.html)
5291
+ * This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
5259
5292
  *
5260
5293
  * Example:
5261
5294
  * ```ts
@@ -5318,7 +5351,6 @@
5318
5351
  writable: true,
5319
5352
  value: []
5320
5353
  });
5321
- mobx.makeObservable(_this);
5322
5354
  _this.hookInitializers = hookInitializers;
5323
5355
  return _this;
5324
5356
  }
@@ -5374,7 +5406,7 @@
5374
5406
  Object.keys(hooks).forEach(function (name) {
5375
5407
  var hook = hooks[name];
5376
5408
  var actionInvoker = createActionInvoker(instance, name, hook);
5377
- ( addHiddenWritableProp)(instance, name, actionInvoker);
5409
+ (addHiddenWritableProp)(instance, name, actionInvoker);
5378
5410
  });
5379
5411
  });
5380
5412
  mobx.intercept(instance, this.willChange);
@@ -5567,15 +5599,13 @@
5567
5599
  node.storedValue.splice(Number(subpath), 1);
5568
5600
  }
5569
5601
  });
5570
- __decorate([
5571
- mobx.action
5572
- ], ArrayType.prototype, "applySnapshot", null);
5573
5602
  return ArrayType;
5574
5603
  }(ComplexType));
5604
+ ArrayType.prototype.applySnapshot = mobx.action(ArrayType.prototype.applySnapshot);
5575
5605
  /**
5576
5606
  * `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
5577
5607
  *
5578
- * This type will always produce [observable arrays](https://mobx.js.org/refguide/array.html)
5608
+ * This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
5579
5609
  *
5580
5610
  * Example:
5581
5611
  * ```ts
@@ -5716,8 +5746,8 @@
5716
5746
  oldNode.identifier !== null &&
5717
5747
  oldNode.identifierAttribute &&
5718
5748
  isPlainObject(newValue) &&
5719
- oldNode.identifier === normalizeIdentifier(newValue[oldNode.identifierAttribute]) &&
5720
- oldNode.type.is(newValue));
5749
+ oldNode.type.is(newValue) &&
5750
+ oldNode.type.isMatchingSnapshotId(oldNode, newValue));
5721
5751
  }
5722
5752
  /**
5723
5753
  * Returns if a given value represents an array type.
@@ -5776,11 +5806,11 @@
5776
5806
  return props;
5777
5807
  // its a function, maybe the user wanted a view?
5778
5808
  }
5779
- else if ( typeof value === "function") {
5809
+ else if (typeof value === "function") {
5780
5810
  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?");
5781
5811
  // no other complex values
5782
5812
  }
5783
- else if ( typeof value === "object") {
5813
+ else if (typeof value === "object") {
5784
5814
  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.");
5785
5815
  // WTF did you pass in mate?
5786
5816
  }
@@ -5880,7 +5910,6 @@
5880
5910
  });
5881
5911
  }
5882
5912
  });
5883
- mobx.makeObservable(_this);
5884
5913
  Object.assign(_this, defaultObjectOptions, opts);
5885
5914
  // ensures that any default value gets converted to its related type
5886
5915
  _this.properties = toPropertiesObject(_this.properties);
@@ -5965,7 +5994,7 @@
5965
5994
  boundAction.$mst_middleware = middlewares;
5966
5995
  var actionInvoker = createActionInvoker(self, name, boundAction);
5967
5996
  actions[name] = actionInvoker;
5968
- ( addHiddenWritableProp)(self, name, actionInvoker);
5997
+ (addHiddenWritableProp)(self, name, actionInvoker);
5969
5998
  });
5970
5999
  }
5971
6000
  });
@@ -6043,22 +6072,11 @@
6043
6072
  // is this a computed property?
6044
6073
  var descriptor = Object.getOwnPropertyDescriptor(views, key);
6045
6074
  if ("get" in descriptor) {
6046
- if (mobx.isComputedProp(self, key)) {
6047
- var computedValue = mobx._getAdministration(self, key);
6048
- // TODO: mobx currently does not allow redefining computes yet, pending #1121
6049
- // FIXME: this binds to the internals of mobx!
6050
- computedValue.derivation = descriptor.get;
6051
- computedValue.scope = self;
6052
- if (descriptor.set)
6053
- computedValue.setter = mobx.action(computedValue.name + "-setter", descriptor.set);
6054
- }
6055
- else {
6056
- Object.defineProperty(self, key, descriptor);
6057
- mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
6058
- }
6075
+ mobx.defineProperty(self, key, descriptor);
6076
+ mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
6059
6077
  }
6060
6078
  else if (typeof descriptor.value === "function") {
6061
- ( addHiddenWritableProp)(self, key, descriptor.value);
6079
+ (addHiddenWritableProp)(self, key, descriptor.value);
6062
6080
  }
6063
6081
  else {
6064
6082
  throw fail$1("A view member should either be a function or getter based property");
@@ -6319,11 +6337,9 @@
6319
6337
  node.storedValue[subpath] = undefined;
6320
6338
  }
6321
6339
  });
6322
- __decorate([
6323
- mobx.action
6324
- ], ModelType.prototype, "applySnapshot", null);
6325
6340
  return ModelType;
6326
6341
  }(ComplexType));
6342
+ ModelType.prototype.applySnapshot = mobx.action(ModelType.prototype.applySnapshot);
6327
6343
  /**
6328
6344
  * `types.model` - Creates a new model type by providing a name, properties, volatile state and actions.
6329
6345
  *
@@ -6885,7 +6901,7 @@
6885
6901
  configurable: true,
6886
6902
  writable: true,
6887
6903
  value: function (current, newValue, parent, subpath) {
6888
- var type = this.determineType(newValue, current.type);
6904
+ var type = this.determineType(newValue, current.getReconciliationType());
6889
6905
  if (!type)
6890
6906
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6891
6907
  return type.reconcile(current, newValue, parent, subpath);
@@ -7245,7 +7261,7 @@
7245
7261
  if (mustSucceed && t === undefined)
7246
7262
  throw fail$1("Late type seems to be used too early, the definition (still) returns undefined");
7247
7263
  if (t) {
7248
- if ( !isType(t))
7264
+ if (!isType(t))
7249
7265
  throw fail$1("Failed to determine subtype, make sure types.late returns a type definition.");
7250
7266
  this._subType = t;
7251
7267
  }