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.
- package/dist/core/action.d.ts +4 -0
- package/dist/core/mst-operations.d.ts +19 -11
- package/dist/internal.d.ts +1 -0
- package/dist/mobx-state-tree.js +267 -47
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +268 -48
- package/dist/mobx-state-tree.umd.js +267 -47
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/complex-types/model.d.ts +2 -2
- package/dist/types/index.d.ts +4 -1
- package/dist/types/primitives.d.ts +24 -1
- package/dist/types/utility-types/enumeration.d.ts +2 -2
- package/dist/types/utility-types/lazy.d.ts +7 -0
- package/package.json +2 -2
- package/LICENSE +0 -21
package/dist/mobx-state-tree.js
CHANGED
|
@@ -200,7 +200,7 @@ function getChildType(object, propertyName) {
|
|
|
200
200
|
/**
|
|
201
201
|
* Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
|
|
202
202
|
* 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.
|
|
203
|
-
* Patches can be used to
|
|
203
|
+
* Patches can be used to deeply observe a model tree.
|
|
204
204
|
*
|
|
205
205
|
* @param target the model instance from which to receive patches
|
|
206
206
|
* @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
|
|
@@ -245,7 +245,7 @@ function applyPatch(target, patch) {
|
|
|
245
245
|
}
|
|
246
246
|
/**
|
|
247
247
|
* Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
|
|
248
|
-
* Returns
|
|
248
|
+
* Returns a recorder object with the following signature:
|
|
249
249
|
*
|
|
250
250
|
* Example:
|
|
251
251
|
* ```ts
|
|
@@ -595,7 +595,7 @@ function getIdentifier(target) {
|
|
|
595
595
|
return getStateTreeNode(target).identifier;
|
|
596
596
|
}
|
|
597
597
|
/**
|
|
598
|
-
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if
|
|
598
|
+
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
|
|
599
599
|
* else it returns undefined.
|
|
600
600
|
*
|
|
601
601
|
* @param getter Function to access the reference.
|
|
@@ -695,9 +695,9 @@ function getRelativePath(base, target) {
|
|
|
695
695
|
}
|
|
696
696
|
/**
|
|
697
697
|
* Returns a deep copy of the given state tree node as new tree.
|
|
698
|
-
*
|
|
698
|
+
* Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
|
|
699
699
|
*
|
|
700
|
-
* _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_
|
|
700
|
+
* _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_
|
|
701
701
|
*
|
|
702
702
|
* @param source
|
|
703
703
|
* @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.
|
|
@@ -842,14 +842,21 @@ function getPropertyMembers(typeOrNode) {
|
|
|
842
842
|
};
|
|
843
843
|
}
|
|
844
844
|
/**
|
|
845
|
-
* Returns a reflection of the model node, including name, properties, views, volatile
|
|
845
|
+
* Returns a reflection of the model node, including name, properties, views, volatile state,
|
|
846
|
+
* and actions. `flowActions` is also provided as a separate array of names for any action that
|
|
847
|
+
* came from a flow generator as well.
|
|
848
|
+
*
|
|
849
|
+
* In the case where a model has two actions: `doSomething` and `doSomethingWithFlow`, where
|
|
850
|
+
* `doSomethingWithFlow` is a flow generator, the `actions` array will contain both actions,
|
|
851
|
+
* i.e. ["doSomething", "doSomethingWithFlow"], and the `flowActions` array will contain only
|
|
852
|
+
* the flow action, i.e. ["doSomethingWithFlow"].
|
|
846
853
|
*
|
|
847
854
|
* @param target
|
|
848
855
|
* @returns
|
|
849
856
|
*/
|
|
850
857
|
function getMembers(target) {
|
|
851
858
|
var type = getStateTreeNode(target).type;
|
|
852
|
-
var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [] });
|
|
859
|
+
var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [], flowActions: [] });
|
|
853
860
|
var props = Object.getOwnPropertyNames(target);
|
|
854
861
|
props.forEach(function (key) {
|
|
855
862
|
if (key in reflected.properties)
|
|
@@ -864,6 +871,8 @@ function getMembers(target) {
|
|
|
864
871
|
}
|
|
865
872
|
if (descriptor.value._isMSTAction === true)
|
|
866
873
|
reflected.actions.push(key);
|
|
874
|
+
if (descriptor.value._isFlowAction === true)
|
|
875
|
+
reflected.flowActions.push(key);
|
|
867
876
|
else if (mobx.isObservableProp(target, key))
|
|
868
877
|
reflected.volatile.push(key);
|
|
869
878
|
else
|
|
@@ -898,13 +907,13 @@ function getMembers(target) {
|
|
|
898
907
|
* ```
|
|
899
908
|
*
|
|
900
909
|
* @param snapshotOrInstance Snapshot or instance
|
|
901
|
-
* @returns The same object
|
|
910
|
+
* @returns The same object cast as an instance
|
|
902
911
|
*/
|
|
903
912
|
function cast(snapshotOrInstance) {
|
|
904
913
|
return snapshotOrInstance;
|
|
905
914
|
}
|
|
906
915
|
/**
|
|
907
|
-
* Casts a node instance type to
|
|
916
|
+
* 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).
|
|
908
917
|
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
|
|
909
918
|
* but just fool typescript into thinking so.
|
|
910
919
|
*
|
|
@@ -928,14 +937,14 @@ function cast(snapshotOrInstance) {
|
|
|
928
937
|
* ```
|
|
929
938
|
*
|
|
930
939
|
* @param snapshotOrInstance Snapshot or instance
|
|
931
|
-
* @returns The same object
|
|
940
|
+
* @returns The same object cast as an input (creation) snapshot
|
|
932
941
|
*/
|
|
933
942
|
function castToSnapshot(snapshotOrInstance) {
|
|
934
943
|
return snapshotOrInstance;
|
|
935
944
|
}
|
|
936
945
|
/**
|
|
937
|
-
* Casts a node instance type to a reference snapshot type so it can be assigned to a
|
|
938
|
-
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a
|
|
946
|
+
* 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).
|
|
947
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
|
|
939
948
|
* but just fool typescript into thinking so.
|
|
940
949
|
*
|
|
941
950
|
* Example:
|
|
@@ -959,7 +968,7 @@ function castToSnapshot(snapshotOrInstance) {
|
|
|
959
968
|
* ```
|
|
960
969
|
*
|
|
961
970
|
* @param instance Instance
|
|
962
|
-
* @returns The same object
|
|
971
|
+
* @returns The same object cast as a reference snapshot (string or number)
|
|
963
972
|
*/
|
|
964
973
|
function castToReferenceSnapshot(instance) {
|
|
965
974
|
return instance;
|
|
@@ -1590,9 +1599,10 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1590
1599
|
enumerable: false,
|
|
1591
1600
|
configurable: true,
|
|
1592
1601
|
writable: true,
|
|
1593
|
-
value: function () {
|
|
1602
|
+
value: function (fireHooks) {
|
|
1603
|
+
if (fireHooks === void 0) { fireHooks = true; }
|
|
1594
1604
|
if (this._observableInstanceState === 0 /* UNINITIALIZED */) {
|
|
1595
|
-
this.createObservableInstance();
|
|
1605
|
+
this.createObservableInstance(fireHooks);
|
|
1596
1606
|
}
|
|
1597
1607
|
}
|
|
1598
1608
|
});
|
|
@@ -1600,8 +1610,9 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1600
1610
|
enumerable: false,
|
|
1601
1611
|
configurable: true,
|
|
1602
1612
|
writable: true,
|
|
1603
|
-
value: function () {
|
|
1604
|
-
var e_1, _a;
|
|
1613
|
+
value: function (fireHooks) {
|
|
1614
|
+
var e_1, _a, e_2, _b;
|
|
1615
|
+
if (fireHooks === void 0) { fireHooks = true; }
|
|
1605
1616
|
if (devMode()) {
|
|
1606
1617
|
if (this.state !== NodeLifeCycle.INITIALIZING) {
|
|
1607
1618
|
// istanbul ignore next
|
|
@@ -1626,7 +1637,8 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1626
1637
|
// initialize the uninitialized parent chain from parent to child
|
|
1627
1638
|
for (var parentChain_1 = __values(parentChain), parentChain_1_1 = parentChain_1.next(); !parentChain_1_1.done; parentChain_1_1 = parentChain_1.next()) {
|
|
1628
1639
|
var p = parentChain_1_1.value;
|
|
1629
|
-
|
|
1640
|
+
// delay firing hooks until after all parents have been created
|
|
1641
|
+
p.createObservableInstanceIfNeeded(false);
|
|
1630
1642
|
}
|
|
1631
1643
|
}
|
|
1632
1644
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -1657,8 +1669,28 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1657
1669
|
this._addSnapshotReaction();
|
|
1658
1670
|
this._childNodes = EMPTY_OBJECT;
|
|
1659
1671
|
this.state = NodeLifeCycle.CREATED;
|
|
1660
|
-
|
|
1661
|
-
|
|
1672
|
+
if (fireHooks) {
|
|
1673
|
+
this.fireHook(Hook.afterCreate);
|
|
1674
|
+
// Note that the parent might not be finalized at this point
|
|
1675
|
+
// so afterAttach won't be called until later in that case
|
|
1676
|
+
this.finalizeCreation();
|
|
1677
|
+
try {
|
|
1678
|
+
// fire the hooks of the parents that we created
|
|
1679
|
+
for (var _c = __values(parentChain.reverse()), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
1680
|
+
var p = _d.value;
|
|
1681
|
+
p.fireHook(Hook.afterCreate);
|
|
1682
|
+
// This will call afterAttach on the child if necessary
|
|
1683
|
+
p.finalizeCreation();
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1687
|
+
finally {
|
|
1688
|
+
try {
|
|
1689
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
1690
|
+
}
|
|
1691
|
+
finally { if (e_2) throw e_2.error; }
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1662
1694
|
}
|
|
1663
1695
|
});
|
|
1664
1696
|
Object.defineProperty(ObjectNode.prototype, "root", {
|
|
@@ -1957,19 +1989,19 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1957
1989
|
value: function () {
|
|
1958
1990
|
var _this = this;
|
|
1959
1991
|
this.baseFinalizeCreation(function () {
|
|
1960
|
-
var
|
|
1992
|
+
var e_3, _a;
|
|
1961
1993
|
try {
|
|
1962
1994
|
for (var _b = __values(_this.getChildren()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1963
1995
|
var child = _c.value;
|
|
1964
1996
|
child.finalizeCreation();
|
|
1965
1997
|
}
|
|
1966
1998
|
}
|
|
1967
|
-
catch (
|
|
1999
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1968
2000
|
finally {
|
|
1969
2001
|
try {
|
|
1970
2002
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1971
2003
|
}
|
|
1972
|
-
finally { if (
|
|
2004
|
+
finally { if (e_3) throw e_3.error; }
|
|
1973
2005
|
}
|
|
1974
2006
|
_this.fireInternalHook(Hook.afterCreationFinalization);
|
|
1975
2007
|
});
|
|
@@ -2293,6 +2325,9 @@ var TypeFlags;
|
|
|
2293
2325
|
TypeFlags[TypeFlags["Integer"] = 131072] = "Integer";
|
|
2294
2326
|
TypeFlags[TypeFlags["Custom"] = 262144] = "Custom";
|
|
2295
2327
|
TypeFlags[TypeFlags["SnapshotProcessor"] = 524288] = "SnapshotProcessor";
|
|
2328
|
+
TypeFlags[TypeFlags["Lazy"] = 1048576] = "Lazy";
|
|
2329
|
+
TypeFlags[TypeFlags["Finite"] = 2097152] = "Finite";
|
|
2330
|
+
TypeFlags[TypeFlags["Float"] = 4194304] = "Float";
|
|
2296
2331
|
})(TypeFlags || (TypeFlags = {}));
|
|
2297
2332
|
/**
|
|
2298
2333
|
* @internal
|
|
@@ -3141,6 +3176,7 @@ function createActionInvoker(target, name, fn) {
|
|
|
3141
3176
|
}, fn);
|
|
3142
3177
|
};
|
|
3143
3178
|
res._isMSTAction = true;
|
|
3179
|
+
res._isFlowAction = fn._isFlowAction;
|
|
3144
3180
|
return res;
|
|
3145
3181
|
}
|
|
3146
3182
|
/**
|
|
@@ -3556,17 +3592,25 @@ var IdentifierCache = /** @class */ (function () {
|
|
|
3556
3592
|
enumerable: false,
|
|
3557
3593
|
configurable: true,
|
|
3558
3594
|
writable: true,
|
|
3559
|
-
value: function (
|
|
3595
|
+
value: function (splitNode) {
|
|
3560
3596
|
var _this = this;
|
|
3561
|
-
var
|
|
3562
|
-
|
|
3597
|
+
var newCache = new IdentifierCache();
|
|
3598
|
+
// The slash is added here so we only match children of the splitNode. In version 5.1.8 and
|
|
3599
|
+
// earlier there was no trailing slash, so non children that started with the same path string
|
|
3600
|
+
// were being matched incorrectly.
|
|
3601
|
+
var basePath = splitNode.path + "/";
|
|
3563
3602
|
mobx.entries(this.cache).forEach(function (_a) {
|
|
3564
3603
|
var _b = __read(_a, 2), id = _b[0], nodes = _b[1];
|
|
3565
3604
|
var modified = false;
|
|
3566
3605
|
for (var i = nodes.length - 1; i >= 0; i--) {
|
|
3567
|
-
|
|
3568
|
-
|
|
3606
|
+
var node = nodes[i];
|
|
3607
|
+
if (node === splitNode || node.path.indexOf(basePath) === 0) {
|
|
3608
|
+
newCache.addNodeToCache(node, false); // no need to update lastUpdated since it is a whole new cache
|
|
3569
3609
|
nodes.splice(i, 1);
|
|
3610
|
+
// remove empty sets from cache
|
|
3611
|
+
if (!nodes.length) {
|
|
3612
|
+
_this.cache.delete(id);
|
|
3613
|
+
}
|
|
3570
3614
|
modified = true;
|
|
3571
3615
|
}
|
|
3572
3616
|
}
|
|
@@ -3574,7 +3618,7 @@ var IdentifierCache = /** @class */ (function () {
|
|
|
3574
3618
|
_this.updateLastCacheModificationPerId(id);
|
|
3575
3619
|
}
|
|
3576
3620
|
});
|
|
3577
|
-
return
|
|
3621
|
+
return newCache;
|
|
3578
3622
|
}
|
|
3579
3623
|
});
|
|
3580
3624
|
Object.defineProperty(IdentifierCache.prototype, "has", {
|
|
@@ -3858,15 +3902,24 @@ function identity(_) {
|
|
|
3858
3902
|
return _;
|
|
3859
3903
|
}
|
|
3860
3904
|
/**
|
|
3861
|
-
* pollyfill (for IE) suggested in MDN:
|
|
3862
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
|
|
3863
3905
|
* @internal
|
|
3864
3906
|
* @hidden
|
|
3865
3907
|
*/
|
|
3866
|
-
var isInteger = Number.isInteger
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3908
|
+
var isInteger = Number.isInteger;
|
|
3909
|
+
/**
|
|
3910
|
+
* @internal
|
|
3911
|
+
* @hidden
|
|
3912
|
+
*/
|
|
3913
|
+
function isFloat(val) {
|
|
3914
|
+
return Number(val) === val && val % 1 !== 0;
|
|
3915
|
+
}
|
|
3916
|
+
/**
|
|
3917
|
+
* @internal
|
|
3918
|
+
* @hidden
|
|
3919
|
+
*/
|
|
3920
|
+
function isFinite(val) {
|
|
3921
|
+
return Number.isFinite(val);
|
|
3922
|
+
}
|
|
3870
3923
|
/**
|
|
3871
3924
|
* @internal
|
|
3872
3925
|
* @hidden
|
|
@@ -4471,6 +4524,7 @@ function createFlowSpawner(name, generator) {
|
|
|
4471
4524
|
}
|
|
4472
4525
|
});
|
|
4473
4526
|
};
|
|
4527
|
+
spawner._isFlowAction = true;
|
|
4474
4528
|
return spawner;
|
|
4475
4529
|
}
|
|
4476
4530
|
|
|
@@ -4884,8 +4938,8 @@ var MapIdentifierMode;
|
|
|
4884
4938
|
})(MapIdentifierMode || (MapIdentifierMode = {}));
|
|
4885
4939
|
var MSTMap = /** @class */ (function (_super) {
|
|
4886
4940
|
__extends(MSTMap, _super);
|
|
4887
|
-
function MSTMap(initialData) {
|
|
4888
|
-
return _super.call(this, initialData, mobx.observable.ref.enhancer) || this;
|
|
4941
|
+
function MSTMap(initialData, name) {
|
|
4942
|
+
return _super.call(this, initialData, mobx.observable.ref.enhancer, name) || this;
|
|
4889
4943
|
}
|
|
4890
4944
|
Object.defineProperty(MSTMap.prototype, "get", {
|
|
4891
4945
|
enumerable: false,
|
|
@@ -5073,7 +5127,7 @@ var MapType = /** @class */ (function (_super) {
|
|
|
5073
5127
|
configurable: true,
|
|
5074
5128
|
writable: true,
|
|
5075
5129
|
value: function (childNodes) {
|
|
5076
|
-
return new MSTMap(childNodes);
|
|
5130
|
+
return new MSTMap(childNodes, this.name);
|
|
5077
5131
|
}
|
|
5078
5132
|
});
|
|
5079
5133
|
Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
|
|
@@ -5100,7 +5154,7 @@ var MapType = /** @class */ (function (_super) {
|
|
|
5100
5154
|
configurable: true,
|
|
5101
5155
|
writable: true,
|
|
5102
5156
|
value: function () {
|
|
5103
|
-
return
|
|
5157
|
+
return this.name;
|
|
5104
5158
|
}
|
|
5105
5159
|
});
|
|
5106
5160
|
Object.defineProperty(MapType.prototype, "getChildren", {
|
|
@@ -5336,7 +5390,7 @@ MapType.prototype.applySnapshot = mobx.action(MapType.prototype.applySnapshot);
|
|
|
5336
5390
|
* @returns
|
|
5337
5391
|
*/
|
|
5338
5392
|
function map(subtype) {
|
|
5339
|
-
return new MapType("
|
|
5393
|
+
return new MapType("Map<string, " + subtype.name + ">", subtype);
|
|
5340
5394
|
}
|
|
5341
5395
|
/**
|
|
5342
5396
|
* Returns if a given value represents a map type.
|
|
@@ -5415,7 +5469,8 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5415
5469
|
configurable: true,
|
|
5416
5470
|
writable: true,
|
|
5417
5471
|
value: function (childNodes) {
|
|
5418
|
-
|
|
5472
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
5473
|
+
return mobx.observable.array(convertChildNodesToArray(childNodes), options);
|
|
5419
5474
|
}
|
|
5420
5475
|
});
|
|
5421
5476
|
Object.defineProperty(ArrayType.prototype, "finalizeNewInstance", {
|
|
@@ -5442,7 +5497,7 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5442
5497
|
configurable: true,
|
|
5443
5498
|
writable: true,
|
|
5444
5499
|
value: function () {
|
|
5445
|
-
return this.
|
|
5500
|
+
return this.name;
|
|
5446
5501
|
}
|
|
5447
5502
|
});
|
|
5448
5503
|
Object.defineProperty(ArrayType.prototype, "getChildren", {
|
|
@@ -5798,8 +5853,18 @@ var defaultObjectOptions = {
|
|
|
5798
5853
|
initializers: EMPTY_ARRAY
|
|
5799
5854
|
};
|
|
5800
5855
|
function toPropertiesObject(declaredProps) {
|
|
5856
|
+
var keysList = Object.keys(declaredProps);
|
|
5857
|
+
var alreadySeenKeys = {};
|
|
5858
|
+
keysList.forEach(function (key) {
|
|
5859
|
+
if (alreadySeenKeys[key]) {
|
|
5860
|
+
throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
|
|
5861
|
+
}
|
|
5862
|
+
else {
|
|
5863
|
+
alreadySeenKeys[key] = true;
|
|
5864
|
+
}
|
|
5865
|
+
});
|
|
5801
5866
|
// loop through properties and ensures that all items are types
|
|
5802
|
-
return
|
|
5867
|
+
return keysList.reduce(function (props, key) {
|
|
5803
5868
|
var _a, _b, _c;
|
|
5804
5869
|
// warn if user intended a HOOK
|
|
5805
5870
|
if (key in Hook)
|
|
@@ -6019,6 +6084,7 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6019
6084
|
// while still allowing the middlewares to register them
|
|
6020
6085
|
var middlewares = action2.$mst_middleware; // make sure middlewares are not lost
|
|
6021
6086
|
var boundAction = action2.bind(actions);
|
|
6087
|
+
boundAction._isFlowAction = action2._isFlowAction || false;
|
|
6022
6088
|
boundAction.$mst_middleware = middlewares;
|
|
6023
6089
|
var actionInvoker = createActionInvoker(self, name, boundAction);
|
|
6024
6090
|
actions[name] = actionInvoker;
|
|
@@ -6144,7 +6210,8 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6144
6210
|
configurable: true,
|
|
6145
6211
|
writable: true,
|
|
6146
6212
|
value: function (childNodes) {
|
|
6147
|
-
|
|
6213
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
6214
|
+
return mobx.observable.object(childNodes, EMPTY_OBJECT, options);
|
|
6148
6215
|
}
|
|
6149
6216
|
});
|
|
6150
6217
|
Object.defineProperty(ModelType.prototype, "finalizeNewInstance", {
|
|
@@ -6220,10 +6287,11 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6220
6287
|
configurable: true,
|
|
6221
6288
|
writable: true,
|
|
6222
6289
|
value: function (node, key) {
|
|
6290
|
+
var _a;
|
|
6223
6291
|
if (!(key in this.properties))
|
|
6224
6292
|
throw fail$1("Not a value property: " + key);
|
|
6225
6293
|
var adm = mobx._getAdministration(node.storedValue, key);
|
|
6226
|
-
var childNode = adm.raw();
|
|
6294
|
+
var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
|
|
6227
6295
|
if (!childNode)
|
|
6228
6296
|
throw fail$1("Node not available for property " + key);
|
|
6229
6297
|
return childNode;
|
|
@@ -6238,7 +6306,14 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6238
6306
|
if (applyPostProcess === void 0) { applyPostProcess = true; }
|
|
6239
6307
|
var res = {};
|
|
6240
6308
|
this.forAllProps(function (name, type) {
|
|
6241
|
-
|
|
6309
|
+
try {
|
|
6310
|
+
// TODO: FIXME, make sure the observable ref is used!
|
|
6311
|
+
var atom = mobx.getAtom(node.storedValue, name);
|
|
6312
|
+
atom.reportObserved();
|
|
6313
|
+
}
|
|
6314
|
+
catch (e) {
|
|
6315
|
+
throw fail$1(name + " property is declared twice");
|
|
6316
|
+
}
|
|
6242
6317
|
res[name] = _this.getChildNode(node, name).snapshot;
|
|
6243
6318
|
});
|
|
6244
6319
|
if (applyPostProcess) {
|
|
@@ -6378,6 +6453,9 @@ function model() {
|
|
|
6378
6453
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
6379
6454
|
args[_i] = arguments[_i];
|
|
6380
6455
|
}
|
|
6456
|
+
if (devMode() && typeof args[0] !== "string" && args[1]) {
|
|
6457
|
+
throw fail$1("Model creation failed. First argument must be a string when two arguments are provided");
|
|
6458
|
+
}
|
|
6381
6459
|
var name = typeof args[0] === "string" ? args.shift() : "AnonymousModel";
|
|
6382
6460
|
var properties = args.shift() || {};
|
|
6383
6461
|
return new ModelType({ name: name, properties: properties });
|
|
@@ -6531,7 +6609,6 @@ var string = new CoreType("string", TypeFlags.String, function (v) { return type
|
|
|
6531
6609
|
var number = new CoreType("number", TypeFlags.Number, function (v) { return typeof v === "number"; });
|
|
6532
6610
|
/**
|
|
6533
6611
|
* `types.integer` - Creates a type that can only contain an integer value.
|
|
6534
|
-
* This type is used for integer values by default
|
|
6535
6612
|
*
|
|
6536
6613
|
* Example:
|
|
6537
6614
|
* ```ts
|
|
@@ -6543,6 +6620,32 @@ var number = new CoreType("number", TypeFlags.Number, function (v) { return type
|
|
|
6543
6620
|
*/
|
|
6544
6621
|
// tslint:disable-next-line:variable-name
|
|
6545
6622
|
var integer = new CoreType("integer", TypeFlags.Integer, function (v) { return isInteger(v); });
|
|
6623
|
+
/**
|
|
6624
|
+
* `types.float` - Creates a type that can only contain an float value.
|
|
6625
|
+
*
|
|
6626
|
+
* Example:
|
|
6627
|
+
* ```ts
|
|
6628
|
+
* const Size = types.model({
|
|
6629
|
+
* width: types.float,
|
|
6630
|
+
* height: 10
|
|
6631
|
+
* })
|
|
6632
|
+
* ```
|
|
6633
|
+
*/
|
|
6634
|
+
// tslint:disable-next-line:variable-name
|
|
6635
|
+
var float = new CoreType("float", TypeFlags.Float, function (v) { return isFloat(v); });
|
|
6636
|
+
/**
|
|
6637
|
+
* `types.finite` - Creates a type that can only contain an finite value.
|
|
6638
|
+
*
|
|
6639
|
+
* Example:
|
|
6640
|
+
* ```ts
|
|
6641
|
+
* const Size = types.model({
|
|
6642
|
+
* width: types.finite,
|
|
6643
|
+
* height: 10
|
|
6644
|
+
* })
|
|
6645
|
+
* ```
|
|
6646
|
+
*/
|
|
6647
|
+
// tslint:disable-next-line:variable-name
|
|
6648
|
+
var finite = new CoreType("finite", TypeFlags.Finite, function (v) { return isFinite(v); });
|
|
6546
6649
|
/**
|
|
6547
6650
|
* `types.boolean` - Creates a type that can only contain a boolean value.
|
|
6548
6651
|
* This type is used for boolean values by default
|
|
@@ -7392,6 +7495,120 @@ function isLateType(type) {
|
|
|
7392
7495
|
return isType(type) && (type.flags & TypeFlags.Late) > 0;
|
|
7393
7496
|
}
|
|
7394
7497
|
|
|
7498
|
+
function lazy(name, options) {
|
|
7499
|
+
// TODO: fix this unknown casting to be stricter
|
|
7500
|
+
return new Lazy(name, options);
|
|
7501
|
+
}
|
|
7502
|
+
/**
|
|
7503
|
+
* @internal
|
|
7504
|
+
* @hidden
|
|
7505
|
+
*/
|
|
7506
|
+
var Lazy = /** @class */ (function (_super) {
|
|
7507
|
+
__extends(Lazy, _super);
|
|
7508
|
+
function Lazy(name, options) {
|
|
7509
|
+
var _this = _super.call(this, name) || this;
|
|
7510
|
+
Object.defineProperty(_this, "options", {
|
|
7511
|
+
enumerable: true,
|
|
7512
|
+
configurable: true,
|
|
7513
|
+
writable: true,
|
|
7514
|
+
value: options
|
|
7515
|
+
});
|
|
7516
|
+
Object.defineProperty(_this, "flags", {
|
|
7517
|
+
enumerable: true,
|
|
7518
|
+
configurable: true,
|
|
7519
|
+
writable: true,
|
|
7520
|
+
value: TypeFlags.Lazy
|
|
7521
|
+
});
|
|
7522
|
+
Object.defineProperty(_this, "loadedType", {
|
|
7523
|
+
enumerable: true,
|
|
7524
|
+
configurable: true,
|
|
7525
|
+
writable: true,
|
|
7526
|
+
value: null
|
|
7527
|
+
});
|
|
7528
|
+
Object.defineProperty(_this, "pendingNodeList", {
|
|
7529
|
+
enumerable: true,
|
|
7530
|
+
configurable: true,
|
|
7531
|
+
writable: true,
|
|
7532
|
+
value: mobx.observable.array()
|
|
7533
|
+
});
|
|
7534
|
+
mobx.when(function () {
|
|
7535
|
+
return _this.pendingNodeList.length > 0 &&
|
|
7536
|
+
_this.pendingNodeList.some(function (node) {
|
|
7537
|
+
return node.isAlive &&
|
|
7538
|
+
_this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
|
|
7539
|
+
});
|
|
7540
|
+
}, function () {
|
|
7541
|
+
_this.options.loadType().then(mobx.action(function (type) {
|
|
7542
|
+
_this.loadedType = type;
|
|
7543
|
+
_this.pendingNodeList.forEach(function (node) {
|
|
7544
|
+
if (!node.parent)
|
|
7545
|
+
return;
|
|
7546
|
+
if (!_this.loadedType)
|
|
7547
|
+
return;
|
|
7548
|
+
node.parent.applyPatches([
|
|
7549
|
+
{
|
|
7550
|
+
op: "replace",
|
|
7551
|
+
path: "/" + node.subpath,
|
|
7552
|
+
value: node.snapshot
|
|
7553
|
+
}
|
|
7554
|
+
]);
|
|
7555
|
+
});
|
|
7556
|
+
}));
|
|
7557
|
+
});
|
|
7558
|
+
return _this;
|
|
7559
|
+
}
|
|
7560
|
+
Object.defineProperty(Lazy.prototype, "describe", {
|
|
7561
|
+
enumerable: false,
|
|
7562
|
+
configurable: true,
|
|
7563
|
+
writable: true,
|
|
7564
|
+
value: function () {
|
|
7565
|
+
return "<lazy " + this.name + ">";
|
|
7566
|
+
}
|
|
7567
|
+
});
|
|
7568
|
+
Object.defineProperty(Lazy.prototype, "instantiate", {
|
|
7569
|
+
enumerable: false,
|
|
7570
|
+
configurable: true,
|
|
7571
|
+
writable: true,
|
|
7572
|
+
value: function (parent, subpath, environment, value) {
|
|
7573
|
+
var _this = this;
|
|
7574
|
+
if (this.loadedType) {
|
|
7575
|
+
return this.loadedType.instantiate(parent, subpath, environment, value);
|
|
7576
|
+
}
|
|
7577
|
+
var node = createScalarNode(this, parent, subpath, environment, deepFreeze(value));
|
|
7578
|
+
this.pendingNodeList.push(node);
|
|
7579
|
+
mobx.when(function () { return !node.isAlive; }, function () { return _this.pendingNodeList.splice(_this.pendingNodeList.indexOf(node), 1); });
|
|
7580
|
+
return node;
|
|
7581
|
+
}
|
|
7582
|
+
});
|
|
7583
|
+
Object.defineProperty(Lazy.prototype, "isValidSnapshot", {
|
|
7584
|
+
enumerable: false,
|
|
7585
|
+
configurable: true,
|
|
7586
|
+
writable: true,
|
|
7587
|
+
value: function (value, context) {
|
|
7588
|
+
if (this.loadedType) {
|
|
7589
|
+
return this.loadedType.validate(value, context);
|
|
7590
|
+
}
|
|
7591
|
+
if (!isSerializable(value)) {
|
|
7592
|
+
return typeCheckFailure(context, value, "Value is not serializable and cannot be lazy");
|
|
7593
|
+
}
|
|
7594
|
+
return typeCheckSuccess();
|
|
7595
|
+
}
|
|
7596
|
+
});
|
|
7597
|
+
Object.defineProperty(Lazy.prototype, "reconcile", {
|
|
7598
|
+
enumerable: false,
|
|
7599
|
+
configurable: true,
|
|
7600
|
+
writable: true,
|
|
7601
|
+
value: function (current, value, parent, subpath) {
|
|
7602
|
+
if (this.loadedType) {
|
|
7603
|
+
current.die();
|
|
7604
|
+
return this.loadedType.instantiate(parent, subpath, parent.environment, value);
|
|
7605
|
+
}
|
|
7606
|
+
return _super.prototype.reconcile.call(this, current, value, parent, subpath);
|
|
7607
|
+
}
|
|
7608
|
+
});
|
|
7609
|
+
return Lazy;
|
|
7610
|
+
}(SimpleType));
|
|
7611
|
+
|
|
7395
7612
|
/**
|
|
7396
7613
|
* @internal
|
|
7397
7614
|
* @hidden
|
|
@@ -8330,6 +8547,8 @@ var types = {
|
|
|
8330
8547
|
boolean: boolean,
|
|
8331
8548
|
number: number,
|
|
8332
8549
|
integer: integer,
|
|
8550
|
+
float: float,
|
|
8551
|
+
finite: finite,
|
|
8333
8552
|
Date: DatePrimitive,
|
|
8334
8553
|
map: map,
|
|
8335
8554
|
array: array,
|
|
@@ -8337,6 +8556,7 @@ var types = {
|
|
|
8337
8556
|
identifier: identifier,
|
|
8338
8557
|
identifierNumber: identifierNumber,
|
|
8339
8558
|
late: late,
|
|
8559
|
+
lazy: lazy,
|
|
8340
8560
|
undefined: undefinedType,
|
|
8341
8561
|
null: nullType,
|
|
8342
8562
|
snapshotProcessor: snapshotProcessor
|