mobx-state-tree 3.14.1 → 3.17.2

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.
@@ -38,7 +38,6 @@ function setLivelynessChecking(mode) {
38
38
  }
39
39
 
40
40
  /**
41
- * @internal
42
41
  * @hidden
43
42
  */
44
43
  var Hook;
@@ -51,18 +50,18 @@ var Hook;
51
50
  })(Hook || (Hook = {}));
52
51
 
53
52
  /*! *****************************************************************************
54
- Copyright (c) Microsoft Corporation. All rights reserved.
55
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
56
- this file except in compliance with the License. You may obtain a copy of the
57
- License at http://www.apache.org/licenses/LICENSE-2.0
53
+ Copyright (c) Microsoft Corporation.
58
54
 
59
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
60
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
61
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
62
- MERCHANTABLITY OR NON-INFRINGEMENT.
55
+ Permission to use, copy, modify, and/or distribute this software for any
56
+ purpose with or without fee is hereby granted.
63
57
 
64
- See the Apache Version 2.0 License for specific language governing permissions
65
- and limitations under the License.
58
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
59
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
60
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
61
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
62
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
63
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
64
+ PERFORMANCE OF THIS SOFTWARE.
66
65
  ***************************************************************************** */
67
66
  /* global Reflect, Promise */
68
67
 
@@ -109,15 +108,44 @@ function __decorate(decorators, target, key, desc) {
109
108
  return c > 3 && r && Object.defineProperty(target, key, r), r;
110
109
  }
111
110
 
