mobx-state-tree 5.1.8 → 5.2.0-alpha.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.
@@ -1,4 +1,4 @@
1
- import { isComputedProp, isObservableProp, createAtom, action, computed, _allowStateChangesInsideComputed, reaction, runInAction, observable, values, entries, _getGlobalState, isObservableArray, isObservableObject, defineProperty as defineProperty$1, ObservableMap, _interceptReads, intercept, observe, _getAdministration, set, makeObservable, getAtom } from 'mobx';
1
+ import { isComputedProp, isObservableProp, createAtom, action, computed, _allowStateChangesInsideComputed, reaction, runInAction, observable, values, entries, _getGlobalState, isObservableArray, isObservableObject, defineProperty as defineProperty$1, ObservableMap, _interceptReads, intercept, observe, _getAdministration, set, makeObservable, getAtom, when } from 'mobx';
2
2
 
3
3
  var livelinessChecking = "warn";
4
4
  /**
@@ -196,7 +196,7 @@ function getChildType(object, propertyName) {
196
196
  /**
197
197
  * Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
198
198
  * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
199
- * Patches can be used to deep observe a model tree.
199
+ * Patches can be used to deeply observe a model tree.
200
200
  *
201
201
  * @param target the model instance from which to receive patches
202
202
  * @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
@@ -241,7 +241,7 @@ function applyPatch(target, patch) {
241
241
  }
242
242
  /**
243
243
  * Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
244
- * Returns an recorder object with the following signature:
244
+ * Returns a recorder object with the following signature:
245
245
  *
246
246
  * Example:
247
247
  * ```ts
@@ -591,7 +591,7 @@ function getIdentifier(target) {
591
591
  return getStateTreeNode(target).identifier;
592
592
  }
593
593
  /**
594
- * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if it the check passes,
594
+ * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
595
595
  * else it returns undefined.
596
596
  *
597
597
  * @param getter Function to access the reference.
@@ -691,9 +691,9 @@ function getRelativePath(base, target) {
691
691
  }
692
692
  /**
693
693
  * Returns a deep copy of the given state tree node as new tree.
694
- * Short hand for `snapshot(x) = getType(x).create(getSnapshot(x))`
694
+ * Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
695
695
  *
696
- * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
696
+ * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
697
697
  *
698
698
  * @param source
699
699
  * @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
@@ -838,14 +838,21 @@ function getPropertyMembers(typeOrNode) {
838
838
  };
839
839
  }
840
840
  /**
841
- * Returns a reflection of the model node, including name, properties, views, volatile and actions.
841
+ * Returns a reflection of the model node, including name, properties, views, volatile state,
842
+ * and actions. `flowActions` is also provided as a separate array of names for any action that
843
+ * came from a flow generator as well.
844
+ *
845
+ * In the case where a model has two actions: `doSomething` and `doSomethingWithFlow`, where
846
+ * `doSomethingWithFlow` is a flow generator, the `actions` array will contain both actions,
847
+ * i.e. ["doSomething", "doSomethingWithFlow"], and the `flowActions` array will contain only
848
+ * the flow action, i.e. ["doSomethingWithFlow"].
842
849
  *
843
850
  * @param target
844
851
  * @returns
845
852
  */
846
853
  function getMembers(target) {
847
854
  var type = getStateTreeNode(target).type;
848
- var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [] });
855
+ var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [], flowActions: [] });
849
856
  var props = Object.getOwnPropertyNames(target);
