mobx-state-tree 6.0.0-pre.2 → 6.0.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.
@@ -352,7 +352,7 @@ function protect(target) {
352
352
  assertIsStateTreeNode(target, 1);
353
353
  var node = getStateTreeNode(target);
354
354
  if (!node.isRoot)
355
- throw fail("`protect` can only be invoked on root nodes");
355
+ throw new MstError("`protect` can only be invoked on root nodes");
356
356
  node.isProtectionEnabled = true;
357
357
  }
358
358
  /**
@@ -384,7 +384,7 @@ function unprotect(target) {
384
384
  assertIsStateTreeNode(target, 1);
385
385
  var node = getStateTreeNode(target);
386
386
  if (!node.isRoot)
387
- throw fail("`unprotect` can only be invoked on root nodes");
387
+ throw new MstError("`unprotect` can only be invoked on root nodes");
388
388
  node.isProtectionEnabled = false;
389
389
  }
390
390
  /**
@@ -467,7 +467,7 @@ function getParent(target, depth) {
467
467
  return parent.storedValue;
468
468
  parent = parent.parent;
469
469
  }
470
- throw fail("Failed to find the parent of ".concat(getStateTreeNode(target), " at depth ").concat(depth));
470
+ throw new MstError("Failed to find the parent of ".concat(getStateTreeNode(target), " at depth ").concat(depth));
471
471
  }
472
472
  /**
473
473
  * Given a model instance, returns `true` if the object has a parent of given type, that is, is part of another object, map or array
@@ -505,7 +505,7 @@ function getParentOfType(target, type) {
505
505
  return parent.storedValue;
506
506
  parent = parent.parent;
507
507
  }
508
- throw fail("Failed to find the parent of ".concat(getStateTreeNode(target), " of a given type"));
508
+ throw new MstError("Failed to find the parent of ".concat(getStateTreeNode(target), " of a given type"));
509
509
  }
510
510
  /**
511
511
  * Given an object in a model tree, returns the root object of that tree.
@@ -622,7 +622,7 @@ function tryReference(getter, checkIfAlive) {
622
622
  }
623
623
  }
624
624
  else {
625
- throw fail("The reference to be checked is not one of node, null or undefined");
625
+ throw new MstError("The reference to be checked is not one of node, null or undefined");
626
626
  }
627
627
  }
628
628
  catch (e) {
@@ -650,7 +650,7 @@ function isValidReference(getter, checkIfAlive) {
650
650
  return checkIfAlive ? isAlive(node) : true;
651
651
  }
652
652
  else {
653
- throw fail("The reference to be checked is not one of node, null or undefined");
653
+ throw new MstError("The reference to be checked is not one of node, null or undefined");
654
654
  }
655
655
  }
656
656
  catch (e) {
@@ -807,7 +807,7 @@ function getEnv(target) {
807
807
  var node = getStateTreeNode(target);
808
808
  var env = node.root.environment;
809
809
  if (!env)
810
- throw fail("Failed to find the environment of ".concat(node, " ").concat(node.path));
810
+ throw new MstError("Failed to find the environment of ".concat(node, " ").concat(node.path));
811
811
  return env;
812
812
  }
813
813
  /**
@@ -821,7 +821,7 @@ function hasEnv(target) {
821
821
  // check all arguments
822
822
  if (process.env.NODE_ENV !== "production") {
823
823
  if (!isStateTreeNode(target))
824
- fail("expected first argument to be a mobx-state-tree node, got " + target + " instead");
824
+ throw new MstError("expected first argument to be a mobx-state-tree node, got " + target + " instead");
825
825
  }
826
826
  var node = getStateTreeNode(target);
827
827
  var env = node.root.environment;
@@ -890,14 +890,18 @@ function getMembers(target) {
890
890
  reflected.volatile.push(key);
891
891
  return;
892
892
  }
893
- if (descriptor.value._isMSTAction === true)
894
- reflected.actions.push(key);
895
- if (descriptor.value._isFlowAction === true)
893
+ if (descriptor.value._isFlowAction === true) {
896
894
  reflected.flowActions.push(key);
897
- else if (isObservableProp(target, key))
895
+ }
896
+ if (descriptor.value._isMSTAction === true) {
897
+ reflected.actions.push(key);
898
+ }
899
+ else if (isObservableProp(target, key)) {
898
900
  reflected.volatile.push(key);
899
- else
901
+ }
902
+ else {
900
903
  reflected.views.push(key);
904
+ }
901
905
  });
902
906
  return reflected;
903
907
  }
@@ -1251,7 +1255,7 @@ var BaseNode = /** @class */ (function () {
1251
1255
  if (devMode()) {
1252
1256
  if (!this.isAlive) {
1253
1257
  // istanbul ignore next
1254
- throw fail("assertion failed: cannot finalize the creation of a node that is already dead");
1258
+ throw new MstError("assertion failed: cannot finalize the creation of a node that is already dead");
1255
1259
  }
1256
1260
  }
1257
1261
  // goal: afterCreate hooks runs depth-first. After attach runs parent first, so on afterAttach the parent has completed already
@@ -1322,7 +1326,7 @@ var ScalarNode = /** @class */ (function (_super) {
1322
1326
  get: function () {
1323
1327
  // future optimization: store root ref in the node and maintain it
1324
1328
  if (!this.parent)
1325
- throw fail("This scalar node is not part of a tree");
1329
+ throw new MstError("This scalar node is not part of a tree");
1326
1330
  return this.parent.root;
1327
1331
  },
1328
1332
  enumerable: false,
@@ -1341,15 +1345,15 @@ var ScalarNode = /** @class */ (function (_super) {
1341
1345
  if (devMode()) {
1342
1346
  if (!subpath) {
1343
1347
  // istanbul ignore next
1344
- throw fail("assertion failed: subpath expected");
1348
+ throw new MstError("assertion failed: subpath expected");
1345
1349
  }
1346
1350
  if (!newParent) {
1347
1351
  // istanbul ignore next
1348
- throw fail("assertion failed: parent expected");
1352
+ throw new MstError("assertion failed: parent expected");
1349
1353
  }
1350
1354
  if (parentChanged) {
1351
1355
  // istanbul ignore next
1352
- throw fail("assertion failed: scalar nodes cannot change their parent");
1356
+ throw new MstError("assertion failed: scalar nodes cannot change their parent");
1353
1357
  }
1354
1358
  }
1355
1359
  this.environment = undefined; // use parent's
@@ -1428,6 +1432,21 @@ var ScalarNode = /** @class */ (function (_super) {
1428
1432
  ScalarNode.prototype.die = action(ScalarNode.prototype.die);
1429
1433
 
1430
1434
  var nextNodeId = 1;
1435
+ var ObservableInstanceLifecycle;
1436
+ (function (ObservableInstanceLifecycle) {
1437
+ // the actual observable instance has not been created yet
1438
+ ObservableInstanceLifecycle[ObservableInstanceLifecycle["UNINITIALIZED"] = 0] = "UNINITIALIZED";
1439
+ // the actual observable instance is being created
1440
+ ObservableInstanceLifecycle[ObservableInstanceLifecycle["CREATING"] = 1] = "CREATING";
1441
+ // the actual observable instance has been created
1442
+ ObservableInstanceLifecycle[ObservableInstanceLifecycle["CREATED"] = 2] = "CREATED";
1443
+ })(ObservableInstanceLifecycle || (ObservableInstanceLifecycle = {}));
1444
+ var InternalEvents;
1445
+ (function (InternalEvents) {
1446
+ InternalEvents["Dispose"] = "dispose";
1447
+ InternalEvents["Patch"] = "patch";
1448
+ InternalEvents["Snapshot"] = "snapshot";
1449
+ })(InternalEvents || (InternalEvents = {}));
1431
1450
  var snapshotReactionOptions = {
1432
1451
  onError: function (e) {
1433
1452
  throw e;
@@ -1523,7 +1542,7 @@ var ObjectNode = /** @class */ (function (_super) {
1523
1542
  enumerable: true,
1524
1543
  configurable: true,
1525
1544
  writable: true,
1526
- value: 0 /* ObservableInstanceLifecycle.UNINITIALIZED */
1545
+ value: ObservableInstanceLifecycle.UNINITIALIZED
1527
1546
  });
1528
1547
  Object.defineProperty(_this, "_childNodes", {
1529
1548
  enumerable: true,
@@ -1590,7 +1609,7 @@ var ObjectNode = /** @class */ (function (_super) {
1590
1609
  }
1591
1610
  }
1592
1611
  if (typeof id !== "string" && typeof id !== "number") {
1593
- throw fail("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string or a number"));
1612
+ throw new MstError("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string or a number"));
1594
1613
  }
1595
1614
  // normalize internal identifier to string
1596
1615
  _this.identifier = normalizeIdentifier(id);
@@ -1628,7 +1647,7 @@ var ObjectNode = /** @class */ (function (_super) {
1628
1647
  writable: true,
1629
1648
  value: function (fireHooks) {
1630
1649
  if (fireHooks === void 0) { fireHooks = true; }
1631
- if (this._observableInstanceState === 0 /* ObservableInstanceLifecycle.UNINITIALIZED */) {
1650
+ if (this._observableInstanceState === ObservableInstanceLifecycle.UNINITIALIZED) {
1632
1651
  this.createObservableInstance(fireHooks);
1633
1652
  }
1634
1653
  }
@@ -1643,10 +1662,10 @@ var ObjectNode = /** @class */ (function (_super) {
1643
1662
  if (devMode()) {
1644
1663
  if (this.state !== NodeLifeCycle.INITIALIZING) {
1645
1664
  // istanbul ignore next
1646
- throw fail("assertion failed: the creation of the observable instance must be done on the initializing phase");
1665
+ throw new MstError("assertion failed: the creation of the observable instance must be done on the initializing phase");
1647
1666
  }
1648
1667
  }
1649
- this._observableInstanceState = 1 /* ObservableInstanceLifecycle.CREATING */;
1668
+ this._observableInstanceState = ObservableInstanceLifecycle.CREATING;
1650
1669
  // make sure the parent chain is created as well
1651
1670
  // array with parent chain from parent to child
1652
1671
  var parentChain = [];
@@ -1656,7 +1675,7 @@ var ObjectNode = /** @class */ (function (_super) {
1656
1675
  // this is done to avoid traversing the whole tree to the root when using
1657
1676
  // the same reference again
1658
1677
  while (parent &&
1659
- parent._observableInstanceState === 0 /* ObservableInstanceLifecycle.UNINITIALIZED */) {
1678
+ parent._observableInstanceState === ObservableInstanceLifecycle.UNINITIALIZED) {
1660
1679
  parentChain.unshift(parent);
1661
1680
  parent = parent.parent;
1662
1681
  }
@@ -1690,7 +1709,7 @@ var ObjectNode = /** @class */ (function (_super) {
1690
1709
  finally {
1691
1710
  this._isRunningAction = false;
1692
1711
  }
1693
- this._observableInstanceState = 2 /* ObservableInstanceLifecycle.CREATED */;
1712
+ this._observableInstanceState = ObservableInstanceLifecycle.CREATED;
1694
1713
  this._snapshotComputed.trackAndCompute();
1695
1714
  if (this.isRoot)
1696
1715
  this._addSnapshotReaction();
@@ -1766,20 +1785,20 @@ var ObjectNode = /** @class */ (function (_super) {
1766
1785
  if (devMode()) {
1767
1786
  if (!subpath) {
1768
1787
  // istanbul ignore next
1769
- throw fail("assertion failed: subpath expected");
1788
+ throw new MstError("assertion failed: subpath expected");
1770
1789
  }
1771
1790
  if (!newParent) {
1772
1791
  // istanbul ignore next
1773
- throw fail("assertion failed: new parent expected");
1792
+ throw new MstError("assertion failed: new parent expected");
1774
1793
  }
1775
1794
  if (this.parent && parentChanged) {
1776
- throw fail("A node cannot exists twice in the state tree. Failed to add ".concat(this, " to path '").concat(newParent.path, "/").concat(subpath, "'."));
1795
+ throw new MstError("A node cannot exists twice in the state tree. Failed to add ".concat(this, " to path '").concat(newParent.path, "/").concat(subpath, "'."));
1777
1796
  }
1778
1797
  if (!this.parent && newParent.root === this) {
1779
- throw fail("A state tree is not allowed to contain itself. Cannot assign ".concat(this, " to path '").concat(newParent.path, "/").concat(subpath, "'"));
1798
+ throw new MstError("A state tree is not allowed to contain itself. Cannot assign ".concat(this, " to path '").concat(newParent.path, "/").concat(subpath, "'"));
1780
1799
  }
1781
1800
  if (!this.parent && !!this.environment && this.environment !== newParent.root.environment) {
1782
- throw fail("A state tree cannot be made part of another state tree as long as their environments are different.");
1801
+ throw new MstError("A state tree cannot be made part of another state tree as long as their environments are different.");
1783
1802
  }
1784
1803
  }
1785
1804
  if (parentChanged) {
@@ -1835,7 +1854,7 @@ var ObjectNode = /** @class */ (function (_super) {
1835
1854
  value: function () {
1836
1855
  if (!this.isAlive)
1837
1856
  return this._snapshotUponDeath;
1838
- return this._observableInstanceState === 2 /* ObservableInstanceLifecycle.CREATED */
1857
+ return this._observableInstanceState === ObservableInstanceLifecycle.CREATED
1839
1858
  ? this._getActualSnapshot()
1840
1859
  : this._getCachedInitialSnapshot();
1841
1860
  }
@@ -1885,7 +1904,7 @@ var ObjectNode = /** @class */ (function (_super) {
1885
1904
  var error = this._getAssertAliveError(context);
1886
1905
  switch (livelinessChecking) {
1887
1906
  case "error":
1888
- throw fail(error);
1907
+ throw new MstError(error);
1889
1908
  case "warn":
1890
1909
  warnError(error);
1891
1910
  }
@@ -1923,7 +1942,7 @@ var ObjectNode = /** @class */ (function (_super) {
1923
1942
  });
1924
1943
  this._autoUnbox = false;
1925
1944
  try {
1926
- return this._observableInstanceState === 2 /* ObservableInstanceLifecycle.CREATED */
1945
+ return this._observableInstanceState === ObservableInstanceLifecycle.CREATED
1927
1946
  ? this.type.getChildNode(this, subpath)
1928
1947
  : this._childNodes[subpath];
1929
1948
  }
@@ -1940,7 +1959,7 @@ var ObjectNode = /** @class */ (function (_super) {
1940
1959
  this.assertAlive(EMPTY_OBJECT);
1941
1960
  this._autoUnbox = false;
1942
1961
  try {
1943
- return this._observableInstanceState === 2 /* ObservableInstanceLifecycle.CREATED */
1962
+ return this._observableInstanceState === ObservableInstanceLifecycle.CREATED
1944
1963
  ? this.type.getChildren(this)
1945
1964
  : convertChildNodesToArray(this._childNodes);
1946
1965
  }
@@ -1971,7 +1990,7 @@ var ObjectNode = /** @class */ (function (_super) {
1971
1990
  value: function (context) {
1972
1991
  this.assertAlive(context);
1973
1992
  if (!this.isRunningAction() && this.isProtected) {
1974
- throw fail("Cannot modify '".concat(this, "', the object is protected and can only be modified by using an action."));
1993
+ throw new MstError("Cannot modify '".concat(this, "', the object is protected and can only be modified by using an action."));
1975
1994
  }
1976
1995
  }
1977
1996
  });
@@ -2038,7 +2057,7 @@ var ObjectNode = /** @class */ (function (_super) {
2038
2057
  writable: true,
2039
2058
  value: function () {
2040
2059
  if (!this.isAlive)
2041
- throw fail("Error while detaching, node is not alive.");
2060
+ throw new MstError("Error while detaching, node is not alive.");
2042
2061
  this.clearParent();
2043
2062
  }
2044
2063
  });
@@ -2086,7 +2105,7 @@ var ObjectNode = /** @class */ (function (_super) {
2086
2105
  configurable: true,
2087
2106
  writable: true,
2088
2107
  value: function () {
2089
- if (this._observableInstanceState === 0 /* ObservableInstanceLifecycle.UNINITIALIZED */) {
2108
+ if (this._observableInstanceState === ObservableInstanceLifecycle.UNINITIALIZED) {
2090
2109
  return;
2091
2110
  }
2092
2111
  this.getChildren().forEach(function (node) {
@@ -2095,8 +2114,8 @@ var ObjectNode = /** @class */ (function (_super) {
2095
2114
  // beforeDestroy should run before the disposers since else we could end up in a situation where
2096
2115
  // a disposer added with addDisposer at this stage (beforeDestroy) is actually never released
2097
2116
  this.baseAboutToDie();
2098
- this._internalEventsEmit("dispose" /* InternalEvents.Dispose */);
2099
- this._internalEventsClear("dispose" /* InternalEvents.Dispose */);
2117
+ this._internalEventsEmit(InternalEvents.Dispose);
2118
+ this._internalEventsClear(InternalEvents.Dispose);
2100
2119
  }
2101
2120
  });
2102
2121
  Object.defineProperty(ObjectNode.prototype, "finalizeDeath", {
@@ -2122,7 +2141,7 @@ var ObjectNode = /** @class */ (function (_super) {
2122
2141
  writable: true,
2123
2142
  value: function (onChange) {
2124
2143
  this._addSnapshotReaction();
2125
- return this._internalEventsRegister("snapshot" /* InternalEvents.Snapshot */, onChange);
2144
+ return this._internalEventsRegister(InternalEvents.Snapshot, onChange);
2126
2145
  }
2127
2146
  });
2128
2147
  Object.defineProperty(ObjectNode.prototype, "emitSnapshot", {
@@ -2130,7 +2149,7 @@ var ObjectNode = /** @class */ (function (_super) {
2130
2149
  configurable: true,
2131
2150
  writable: true,
2132
2151
  value: function (snapshot) {
2133
- this._internalEventsEmit("snapshot" /* InternalEvents.Snapshot */, snapshot);
2152
+ this._internalEventsEmit(InternalEvents.Snapshot, snapshot);
2134
2153
  }
2135
2154
  });
2136
2155
  Object.defineProperty(ObjectNode.prototype, "onPatch", {
@@ -2138,7 +2157,7 @@ var ObjectNode = /** @class */ (function (_super) {
2138
2157
  configurable: true,
2139
2158
  writable: true,
2140
2159
  value: function (handler) {
2141
- return this._internalEventsRegister("patch" /* InternalEvents.Patch */, handler);
2160
+ return this._internalEventsRegister(InternalEvents.Patch, handler);
2142
2161
  }
2143
2162
  });
2144
2163
  Object.defineProperty(ObjectNode.prototype, "emitPatch", {
@@ -2146,12 +2165,12 @@ var ObjectNode = /** @class */ (function (_super) {
2146
2165
  configurable: true,
2147
2166
  writable: true,
2148
2167
  value: function (basePatch, source) {
2149
- if (this._internalEventsHasSubscribers("patch" /* InternalEvents.Patch */)) {
2168
+ if (this._internalEventsHasSubscribers(InternalEvents.Patch)) {
2150
2169
  // calculate the relative path of the patch
2151
2170
  var path = source.path.substr(this.path.length) + (basePatch.path ? "/" + basePatch.path : "");
2152
2171
  var localizedPatch = extend({}, basePatch, { path: path });
2153
2172
  var _a = __read(splitPatch(localizedPatch), 2), patch = _a[0], reversePatch = _a[1];
2154
- this._internalEventsEmit("patch" /* InternalEvents.Patch */, patch, reversePatch);
2173
+ this._internalEventsEmit(InternalEvents.Patch, patch, reversePatch);
2155
2174
  }
2156
2175
  if (this.parent)
2157
2176
  this.parent.emitPatch(basePatch, source);
@@ -2162,7 +2181,7 @@ var ObjectNode = /** @class */ (function (_super) {
2162
2181
  configurable: true,
2163
2182
  writable: true,
2164
2183
  value: function (disposer) {
2165
- return this._internalEventsHas("dispose" /* InternalEvents.Dispose */, disposer);
2184
+ return this._internalEventsHas(InternalEvents.Dispose, disposer);
2166
2185
  }
2167
2186
  });
2168
2187
  Object.defineProperty(ObjectNode.prototype, "addDisposer", {
@@ -2171,10 +2190,10 @@ var ObjectNode = /** @class */ (function (_super) {
2171
2190
  writable: true,
2172
2191
  value: function (disposer) {
2173
2192
  if (!this.hasDisposer(disposer)) {
2174
- this._internalEventsRegister("dispose" /* InternalEvents.Dispose */, disposer, true);
2193
+ this._internalEventsRegister(InternalEvents.Dispose, disposer, true);
2175
2194
  return;
2176
2195
  }
2177
- throw fail("cannot add a disposer when it is already registered for execution");
2196
+ throw new MstError("cannot add a disposer when it is already registered for execution");
2178
2197
  }
2179
2198
  });
2180
2199
  Object.defineProperty(ObjectNode.prototype, "removeDisposer", {
@@ -2182,10 +2201,10 @@ var ObjectNode = /** @class */ (function (_super) {
2182
2201
  configurable: true,
2183
2202
  writable: true,
2184
2203
  value: function (disposer) {
2185
- if (!this._internalEventsHas("dispose" /* InternalEvents.Dispose */, disposer)) {
2186
- throw fail("cannot remove a disposer which was never registered for execution");
2204
+ if (!this._internalEventsHas(InternalEvents.Dispose, disposer)) {
2205
+ throw new MstError("cannot remove a disposer which was never registered for execution");
2187
2206
  }
2188
- this._internalEventsUnregister("dispose" /* InternalEvents.Dispose */, disposer);
2207
+ this._internalEventsUnregister(InternalEvents.Dispose, disposer);
2189
2208
  }
2190
2209
  });
2191
2210
  Object.defineProperty(ObjectNode.prototype, "removeMiddleware", {
@@ -2429,7 +2448,7 @@ var BaseType = /** @class */ (function () {
2429
2448
  writable: true,
2430
2449
  value: function (node, applyPostProcess) {
2431
2450
  // istanbul ignore next
2432
- throw fail("unimplemented method");
2451
+ throw new MstError("unimplemented method");
2433
2452
  }
2434
2453
  });
2435
2454
  Object.defineProperty(BaseType.prototype, "isAssignableFrom", {
@@ -2467,7 +2486,7 @@ var BaseType = /** @class */ (function () {
2467
2486
  Object.defineProperty(BaseType.prototype, "Type", {
2468
2487
  get: function () {
2469
2488
  // istanbul ignore next
2470
- throw fail("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`");
2489
+ throw new MstError("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`");
2471
2490
  },
2472
2491
  enumerable: false,
2473
2492
  configurable: true
@@ -2475,7 +2494,7 @@ var BaseType = /** @class */ (function () {
2475
2494
  Object.defineProperty(BaseType.prototype, "TypeWithoutSTN", {
2476
2495
  get: function () {
2477
2496
  // istanbul ignore next
2478
- throw fail("Factory.TypeWithoutSTN should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.TypeWithoutSTN`");
2497
+ throw new MstError("Factory.TypeWithoutSTN should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.TypeWithoutSTN`");
2479
2498
  },
2480
2499
  enumerable: false,
2481
2500
  configurable: true
@@ -2483,7 +2502,7 @@ var BaseType = /** @class */ (function () {
2483
2502
  Object.defineProperty(BaseType.prototype, "SnapshotType", {
2484
2503
  get: function () {
2485
2504
  // istanbul ignore next
2486
- throw fail("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`");
2505
+ throw new MstError("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`");
2487
2506
  },
2488
2507
  enumerable: false,
2489
2508
  configurable: true
@@ -2491,7 +2510,7 @@ var BaseType = /** @class */ (function () {
2491
2510
  Object.defineProperty(BaseType.prototype, "CreationType", {
2492
2511
  get: function () {
2493
2512
  // istanbul ignore next
2494
- throw fail("Factory.CreationType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.CreationType`");
2513
+ throw new MstError("Factory.CreationType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.CreationType`");
2495
2514
  },
2496
2515
  enumerable: false,
2497
2516
  configurable: true
@@ -2981,7 +3000,7 @@ function applyAction(target, actions) {
2981
3000
  function baseApplyAction(target, action) {
2982
3001
  var resolvedTarget = tryResolve(target, action.path || "");
2983
3002
  if (!resolvedTarget)
2984
- throw fail("Invalid action path: ".concat(action.path || ""));
3003
+ throw new MstError("Invalid action path: ".concat(action.path || ""));
2985
3004
  var node = getStateTreeNode(resolvedTarget);
2986
3005
  // Reserved functions
2987
3006
  if (action.name === "@APPLY_PATCHES") {
@@ -2991,7 +3010,7 @@ function baseApplyAction(target, action) {
2991
3010
  return applySnapshot.call(null, resolvedTarget, action.args[0]);
2992
3011
  }
2993
3012
  if (!(typeof resolvedTarget[action.name] === "function"))
2994
- throw fail("Action '".concat(action.name, "' does not exist in '").concat(node.path, "'"));
3013
+ throw new MstError("Action '".concat(action.name, "' does not exist in '").concat(node.path, "'"));
2995
3014
  return resolvedTarget[action.name].apply(resolvedTarget, action.args ? action.args.map(function (v) { return deserializeArgument(node, v); }) : []);
2996
3015
  }
2997
3016
  /**
@@ -3352,11 +3371,11 @@ function runMiddleWares(node, baseCall, originalFn) {
3352
3371
  if (devMode()) {
3353
3372
  if (!nextInvoked && !abortInvoked) {
3354
3373
  var node2 = getStateTreeNode(call.tree);
3355
- throw fail("Neither the next() nor the abort() callback within the middleware ".concat(handler.name, " for the action: \"").concat(call.name, "\" on the node: ").concat(node2.type.name, " was invoked."));
3374
+ throw new MstError("Neither the next() nor the abort() callback within the middleware ".concat(handler.name, " for the action: \"").concat(call.name, "\" on the node: ").concat(node2.type.name, " was invoked."));
3356
3375
  }
3357
3376
  else if (nextInvoked && abortInvoked) {
3358
3377
  var node2 = getStateTreeNode(call.tree);
3359
- throw fail("The next() and abort() callback within the middleware ".concat(handler.name, " for the action: \"").concat(call.name, "\" on the node: ").concat(node2.type.name, " were invoked."));
3378
+ throw new MstError("The next() and abort() callback within the middleware ".concat(handler.name, " for the action: \"").concat(call.name, "\" on the node: ").concat(node2.type.name, " were invoked."));
3360
3379
  }
3361
3380
  }
3362
3381
  return result;
@@ -3503,7 +3522,7 @@ function typecheckInternal(type, value) {
3503
3522
  function typecheck(type, value) {
3504
3523
  var errors = type.validate(value, [{ path: "", type: type }]);
3505
3524
  if (errors.length > 0) {
3506
- throw fail(validationErrorsToString(type, value, errors));
3525
+ throw new MstError(validationErrorsToString(type, value, errors));
3507
3526
  }
3508
3527
  }
3509
3528
  function validationErrorsToString(type, value, errors) {
@@ -3574,7 +3593,7 @@ var IdentifierCache = /** @class */ (function () {
3574
3593
  }
3575
3594
  var set = this.cache.get(identifier);
3576
3595
  if (set.indexOf(node) !== -1)
3577
- throw fail("Already registered");
3596
+ throw new MstError("Already registered");
3578
3597
  set.push(node);
3579
3598
  if (lastCacheUpdate) {
3580
3599
  this.updateLastCacheModificationPerId(identifier);
@@ -3673,7 +3692,7 @@ var IdentifierCache = /** @class */ (function () {
3673
3692
  case 1:
3674
3693
  return matches[0];
3675
3694
  default:
3676
- throw fail("Cannot resolve a reference to type '".concat(type.name, "' with id: '").concat(identifier, "' unambigously, there are multiple candidates: ").concat(matches
3695
+ throw new MstError("Cannot resolve a reference to type '".concat(type.name, "' with id: '").concat(identifier, "' unambigously, there are multiple candidates: ").concat(matches
3677
3696
  .map(function (n) { return n.path; })
3678
3697
  .join(", ")));
3679
3698
  }
@@ -3691,7 +3710,7 @@ function createObjectNode(type, parent, subpath, environment, initialValue) {
3691
3710
  if (existingNode) {
3692
3711
  if (existingNode.parent) {
3693
3712
  // istanbul ignore next
3694
- throw fail("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '".concat(parent ? parent.path : "", "/").concat(subpath, "', but it lives already at '").concat(existingNode.path, "'"));
3713
+ throw new MstError("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '".concat(parent ? parent.path : "", "/").concat(subpath, "', but it lives already at '").concat(existingNode.path, "'"));
3695
3714
  }
3696
3715
  if (parent) {
3697
3716
  existingNode.setParent(parent, subpath);
@@ -3754,7 +3773,7 @@ function assertIsStateTreeNode(value, argNumber) {
3754
3773
  function getStateTreeNode(value) {
3755
3774
  if (!isStateTreeNode(value)) {
3756
3775
  // istanbul ignore next
3757
- throw fail("Value ".concat(value, " is no MST Node"));
3776
+ throw new MstError("Value ".concat(value, " is no MST Node"));
3758
3777
  }
3759
3778
  return value.$treenode;
3760
3779
  }
@@ -3780,7 +3799,7 @@ var doubleDot = function (_) { return ".."; };
3780
3799
  function getRelativePathBetweenNodes(base, target) {
3781
3800
  // PRE condition target is (a child of) base!
3782
3801
  if (base.root !== target.root) {
3783
- throw fail("Cannot calculate relative path: objects '".concat(base, "' and '").concat(target, "' are not part of the same object tree"));
3802
+ throw new MstError("Cannot calculate relative path: objects '".concat(base, "' and '").concat(target, "' are not part of the same object tree"));
3784
3803
  }
3785
3804
  var baseParts = splitJsonPath(base.path);
3786
3805
  var targetParts = splitJsonPath(target.path);
@@ -3837,7 +3856,7 @@ function resolveNodeByPathParts(base, pathParts, failIfResolveFails) {
3837
3856
  }
3838
3857
  }
3839
3858
  }
3840
- throw fail("Could not resolve '".concat(part, "' in path '").concat(joinJsonPath(pathParts.slice(0, i)) || "/", "' while resolving '").concat(joinJsonPath(pathParts), "'"));
3859
+ throw new MstError("Could not resolve '".concat(part, "' in path '").concat(joinJsonPath(pathParts.slice(0, i)) || "/", "' while resolving '").concat(joinJsonPath(pathParts), "'"));
3841
3860
  }
3842
3861
  }
3843
3862
  catch (e) {
@@ -3913,10 +3932,14 @@ Object.freeze(mobxShallow);
3913
3932
  * @internal
3914
3933
  * @hidden
3915
3934
  */
3916
- function fail(message) {
3917
- if (message === void 0) { message = "Illegal state"; }
3918
- return new Error("[mobx-state-tree] " + message);
3919
- }
3935
+ var MstError = /** @class */ (function (_super) {
3936
+ __extends(MstError, _super);
3937
+ function MstError(message) {
3938
+ if (message === void 0) { message = "Illegal state"; }
3939
+ return _super.call(this, "[mobx-state-tree] ".concat(message)) || this;
3940
+ }
3941
+ return MstError;
3942
+ }(Error));
3920
3943
  /**
3921
3944
  * @internal
3922
3945
  * @hidden
@@ -4320,7 +4343,7 @@ function assertArg(value, fn, typeName, argNumber) {
4320
4343
  if (devMode()) {
4321
4344
  if (!fn(value)) {
4322
4345
  // istanbul ignore next
4323
- throw fail("expected ".concat(typeName, " as argument ").concat(asArray(argNumber).join(" or "), ", got ").concat(value, " instead"));
4346
+ throw new MstError("expected ".concat(typeName, " as argument ").concat(asArray(argNumber).join(" or "), ", got ").concat(value, " instead"));
4324
4347
  }
4325
4348
  }
4326
4349
  }
@@ -4465,11 +4488,11 @@ function createFlowSpawner(name, generator) {
4465
4488
  var runId = getNextActionId();
4466
4489
  var parentContext = getCurrentActionContext();
4467
4490
  if (!parentContext) {
4468
- throw fail("a mst flow must always have a parent context");
4491
+ throw new MstError("a mst flow must always have a parent context");
4469
4492
  }
4470
4493
  var parentActionContext = getParentActionContext(parentContext);
4471
4494
  if (!parentActionContext) {
4472
- throw fail("a mst flow must always have a parent action context");
4495
+ throw new MstError("a mst flow must always have a parent action context");
4473
4496
  }
4474
4497
  var contextBase = {
4475
4498
  name: name,
@@ -4540,7 +4563,7 @@ function createFlowSpawner(name, generator) {
4540
4563
  // TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100
4541
4564
  if (!ret.value || typeof ret.value.then !== "function") {
4542
4565
  // istanbul ignore next
4543
- throw fail("Only promises can be yielded to `async`, got: " + ret);
4566
+ throw new MstError("Only promises can be yielded to `async`, got: " + ret);
4544
4567
  }
4545
4568
  return ret.value.then(onFulfilled, onRejected);
4546
4569
  }
@@ -4556,7 +4579,7 @@ function createFlowSpawner(name, generator) {
4556
4579
  */
4557
4580
  function splitPatch(patch) {
4558
4581
  if (!("oldValue" in patch))
4559
- throw fail("Patches without `oldValue` field cannot be inversed");
4582
+ throw new MstError("Patches without `oldValue` field cannot be inversed");
4560
4583
  return [stripPatch(patch), invertPatch(patch)];
4561
4584
  }
4562
4585
  /**
@@ -4657,7 +4680,7 @@ function splitJsonPath(path) {
4657
4680
  stringStartsWith(path, "./") ||
4658
4681
  stringStartsWith(path, "../");
4659
4682
  if (!valid) {
4660
- throw fail("a json path must be either rooted, empty or relative, but got '".concat(path, "'"));
4683
+ throw new MstError("a json path must be either rooted, empty or relative, but got '".concat(path, "'"));
4661
4684
  }
4662
4685
  // '/a/b/c' -> ["a", "b", "c"]
4663
4686
  // '../../b/c' -> ["..", "..", "b", "c"]
@@ -4912,11 +4935,11 @@ function snapshotProcessor(type, processors, name) {
4912
4935
  if (devMode()) {
4913
4936
  if (processors.postProcessor && typeof processors.postProcessor !== "function") {
4914
4937
  // istanbul ignore next
4915
- throw fail("postSnapshotProcessor must be a function");
4938
+ throw new MstError("postSnapshotProcessor must be a function");
4916
4939
  }
4917
4940
  if (processors.preProcessor && typeof processors.preProcessor !== "function") {
4918
4941
  // istanbul ignore next
4919
- throw fail("preSnapshotProcessor must be a function");
4942
+ throw new MstError("preSnapshotProcessor must be a function");
4920
4943
  }
4921
4944
  }
4922
4945
  return new SnapshotProcessor(type, processors, name);
@@ -5005,28 +5028,28 @@ var MSTMap = /** @class */ (function (_super) {
5005
5028
  writable: true,
5006
5029
  value: function (value) {
5007
5030
  if (!value)
5008
- throw fail("Map.put cannot be used to set empty values");
5031
+ throw new MstError("Map.put cannot be used to set empty values");
5009
5032
  if (isStateTreeNode(value)) {
5010
5033
  var node = getStateTreeNode(value);
5011
5034
  if (devMode()) {
5012
5035
  if (!node.identifierAttribute) {
5013
- throw fail(needsIdentifierError);
5036
+ throw new MstError(needsIdentifierError);
5014
5037
  }
5015
5038
  }
5016
5039
  if (node.identifier === null) {
5017
- throw fail(needsIdentifierError);
5040
+ throw new MstError(needsIdentifierError);
5018
5041
  }
5019
5042
  this.set(node.identifier, value);
5020
5043
  return value;
5021
5044
  }
5022
5045
  else if (!isMutable(value)) {
5023
- throw fail("Map.put can only be used to store complex values");
5046
+ throw new MstError("Map.put can only be used to store complex values");
5024
5047
  }
5025
5048
  else {
5026
5049
  var mapNode = getStateTreeNode(this);
5027
5050
  var mapType = mapNode.type;
5028
5051
  if (mapType.identifierMode !== MapIdentifierMode.YES) {
5029
- throw fail(needsIdentifierError);
5052
+ throw new MstError(needsIdentifierError);
5030
5053
  }
5031
5054
  var idAttr = mapType.mapIdentifierAttribute;
5032
5055
  var id = value[idAttr];
@@ -5119,7 +5142,7 @@ var MapType = /** @class */ (function (_super) {
5119
5142
  if (!type.identifierAttribute)
5120
5143
  return current;
5121
5144
  if (current && current !== type.identifierAttribute) {
5122
- throw fail("The objects in a map should all have the same identifier attribute, expected '".concat(current, "', but child of type '").concat(type.name, "' declared attribute '").concat(type.identifierAttribute, "' as identifier"));
5145
+ throw new MstError("The objects in a map should all have the same identifier attribute, expected '".concat(current, "', but child of type '").concat(type.name, "' declared attribute '").concat(type.identifierAttribute, "' as identifier"));
5123
5146
  }
5124
5147
  return type.identifierAttribute;
5125
5148
  }, undefined);
@@ -5198,7 +5221,7 @@ var MapType = /** @class */ (function (_super) {
5198
5221
  value: function (node, key) {
5199
5222
  var childNode = node.storedValue.get("" + key);
5200
5223
  if (!childNode)
5201
- throw fail("Not a child " + key);
5224
+ throw new MstError("Not a child " + key);
5202
5225
  return childNode;
5203
5226
  }
5204
5227
  });
@@ -5243,7 +5266,7 @@ var MapType = /** @class */ (function (_super) {
5243
5266
  if (this.identifierMode === MapIdentifierMode.YES && node instanceof ObjectNode) {
5244
5267
  var identifier = node.identifier;
5245
5268
  if (identifier !== expected)
5246
- throw fail("A map of objects containing an identifier should always store the object under their own identifier. Trying to store key '".concat(identifier, "', but expected: '").concat(expected, "'"));
5269
+ throw new MstError("A map of objects containing an identifier should always store the object under their own identifier. Trying to store key '".concat(identifier, "', but expected: '").concat(expected, "'"));
5247
5270
  }
5248
5271
  }
5249
5272
  });
@@ -5541,7 +5564,7 @@ var ArrayType = /** @class */ (function (_super) {
5541
5564
  var index = Number(key);
5542
5565
  if (index < node.storedValue.length)
5543
5566
  return node.storedValue[index];
5544
- throw fail("Not a child: " + key);
5567
+ throw new MstError("Not a child: " + key);
5545
5568
  }
5546
5569
  });
5547
5570
  Object.defineProperty(ArrayType.prototype, "willChange", {
@@ -5775,7 +5798,7 @@ function reconcileArrayChildren(parent, childType, oldNodes, newValues, newPaths
5775
5798
  // check if already belongs to the same parent. if so, avoid pushing item in. only swapping can occur.
5776
5799
  if (isStateTreeNode(newValue) && getStateTreeNode(newValue).parent === parent) {
5777
5800
  // this node is owned by this parent, but not in the reconcilable set, so it must be double
5778
- throw fail("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '".concat(parent.path, "/").concat(newPath, "', but it lives already at '").concat(getStateTreeNode(newValue).path, "'"));
5801
+ throw new MstError("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '".concat(parent.path, "/").concat(newPath, "', but it lives already at '").concat(getStateTreeNode(newValue).path, "'"));
5779
5802
  }
5780
5803
  nothingChanged = false;
5781
5804
  var newNode = valueAsNode(childType, parent, newPath, newValue);
@@ -5891,7 +5914,7 @@ function toPropertiesObject(declaredProps) {
5891
5914
  var alreadySeenKeys = new Set();
5892
5915
  keysList.forEach(function (key) {
5893
5916
  if (alreadySeenKeys.has(key)) {
5894
- throw fail("".concat(key, " is declared twice in the model. Model should not contain the same keys"));
5917
+ throw new MstError("".concat(key, " is declared twice in the model. Model should not contain the same keys"));
5895
5918
  }
5896
5919
  alreadySeenKeys.add(key);
5897
5920
  });
@@ -5899,17 +5922,17 @@ function toPropertiesObject(declaredProps) {
5899
5922
  return keysList.reduce(function (props, key) {
5900
5923
  // warn if user intended a HOOK
5901
5924
  if (key in Hook) {
5902
- throw fail("Hook '".concat(key, "' was defined as property. Hooks should be defined as part of the actions"));
5925
+ throw new MstError("Hook '".concat(key, "' was defined as property. Hooks should be defined as part of the actions"));
5903
5926
  }
5904
5927
  // the user intended to use a view
5905
5928
  var descriptor = Object.getOwnPropertyDescriptor(declaredProps, key);
5906
5929
  if ("get" in descriptor) {
5907
- throw fail("Getters are not supported as properties. Please use views instead");
5930
+ throw new MstError("Getters are not supported as properties. Please use views instead");
5908
5931
  }
5909
5932
  // undefined and null are not valid
5910
5933
  var value = descriptor.value;
5911
5934
  if (value === null || value === undefined) {
5912
- throw fail("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
5935
+ throw new MstError("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
5913
5936
  }
5914
5937
  // its a primitive, convert to its type
5915
5938
  else if (isPrimitive(value)) {
@@ -5926,17 +5949,17 @@ function toPropertiesObject(declaredProps) {
5926
5949
  else if (isType(value)) ;
5927
5950
  // its a function, maybe the user wanted a view?
5928
5951
  else if (devMode() && typeof value === "function") {
5929
- throw fail("Invalid type definition for property '".concat(key, "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?"));
5952
+ throw new MstError("Invalid type definition for property '".concat(key, "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?"));
5930
5953
  }
5931
5954
  // no other complex values
5932
5955
  else if (devMode() && typeof value === "object") {
5933
- throw fail("Invalid type definition for property '".concat(key, "', it looks like you passed an object. Try passing another model type or a types.frozen."));
5956
+ throw new MstError("Invalid type definition for property '".concat(key, "', it looks like you passed an object. Try passing another model type or a types.frozen."));
5934
5957
  }
5935
5958
  else {
5936
- throw fail("Invalid type definition for property '".concat(key, "', cannot infer a type from a value like '").concat(value, "' (").concat(typeof value, ")"));
5959
+ throw new MstError("Invalid type definition for property '".concat(key, "', cannot infer a type from a value like '").concat(value, "' (").concat(typeof value, ")"));
5937
5960
  }
5938
5961
  return props;
5939
- }, declaredProps);
5962
+ }, __assign({}, declaredProps));
5940
5963
  }
5941
5964
  /**
5942
5965
  * @internal
@@ -6046,7 +6069,7 @@ var ModelType = /** @class */ (function (_super) {
6046
6069
  this.forAllProps(function (propName, propType) {
6047
6070
  if (propType.flags & TypeFlags.Identifier) {
6048
6071
  if (identifierAttribute)
6049
- throw fail("Cannot define property '".concat(propName, "' as object identifier, property '").concat(identifierAttribute, "' is already defined as identifier property"));
6072
+ throw new MstError("Cannot define property '".concat(propName, "' as object identifier, property '").concat(identifierAttribute, "' is already defined as identifier property"));
6050
6073
  identifierAttribute = propName;
6051
6074
  }
6052
6075
  });
@@ -6087,15 +6110,15 @@ var ModelType = /** @class */ (function (_super) {
6087
6110
  value: function (self, actions) {
6088
6111
  // check if return is correct
6089
6112
  if (!isPlainObject(actions))
6090
- throw fail("actions initializer should return a plain object containing actions");
6113
+ throw new MstError("actions initializer should return a plain object containing actions");
6091
6114
  // bind actions to the object created
6092
6115
  Object.keys(actions).forEach(function (name) {
6093
6116
  // warn if preprocessor was given
6094
6117
  if (name === PRE_PROCESS_SNAPSHOT)
6095
- throw fail("Cannot define action '".concat(PRE_PROCESS_SNAPSHOT, "', it should be defined using 'type.preProcessSnapshot(fn)' instead"));
6118
+ throw new MstError("Cannot define action '".concat(PRE_PROCESS_SNAPSHOT, "', it should be defined using 'type.preProcessSnapshot(fn)' instead"));
6096
6119
  // warn if postprocessor was given
6097
6120
  if (name === POST_PROCESS_SNAPSHOT)
6098
- throw fail("Cannot define action '".concat(POST_PROCESS_SNAPSHOT, "', it should be defined using 'type.postProcessSnapshot(fn)' instead"));
6121
+ throw new MstError("Cannot define action '".concat(POST_PROCESS_SNAPSHOT, "', it should be defined using 'type.postProcessSnapshot(fn)' instead"));
6099
6122
  var action2 = actions[name];
6100
6123
  // apply hook composition
6101
6124
  var baseAction = self[name];
@@ -6125,7 +6148,7 @@ var ModelType = /** @class */ (function (_super) {
6125
6148
  value: function (fn) {
6126
6149
  var _this = this;
6127
6150
  if (typeof fn !== "function") {
6128
- throw fail("You passed an ".concat(typeof fn, " to volatile state as an argument, when function is expected"));
6151
+ throw new MstError("You passed an ".concat(typeof fn, " to volatile state as an argument, when function is expected"));
6129
6152
  }
6130
6153
  var stateInitializer = function (self) {
6131
6154
  _this.instantiateVolatileState(self, fn(self));
@@ -6141,7 +6164,7 @@ var ModelType = /** @class */ (function (_super) {
6141
6164
  value: function (self, state) {
6142
6165
  // check views return
6143
6166
  if (!isPlainObject(state))
6144
- throw fail("volatile state initializer should return a plain object containing state");
6167
+ throw new MstError("volatile state initializer should return a plain object containing state");
6145
6168
  set(self, state);
6146
6169
  }
6147
6170
  });
@@ -6154,7 +6177,7 @@ var ModelType = /** @class */ (function (_super) {
6154
6177
  var initializer = function (self) {
6155
6178
  var _a = fn(self), actions = _a.actions, views = _a.views, state = _a.state, rest = __rest(_a, ["actions", "views", "state"]);
6156
6179
  for (var key in rest)
6157
- throw fail("The `extend` function should return an object with a subset of the fields 'actions', 'views' and 'state'. Found invalid key '".concat(key, "'"));
6180
+ throw new MstError("The `extend` function should return an object with a subset of the fields 'actions', 'views' and 'state'. Found invalid key '".concat(key, "'"));
6158
6181
  if (state)
6159
6182
  _this.instantiateVolatileState(self, state);
6160
6183
  if (views)
@@ -6186,7 +6209,7 @@ var ModelType = /** @class */ (function (_super) {
6186
6209
  value: function (self, views) {
6187
6210
  // check views return
6188
6211
  if (!isPlainObject(views))
6189
- throw fail("views initializer should return a plain object containing views");
6212
+ throw new MstError("views initializer should return a plain object containing views");
6190
6213
  Object.getOwnPropertyNames(views).forEach(function (key) {
6191
6214
  var _a;
6192
6215
  // is this a computed property?
@@ -6199,7 +6222,7 @@ var ModelType = /** @class */ (function (_super) {
6199
6222
  (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, key, descriptor.value);
6200
6223
  }
6201
6224
  else {
6202
- throw fail("A view member should either be a function or getter based property");
6225
+ throw new MstError("A view member should either be a function or getter based property");
6203
6226
  }
6204
6227
  });
6205
6228
  }
@@ -6315,11 +6338,11 @@ var ModelType = /** @class */ (function (_super) {
6315
6338
  value: function (node, key) {
6316
6339
  var _a;
6317
6340
  if (!(key in this.properties))
6318
- throw fail("Not a value property: " + key);
6341
+ throw new MstError("Not a value property: " + key);
6319
6342
  var adm = _getAdministration(node.storedValue, key);
6320
6343
  var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
6321
6344
  if (!childNode)
6322
- throw fail("Node not available for property " + key);
6345
+ throw new MstError("Node not available for property " + key);
6323
6346
  return childNode;
6324
6347
  }
6325
6348
  });
@@ -6338,7 +6361,7 @@ var ModelType = /** @class */ (function (_super) {
6338
6361
  atom.reportObserved();
6339
6362
  }
6340
6363
  catch (e) {
6341
- throw fail("".concat(name, " property is declared twice"));
6364
+ throw new MstError("".concat(name, " property is declared twice"));
6342
6365
  }
6343
6366
  res[name] = _this.getChildNode(node, name).snapshot;
6344
6367
  });
@@ -6366,7 +6389,7 @@ var ModelType = /** @class */ (function (_super) {
6366
6389
  writable: true,
6367
6390
  value: function (node, subpath, patch) {
6368
6391
  if (!(patch.op === "replace" || patch.op === "add")) {
6369
- throw fail("object does not support operation ".concat(patch.op));
6392
+ throw new MstError("object does not support operation ".concat(patch.op));
6370
6393
  }
6371
6394
  node.storedValue[subpath] = patch.value;
6372
6395
  }
@@ -6478,7 +6501,7 @@ function model() {
6478
6501
  args[_i] = arguments[_i];
6479
6502
  }
6480
6503
  if (devMode() && typeof args[0] !== "string" && args[1]) {
6481
- throw fail("Model creation failed. First argument must be a string when two arguments are provided");
6504
+ throw new MstError("Model creation failed. First argument must be a string when two arguments are provided");
6482
6505
  }
6483
6506
  var name = typeof args[0] === "string" ? args.shift() : "AnonymousModel";
6484
6507
  var properties = args.shift() || {};
@@ -6725,7 +6748,7 @@ function getPrimitiveFactoryFromValue(value) {
6725
6748
  if (value instanceof Date)
6726
6749
  return DatePrimitive;
6727
6750
  }
6728
- throw fail("Cannot determine primitive type from value " + value);
6751
+ throw new MstError("Cannot determine primitive type from value " + value);
6729
6752
  }
6730
6753
  /**
6731
6754
  * Returns if a given value represents a primitive type.
@@ -7045,7 +7068,7 @@ var Union = /** @class */ (function (_super) {
7045
7068
  value: function (parent, subpath, environment, initialValue) {
7046
7069
  var type = this.determineType(initialValue, undefined);
7047
7070
  if (!type)
7048
- throw fail("No matching type for union " + this.describe()); // can happen in prod builds
7071
+ throw new MstError("No matching type for union " + this.describe()); // can happen in prod builds
7049
7072
  return type.instantiate(parent, subpath, environment, initialValue);
7050
7073
  }
7051
7074
  });
@@ -7056,7 +7079,7 @@ var Union = /** @class */ (function (_super) {
7056
7079
  value: function (current, newValue, parent, subpath) {
7057
7080
  var type = this.determineType(newValue, current.getReconciliationType());
7058
7081
  if (!type)
7059
- throw fail("No matching type for union " + this.describe()); // can happen in prod builds
7082
+ throw new MstError("No matching type for union " + this.describe()); // can happen in prod builds
7060
7083
  return type.reconcile(current, newValue, parent, subpath);
7061
7084
  }
7062
7085
  });
@@ -7271,7 +7294,7 @@ var OptionalValue = /** @class */ (function (_super) {
7271
7294
  function checkOptionalPreconditions(type, defaultValueOrFunction) {
7272
7295
  // make sure we never pass direct instances
7273
7296
  if (typeof defaultValueOrFunction !== "function" && isStateTreeNode(defaultValueOrFunction)) {
7274
- throw fail("default value cannot be an instance, pass a snapshot or a function that creates an instance/snapshot instead");
7297
+ throw new MstError("default value cannot be an instance, pass a snapshot or a function that creates an instance/snapshot instead");
7275
7298
  }
7276
7299
  assertIsType(type, 1);
7277
7300
  if (devMode()) {
@@ -7410,10 +7433,10 @@ var Late = /** @class */ (function (_super) {
7410
7433
  throw e;
7411
7434
  }
7412
7435
  if (mustSucceed && t === undefined)
7413
- throw fail("Late type seems to be used too early, the definition (still) returns undefined");
7436
+ throw new MstError("Late type seems to be used too early, the definition (still) returns undefined");
7414
7437
  if (t) {
7415
7438
  if (devMode() && !isType(t))
7416
- throw fail("Failed to determine subtype, make sure types.late returns a type definition.");
7439
+ throw new MstError("Failed to determine subtype, make sure types.late returns a type definition.");
7417
7440
  this._subType = t;
7418
7441
  }
7419
7442
  }
@@ -7500,7 +7523,7 @@ function late(nameOrType, maybeType) {
7500
7523
  // checks that the type is actually a late type
7501
7524
  if (devMode()) {
7502
7525
  if (!(typeof type === "function" && type.length === 0))
7503
- throw fail("Invalid late type, expected a function with zero arguments that returns a type, got: " +
7526
+ throw new MstError("Invalid late type, expected a function with zero arguments that returns a type, got: " +
7504
7527
  type);
7505
7528
  }
7506
7529
  return new Late(name, type);
@@ -7783,15 +7806,15 @@ var StoredReference = /** @class */ (function () {
7783
7806
  else if (isStateTreeNode(value)) {
7784
7807
  var targetNode = getStateTreeNode(value);
7785
7808
  if (!targetNode.identifierAttribute)
7786
- throw fail("Can only store references with a defined identifier attribute.");
7809
+ throw new MstError("Can only store references with a defined identifier attribute.");
7787
7810
  var id = targetNode.unnormalizedIdentifier;
7788
7811
  if (id === null || id === undefined) {
7789
- throw fail("Can only store references to tree nodes with a defined identifier.");
7812
+ throw new MstError("Can only store references to tree nodes with a defined identifier.");
7790
7813
  }
7791
7814
  this.identifier = id;
7792
7815
  }
7793
7816
  else {
7794
- throw fail("Can only store references to tree nodes or identifiers, got: '".concat(value, "'"));
7817
+ throw new MstError("Can only store references to tree nodes or identifiers, got: '".concat(value, "'"));
7795
7818
  }
7796
7819
  }
7797
7820
  Object.defineProperty(StoredReference.prototype, "updateResolvedReference", {
@@ -8173,7 +8196,7 @@ function reference(subType, options) {
8173
8196
  if (devMode()) {
8174
8197
  if (arguments.length === 2 && typeof arguments[1] === "string") {
8175
8198
  // istanbul ignore next
8176
- throw fail("References with base path are no longer supported. Please remove the base path.");
8199
+ throw new MstError("References with base path are no longer supported. Please remove the base path.");
8177
8200
  }
8178
8201
  }
8179
8202
  var getSetOptions = options ? options : undefined;
@@ -8183,7 +8206,7 @@ function reference(subType, options) {
8183
8206
  if (getSetOptions && (getSetOptions.get || getSetOptions.set)) {
8184
8207
  if (devMode()) {
8185
8208
  if (!getSetOptions.get || !getSetOptions.set) {
8186
- throw fail("reference options must either contain both a 'get' and a 'set' method or none of them");
8209
+ throw new MstError("reference options must either contain both a 'get' and a 'set' method or none of them");
8187
8210
  }
8188
8211
  }
8189
8212
  return new CustomReferenceType(subType, {
@@ -8261,7 +8284,7 @@ var BaseIdentifierType = /** @class */ (function (_super) {
8261
8284
  writable: true,
8262
8285
  value: function (parent, subpath, environment, initialValue) {
8263
8286
  if (!parent || !(parent.type instanceof ModelType))
8264
- throw fail("Identifier types can only be instantiated as direct child of a model type");
8287
+ throw new MstError("Identifier types can only be instantiated as direct child of a model type");
8265
8288
  return createScalarNode(this, parent, subpath, environment, initialValue);
8266
8289
  }
8267
8290
  });
@@ -8272,7 +8295,7 @@ var BaseIdentifierType = /** @class */ (function (_super) {
8272
8295
  value: function (current, newValue, parent, subpath) {
8273
8296
  // we don't consider detaching here since identifier are scalar nodes, and scalar nodes cannot be detached
8274
8297
  if (current.storedValue !== newValue)
8275
- throw fail("Tried to change identifier from '".concat(current.storedValue, "' to '").concat(newValue, "'. Changing identifiers is not allowed."));
8298
+ throw new MstError("Tried to change identifier from '".concat(current.storedValue, "' to '").concat(newValue, "'. Changing identifiers is not allowed."));
8276
8299
  current.setParent(parent, subpath);
8277
8300
  return current;
8278
8301
  }