111
+ function __generator(thisArg, body) {
112
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
113
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
114
+ function verb(n) { return function (v) { return step([n, v]); }; }
115
+ function step(op) {
116
+ if (f) throw new TypeError("Generator is already executing.");
117
+ while (_) try {
118
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
119
+ if (y = 0, t) op = [op[0] & 2, t.value];
120
+ switch (op[0]) {
121
+ case 0: case 1: t = op; break;
122
+ case 4: _.label++; return { value: op[1], done: false };
123
+ case 5: _.label++; y = op[1]; op = [0]; continue;
124
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
125
+ default:
126
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
127
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
128
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
129
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
130
+ if (t[2]) _.ops.pop();
131
+ _.trys.pop(); continue;
132
+ }
133
+ op = body.call(thisArg, _);
134
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
135
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
136
+ }
137
+ }
138
+
112
139
  function __values(o) {
113
- var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
140
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
114
141
  if (m) return m.call(o);
115
- return {
142
+ if (o && typeof o.length === "number") return {
116
143
  next: function () {
117
144
  if (o && i >= o.length) o = void 0;
118
145
  return { value: o && o[i++], done: !o };
119
146
  }
120
147
  };
148
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
121
149
  }
122
150
 
123
151
  function __read(o, n) {
@@ -825,7 +853,7 @@ function getPropertyMembers(typeOrNode) {
825
853
  */
826
854
  function getMembers(target) {
827
855
  var type = getStateTreeNode(target).type;
828
- var reflected = __assign({}, getPropertyMembers(type), { actions: [], volatile: [], views: [] });
856
+ var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [] });
829
857
  var props = Object.getOwnPropertyNames(target);
830
858
  props.forEach(function (key) {
831
859
  if (key in reflected.properties)
@@ -970,28 +998,28 @@ var BaseNode = /** @class */ (function () {
970
998
  get: function () {
971
999
  return this._subpath;
972
1000
  },
973
- enumerable: true,
1001
+ enumerable: false,
974
1002
  configurable: true
975
1003
  });
976
1004
  Object.defineProperty(BaseNode.prototype, "subpathUponDeath", {
977
1005
  get: function () {
978
1006
  return this._subpathUponDeath;
979
1007
  },
980
- enumerable: true,
1008
+ enumerable: false,
981
1009
  configurable: true
982
1010
  });
983
1011
  Object.defineProperty(BaseNode.prototype, "pathUponDeath", {
984
1012
  get: function () {
985
1013
  return this._pathUponDeath;
986
1014
  },
987
- enumerable: true,
1015
+ enumerable: false,
988
1016
  configurable: true
989
1017
  });
990
1018
  Object.defineProperty(BaseNode.prototype, "value", {
991
1019
  get: function () {
992
1020
  return this.type.getValue(this);
993
1021
  },
994
- enumerable: true,
1022
+ enumerable: false,
995
1023
  configurable: true
996
1024
  });
997
1025
  Object.defineProperty(BaseNode.prototype, "state", {
@@ -1006,7 +1034,7 @@ var BaseNode = /** @class */ (function () {
1006
1034
  this.aliveAtom.reportChanged();
1007
1035
  }
1008
1036
  },
1009
- enumerable: true,
1037
+ enumerable: false,
1010
1038
  configurable: true
1011
1039
  });
1012
1040
  BaseNode.prototype.fireInternalHook = function (name) {
@@ -1024,7 +1052,7 @@ var BaseNode = /** @class */ (function () {
1024
1052
  get: function () {
1025
1053
  return this._parent;
1026
1054
  },
1027
- enumerable: true,
1055
+ enumerable: false,
1028
1056
  configurable: true
1029
1057
  });
1030
1058
  BaseNode.prototype.baseSetParent = function (parent, subpath) {
@@ -1042,7 +1070,7 @@ var BaseNode = /** @class */ (function () {
1042
1070
  get: function () {
1043
1071
  return this.getEscapedPath(true);
1044
1072
  },
1045
- enumerable: true,
1073
+ enumerable: false,
1046
1074
  configurable: true
1047
1075
  });
1048
1076
  BaseNode.prototype.getEscapedPath = function (reportObserved) {
@@ -1064,21 +1092,21 @@ var BaseNode = /** @class */ (function () {
1064
1092
  get: function () {
1065
1093
  return this.parent === null;
1066
1094
  },
1067
- enumerable: true,
1095
+ enumerable: false,
1068
1096
  configurable: true
1069
1097
  });
1070
1098
  Object.defineProperty(BaseNode.prototype, "isAlive", {
1071
1099
  get: function () {
1072
1100
  return this.state !== NodeLifeCycle.DEAD;
1073
1101
  },
1074
- enumerable: true,
1102
+ enumerable: false,
1075
1103
  configurable: true
1076
1104
  });
1077
1105
  Object.defineProperty(BaseNode.prototype, "isDetaching", {
1078
1106
  get: function () {
1079
1107
  return this.state === NodeLifeCycle.DETACHING;
1080
1108
  },
1081
- enumerable: true,
1109
+ enumerable: false,
1082
1110
  configurable: true
1083
1111
  });
1084
1112
  Object.defineProperty(BaseNode.prototype, "observableIsAlive", {
@@ -1089,7 +1117,7 @@ var BaseNode = /** @class */ (function () {
1089
1117
  this.aliveAtom.reportObserved();
1090
1118
  return this.isAlive;
1091
1119
  },
1092
- enumerable: true,
1120
+ enumerable: false,
1093
1121
  configurable: true
1094
1122
  });
1095
1123
  BaseNode.prototype.baseFinalizeCreation = function (whenFinalized) {
@@ -1159,7 +1187,7 @@ var ScalarNode = /** @class */ (function (_super) {
1159
1187
  throw fail$1("This scalar node is not part of a tree");
1160
1188
  return this.parent.root;
1161
1189
  },
1162
- enumerable: true,
1190
+ enumerable: false,
1163
1191
  configurable: true
1164
1192
  });
1165
1193
  ScalarNode.prototype.setParent = function (newParent, subpath) {
@@ -1189,7 +1217,7 @@ var ScalarNode = /** @class */ (function (_super) {
1189
1217
  get: function () {
1190
1218
  return freeze(this.getSnapshot());
1191
1219
  },
1192
- enumerable: true,
1220
+ enumerable: false,
1193
1221
  configurable: true
1194
1222
  });
1195
1223
  ScalarNode.prototype.getSnapshot = function () {
@@ -1288,10 +1316,12 @@ var ObjectNode = /** @class */ (function (_super) {
1288
1316
  this._applySnapshot(snapshot);
1289
1317
  };
1290
1318
  ObjectNode.prototype.createObservableInstanceIfNeeded = function () {
1291
- var e_1, _a;
1292
- if (this._observableInstanceState !== 0 /* UNINITIALIZED */) {
1293
- return;
1319
+ if (this._observableInstanceState === 0 /* UNINITIALIZED */) {
1320
+ this.createObservableInstance();
1294
1321
  }
1322
+ };
1323
+ ObjectNode.prototype.createObservableInstance = function () {
1324
+ var e_1, _a;
1295
1325
  if (devMode()) {
1296
1326
  if (this.state !== NodeLifeCycle.INITIALIZING) {
1297
1327
  // istanbul ignore next
@@ -1357,7 +1387,7 @@ var ObjectNode = /** @class */ (function (_super) {
1357
1387
  var parent = this.parent;
1358
1388
  return parent ? parent.root : this;
1359
1389
  },
1360
- enumerable: true,
1390
+ enumerable: false,
1361
1391
  configurable: true
1362
1392
  });
1363
1393
  ObjectNode.prototype.clearParent = function () {
@@ -1442,7 +1472,7 @@ var ObjectNode = /** @class */ (function (_super) {
1442
1472
  get: function () {
1443
1473
  return freeze(this.getSnapshot());
1444
1474
  },
1445
- enumerable: true,
1475
+ enumerable: false,
1446
1476
  configurable: true
1447
1477
  });
1448
1478
  // NOTE: we use this method to get snapshot without creating @computed overhead
@@ -1535,7 +1565,7 @@ var ObjectNode = /** @class */ (function (_super) {
1535
1565
  get: function () {
1536
1566
  return this.root.isProtectionEnabled;
1537
1567
  },
1538
- enumerable: true,
1568
+ enumerable: false,
1539
1569
  configurable: true
1540
1570
  });
1541
1571
  ObjectNode.prototype.assertWritable = function (context) {
@@ -1749,7 +1779,7 @@ var ObjectNode = /** @class */ (function (_super) {
1749
1779
  };
1750
1780
  __decorate([
1751
1781
  mobx.action
1752
- ], ObjectNode.prototype, "createObservableInstanceIfNeeded", null);
1782
+ ], ObjectNode.prototype, "createObservableInstance", null);
1753
1783
  __decorate([
1754
1784
  mobx.computed
1755
1785
  ], ObjectNode.prototype, "snapshot", null);
@@ -1835,7 +1865,7 @@ var BaseType = /** @class */ (function () {
1835
1865
  // istanbul ignore next
1836
1866
  throw fail$1("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`");
1837
1867
  },
1838
- enumerable: true,
1868
+ enumerable: false,
1839
1869
  configurable: true
1840
1870
  });
1841
1871
  Object.defineProperty(BaseType.prototype, "TypeWithoutSTN", {
@@ -1843,7 +1873,7 @@ var BaseType = /** @class */ (function () {
1843
1873
  // istanbul ignore next
1844
1874
  throw fail$1("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`");
1845
1875
  },
1846
- enumerable: true,
1876
+ enumerable: false,
1847
1877
  configurable: true
1848
1878
  });
1849
1879
  Object.defineProperty(BaseType.prototype, "SnapshotType", {
@@ -1851,7 +1881,7 @@ var BaseType = /** @class */ (function () {
1851
1881
  // istanbul ignore next
1852
1882
  throw fail$1("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`");
1853
1883
  },
1854
- enumerable: true,
1884
+ enumerable: false,
1855
1885
  configurable: true
1856
1886
  });
1857
1887
  Object.defineProperty(BaseType.prototype, "CreationType", {
@@ -1859,7 +1889,7 @@ var BaseType = /** @class */ (function () {
1859
1889
  // istanbul ignore next
1860
1890
  throw fail$1("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`");
1861
1891
  },
1862
- enumerable: true,
1892
+ enumerable: false,
1863
1893
  configurable: true
1864
1894
  });
1865
1895
  __decorate([
@@ -2096,7 +2126,7 @@ var RunningAction = /** @class */ (function () {
2096
2126
  get: function () {
2097
2127
  return this.flowsPending > 0;
2098
2128
  },
2099
- enumerable: true,
2129
+ enumerable: false,
2100
2130
  configurable: true
2101
2131
  });
2102
2132
  return RunningAction;
@@ -2132,7 +2162,7 @@ function createActionTrackingMiddleware2(middlewareHooks) {
2132
2162
  ? runningActions.get(call.parentActionEvent)
2133
2163
  : undefined;
2134
2164
  if (call.type === "action") {
2135
- var newCall = __assign({}, call, {
2165
+ var newCall = __assign(__assign({}, call), {
2136
2166
  // make a shallow copy of the parent action env
2137
2167
  env: parentRunningAction && parentRunningAction.call.env, parentCall: parentRunningAction && parentRunningAction.call });
2138
2168
  var passesFilter = !middlewareHooks.filter || middlewareHooks.filter(newCall);
@@ -2476,7 +2506,7 @@ function createActionInvoker(target, name, fn) {
2476
2506
  * Middleware can be used to intercept any action is invoked on the subtree where it is attached.
2477
2507
  * If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
2478
2508
  *
2479
- * For more details, see the [middleware docs](../middleware.md)
2509
+ * For more details, see the [middleware docs](concepts/middleware.md)
2480
2510
  *
2481
2511
  * @param target Node to apply the middleware to.
2482
2512
  * @param middleware Middleware to apply.
@@ -2543,7 +2573,7 @@ var CollectedMiddlewares = /** @class */ (function () {
2543
2573
  get: function () {
2544
2574
  return this.middlewares.length <= 0;
2545
2575
  },
2546
- enumerable: true,
2576
+ enumerable: false,
2547
2577
  configurable: true
2548
2578
  });
2549
2579
  CollectedMiddlewares.prototype.getNextMiddleware = function () {
@@ -2974,10 +3004,7 @@ function getRelativePathBetweenNodes(base, target) {
2974
3004
  break;
2975
3005
  }
2976
3006
  // TODO: assert that no targetParts paths are "..", "." or ""!
2977
- return (baseParts
2978
- .slice(common)
2979
- .map(doubleDot)
2980
- .join("/") + joinJsonPath(targetParts.slice(common)));
3007
+ return (baseParts.slice(common).map(doubleDot).join("/") + joinJsonPath(targetParts.slice(common)));
2981
3008
  }
2982
3009
  /**
2983
3010
  * @internal
@@ -3032,8 +3059,7 @@ function resolveNodeByPathParts(base, pathParts, failIfResolveFails) {
3032
3059
  }
3033
3060
  }
3034
3061
  if (failIfResolveFails)
3035
- throw fail$1("Could not resolve '" + part + "' in path '" + (joinJsonPath(pathParts.slice(0, i)) ||
3036
- "/") + "' while resolving '" + joinJsonPath(pathParts) + "'");
3062
+ throw fail$1("Could not resolve '" + part + "' in path '" + (joinJsonPath(pathParts.slice(0, i)) || "/") + "' while resolving '" + joinJsonPath(pathParts) + "'");
3037
3063
  else
3038
3064
  return undefined;
3039
3065
  }
@@ -3263,7 +3289,7 @@ var EventHandler = /** @class */ (function () {
3263
3289
  get: function () {
3264
3290
  return this.handlers.length > 0;
3265
3291
  },
3266
- enumerable: true,
3292
+ enumerable: false,
3267
3293
  configurable: true
3268
3294
  });
3269
3295
  EventHandler.prototype.register = function (fn, atTheBeginning) {
@@ -3462,9 +3488,24 @@ function assertIsString(value, argNumber, canBeEmpty) {
3462
3488
  assertArg(value, function (s) { return s !== ""; }, "not empty string", argNumber);
3463
3489
  }
3464
3490
  }
3491
+ /**
3492
+ * @internal
3493
+ * @hidden
3494
+ */
3495
+ function setImmediateWithFallback(fn) {
3496
+ if (typeof queueMicrotask === "function") {
3497
+ queueMicrotask(fn);
3498
+ }
3499
+ else if (typeof setImmediate === "function") {
3500
+ setImmediate(fn);
3501
+ }
3502
+ else {
3503
+ setTimeout(fn, 1);
3504
+ }
3505
+ }
3465
3506
 
3466
3507
  /**
3467
- * See [asynchronous actions](https://github.com/mobxjs/mobx-state-tree/blob/master/docs/async-actions.md).
3508
+ * See [asynchronous actions](concepts/async-actions.md).
3468
3509
  *
3469
3510
  * @returns The flow as a promise.
3470
3511
  */
@@ -3472,7 +3513,8 @@ function flow(generator) {
3472
3513
  return createFlowSpawner(generator.name, generator);
3473
3514
  }
3474
3515
  /**
3475
- * Used for TypeScript to make flows that return a promise return the actual promise result.
3516
+ * @deprecated Not needed since TS3.6.
3517
+ * Used for TypeScript to make flows that return a promise return the actual promise result.
3476
3518
  *
3477
3519
  * @param val
3478
3520
  * @returns
@@ -3480,6 +3522,72 @@ function flow(generator) {
3480
3522
  function castFlowReturn(val) {
3481
3523
  return val;
3482
3524
  }
3525
+ /**
3526
+ * @experimental
3527
+ * experimental api - might change on minor/patch releases
3528
+ *
3529
+ * Convert a promise-returning function to a generator-returning one.
3530
+ * This is intended to allow for usage of `yield*` in async actions to
3531
+ * retain the promise return type.
3532
+ *
3533
+ * Example:
3534
+ * ```ts
3535
+ * function getDataAsync(input: string): Promise<number> { ... }
3536
+ * const getDataGen = toGeneratorFunction(getDataAsync);
3537
+ *
3538
+ * const someModel.actions(self => ({
3539
+ * someAction: flow(function*() {
3540
+ * // value is typed as number
3541
+ * const value = yield* getDataGen("input value");
3542
+ * ...
3543
+ * })
3544
+ * }))
3545
+ * ```
3546
+ */
3547
+ function toGeneratorFunction(p) {
3548
+ return function () {
3549
+ var _i;
3550
+ var args = [];
3551
+ for (_i = 0; _i < arguments.length; _i++) {
3552
+ args[_i] = arguments[_i];
3553
+ }
3554
+ return __generator(this, function (_a) {
3555
+ switch (_a.label) {
3556
+ case 0: return [4 /*yield*/, p.apply(void 0, __spread(args))];
3557
+ case 1: return [2 /*return*/, (_a.sent())];
3558
+ }
3559
+ });
3560
+ };
3561
+ }
3562
+ /**
3563
+ * @experimental
3564
+ * experimental api - might change on minor/patch releases
3565
+ *
3566
+ * Convert a promise to a generator yielding that promise
3567
+ * This is intended to allow for usage of `yield*` in async actions to
3568
+ * retain the promise return type.
3569
+ *
3570
+ * Example:
3571
+ * ```ts
3572
+ * function getDataAsync(input: string): Promise<number> { ... }
3573
+ *
3574
+ * const someModel.actions(self => ({
3575
+ * someAction: flow(function*() {
3576
+ * // value is typed as number
3577
+ * const value = yield* toGenerator(getDataAsync("input value"));
3578
+ * ...
3579
+ * })
3580
+ * }))
3581
+ * ```
3582
+ */
3583
+ function toGenerator(p) {
3584
+ return __generator(this, function (_a) {
3585
+ switch (_a.label) {
3586
+ case 0: return [4 /*yield*/, p];
3587
+ case 1: return [2 /*return*/, (_a.sent())];
3588
+ }
3589
+ });
3590
+ }
3483
3591
  /**
3484
3592
  * @internal
3485
3593
  * @hidden
@@ -3510,7 +3618,7 @@ function createFlowSpawner(name, generator) {
3510
3618
  var args = arguments;
3511
3619
  function wrap(fn, type, arg) {
3512
3620
  fn.$mst_middleware = spawner.$mst_middleware; // pick up any middleware attached to the flow
3513
- runWithActionContext(__assign({}, contextBase, { type: type, args: [arg] }), fn);
3621
+ runWithActionContext(__assign(__assign({}, contextBase), { type: type, args: [arg] }), fn);
3514
3622
  }
3515
3623
  return new Promise(function (resolve, reject) {
3516
3624
  var gen;
@@ -3519,7 +3627,7 @@ function createFlowSpawner(name, generator) {
3519
3627
  onFulfilled(undefined); // kick off the flow
3520
3628
  };
3521
3629
  init.$mst_middleware = spawner.$mst_middleware;
3522
- runWithActionContext(__assign({}, contextBase, { type: "flow_spawn", args: argsToArray(args) }), init);
3630
+ runWithActionContext(__assign(__assign({}, contextBase), { type: "flow_spawn", args: argsToArray(args) }), init);
3523
3631
  function onFulfilled(res) {
3524
3632
  var ret;
3525
3633
  try {
@@ -3528,7 +3636,7 @@ function createFlowSpawner(name, generator) {
3528
3636
  }
3529
3637
  catch (e) {
3530
3638
  // prettier-ignore
3531
- setImmediate(function () {
3639
+ setImmediateWithFallback(function () {
3532
3640
  wrap(function (r) { reject(e); }, "flow_throw", e);
3533
3641
  });
3534
3642
  return;
@@ -3544,7 +3652,7 @@ function createFlowSpawner(name, generator) {
3544
3652
  }
3545
3653
  catch (e) {
3546
3654
  // prettier-ignore
3547
- setImmediate(function () {
3655
+ setImmediateWithFallback(function () {
3548
3656
  wrap(function (r) { reject(e); }, "flow_throw", e);
3549
3657
  });
3550
3658
  return;
@@ -3554,7 +3662,7 @@ function createFlowSpawner(name, generator) {
3554
3662
  function next(ret) {
3555
3663
  if (ret.done) {
3556
3664
  // prettier-ignore
3557
- setImmediate(function () {
3665
+ setImmediateWithFallback(function () {
3558
3666
  wrap(function (r) { resolve(r); }, "flow_return", ret.value);
3559
3667
  });
3560
3668
  return;
@@ -3704,7 +3812,7 @@ var SnapshotProcessor = /** @class */ (function (_super) {
3704
3812
  get: function () {
3705
3813
  return this._subtype.flags | TypeFlags.SnapshotProcessor;
3706
3814
  },
3707
- enumerable: true,
3815
+ enumerable: false,
3708
3816
  configurable: true
3709
3817
  });
3710
3818
  SnapshotProcessor.prototype.describe = function () {
@@ -3759,9 +3867,12 @@ var SnapshotProcessor = /** @class */ (function (_super) {
3759
3867
  return this._subtype;
3760
3868
  };
3761
3869
  SnapshotProcessor.prototype.is = function (thing) {
3762
- return (this._subtype.validate(isType(thing) ? this._subtype : this.preProcessSnapshot(thing), [
3763
- { path: "", type: this._subtype }
3764
- ]).length === 0);
3870
+ var value = isType(thing)
3871
+ ? this._subtype
3872
+ : isStateTreeNode(thing)
3873
+ ? getSnapshot(thing, false)
3874
+ : this.preProcessSnapshot(thing);
3875
+ return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
3765
3876
  };
3766
3877
  return SnapshotProcessor;
3767
3878
  }(BaseType));
@@ -3934,15 +4045,22 @@ var MSTMap = /** @class */ (function (_super) {
3934
4045
  */
3935
4046
  var MapType = /** @class */ (function (_super) {
3936
4047
  __extends(MapType, _super);
3937
- function MapType(name, _subType) {
4048
+ function MapType(name, _subType, hookInitializers) {
4049
+ if (hookInitializers === void 0) { hookInitializers = []; }
3938
4050
  var _this = _super.call(this, name) || this;
3939
4051
  _this._subType = _subType;
3940
4052
  _this.identifierMode = MapIdentifierMode.UNKNOWN;
3941
4053
  _this.mapIdentifierAttribute = undefined;
3942
4054
  _this.flags = TypeFlags.Map;
4055
+ _this.hookInitializers = [];
3943
4056
  _this._determineIdentifierMode();
4057
+ _this.hookInitializers = hookInitializers;
3944
4058
  return _this;
3945
4059
  }
4060
+ MapType.prototype.hooks = function (hooks) {
4061
+ var hookInitializers = this.hookInitializers.length > 0 ? this.hookInitializers.concat(hooks) : [hooks];
4062
+ return new MapType(this.name, this._subType, hookInitializers);
4063
+ };
3946
4064
  MapType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
3947
4065
  this._determineIdentifierMode();
3948
4066
  return createObjectNode(this, parent, subpath, environment, initialValue);
@@ -3985,6 +4103,15 @@ var MapType = /** @class */ (function (_super) {
3985
4103
  };
3986
4104
  MapType.prototype.finalizeNewInstance = function (node, instance) {
3987
4105
  mobx._interceptReads(instance, node.unbox);
4106
+ var type = node.type;
4107
+ type.hookInitializers.forEach(function (initializer) {
4108
+ var hooks = initializer(instance);
4109
+ Object.keys(hooks).forEach(function (name) {
4110
+ var hook = hooks[name];
4111
+ var actionInvoker = createActionInvoker(instance, name, hook);
4112
+ (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(instance, name, actionInvoker);
4113
+ });
4114
+ });
3988
4115
  mobx.intercept(instance, this.willChange);
3989
4116
  mobx.observe(instance, this.didChange);
3990
4117
  };
@@ -4179,12 +4306,19 @@ function isMapType(type) {
4179
4306
  */
4180
4307
  var ArrayType = /** @class */ (function (_super) {
4181
4308
  __extends(ArrayType, _super);
4182
- function ArrayType(name, _subType) {
4309
+ function ArrayType(name, _subType, hookInitializers) {
4310
+ if (hookInitializers === void 0) { hookInitializers = []; }
4183
4311
  var _this = _super.call(this, name) || this;
4184
4312
  _this._subType = _subType;
4185
4313
  _this.flags = TypeFlags.Array;
4314
+ _this.hookInitializers = [];
4315
+ _this.hookInitializers = hookInitializers;
4186
4316
  return _this;
4187
4317
  }
4318
+ ArrayType.prototype.hooks = function (hooks) {
4319
+ var hookInitializers = this.hookInitializers.length > 0 ? this.hookInitializers.concat(hooks) : [hooks];
4320
+ return new ArrayType(this.name, this._subType, hookInitializers);
4321
+ };
4188
4322
  ArrayType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
4189
4323
  return createObjectNode(this, parent, subpath, environment, initialValue);
4190
4324
  };
@@ -4203,6 +4337,15 @@ var ArrayType = /** @class */ (function (_super) {
4203
4337
  };
4204
4338
  ArrayType.prototype.finalizeNewInstance = function (node, instance) {
4205
4339
  mobx._getAdministration(instance).dehancer = node.unbox;
4340
+ var type = node.type;
4341
+ type.hookInitializers.forEach(function (initializer) {
4342
+ var hooks = initializer(instance);
4343
+ Object.keys(hooks).forEach(function (name) {
4344
+ var hook = hooks[name];
4345
+ var actionInvoker = createActionInvoker(instance, name, hook);
4346
+ (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(instance, name, actionInvoker);
4347
+ });
4348
+ });
4206
4349
  mobx.intercept(instance, this.willChange);
4207
4350
  mobx.observe(instance, this.didChange);
4208
4351
  };
@@ -4582,10 +4725,6 @@ var ModelType = /** @class */ (function (_super) {
4582
4725
  postProcessor: function (snapshot) { return postProcessor(currentPostprocessor(snapshot)); }
4583
4726
  });
4584
4727
  };
4585
- var name = opts.name || defaultObjectOptions.name;
4586
- // TODO: this test still needed?
4587
- if (!/^\w[\w\d_]*$/.test(name))
4588
- throw fail$1("Typename should be a valid identifier: " + name);
4589
4728
  Object.assign(_this, defaultObjectOptions, opts);
4590
4729
  // ensures that any default value gets converted to its related type
4591
4730
  _this.properties = toPropertiesObject(_this.properties);
@@ -4656,6 +4795,9 @@ var ModelType = /** @class */ (function (_super) {
4656
4795
  };
4657
4796
  ModelType.prototype.volatile = function (fn) {
4658
4797
  var _this = this;
4798
+ if (typeof fn !== "function") {
4799
+ throw fail$1("You passed an " + typeof fn + " to volatile state as an argument, when function is expected");
4800
+ }
4659
4801
  var stateInitializer = function (self) {
4660
4802
  _this.instantiateVolatileState(self, fn(self));
4661
4803
  return self;
@@ -4862,7 +5004,9 @@ var ModelType = /** @class */ (function (_super) {
4862
5004
  var _this = this;
4863
5005
  // optimization: cache
4864
5006
  return ("{ " +
4865
- this.propertyNames.map(function (key) { return key + ": " + _this.properties[key].describe(); }).join("; ") +
5007
+ this.propertyNames
5008
+ .map(function (key) { return key + ": " + _this.properties[key].describe(); })
5009
+ .join("; ") +
4866
5010
  " }");
4867
5011
  };
4868
5012
  ModelType.prototype.getDefaultSnapshot = function () {
@@ -4879,7 +5023,7 @@ var ModelType = /** @class */ (function (_super) {
4879
5023
  /**
4880
5024
  * `types.model` - Creates a new model type by providing a name, properties, volatile state and actions.
4881
5025
  *
4882
- * See the [model type](https://github.com/mobxjs/mobx-state-tree#creating-models) description or the [getting started](https://github.com/mobxjs/mobx-state-tree/blob/master/docs/getting-started.md#getting-started-1) tutorial.
5026
+ * See the [model type](/concepts/trees#creating-models) description or the [getting started](intro/getting-started.md#getting-started-1) tutorial.
4883
5027
  */
4884
5028
  function model() {
4885
5029
  var args = [];
@@ -5160,7 +5304,7 @@ var Refinement = /** @class */ (function (_super) {
5160
5304
  get: function () {
5161
5305
  return this._subtype.flags | TypeFlags.Refinement;
5162
5306
  },
5163
- enumerable: true,
5307
+ enumerable: false,
5164
5308
  configurable: true
5165
5309
  });
5166
5310
  Refinement.prototype.describe = function () {
@@ -5280,7 +5424,7 @@ var Union = /** @class */ (function (_super) {
5280
5424
  });
5281
5425
  return result;
5282
5426
  },
5283
- enumerable: true,
5427
+ enumerable: false,
5284
5428
  configurable: true
5285
5429
  });
5286
5430
  Union.prototype.isAssignableFrom = function (type) {
@@ -5312,7 +5456,9 @@ var Union = /** @class */ (function (_super) {
5312
5456
  if (reconcileCurrentType.is(value)) {
5313
5457
  return reconcileCurrentType;
5314
5458
  }
5315
- return this._types.filter(function (t) { return t !== reconcileCurrentType; }).find(function (type) { return type.is(value); });
5459
+ return this._types
5460
+ .filter(function (t) { return t !== reconcileCurrentType; })
5461
+ .find(function (type) { return type.is(value); });
5316
5462
  }
5317
5463
  else {
5318
5464
  return this._types.find(function (type) { return type.is(value); });
@@ -5399,7 +5545,7 @@ var OptionalValue = /** @class */ (function (_super) {
5399
5545
  get: function () {
5400
5546
  return this._subtype.flags | TypeFlags.Optional;
5401
5547
  },
5402
- enumerable: true,
5548
+ enumerable: false,
5403
5549
  configurable: true
5404
5550
  });
5405
5551
  OptionalValue.prototype.describe = function () {
@@ -5554,7 +5700,7 @@ var Late = /** @class */ (function (_super) {
5554
5700
  get: function () {
5555
5701
  return (this._subType ? this._subType.flags : 0) | TypeFlags.Late;
5556
5702
  },
5557
- enumerable: true,
5703
+ enumerable: false,
5558
5704
  configurable: true
5559
5705
  });
5560
5706
  Late.prototype.getSubType = function (mustSucceed) {
@@ -5786,7 +5932,7 @@ var StoredReference = /** @class */ (function () {
5786
5932
  this.updateResolvedReference(this.node);
5787
5933
  return this.resolvedReference.node.value;
5788
5934
  },
5789
- enumerable: true,
5935
+ enumerable: false,
5790
5936
  configurable: true
5791
5937
  });
5792
5938
  return StoredReference;
@@ -6100,7 +6246,7 @@ function isReferenceType(type) {
6100
6246
  * @returns
6101
6247
  */
6102
6248
  function safeReference(subType, options) {
6103
- var refType = reference(subType, __assign({}, options, { onInvalidated: function (ev) {
6249
+ var refType = reference(subType, __assign(__assign({}, options), { onInvalidated: function (ev) {
6104
6250
  ev.removeRef();
6105
6251
  } }));
6106
6252
  if (options && options.acceptsUndefined === false) {
@@ -6243,8 +6389,8 @@ function assertIsValidIdentifier(id, argNumber) {
6243
6389
  * export interface CustomTypeOptions<S, T> {
6244
6390
  * // Friendly name
6245
6391
  * name: string
6246
- * // given a serialized value, how to turn it into the target type
6247
- * fromSnapshot(snapshot: S): T
6392
+ * // given a serialized value and environment, how to turn it into the target type
6393
+ * fromSnapshot(snapshot: S, env: any): T
6248
6394
  * // return the serialization of the current value
6249
6395
  * toSnapshot(value: T): S
6250
6396
  * // if true, this is a converted value, if false, it's a snapshot
@@ -6314,7 +6460,7 @@ var CustomType = /** @class */ (function (_super) {
6314
6460
  CustomType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
6315
6461
  var valueToStore = this.options.isTargetType(initialValue)
6316
6462
  ? initialValue
6317
- : this.options.fromSnapshot(initialValue);
6463
+ : this.options.fromSnapshot(initialValue, parent && parent.root.environment);
6318
6464
  return createScalarNode(this, parent, subpath, environment, valueToStore);
6319
6465
  };
6320
6466
  CustomType.prototype.reconcile = function (current, value, parent, subpath) {
@@ -6328,7 +6474,9 @@ var CustomType = /** @class */ (function (_super) {
6328
6474
  return current;
6329
6475
  }
6330
6476
  }
6331
- var valueToStore = isSnapshot ? this.options.fromSnapshot(value) : value;
6477
+ var valueToStore = isSnapshot
6478
+ ? this.options.fromSnapshot(value, parent.root.environment)
6479
+ : value;
6332
6480
  var newNode = this.instantiate(parent, subpath, undefined, valueToStore);
6333
6481
  current.die(); // noop if detaching
6334
6482
  return newNode;
@@ -6366,13 +6514,6 @@ var types = {
6366
6514
  snapshotProcessor: snapshotProcessor
6367
6515
  };
6368
6516
 
6369
- /*
6370
- * All imports / exports should be proxied through this file.
6371
- * Why? It gives us full control over the module load order, preventing circular dependency isses
6372
- */
6373
-
6374
- /* all code is initially loaded through internal, to avoid circular dep issues */
6375
-
6376
6517
  exports.addDisposer = addDisposer;
6377
6518
  exports.addMiddleware = addMiddleware;
6378
6519
  exports.applyAction = applyAction;
@@ -6441,6 +6582,8 @@ exports.resolvePath = resolvePath;
6441
6582
  exports.setLivelinessChecking = setLivelinessChecking;
6442
6583
  exports.setLivelynessChecking = setLivelynessChecking;
6443
6584
  exports.splitJsonPath = splitJsonPath;
6585
+ exports.toGenerator = toGenerator;
6586
+ exports.toGeneratorFunction = toGeneratorFunction;
6444
6587
  exports.tryReference = tryReference;
6445
6588
  exports.tryResolve = tryResolve;
6446
6589
  exports.typecheck = typecheck;