850
857
  props.forEach(function (key) {
851
858
  if (key in reflected.properties)
@@ -860,6 +867,8 @@ function getMembers(target) {
860
867
  }
861
868
  if (descriptor.value._isMSTAction === true)
862
869
  reflected.actions.push(key);
870
+ if (descriptor.value._isFlowAction === true)
871
+ reflected.flowActions.push(key);
863
872
  else if (isObservableProp(target, key))
864
873
  reflected.volatile.push(key);
865
874
  else
@@ -894,13 +903,13 @@ function getMembers(target) {
894
903
  * ```
895
904
  *
896
905
  * @param snapshotOrInstance Snapshot or instance
897
- * @returns The same object casted as an instance
906
+ * @returns The same object cast as an instance
898
907
  */
899
908
  function cast(snapshotOrInstance) {
900
909
  return snapshotOrInstance;
901
910
  }
902
911
  /**
903
- * Casts a node instance type to an snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
912
+ * Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
904
913
  * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
905
914
  * but just fool typescript into thinking so.
906
915
  *
@@ -924,14 +933,14 @@ function cast(snapshotOrInstance) {
924
933
  * ```
925
934
  *
926
935
  * @param snapshotOrInstance Snapshot or instance
927
- * @returns The same object casted as an input (creation) snapshot
936
+ * @returns The same object cast as an input (creation) snapshot
928
937
  */
929
938
  function castToSnapshot(snapshotOrInstance) {
930
939
  return snapshotOrInstance;
931
940
  }
932
941
  /**
933
- * Casts a node instance type to a reference snapshot type so it can be assigned to a refernence snapshot (e.g. to be used inside a create call).
934
- * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a refererence snapshot,
942
+ * Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
943
+ * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
935
944
  * but just fool typescript into thinking so.
936
945
  *
937
946
  * Example:
@@ -955,7 +964,7 @@ function castToSnapshot(snapshotOrInstance) {
955
964
  * ```
956
965
  *
957
966
  * @param instance Instance
958
- * @returns The same object casted as an reference snapshot (string or number)
967
+ * @returns The same object cast as a reference snapshot (string or number)
959
968
  */
960
969
  function castToReferenceSnapshot(instance) {
961
970
  return instance;
@@ -1586,9 +1595,10 @@ var ObjectNode = /** @class */ (function (_super) {
1586
1595
  enumerable: false,
1587
1596
  configurable: true,
1588
1597
  writable: true,
1589
- value: function () {
1598
+ value: function (fireHooks) {
1599
+ if (fireHooks === void 0) { fireHooks = true; }
1590
1600
  if (this._observableInstanceState === 0 /* UNINITIALIZED */) {
1591
- this.createObservableInstance();
1601
+ this.createObservableInstance(fireHooks);
1592
1602
  }
1593
1603
  }
1594
1604
  });
@@ -1596,8 +1606,9 @@ var ObjectNode = /** @class */ (function (_super) {
1596
1606
  enumerable: false,
1597
1607
  configurable: true,
1598
1608
  writable: true,
1599
- value: function () {
1600
- var e_1, _a;
1609
+ value: function (fireHooks) {
1610
+ var e_1, _a, e_2, _b;
1611
+ if (fireHooks === void 0) { fireHooks = true; }
1601
1612
  if (devMode()) {
1602
1613
  if (this.state !== NodeLifeCycle.INITIALIZING) {
1603
1614
  // istanbul ignore next
@@ -1622,7 +1633,8 @@ var ObjectNode = /** @class */ (function (_super) {
1622
1633
  // initialize the uninitialized parent chain from parent to child
1623
1634
  for (var parentChain_1 = __values(parentChain), parentChain_1_1 = parentChain_1.next(); !parentChain_1_1.done; parentChain_1_1 = parentChain_1.next()) {
1624
1635
  var p = parentChain_1_1.value;
1625
- p.createObservableInstanceIfNeeded();
1636
+ // delay firing hooks until after all parents have been created
1637
+ p.createObservableInstanceIfNeeded(false);
1626
1638
  }
1627
1639
  }
1628
1640
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -1653,8 +1665,28 @@ var ObjectNode = /** @class */ (function (_super) {
1653
1665
  this._addSnapshotReaction();
1654
1666
  this._childNodes = EMPTY_OBJECT;
1655
1667
  this.state = NodeLifeCycle.CREATED;
1656
- this.fireHook(Hook.afterCreate);
1657
- this.finalizeCreation();
1668
+ if (fireHooks) {
1669
+ this.fireHook(Hook.afterCreate);
1670
+ // Note that the parent might not be finalized at this point
1671
+ // so afterAttach won't be called until later in that case
1672
+ this.finalizeCreation();
1673
+ try {
1674
+ // fire the hooks of the parents that we created
1675
+ for (var _c = __values(parentChain.reverse()), _d = _c.next(); !_d.done; _d = _c.next()) {
1676
+ var p = _d.value;
1677
+ p.fireHook(Hook.afterCreate);
1678
+ // This will call afterAttach on the child if necessary
1679
+ p.finalizeCreation();
1680
+ }
1681
+ }
1682
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1683
+ finally {
1684
+ try {
1685
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1686
+ }
1687
+ finally { if (e_2) throw e_2.error; }
1688
+ }
1689
+ }
1658
1690
  }
1659
1691
  });
1660
1692
  Object.defineProperty(ObjectNode.prototype, "root", {
@@ -1953,19 +1985,19 @@ var ObjectNode = /** @class */ (function (_super) {
1953
1985
  value: function () {
1954
1986
  var _this = this;
1955
1987
  this.baseFinalizeCreation(function () {
1956
- var e_2, _a;
1988
+ var e_3, _a;
1957
1989
  try {
1958
1990
  for (var _b = __values(_this.getChildren()), _c = _b.next(); !_c.done; _c = _b.next()) {
1959
1991
  var child = _c.value;
1960
1992
  child.finalizeCreation();
1961
1993
  }
1962
1994
  }
1963
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1995
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1964
1996
  finally {
1965
1997
  try {
1966
1998
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1967
1999
  }
1968
- finally { if (e_2) throw e_2.error; }
2000
+ finally { if (e_3) throw e_3.error; }
1969
2001
  }
1970
2002
  _this.fireInternalHook(Hook.afterCreationFinalization);
1971
2003
  });
@@ -2289,6 +2321,9 @@ var TypeFlags;
2289
2321
  TypeFlags[TypeFlags["Integer"] = 131072] = "Integer";
2290
2322
  TypeFlags[TypeFlags["Custom"] = 262144] = "Custom";
2291
2323
  TypeFlags[TypeFlags["SnapshotProcessor"] = 524288] = "SnapshotProcessor";
2324
+ TypeFlags[TypeFlags["Lazy"] = 1048576] = "Lazy";
2325
+ TypeFlags[TypeFlags["Finite"] = 2097152] = "Finite";
2326
+ TypeFlags[TypeFlags["Float"] = 4194304] = "Float";
2292
2327
  })(TypeFlags || (TypeFlags = {}));
2293
2328
  /**
2294
2329
  * @internal
@@ -3137,6 +3172,7 @@ function createActionInvoker(target, name, fn) {
3137
3172
  }, fn);
3138
3173
  };
3139
3174
  res._isMSTAction = true;
3175
+ res._isFlowAction = fn._isFlowAction;
3140
3176
  return res;
3141
3177
  }
3142
3178
  /**
@@ -3552,17 +3588,25 @@ var IdentifierCache = /** @class */ (function () {
3552
3588
  enumerable: false,
3553
3589
  configurable: true,
3554
3590
  writable: true,
3555
- value: function (node) {
3591
+ value: function (splitNode) {
3556
3592
  var _this = this;
3557
- var res = new IdentifierCache();
3558
- var basePath = node.path;
3593
+ var newCache = new IdentifierCache();
3594
+ // The slash is added here so we only match children of the splitNode. In version 5.1.8 and
3595
+ // earlier there was no trailing slash, so non children that started with the same path string
3596
+ // were being matched incorrectly.
3597
+ var basePath = splitNode.path + "/";
3559
3598
  entries(this.cache).forEach(function (_a) {
3560
3599
  var _b = __read(_a, 2), id = _b[0], nodes = _b[1];
3561
3600
  var modified = false;
3562
3601
  for (var i = nodes.length - 1; i >= 0; i--) {
3563
- if (nodes[i].path.indexOf(basePath) === 0) {
3564
- res.addNodeToCache(nodes[i], false); // no need to update lastUpdated since it is a whole new cache
3602
+ var node = nodes[i];
3603
+ if (node === splitNode || node.path.indexOf(basePath) === 0) {
3604
+ newCache.addNodeToCache(node, false); // no need to update lastUpdated since it is a whole new cache
3565
3605
  nodes.splice(i, 1);
3606
+ // remove empty sets from cache
3607
+ if (!nodes.length) {
3608
+ _this.cache.delete(id);
3609
+ }
3566
3610
  modified = true;
3567
3611
  }
3568
3612
  }
@@ -3570,7 +3614,7 @@ var IdentifierCache = /** @class */ (function () {
3570
3614
  _this.updateLastCacheModificationPerId(id);
3571
3615
  }
3572
3616
  });
3573
- return res;
3617
+ return newCache;
3574
3618
  }
3575
3619
  });
3576
3620
  Object.defineProperty(IdentifierCache.prototype, "has", {
@@ -3854,15 +3898,24 @@ function identity(_) {
3854
3898
  return _;
3855
3899
  }
3856
3900
  /**
3857
- * pollyfill (for IE) suggested in MDN:
3858
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
3859
3901
  * @internal
3860
3902
  * @hidden
3861
3903
  */
3862
- var isInteger = Number.isInteger ||
3863
- function (value) {
3864
- return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
3865
- };
3904
+ var isInteger = Number.isInteger;
3905
+ /**
3906
+ * @internal
3907
+ * @hidden
3908
+ */
3909
+ function isFloat(val) {
3910
+ return Number(val) === val && val % 1 !== 0;
3911
+ }
3912
+ /**
3913
+ * @internal
3914
+ * @hidden
3915
+ */
3916
+ function isFinite(val) {
3917
+ return Number.isFinite(val);
3918
+ }
3866
3919
  /**
3867
3920
  * @internal
3868
3921
  * @hidden
@@ -4467,6 +4520,7 @@ function createFlowSpawner(name, generator) {
4467
4520
  }
4468
4521
  });
4469
4522
  };
4523
+ spawner._isFlowAction = true;
4470
4524
  return spawner;
4471
4525
  }
4472
4526
 
@@ -4880,8 +4934,8 @@ var MapIdentifierMode;
4880
4934
  })(MapIdentifierMode || (MapIdentifierMode = {}));
4881
4935
  var MSTMap = /** @class */ (function (_super) {
4882
4936
  __extends(MSTMap, _super);
4883
- function MSTMap(initialData) {
4884
- return _super.call(this, initialData, observable.ref.enhancer) || this;
4937
+ function MSTMap(initialData, name) {
4938
+ return _super.call(this, initialData, observable.ref.enhancer, name) || this;
4885
4939
  }
4886
4940
  Object.defineProperty(MSTMap.prototype, "get", {
4887
4941
  enumerable: false,
@@ -5069,7 +5123,7 @@ var MapType = /** @class */ (function (_super) {
5069
5123
  configurable: true,
5070
5124
  writable: true,
5071
5125
  value: function (childNodes) {
5072
- return new MSTMap(childNodes);
5126
+ return new MSTMap(childNodes, this.name);
5073
5127
  }
5074
5128
  });
5075
5129
  Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
@@ -5096,7 +5150,7 @@ var MapType = /** @class */ (function (_super) {
5096
5150
  configurable: true,
5097
5151
  writable: true,
5098
5152
  value: function () {
5099
- return "Map<string, " + this._subType.describe() + ">";
5153
+ return this.name;
5100
5154
  }
5101
5155
  });
5102
5156
  Object.defineProperty(MapType.prototype, "getChildren", {
@@ -5332,7 +5386,7 @@ MapType.prototype.applySnapshot = action(MapType.prototype.applySnapshot);
5332
5386
  * @returns
5333
5387
  */
5334
5388
  function map(subtype) {
5335
- return new MapType("map<string, " + subtype.name + ">", subtype);
5389
+ return new MapType("Map<string, " + subtype.name + ">", subtype);
5336
5390
  }
5337
5391
  /**
5338
5392
  * Returns if a given value represents a map type.
@@ -5411,7 +5465,8 @@ var ArrayType = /** @class */ (function (_super) {
5411
5465
  configurable: true,
5412
5466
  writable: true,
5413
5467
  value: function (childNodes) {
5414
- return observable.array(convertChildNodesToArray(childNodes), mobxShallow);
5468
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
5469
+ return observable.array(convertChildNodesToArray(childNodes), options);
5415
5470
  }
5416
5471
  });
5417
5472
  Object.defineProperty(ArrayType.prototype, "finalizeNewInstance", {
@@ -5438,7 +5493,7 @@ var ArrayType = /** @class */ (function (_super) {
5438
5493
  configurable: true,
5439
5494
  writable: true,
5440
5495
  value: function () {
5441
- return this._subType.describe() + "[]";
5496
+ return this.name;
5442
5497
  }
5443
5498
  });
5444
5499
  Object.defineProperty(ArrayType.prototype, "getChildren", {
@@ -5794,8 +5849,18 @@ var defaultObjectOptions = {
5794
5849
  initializers: EMPTY_ARRAY
5795
5850
  };
5796
5851
  function toPropertiesObject(declaredProps) {
5852
+ var keysList = Object.keys(declaredProps);
5853
+ var alreadySeenKeys = {};
5854
+ keysList.forEach(function (key) {
5855
+ if (alreadySeenKeys[key]) {
5856
+ throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
5857
+ }
5858
+ else {
5859
+ alreadySeenKeys[key] = true;
5860
+ }
5861
+ });
5797
5862
  // loop through properties and ensures that all items are types
5798
- return Object.keys(declaredProps).reduce(function (props, key) {
5863
+ return keysList.reduce(function (props, key) {
5799
5864
  var _a, _b, _c;
5800
5865
  // warn if user intended a HOOK
5801
5866
  if (key in Hook)
@@ -6015,6 +6080,7 @@ var ModelType = /** @class */ (function (_super) {
6015
6080
  // while still allowing the middlewares to register them
6016
6081
  var middlewares = action2.$mst_middleware; // make sure middlewares are not lost
6017
6082
  var boundAction = action2.bind(actions);
6083
+ boundAction._isFlowAction = action2._isFlowAction || false;
6018
6084
  boundAction.$mst_middleware = middlewares;
6019
6085
  var actionInvoker = createActionInvoker(self, name, boundAction);
6020
6086
  actions[name] = actionInvoker;
@@ -6140,7 +6206,8 @@ var ModelType = /** @class */ (function (_super) {
6140
6206
  configurable: true,
6141
6207
  writable: true,
6142
6208
  value: function (childNodes) {
6143
- return observable.object(childNodes, EMPTY_OBJECT, mobxShallow);
6209
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
6210
+ return observable.object(childNodes, EMPTY_OBJECT, options);
6144
6211
  }
6145
6212
  });
6146
6213
  Object.defineProperty(ModelType.prototype, "finalizeNewInstance", {
@@ -6216,10 +6283,11 @@ var ModelType = /** @class */ (function (_super) {
6216
6283
  configurable: true,
6217
6284
  writable: true,
6218
6285
  value: function (node, key) {
6286
+ var _a;
6219
6287
  if (!(key in this.properties))
6220
6288
  throw fail$1("Not a value property: " + key);
6221
6289
  var adm = _getAdministration(node.storedValue, key);
6222
- var childNode = adm.raw();
6290
+ var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
6223
6291
  if (!childNode)
6224
6292
  throw fail$1("Node not available for property " + key);
6225
6293
  return childNode;
@@ -6234,7 +6302,14 @@ var ModelType = /** @class */ (function (_super) {
6234
6302
  if (applyPostProcess === void 0) { applyPostProcess = true; }
6235
6303
  var res = {};
6236
6304
  this.forAllProps(function (name, type) {
6237
- getAtom(node.storedValue, name).reportObserved();
6305
+ try {
6306
+ // TODO: FIXME, make sure the observable ref is used!
6307
+ var atom = getAtom(node.storedValue, name);
6308
+ atom.reportObserved();
6309
+ }
6310
+ catch (e) {
6311
+ throw fail$1(name + " property is declared twice");
6312
+ }
6238
6313
  res[name] = _this.getChildNode(node, name).snapshot;
6239
6314
  });
6240
6315
  if (applyPostProcess) {
@@ -6374,6 +6449,9 @@ function model() {
6374
6449
  for (var _i = 0; _i < arguments.length; _i++) {
6375
6450
  args[_i] = arguments[_i];
6376
6451
  }
6452
+ if (devMode() && typeof args[0] !== "string" && args[1]) {
6453
+ throw fail$1("Model creation failed. First argument must be a string when two arguments are provided");
6454
+ }
6377
6455
  var name = typeof args[0] === "string" ? args.shift() : "AnonymousModel";
6378
6456
  var properties = args.shift() || {};
6379
6457
  return new ModelType({ name: name, properties: properties });
@@ -6527,7 +6605,6 @@ var string = new CoreType("string", TypeFlags.String, function (v) { return type
6527
6605
  var number = new CoreType("number", TypeFlags.Number, function (v) { return typeof v === "number"; });
6528
6606
  /**
6529
6607
  * `types.integer` - Creates a type that can only contain an integer value.
6530
- * This type is used for integer values by default
6531
6608
  *
6532
6609
  * Example:
6533
6610
  * ```ts
@@ -6539,6 +6616,32 @@ var number = new CoreType("number", TypeFlags.Number, function (v) { return type
6539
6616
  */
6540
6617
  // tslint:disable-next-line:variable-name
6541
6618
  var integer = new CoreType("integer", TypeFlags.Integer, function (v) { return isInteger(v); });
6619
+ /**
6620
+ * `types.float` - Creates a type that can only contain an float value.
6621
+ *
6622
+ * Example:
6623
+ * ```ts
6624
+ * const Size = types.model({
6625
+ * width: types.float,
6626
+ * height: 10
6627
+ * })
6628
+ * ```
6629
+ */
6630
+ // tslint:disable-next-line:variable-name
6631
+ var float = new CoreType("float", TypeFlags.Float, function (v) { return isFloat(v); });
6632
+ /**
6633
+ * `types.finite` - Creates a type that can only contain an finite value.
6634
+ *
6635
+ * Example:
6636
+ * ```ts
6637
+ * const Size = types.model({
6638
+ * width: types.finite,
6639
+ * height: 10
6640
+ * })
6641
+ * ```
6642
+ */
6643
+ // tslint:disable-next-line:variable-name
6644
+ var finite = new CoreType("finite", TypeFlags.Finite, function (v) { return isFinite(v); });
6542
6645
  /**
6543
6646
  * `types.boolean` - Creates a type that can only contain a boolean value.
6544
6647
  * This type is used for boolean values by default
@@ -7388,6 +7491,120 @@ function isLateType(type) {
7388
7491
  return isType(type) && (type.flags & TypeFlags.Late) > 0;
7389
7492
  }
7390
7493
 
7494
+ function lazy(name, options) {
7495
+ // TODO: fix this unknown casting to be stricter
7496
+ return new Lazy(name, options);
7497
+ }
7498
+ /**
7499
+ * @internal
7500
+ * @hidden
7501
+ */
7502
+ var Lazy = /** @class */ (function (_super) {
7503
+ __extends(Lazy, _super);
7504
+ function Lazy(name, options) {
7505
+ var _this = _super.call(this, name) || this;
7506
+ Object.defineProperty(_this, "options", {
7507
+ enumerable: true,
7508
+ configurable: true,
7509
+ writable: true,
7510
+ value: options
7511
+ });
7512
+ Object.defineProperty(_this, "flags", {
7513
+ enumerable: true,
7514
+ configurable: true,
7515
+ writable: true,
7516
+ value: TypeFlags.Lazy
7517
+ });
7518
+ Object.defineProperty(_this, "loadedType", {
7519
+ enumerable: true,
7520
+ configurable: true,
7521
+ writable: true,
7522
+ value: null
7523
+ });
7524
+ Object.defineProperty(_this, "pendingNodeList", {
7525
+ enumerable: true,
7526
+ configurable: true,
7527
+ writable: true,
7528
+ value: observable.array()
7529
+ });
7530
+ when(function () {
7531
+ return _this.pendingNodeList.length > 0 &&
7532
+ _this.pendingNodeList.some(function (node) {
7533
+ return node.isAlive &&
7534
+ _this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
7535
+ });
7536
+ }, function () {
7537
+ _this.options.loadType().then(action(function (type) {
7538
+ _this.loadedType = type;
7539
+ _this.pendingNodeList.forEach(function (node) {
7540
+ if (!node.parent)
7541
+ return;
7542
+ if (!_this.loadedType)
7543
+ return;
7544
+ node.parent.applyPatches([
7545
+ {
7546
+ op: "replace",
7547
+ path: "/" + node.subpath,
7548
+ value: node.snapshot
7549
+ }
7550
+ ]);
7551
+ });
7552
+ }));
7553
+ });
7554
+ return _this;
7555
+ }
7556
+ Object.defineProperty(Lazy.prototype, "describe", {
7557
+ enumerable: false,
7558
+ configurable: true,
7559
+ writable: true,
7560
+ value: function () {
7561
+ return "<lazy " + this.name + ">";
7562
+ }
7563
+ });
7564
+ Object.defineProperty(Lazy.prototype, "instantiate", {
7565
+ enumerable: false,
7566
+ configurable: true,
7567
+ writable: true,
7568
+ value: function (parent, subpath, environment, value) {
7569
+ var _this = this;
7570
+ if (this.loadedType) {
7571
+ return this.loadedType.instantiate(parent, subpath, environment, value);
7572
+ }
7573
+ var node = createScalarNode(this, parent, subpath, environment, deepFreeze(value));
7574
+ this.pendingNodeList.push(node);
7575
+ when(function () { return !node.isAlive; }, function () { return _this.pendingNodeList.splice(_this.pendingNodeList.indexOf(node), 1); });
7576
+ return node;
7577
+ }
7578
+ });
7579
+ Object.defineProperty(Lazy.prototype, "isValidSnapshot", {
7580
+ enumerable: false,
7581
+ configurable: true,
7582
+ writable: true,
7583
+ value: function (value, context) {
7584
+ if (this.loadedType) {
7585
+ return this.loadedType.validate(value, context);
7586
+ }
7587
+ if (!isSerializable(value)) {
7588
+ return typeCheckFailure(context, value, "Value is not serializable and cannot be lazy");
7589
+ }
7590
+ return typeCheckSuccess();
7591
+ }
7592
+ });
7593
+ Object.defineProperty(Lazy.prototype, "reconcile", {
7594
+ enumerable: false,
7595
+ configurable: true,
7596
+ writable: true,
7597
+ value: function (current, value, parent, subpath) {
7598
+ if (this.loadedType) {
7599
+ current.die();
7600
+ return this.loadedType.instantiate(parent, subpath, parent.environment, value);
7601
+ }
7602
+ return _super.prototype.reconcile.call(this, current, value, parent, subpath);
7603
+ }
7604
+ });
7605
+ return Lazy;
7606
+ }(SimpleType));
7607
+
7391
7608
  /**
7392
7609
  * @internal
7393
7610
  * @hidden
@@ -8326,6 +8543,8 @@ var types = {
8326
8543
  boolean: boolean,
8327
8544
  number: number,
8328
8545
  integer: integer,
8546
+ float: float,
8547
+ finite: finite,
8329
8548
  Date: DatePrimitive,
8330
8549
  map: map,
8331
8550
  array: array,
@@ -8333,6 +8552,7 @@ var types = {
8333
8552
  identifier: identifier,
8334
8553
  identifierNumber: identifierNumber,
8335
8554
  late: late,
8555
+ lazy: lazy,
8336
8556
  undefined: undefinedType,
8337
8557
  null: nullType,
8338
8558
  snapshotProcessor: snapshotProcessor