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
|
@@ -200,7 +200,7 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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
|
{
|
|
1606
1617
|
if (this.state !== NodeLifeCycle.INITIALIZING) {
|
|
1607
1618
|
// istanbul ignore next
|
|
@@ -1626,7 +1637,8 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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
|
|
@@ -4449,6 +4502,7 @@
|
|
|
4449
4502
|
}
|
|
4450
4503
|
});
|
|
4451
4504
|
};
|
|
4505
|
+
spawner._isFlowAction = true;
|
|
4452
4506
|
return spawner;
|
|
4453
4507
|
}
|
|
4454
4508
|
|
|
@@ -4862,8 +4916,8 @@
|
|
|
4862
4916
|
})(MapIdentifierMode || (MapIdentifierMode = {}));
|
|
4863
4917
|
var MSTMap = /** @class */ (function (_super) {
|
|
4864
4918
|
__extends(MSTMap, _super);
|
|
4865
|
-
function MSTMap(initialData) {
|
|
4866
|
-
return _super.call(this, initialData, mobx.observable.ref.enhancer) || this;
|
|
4919
|
+
function MSTMap(initialData, name) {
|
|
4920
|
+
return _super.call(this, initialData, mobx.observable.ref.enhancer, name) || this;
|
|
4867
4921
|
}
|
|
4868
4922
|
Object.defineProperty(MSTMap.prototype, "get", {
|
|
4869
4923
|
enumerable: false,
|
|
@@ -5051,7 +5105,7 @@
|
|
|
5051
5105
|
configurable: true,
|
|
5052
5106
|
writable: true,
|
|
5053
5107
|
value: function (childNodes) {
|
|
5054
|
-
return new MSTMap(childNodes);
|
|
5108
|
+
return new MSTMap(childNodes, this.name);
|
|
5055
5109
|
}
|
|
5056
5110
|
});
|
|
5057
5111
|
Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
|
|
@@ -5078,7 +5132,7 @@
|
|
|
5078
5132
|
configurable: true,
|
|
5079
5133
|
writable: true,
|
|
5080
5134
|
value: function () {
|
|
5081
|
-
return
|
|
5135
|
+
return this.name;
|
|
5082
5136
|
}
|
|
5083
5137
|
});
|
|
5084
5138
|
Object.defineProperty(MapType.prototype, "getChildren", {
|
|
@@ -5314,7 +5368,7 @@
|
|
|
5314
5368
|
* @returns
|
|
5315
5369
|
*/
|
|
5316
5370
|
function map(subtype) {
|
|
5317
|
-
return new MapType("
|
|
5371
|
+
return new MapType("Map<string, " + subtype.name + ">", subtype);
|
|
5318
5372
|
}
|
|
5319
5373
|
/**
|
|
5320
5374
|
* Returns if a given value represents a map type.
|
|
@@ -5393,7 +5447,8 @@
|
|
|
5393
5447
|
configurable: true,
|
|
5394
5448
|
writable: true,
|
|
5395
5449
|
value: function (childNodes) {
|
|
5396
|
-
|
|
5450
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
5451
|
+
return mobx.observable.array(convertChildNodesToArray(childNodes), options);
|
|
5397
5452
|
}
|
|
5398
5453
|
});
|
|
5399
5454
|
Object.defineProperty(ArrayType.prototype, "finalizeNewInstance", {
|
|
@@ -5420,7 +5475,7 @@
|
|
|
5420
5475
|
configurable: true,
|
|
5421
5476
|
writable: true,
|
|
5422
5477
|
value: function () {
|
|
5423
|
-
return this.
|
|
5478
|
+
return this.name;
|
|
5424
5479
|
}
|
|
5425
5480
|
});
|
|
5426
5481
|
Object.defineProperty(ArrayType.prototype, "getChildren", {
|
|
@@ -5776,8 +5831,18 @@
|
|
|
5776
5831
|
initializers: EMPTY_ARRAY
|
|
5777
5832
|
};
|
|
5778
5833
|
function toPropertiesObject(declaredProps) {
|
|
5834
|
+
var keysList = Object.keys(declaredProps);
|
|
5835
|
+
var alreadySeenKeys = {};
|
|
5836
|
+
keysList.forEach(function (key) {
|
|
5837
|
+
if (alreadySeenKeys[key]) {
|
|
5838
|
+
throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
|
|
5839
|
+
}
|
|
5840
|
+
else {
|
|
5841
|
+
alreadySeenKeys[key] = true;
|
|
5842
|
+
}
|
|
5843
|
+
});
|
|
5779
5844
|
// loop through properties and ensures that all items are types
|
|
5780
|
-
return
|
|
5845
|
+
return keysList.reduce(function (props, key) {
|
|
5781
5846
|
var _a, _b, _c;
|
|
5782
5847
|
// warn if user intended a HOOK
|
|
5783
5848
|
if (key in Hook)
|
|
@@ -5997,6 +6062,7 @@
|
|
|
5997
6062
|
// while still allowing the middlewares to register them
|
|
5998
6063
|
var middlewares = action2.$mst_middleware; // make sure middlewares are not lost
|
|
5999
6064
|
var boundAction = action2.bind(actions);
|
|
6065
|
+
boundAction._isFlowAction = action2._isFlowAction || false;
|
|
6000
6066
|
boundAction.$mst_middleware = middlewares;
|
|
6001
6067
|
var actionInvoker = createActionInvoker(self, name, boundAction);
|
|
6002
6068
|
actions[name] = actionInvoker;
|
|
@@ -6122,7 +6188,8 @@
|
|
|
6122
6188
|
configurable: true,
|
|
6123
6189
|
writable: true,
|
|
6124
6190
|
value: function (childNodes) {
|
|
6125
|
-
|
|
6191
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
6192
|
+
return mobx.observable.object(childNodes, EMPTY_OBJECT, options);
|
|
6126
6193
|
}
|
|
6127
6194
|
});
|
|
6128
6195
|
Object.defineProperty(ModelType.prototype, "finalizeNewInstance", {
|
|
@@ -6198,10 +6265,11 @@
|
|
|
6198
6265
|
configurable: true,
|
|
6199
6266
|
writable: true,
|
|
6200
6267
|
value: function (node, key) {
|
|
6268
|
+
var _a;
|
|
6201
6269
|
if (!(key in this.properties))
|
|
6202
6270
|
throw fail$1("Not a value property: " + key);
|
|
6203
6271
|
var adm = mobx._getAdministration(node.storedValue, key);
|
|
6204
|
-
var childNode = adm.raw();
|
|
6272
|
+
var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
|
|
6205
6273
|
if (!childNode)
|
|
6206
6274
|
throw fail$1("Node not available for property " + key);
|
|
6207
6275
|
return childNode;
|
|
@@ -6216,7 +6284,14 @@
|
|
|
6216
6284
|
if (applyPostProcess === void 0) { applyPostProcess = true; }
|
|
6217
6285
|
var res = {};
|
|
6218
6286
|
this.forAllProps(function (name, type) {
|
|
6219
|
-
|
|
6287
|
+
try {
|
|
6288
|
+
// TODO: FIXME, make sure the observable ref is used!
|
|
6289
|
+
var atom = mobx.getAtom(node.storedValue, name);
|
|
6290
|
+
atom.reportObserved();
|
|
6291
|
+
}
|
|
6292
|
+
catch (e) {
|
|
6293
|
+
throw fail$1(name + " property is declared twice");
|
|
6294
|
+
}
|
|
6220
6295
|
res[name] = _this.getChildNode(node, name).snapshot;
|
|
6221
6296
|
});
|
|
6222
6297
|
if (applyPostProcess) {
|
|
@@ -6356,6 +6431,9 @@
|
|
|
6356
6431
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
6357
6432
|
args[_i] = arguments[_i];
|
|
6358
6433
|
}
|
|
6434
|
+
if (typeof args[0] !== "string" && args[1]) {
|
|
6435
|
+
throw fail$1("Model creation failed. First argument must be a string when two arguments are provided");
|
|
6436
|
+
}
|
|
6359
6437
|
var name = typeof args[0] === "string" ? args.shift() : "AnonymousModel";
|
|
6360
6438
|
var properties = args.shift() || {};
|
|
6361
6439
|
return new ModelType({ name: name, properties: properties });
|
|
@@ -6509,7 +6587,6 @@
|
|
|
6509
6587
|
var number = new CoreType("number", TypeFlags.Number, function (v) { return typeof v === "number"; });
|
|
6510
6588
|
/**
|
|
6511
6589
|
* `types.integer` - Creates a type that can only contain an integer value.
|
|
6512
|
-
* This type is used for integer values by default
|
|
6513
6590
|
*
|
|
6514
6591
|
* Example:
|
|
6515
6592
|
* ```ts
|
|
@@ -6521,6 +6598,32 @@
|
|
|
6521
6598
|
*/
|
|
6522
6599
|
// tslint:disable-next-line:variable-name
|
|
6523
6600
|
var integer = new CoreType("integer", TypeFlags.Integer, function (v) { return isInteger(v); });
|
|
6601
|
+
/**
|
|
6602
|
+
* `types.float` - Creates a type that can only contain an float value.
|
|
6603
|
+
*
|
|
6604
|
+
* Example:
|
|
6605
|
+
* ```ts
|
|
6606
|
+
* const Size = types.model({
|
|
6607
|
+
* width: types.float,
|
|
6608
|
+
* height: 10
|
|
6609
|
+
* })
|
|
6610
|
+
* ```
|
|
6611
|
+
*/
|
|
6612
|
+
// tslint:disable-next-line:variable-name
|
|
6613
|
+
var float = new CoreType("float", TypeFlags.Float, function (v) { return isFloat(v); });
|
|
6614
|
+
/**
|
|
6615
|
+
* `types.finite` - Creates a type that can only contain an finite value.
|
|
6616
|
+
*
|
|
6617
|
+
* Example:
|
|
6618
|
+
* ```ts
|
|
6619
|
+
* const Size = types.model({
|
|
6620
|
+
* width: types.finite,
|
|
6621
|
+
* height: 10
|
|
6622
|
+
* })
|
|
6623
|
+
* ```
|
|
6624
|
+
*/
|
|
6625
|
+
// tslint:disable-next-line:variable-name
|
|
6626
|
+
var finite = new CoreType("finite", TypeFlags.Finite, function (v) { return isFinite(v); });
|
|
6524
6627
|
/**
|
|
6525
6628
|
* `types.boolean` - Creates a type that can only contain a boolean value.
|
|
6526
6629
|
* This type is used for boolean values by default
|
|
@@ -7370,6 +7473,120 @@
|
|
|
7370
7473
|
return isType(type) && (type.flags & TypeFlags.Late) > 0;
|
|
7371
7474
|
}
|
|
7372
7475
|
|
|
7476
|
+
function lazy(name, options) {
|
|
7477
|
+
// TODO: fix this unknown casting to be stricter
|
|
7478
|
+
return new Lazy(name, options);
|
|
7479
|
+
}
|
|
7480
|
+
/**
|
|
7481
|
+
* @internal
|
|
7482
|
+
* @hidden
|
|
7483
|
+
*/
|
|
7484
|
+
var Lazy = /** @class */ (function (_super) {
|
|
7485
|
+
__extends(Lazy, _super);
|
|
7486
|
+
function Lazy(name, options) {
|
|
7487
|
+
var _this = _super.call(this, name) || this;
|
|
7488
|
+
Object.defineProperty(_this, "options", {
|
|
7489
|
+
enumerable: true,
|
|
7490
|
+
configurable: true,
|
|
7491
|
+
writable: true,
|
|
7492
|
+
value: options
|
|
7493
|
+
});
|
|
7494
|
+
Object.defineProperty(_this, "flags", {
|
|
7495
|
+
enumerable: true,
|
|
7496
|
+
configurable: true,
|
|
7497
|
+
writable: true,
|
|
7498
|
+
value: TypeFlags.Lazy
|
|
7499
|
+
});
|
|
7500
|
+
Object.defineProperty(_this, "loadedType", {
|
|
7501
|
+
enumerable: true,
|
|
7502
|
+
configurable: true,
|
|
7503
|
+
writable: true,
|
|
7504
|
+
value: null
|
|
7505
|
+
});
|
|
7506
|
+
Object.defineProperty(_this, "pendingNodeList", {
|
|
7507
|
+
enumerable: true,
|
|
7508
|
+
configurable: true,
|
|
7509
|
+
writable: true,
|
|
7510
|
+
value: mobx.observable.array()
|
|
7511
|
+
});
|
|
7512
|
+
mobx.when(function () {
|
|
7513
|
+
return _this.pendingNodeList.length > 0 &&
|
|
7514
|
+
_this.pendingNodeList.some(function (node) {
|
|
7515
|
+
return node.isAlive &&
|
|
7516
|
+
_this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
|
|
7517
|
+
});
|
|
7518
|
+
}, function () {
|
|
7519
|
+
_this.options.loadType().then(mobx.action(function (type) {
|
|
7520
|
+
_this.loadedType = type;
|
|
7521
|
+
_this.pendingNodeList.forEach(function (node) {
|
|
7522
|
+
if (!node.parent)
|
|
7523
|
+
return;
|
|
7524
|
+
if (!_this.loadedType)
|
|
7525
|
+
return;
|
|
7526
|
+
node.parent.applyPatches([
|
|
7527
|
+
{
|
|
7528
|
+
op: "replace",
|
|
7529
|
+
path: "/" + node.subpath,
|
|
7530
|
+
value: node.snapshot
|
|
7531
|
+
}
|
|
7532
|
+
]);
|
|
7533
|
+
});
|
|
7534
|
+
}));
|
|
7535
|
+
});
|
|
7536
|
+
return _this;
|
|
7537
|
+
}
|
|
7538
|
+
Object.defineProperty(Lazy.prototype, "describe", {
|
|
7539
|
+
enumerable: false,
|
|
7540
|
+
configurable: true,
|
|
7541
|
+
writable: true,
|
|
7542
|
+
value: function () {
|
|
7543
|
+
return "<lazy " + this.name + ">";
|
|
7544
|
+
}
|
|
7545
|
+
});
|
|
7546
|
+
Object.defineProperty(Lazy.prototype, "instantiate", {
|
|
7547
|
+
enumerable: false,
|
|
7548
|
+
configurable: true,
|
|
7549
|
+
writable: true,
|
|
7550
|
+
value: function (parent, subpath, environment, value) {
|
|
7551
|
+
var _this = this;
|
|
7552
|
+
if (this.loadedType) {
|
|
7553
|
+
return this.loadedType.instantiate(parent, subpath, environment, value);
|
|
7554
|
+
}
|
|
7555
|
+
var node = createScalarNode(this, parent, subpath, environment, deepFreeze(value));
|
|
7556
|
+
this.pendingNodeList.push(node);
|
|
7557
|
+
mobx.when(function () { return !node.isAlive; }, function () { return _this.pendingNodeList.splice(_this.pendingNodeList.indexOf(node), 1); });
|
|
7558
|
+
return node;
|
|
7559
|
+
}
|
|
7560
|
+
});
|
|
7561
|
+
Object.defineProperty(Lazy.prototype, "isValidSnapshot", {
|
|
7562
|
+
enumerable: false,
|
|
7563
|
+
configurable: true,
|
|
7564
|
+
writable: true,
|
|
7565
|
+
value: function (value, context) {
|
|
7566
|
+
if (this.loadedType) {
|
|
7567
|
+
return this.loadedType.validate(value, context);
|
|
7568
|
+
}
|
|
7569
|
+
if (!isSerializable(value)) {
|
|
7570
|
+
return typeCheckFailure(context, value, "Value is not serializable and cannot be lazy");
|
|
7571
|
+
}
|
|
7572
|
+
return typeCheckSuccess();
|
|
7573
|
+
}
|
|
7574
|
+
});
|
|
7575
|
+
Object.defineProperty(Lazy.prototype, "reconcile", {
|
|
7576
|
+
enumerable: false,
|
|
7577
|
+
configurable: true,
|
|
7578
|
+
writable: true,
|
|
7579
|
+
value: function (current, value, parent, subpath) {
|
|
7580
|
+
if (this.loadedType) {
|
|
7581
|
+
current.die();
|
|
7582
|
+
return this.loadedType.instantiate(parent, subpath, parent.environment, value);
|
|
7583
|
+
}
|
|
7584
|
+
return _super.prototype.reconcile.call(this, current, value, parent, subpath);
|
|
7585
|
+
}
|
|
7586
|
+
});
|
|
7587
|
+
return Lazy;
|
|
7588
|
+
}(SimpleType));
|
|
7589
|
+
|
|
7373
7590
|
/**
|
|
7374
7591
|
* @internal
|
|
7375
7592
|
* @hidden
|
|
@@ -8308,6 +8525,8 @@
|
|
|
8308
8525
|
boolean: boolean,
|
|
8309
8526
|
number: number,
|
|
8310
8527
|
integer: integer,
|
|
8528
|
+
float: float,
|
|
8529
|
+
finite: finite,
|
|
8311
8530
|
Date: DatePrimitive,
|
|
8312
8531
|
map: map,
|
|
8313
8532
|
array: array,
|
|
@@ -8315,6 +8534,7 @@
|
|
|
8315
8534
|
identifier: identifier,
|
|
8316
8535
|
identifierNumber: identifierNumber,
|
|
8317
8536
|
late: late,
|
|
8537
|
+
lazy: lazy,
|
|
8318
8538
|
undefined: undefinedType,
|
|
8319
8539
|
null: nullType,
|
|
8320
8540
|
snapshotProcessor: snapshotProcessor
|