mobx-state-tree 7.0.2 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/mst-operations.d.ts +1 -1
- package/dist/core/type/type.d.ts +3 -1
- package/dist/mobx-state-tree.js +398 -36
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +398 -36
- package/dist/mobx-state-tree.umd.js +398 -36
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/complex-types/model.d.ts +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/primitives.d.ts +15 -1
- package/dist/types/utility-types/identifier.d.ts +18 -4
- package/dist/types/utility-types/snapshotProcessor.d.ts +2 -0
- package/package.json +2 -2
|
@@ -795,7 +795,7 @@
|
|
|
795
795
|
}
|
|
796
796
|
/**
|
|
797
797
|
* Returns the environment of the current state tree, or throws. For more info on environments,
|
|
798
|
-
* see [Dependency injection](
|
|
798
|
+
* see [Dependency injection](/concepts/dependency-injection)
|
|
799
799
|
*
|
|
800
800
|
* Please note that in child nodes access to the root is only possible
|
|
801
801
|
* once the `afterAttach` hook has fired
|
|
@@ -1542,6 +1542,12 @@
|
|
|
1542
1542
|
writable: true,
|
|
1543
1543
|
value: false
|
|
1544
1544
|
});
|
|
1545
|
+
Object.defineProperty(_this, "_endOfActionCallbacks", {
|
|
1546
|
+
enumerable: true,
|
|
1547
|
+
configurable: true,
|
|
1548
|
+
writable: true,
|
|
1549
|
+
value: void 0
|
|
1550
|
+
});
|
|
1545
1551
|
Object.defineProperty(_this, "_observableInstanceState", {
|
|
1546
1552
|
enumerable: true,
|
|
1547
1553
|
configurable: true,
|
|
@@ -1612,8 +1618,8 @@
|
|
|
1612
1618
|
id = childNode.value;
|
|
1613
1619
|
}
|
|
1614
1620
|
}
|
|
1615
|
-
if (typeof id !== "string" && typeof id !== "number") {
|
|
1616
|
-
throw new MstError("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string or a
|
|
1621
|
+
if (typeof id !== "string" && typeof id !== "number" && typeof id !== "bigint") {
|
|
1622
|
+
throw new MstError("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string, a number or a bigint"));
|
|
1617
1623
|
}
|
|
1618
1624
|
// normalize internal identifier to string
|
|
1619
1625
|
_this.identifier = normalizeIdentifier(id);
|
|
@@ -1751,6 +1757,33 @@
|
|
|
1751
1757
|
enumerable: false,
|
|
1752
1758
|
configurable: true
|
|
1753
1759
|
});
|
|
1760
|
+
Object.defineProperty(ObjectNode.prototype, "enqueueEndOfAction", {
|
|
1761
|
+
enumerable: false,
|
|
1762
|
+
configurable: true,
|
|
1763
|
+
writable: true,
|
|
1764
|
+
value: function (callback) {
|
|
1765
|
+
var root = this.root;
|
|
1766
|
+
if (!root._endOfActionCallbacks) {
|
|
1767
|
+
root._endOfActionCallbacks = [];
|
|
1768
|
+
}
|
|
1769
|
+
root._endOfActionCallbacks.push(callback);
|
|
1770
|
+
}
|
|
1771
|
+
});
|
|
1772
|
+
Object.defineProperty(ObjectNode.prototype, "flushEndOfActionCallbacks", {
|
|
1773
|
+
enumerable: false,
|
|
1774
|
+
configurable: true,
|
|
1775
|
+
writable: true,
|
|
1776
|
+
value: function () {
|
|
1777
|
+
var root = this.root;
|
|
1778
|
+
while (root._endOfActionCallbacks && root._endOfActionCallbacks.length > 0) {
|
|
1779
|
+
var callbacks = root._endOfActionCallbacks;
|
|
1780
|
+
root._endOfActionCallbacks = undefined;
|
|
1781
|
+
callbacks.forEach(function (callback) {
|
|
1782
|
+
callback();
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
});
|
|
1754
1787
|
Object.defineProperty(ObjectNode.prototype, "clearParent", {
|
|
1755
1788
|
enumerable: false,
|
|
1756
1789
|
configurable: true,
|
|
@@ -2169,6 +2202,17 @@
|
|
|
2169
2202
|
return this._internalEventsRegister(InternalEvents.Patch, handler);
|
|
2170
2203
|
}
|
|
2171
2204
|
});
|
|
2205
|
+
Object.defineProperty(ObjectNode.prototype, "hasPatchSubscribers", {
|
|
2206
|
+
enumerable: false,
|
|
2207
|
+
configurable: true,
|
|
2208
|
+
writable: true,
|
|
2209
|
+
value: function () {
|
|
2210
|
+
if (this._internalEventsHasSubscribers(InternalEvents.Patch)) {
|
|
2211
|
+
return true;
|
|
2212
|
+
}
|
|
2213
|
+
return this.parent ? this.parent.hasPatchSubscribers() : false;
|
|
2214
|
+
}
|
|
2215
|
+
});
|
|
2172
2216
|
Object.defineProperty(ObjectNode.prototype, "emitPatch", {
|
|
2173
2217
|
enumerable: false,
|
|
2174
2218
|
configurable: true,
|
|
@@ -2381,6 +2425,7 @@
|
|
|
2381
2425
|
TypeFlags[TypeFlags["Lazy"] = 1048576] = "Lazy";
|
|
2382
2426
|
TypeFlags[TypeFlags["Finite"] = 2097152] = "Finite";
|
|
2383
2427
|
TypeFlags[TypeFlags["Float"] = 4194304] = "Float";
|
|
2428
|
+
TypeFlags[TypeFlags["BigInt"] = 8388608] = "BigInt";
|
|
2384
2429
|
})(TypeFlags || (TypeFlags = {}));
|
|
2385
2430
|
/**
|
|
2386
2431
|
* @internal
|
|
@@ -2697,6 +2742,38 @@
|
|
|
2697
2742
|
});
|
|
2698
2743
|
return SimpleType;
|
|
2699
2744
|
}(BaseType));
|
|
2745
|
+
function canApplyDirectSnapshot(childType, childNode, newValue) {
|
|
2746
|
+
var reconciliationType = childNode.getReconciliationType();
|
|
2747
|
+
var expectedReconciliationType = resolveDirectApplyType(childType);
|
|
2748
|
+
// Only types that reconcile as the same transparent-wrapper-adjusted type can safely reuse
|
|
2749
|
+
// the existing child node. Wrappers with their own validation or snapshot semantics
|
|
2750
|
+
// intentionally keep a distinct reconciliation type so they fall back to the validated path.
|
|
2751
|
+
var hasMatchingReconciliationType = reconciliationType === expectedReconciliationType;
|
|
2752
|
+
return (childNode instanceof ObjectNode &&
|
|
2753
|
+
hasMatchingReconciliationType &&
|
|
2754
|
+
reconciliationType instanceof ComplexType &&
|
|
2755
|
+
isMutable(newValue) &&
|
|
2756
|
+
!isStateTreeNode(newValue) &&
|
|
2757
|
+
reconciliationType.isMatchingSnapshotId(childNode, newValue));
|
|
2758
|
+
}
|
|
2759
|
+
function resolveDirectApplyType(type) {
|
|
2760
|
+
var subTypes = type.getSubTypes();
|
|
2761
|
+
if (!subTypes ||
|
|
2762
|
+
Array.isArray(subTypes) ||
|
|
2763
|
+
subTypes === cannotDetermineSubtype ||
|
|
2764
|
+
!canUnwrapDirectApplyType(type)) {
|
|
2765
|
+
return type;
|
|
2766
|
+
}
|
|
2767
|
+
// Only unwrap wrappers that are transparent for direct apply. Wrappers with their own
|
|
2768
|
+
// validation or snapshot processing must keep their wrapper identity here.
|
|
2769
|
+
return resolveDirectApplyType(subTypes);
|
|
2770
|
+
}
|
|
2771
|
+
function canUnwrapDirectApplyType(type) {
|
|
2772
|
+
var transparentWrapperFlags = TypeFlags.Optional | TypeFlags.Late;
|
|
2773
|
+
var nonTransparentWrapperFlags = TypeFlags.Refinement | TypeFlags.SnapshotProcessor | TypeFlags.Union;
|
|
2774
|
+
return ((type.flags & transparentWrapperFlags) > 0 &&
|
|
2775
|
+
(type.flags & nonTransparentWrapperFlags) === 0);
|
|
2776
|
+
}
|
|
2700
2777
|
/**
|
|
2701
2778
|
* Returns if a given value represents a type.
|
|
2702
2779
|
*
|
|
@@ -3188,9 +3265,10 @@
|
|
|
3188
3265
|
var baseIsRunningAction = node._isRunningAction;
|
|
3189
3266
|
node._isRunningAction = true;
|
|
3190
3267
|
var previousContext = currentActionContext;
|
|
3268
|
+
var isRootActionContext = !previousContext;
|
|
3191
3269
|
currentActionContext = context;
|
|
3192
3270
|
try {
|
|
3193
|
-
return runMiddleWares(node, context, fn);
|
|
3271
|
+
return runMiddleWares(node, context, fn, isRootActionContext);
|
|
3194
3272
|
}
|
|
3195
3273
|
finally {
|
|
3196
3274
|
currentActionContext = previousContext;
|
|
@@ -3344,17 +3422,44 @@
|
|
|
3344
3422
|
});
|
|
3345
3423
|
return CollectedMiddlewares;
|
|
3346
3424
|
}());
|
|
3347
|
-
function runMiddleWares(node, baseCall, originalFn) {
|
|
3425
|
+
function runMiddleWares(node, baseCall, originalFn, isRootActionContext) {
|
|
3426
|
+
function runInActionScope(call, fn) {
|
|
3427
|
+
var execute = function () {
|
|
3428
|
+
var result;
|
|
3429
|
+
var error;
|
|
3430
|
+
try {
|
|
3431
|
+
result = fn.apply(null, call.args);
|
|
3432
|
+
}
|
|
3433
|
+
catch (e) {
|
|
3434
|
+
error = e;
|
|
3435
|
+
}
|
|
3436
|
+
if (isRootActionContext || !call.parentActionEvent) {
|
|
3437
|
+
try {
|
|
3438
|
+
node.root.flushEndOfActionCallbacks();
|
|
3439
|
+
}
|
|
3440
|
+
catch (flushError) {
|
|
3441
|
+
if (!error) {
|
|
3442
|
+
error = flushError;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
if (error) {
|
|
3447
|
+
throw error;
|
|
3448
|
+
}
|
|
3449
|
+
return result;
|
|
3450
|
+
};
|
|
3451
|
+
return call.name ? mobx.action(call.name, execute)() : mobx.action(execute)();
|
|
3452
|
+
}
|
|
3348
3453
|
var middlewares = new CollectedMiddlewares(node, originalFn);
|
|
3349
3454
|
// Short circuit
|
|
3350
3455
|
if (middlewares.isEmpty)
|
|
3351
|
-
return
|
|
3456
|
+
return runInActionScope(baseCall, originalFn);
|
|
3352
3457
|
var result = null;
|
|
3353
3458
|
function runNextMiddleware(call) {
|
|
3354
3459
|
var middleware = middlewares.getNextMiddleware();
|
|
3355
3460
|
var handler = middleware && middleware.handler;
|
|
3356
3461
|
if (!handler) {
|
|
3357
|
-
return
|
|
3462
|
+
return runInActionScope(call, originalFn);
|
|
3358
3463
|
}
|
|
3359
3464
|
// skip hooks if asked to
|
|
3360
3465
|
if (!middleware.includeHooks && Hook[call.name]) {
|
|
@@ -3390,6 +3495,9 @@
|
|
|
3390
3495
|
throw new MstError("The next() and abort() callback within the middleware ".concat(handler.name, " for the action: \"").concat(call.name, "\" on the node: ").concat(node2.type.name, " were invoked."));
|
|
3391
3496
|
}
|
|
3392
3497
|
}
|
|
3498
|
+
if (abortInvoked && (isRootActionContext || !call.parentActionEvent)) {
|
|
3499
|
+
return runInActionScope(call, function () { return result; });
|
|
3500
|
+
}
|
|
3393
3501
|
return result;
|
|
3394
3502
|
}
|
|
3395
3503
|
return runNextMiddleware(baseCall);
|
|
@@ -4046,6 +4154,7 @@
|
|
|
4046
4154
|
typeof value === "string" ||
|
|
4047
4155
|
typeof value === "number" ||
|
|
4048
4156
|
typeof value === "boolean" ||
|
|
4157
|
+
typeof value === "bigint" ||
|
|
4049
4158
|
(includeDate && value instanceof Date));
|
|
4050
4159
|
}
|
|
4051
4160
|
/**
|
|
@@ -4908,6 +5017,8 @@
|
|
|
4908
5017
|
/**
|
|
4909
5018
|
* `types.snapshotProcessor` - Runs a pre/post snapshot processor before/after serializing a given type.
|
|
4910
5019
|
*
|
|
5020
|
+
* [See known issue with `applySnapshot` and `preProcessSnapshot`](https://github.com/mobxjs/mobx-state-tree/issues/1317)
|
|
5021
|
+
*
|
|
4911
5022
|
* Example:
|
|
4912
5023
|
* ```ts
|
|
4913
5024
|
* const Todo1 = types.model({ text: types.string })
|
|
@@ -5358,23 +5469,33 @@
|
|
|
5358
5469
|
configurable: true,
|
|
5359
5470
|
writable: true,
|
|
5360
5471
|
value: function (node, snapshot) {
|
|
5361
|
-
|
|
5472
|
+
if (!isPlainObject(snapshot)) {
|
|
5473
|
+
typecheckInternal(this, snapshot);
|
|
5474
|
+
return;
|
|
5475
|
+
}
|
|
5362
5476
|
var target = node.storedValue;
|
|
5363
|
-
var
|
|
5477
|
+
var childType = this.getChildType();
|
|
5364
5478
|
Array.from(target.keys()).forEach(function (key) {
|
|
5365
|
-
|
|
5366
|
-
});
|
|
5367
|
-
if (snapshot) {
|
|
5368
|
-
// Don't use target.replace, as it will throw away all existing items first
|
|
5369
|
-
for (var key in snapshot) {
|
|
5370
|
-
target.set(key, snapshot[key]);
|
|
5371
|
-
currentKeys["" + key] = true;
|
|
5372
|
-
}
|
|
5373
|
-
}
|
|
5374
|
-
Object.keys(currentKeys).forEach(function (key) {
|
|
5375
|
-
if (currentKeys[key] === false)
|
|
5479
|
+
if (!Object.prototype.hasOwnProperty.call(snapshot, key)) {
|
|
5376
5480
|
target.delete(key);
|
|
5481
|
+
return;
|
|
5482
|
+
}
|
|
5483
|
+
var childNode = node.getChildNode(key);
|
|
5484
|
+
var newValue = snapshot[key];
|
|
5485
|
+
if (childNode.snapshot === newValue)
|
|
5486
|
+
return;
|
|
5487
|
+
if (canApplyDirectSnapshot(childType, childNode, newValue)) {
|
|
5488
|
+
childNode.applySnapshot(newValue);
|
|
5489
|
+
}
|
|
5490
|
+
else {
|
|
5491
|
+
target.set(key, newValue);
|
|
5492
|
+
}
|
|
5377
5493
|
});
|
|
5494
|
+
for (var key in snapshot) {
|
|
5495
|
+
if (!Object.prototype.hasOwnProperty.call(snapshot, key) || target.has(key))
|
|
5496
|
+
continue;
|
|
5497
|
+
target.set(key, snapshot[key]);
|
|
5498
|
+
}
|
|
5378
5499
|
}
|
|
5379
5500
|
});
|
|
5380
5501
|
Object.defineProperty(MapType.prototype, "getChildType", {
|
|
@@ -5700,9 +5821,52 @@
|
|
|
5700
5821
|
configurable: true,
|
|
5701
5822
|
writable: true,
|
|
5702
5823
|
value: function (node, snapshot) {
|
|
5703
|
-
|
|
5824
|
+
if (!isArray(snapshot)) {
|
|
5825
|
+
typecheckInternal(this, snapshot);
|
|
5826
|
+
return;
|
|
5827
|
+
}
|
|
5704
5828
|
var target = node.storedValue;
|
|
5705
|
-
|
|
5829
|
+
var childNodes = node.getChildren();
|
|
5830
|
+
var oldLength = childNodes.length;
|
|
5831
|
+
var newLength = snapshot.length;
|
|
5832
|
+
var childType = this.getChildType();
|
|
5833
|
+
var minLength = Math.min(oldLength, newLength);
|
|
5834
|
+
var firstChangedIndex = findFirstChangedIndex(childNodes, snapshot, minLength);
|
|
5835
|
+
// If all array items are the same and the length did not change, there is nothing to do.
|
|
5836
|
+
if (firstChangedIndex === oldLength && firstChangedIndex === newLength) {
|
|
5837
|
+
return;
|
|
5838
|
+
}
|
|
5839
|
+
// Preserve the old "always replace" behavior for length changes and when patches are being
|
|
5840
|
+
// observed, since those cases are more sensitive to patch shape (for example, splice
|
|
5841
|
+
// would otherwise emit add/remove patch sequences) and benchmark variance.
|
|
5842
|
+
if (oldLength !== newLength || node.hasPatchSubscribers()) {
|
|
5843
|
+
target.replace(snapshot);
|
|
5844
|
+
return;
|
|
5845
|
+
}
|
|
5846
|
+
// When every changed entry can reuse its existing child node, update them in place
|
|
5847
|
+
// instead of going through array replacement/splice.
|
|
5848
|
+
if (canApplyDirectSnapshotsInRange(childType, childNodes, snapshot, firstChangedIndex)) {
|
|
5849
|
+
applyDirectSnapshotsInRange(childNodes, snapshot, firstChangedIndex);
|
|
5850
|
+
return;
|
|
5851
|
+
}
|
|
5852
|
+
var oldEnd = oldLength - 1;
|
|
5853
|
+
var newEnd = newLength - 1;
|
|
5854
|
+
while (oldEnd >= firstChangedIndex &&
|
|
5855
|
+
newEnd >= firstChangedIndex &&
|
|
5856
|
+
childNodes[oldEnd].snapshot === snapshot[newEnd]) {
|
|
5857
|
+
oldEnd--;
|
|
5858
|
+
newEnd--;
|
|
5859
|
+
}
|
|
5860
|
+
// Trim the unchanged suffix too, so we only replace the minimal changed window.
|
|
5861
|
+
var replacedCount = oldEnd >= firstChangedIndex ? oldEnd - firstChangedIndex + 1 : 0;
|
|
5862
|
+
var replacementSnapshots = newEnd >= firstChangedIndex
|
|
5863
|
+
? snapshot.slice(firstChangedIndex, newEnd + 1)
|
|
5864
|
+
: EMPTY_ARRAY;
|
|
5865
|
+
if (replacedCount === 1 && replacementSnapshots.length === 1) {
|
|
5866
|
+
target[firstChangedIndex] = replacementSnapshots[0];
|
|
5867
|
+
return;
|
|
5868
|
+
}
|
|
5869
|
+
target.splice.apply(target, __spreadArray([firstChangedIndex, replacedCount], __read(replacementSnapshots), false));
|
|
5706
5870
|
}
|
|
5707
5871
|
});
|
|
5708
5872
|
Object.defineProperty(ArrayType.prototype, "getChildType", {
|
|
@@ -5774,6 +5938,31 @@
|
|
|
5774
5938
|
assertIsType(subtype, 1);
|
|
5775
5939
|
return new ArrayType("".concat(subtype.name, "[]"), subtype);
|
|
5776
5940
|
}
|
|
5941
|
+
function findFirstChangedIndex(childNodes, snapshot, length) {
|
|
5942
|
+
var index = 0;
|
|
5943
|
+
while (index < length && childNodes[index].snapshot === snapshot[index]) {
|
|
5944
|
+
index++;
|
|
5945
|
+
}
|
|
5946
|
+
return index;
|
|
5947
|
+
}
|
|
5948
|
+
function canApplyDirectSnapshotsInRange(childType, childNodes, snapshot, startIndex) {
|
|
5949
|
+
for (var i = startIndex; i < childNodes.length; i++) {
|
|
5950
|
+
if (childNodes[i].snapshot === snapshot[i])
|
|
5951
|
+
continue;
|
|
5952
|
+
if (!canApplyDirectSnapshot(childType, childNodes[i], snapshot[i])) {
|
|
5953
|
+
return false;
|
|
5954
|
+
}
|
|
5955
|
+
}
|
|
5956
|
+
return true;
|
|
5957
|
+
}
|
|
5958
|
+
function applyDirectSnapshotsInRange(childNodes, snapshot, startIndex) {
|
|
5959
|
+
for (var i = startIndex; i < childNodes.length; i++) {
|
|
5960
|
+
var childNode = childNodes[i];
|
|
5961
|
+
if (childNode.snapshot === snapshot[i])
|
|
5962
|
+
continue;
|
|
5963
|
+
childNode.applySnapshot(snapshot[i]);
|
|
5964
|
+
}
|
|
5965
|
+
}
|
|
5777
5966
|
function reconcileArrayChildren(parent, childType, oldNodes, newValues, newPaths) {
|
|
5778
5967
|
var nothingChanged = true;
|
|
5779
5968
|
for (var i = 0;; i++) {
|
|
@@ -6419,10 +6608,25 @@
|
|
|
6419
6608
|
configurable: true,
|
|
6420
6609
|
writable: true,
|
|
6421
6610
|
value: function (node, snapshot) {
|
|
6422
|
-
typecheckInternal(this, snapshot);
|
|
6423
6611
|
var preProcessedSnapshot = this.applySnapshotPreProcessor(snapshot);
|
|
6424
|
-
|
|
6425
|
-
|
|
6612
|
+
var isPreProcessedSnapshotNonPlain = !isPlainObject(preProcessedSnapshot);
|
|
6613
|
+
// Fast-path plain-object snapshots, but still validate when preprocessing is required
|
|
6614
|
+
// or when preprocessing produces an invalid model snapshot.
|
|
6615
|
+
if (!isPlainObject(snapshot) || isPreProcessedSnapshotNonPlain) {
|
|
6616
|
+
typecheckInternal(this, snapshot);
|
|
6617
|
+
if (isPreProcessedSnapshotNonPlain)
|
|
6618
|
+
return;
|
|
6619
|
+
}
|
|
6620
|
+
this.forAllProps(function (name, childType) {
|
|
6621
|
+
var childNode = node.getChildNode(name);
|
|
6622
|
+
var newValue = preProcessedSnapshot[name];
|
|
6623
|
+
if (childNode.snapshot === newValue)
|
|
6624
|
+
return;
|
|
6625
|
+
if (canApplyDirectSnapshot(childType, childNode, newValue)) {
|
|
6626
|
+
childNode.applySnapshot(newValue);
|
|
6627
|
+
return;
|
|
6628
|
+
}
|
|
6629
|
+
node.storedValue[name] = newValue;
|
|
6426
6630
|
});
|
|
6427
6631
|
}
|
|
6428
6632
|
});
|
|
@@ -6713,6 +6917,37 @@
|
|
|
6713
6917
|
*/
|
|
6714
6918
|
// tslint:disable-next-line:variable-name
|
|
6715
6919
|
var finite = new CoreType("finite", TypeFlags.Finite, function (v) { return isFinite(v); });
|
|
6920
|
+
var _BigIntPrimitive = new CoreType("bigint", TypeFlags.BigInt, function (v) {
|
|
6921
|
+
if (typeof v === "bigint") {
|
|
6922
|
+
return true;
|
|
6923
|
+
}
|
|
6924
|
+
if (typeof v === "string" || typeof v === "number") {
|
|
6925
|
+
try {
|
|
6926
|
+
// BigInt primitive constructor verifies whether the value is a valid integer
|
|
6927
|
+
BigInt(v);
|
|
6928
|
+
return true;
|
|
6929
|
+
}
|
|
6930
|
+
catch (_a) { }
|
|
6931
|
+
}
|
|
6932
|
+
return false;
|
|
6933
|
+
}, function (v) { return (typeof v === "bigint" ? v : BigInt(v)); });
|
|
6934
|
+
_BigIntPrimitive.getSnapshot = function (node) {
|
|
6935
|
+
return String(node.storedValue);
|
|
6936
|
+
};
|
|
6937
|
+
/**
|
|
6938
|
+
* `types.bigint` - Creates a type that can only contain a bigint value.
|
|
6939
|
+
* Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
|
|
6940
|
+
*
|
|
6941
|
+
* Example:
|
|
6942
|
+
* ```ts
|
|
6943
|
+
* const BigId = types.model({
|
|
6944
|
+
* id: types.identifier,
|
|
6945
|
+
* value: types.bigint
|
|
6946
|
+
* })
|
|
6947
|
+
* getSnapshot(store).value // "0" (string, JSON-safe)
|
|
6948
|
+
* ```
|
|
6949
|
+
*/
|
|
6950
|
+
var bigint = _BigIntPrimitive;
|
|
6716
6951
|
/**
|
|
6717
6952
|
* `types.boolean` - Creates a type that can only contain a boolean value.
|
|
6718
6953
|
* This type is used for boolean values by default
|
|
@@ -6764,6 +6999,8 @@
|
|
|
6764
6999
|
return number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
|
|
6765
7000
|
case "boolean":
|
|
6766
7001
|
return boolean;
|
|
7002
|
+
case "bigint":
|
|
7003
|
+
return bigint;
|
|
6767
7004
|
case "object":
|
|
6768
7005
|
if (value instanceof Date)
|
|
6769
7006
|
return DatePrimitive;
|
|
@@ -6783,7 +7020,8 @@
|
|
|
6783
7020
|
TypeFlags.Number |
|
|
6784
7021
|
TypeFlags.Integer |
|
|
6785
7022
|
TypeFlags.Boolean |
|
|
6786
|
-
TypeFlags.Date
|
|
7023
|
+
TypeFlags.Date |
|
|
7024
|
+
TypeFlags.BigInt)) >
|
|
6787
7025
|
0);
|
|
6788
7026
|
}
|
|
6789
7027
|
|
|
@@ -7937,7 +8175,7 @@
|
|
|
7937
8175
|
value: function (value, context) {
|
|
7938
8176
|
return isValidIdentifier(value)
|
|
7939
8177
|
? typeCheckSuccess()
|
|
7940
|
-
: typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a
|
|
8178
|
+
: typeCheckFailure(context, value, "Value is not a valid identifier, which is a string, number or a bigint");
|
|
7941
8179
|
}
|
|
7942
8180
|
});
|
|
7943
8181
|
Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
|
|
@@ -8026,6 +8264,23 @@
|
|
|
8026
8264
|
onRefTargetDestroyedHookDisposer();
|
|
8027
8265
|
}
|
|
8028
8266
|
});
|
|
8267
|
+
var scheduleRetryAfterReconciliation = function () {
|
|
8268
|
+
var retry = function () {
|
|
8269
|
+
if (!storedRefNode.isAlive) {
|
|
8270
|
+
return;
|
|
8271
|
+
}
|
|
8272
|
+
if (_this.getSnapshot(storedRefNode) !== identifier) {
|
|
8273
|
+
return;
|
|
8274
|
+
}
|
|
8275
|
+
startWatching(false);
|
|
8276
|
+
};
|
|
8277
|
+
if (getCurrentActionContext()) {
|
|
8278
|
+
storedRefNode.root.enqueueEndOfAction(retry);
|
|
8279
|
+
}
|
|
8280
|
+
else {
|
|
8281
|
+
setImmediateWithFallback(retry);
|
|
8282
|
+
}
|
|
8283
|
+
};
|
|
8029
8284
|
var startWatching = function (sync) {
|
|
8030
8285
|
// re-create hook in case the stored ref gets reattached
|
|
8031
8286
|
if (onRefTargetDestroyedHookDisposer) {
|
|
@@ -8043,12 +8298,13 @@
|
|
|
8043
8298
|
refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
|
|
8044
8299
|
}
|
|
8045
8300
|
if (!refTargetNodeExists) {
|
|
8046
|
-
// we
|
|
8047
|
-
//
|
|
8048
|
-
//
|
|
8049
|
-
|
|
8050
|
-
|
|
8051
|
-
|
|
8301
|
+
// if the tree is already attached we may still be in the middle of a reconcile/applySnapshot,
|
|
8302
|
+
// so wait until the current MST action finishes before deciding whether to invalidate or
|
|
8303
|
+
// attach a watcher to a target that appeared later in the same action.
|
|
8304
|
+
if (sync) {
|
|
8305
|
+
scheduleRetryAfterReconciliation();
|
|
8306
|
+
}
|
|
8307
|
+
else {
|
|
8052
8308
|
_this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
|
|
8053
8309
|
}
|
|
8054
8310
|
}
|
|
@@ -8398,7 +8654,7 @@
|
|
|
8398
8654
|
* Inside a state tree, for each type can exist only one instance for each given identifier.
|
|
8399
8655
|
* For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
|
|
8400
8656
|
* Identifier can be used only as type property of a model.
|
|
8401
|
-
* This type accepts as parameter the value type of the identifier field that can be either string or
|
|
8657
|
+
* This type accepts as parameter the value type of the identifier field that can be either string, number or bigint.
|
|
8402
8658
|
*
|
|
8403
8659
|
* Example:
|
|
8404
8660
|
* ```ts
|
|
@@ -8425,6 +8681,110 @@
|
|
|
8425
8681
|
* @returns
|
|
8426
8682
|
*/
|
|
8427
8683
|
var identifierNumber = new IdentifierNumberType();
|
|
8684
|
+
/**
|
|
8685
|
+
* @internal
|
|
8686
|
+
* @hidden
|
|
8687
|
+
* IdentifierBigintType uses SimpleType<bigint | string | number, string, bigint> so snapshots serialize to string (JSON-safe).
|
|
8688
|
+
*/
|
|
8689
|
+
var IdentifierBigintType = /** @class */ (function (_super) {
|
|
8690
|
+
__extends(IdentifierBigintType, _super);
|
|
8691
|
+
function IdentifierBigintType() {
|
|
8692
|
+
var _this = _super.call(this, "identifierBigint") || this;
|
|
8693
|
+
Object.defineProperty(_this, "flags", {
|
|
8694
|
+
enumerable: true,
|
|
8695
|
+
configurable: true,
|
|
8696
|
+
writable: true,
|
|
8697
|
+
value: TypeFlags.Identifier
|
|
8698
|
+
});
|
|
8699
|
+
return _this;
|
|
8700
|
+
}
|
|
8701
|
+
Object.defineProperty(IdentifierBigintType.prototype, "createNewInstance", {
|
|
8702
|
+
enumerable: false,
|
|
8703
|
+
configurable: true,
|
|
8704
|
+
writable: true,
|
|
8705
|
+
value: function (snapshot) {
|
|
8706
|
+
if (typeof snapshot === "bigint")
|
|
8707
|
+
return snapshot;
|
|
8708
|
+
return BigInt(snapshot);
|
|
8709
|
+
}
|
|
8710
|
+
});
|
|
8711
|
+
Object.defineProperty(IdentifierBigintType.prototype, "instantiate", {
|
|
8712
|
+
enumerable: false,
|
|
8713
|
+
configurable: true,
|
|
8714
|
+
writable: true,
|
|
8715
|
+
value: function (parent, subpath, environment, initialValue) {
|
|
8716
|
+
if (!parent || !(parent.type instanceof ModelType))
|
|
8717
|
+
throw new MstError("Identifier types can only be instantiated as direct child of a model type");
|
|
8718
|
+
return createScalarNode(this, parent, subpath, environment, initialValue);
|
|
8719
|
+
}
|
|
8720
|
+
});
|
|
8721
|
+
Object.defineProperty(IdentifierBigintType.prototype, "reconcile", {
|
|
8722
|
+
enumerable: false,
|
|
8723
|
+
configurable: true,
|
|
8724
|
+
writable: true,
|
|
8725
|
+
value: function (current, newValue, parent, subpath) {
|
|
8726
|
+
var currentVal = current.storedValue;
|
|
8727
|
+
var newVal = typeof newValue === "bigint" ? newValue : BigInt(newValue);
|
|
8728
|
+
if (currentVal !== newVal)
|
|
8729
|
+
throw new MstError("Tried to change identifier from '".concat(currentVal, "' to '").concat(newVal, "'. Changing identifiers is not allowed."));
|
|
8730
|
+
current.setParent(parent, subpath);
|
|
8731
|
+
return current;
|
|
8732
|
+
}
|
|
8733
|
+
});
|
|
8734
|
+
Object.defineProperty(IdentifierBigintType.prototype, "isValidSnapshot", {
|
|
8735
|
+
enumerable: false,
|
|
8736
|
+
configurable: true,
|
|
8737
|
+
writable: true,
|
|
8738
|
+
value: function (value, context) {
|
|
8739
|
+
if (typeof value === "bigint") {
|
|
8740
|
+
return typeCheckSuccess();
|
|
8741
|
+
}
|
|
8742
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
8743
|
+
try {
|
|
8744
|
+
// BigInt primitive constructor verifies whether the value is a valid integer
|
|
8745
|
+
BigInt(value);
|
|
8746
|
+
return typeCheckSuccess();
|
|
8747
|
+
}
|
|
8748
|
+
catch (e) {
|
|
8749
|
+
var errorMessage = e instanceof Error ? e.message : String(e);
|
|
8750
|
+
return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ": ").concat(errorMessage));
|
|
8751
|
+
}
|
|
8752
|
+
}
|
|
8753
|
+
return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ", expected a bigint, a string or a number"));
|
|
8754
|
+
}
|
|
8755
|
+
});
|
|
8756
|
+
Object.defineProperty(IdentifierBigintType.prototype, "getSnapshot", {
|
|
8757
|
+
enumerable: false,
|
|
8758
|
+
configurable: true,
|
|
8759
|
+
writable: true,
|
|
8760
|
+
value: function (node) {
|
|
8761
|
+
return String(node.storedValue);
|
|
8762
|
+
}
|
|
8763
|
+
});
|
|
8764
|
+
Object.defineProperty(IdentifierBigintType.prototype, "describe", {
|
|
8765
|
+
enumerable: false,
|
|
8766
|
+
configurable: true,
|
|
8767
|
+
writable: true,
|
|
8768
|
+
value: function () {
|
|
8769
|
+
return "identifierBigint";
|
|
8770
|
+
}
|
|
8771
|
+
});
|
|
8772
|
+
return IdentifierBigintType;
|
|
8773
|
+
}(SimpleType));
|
|
8774
|
+
/**
|
|
8775
|
+
* `types.identifierBigint` - Similar to `types.identifier`. Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
|
|
8776
|
+
*
|
|
8777
|
+
* Example:
|
|
8778
|
+
* ```ts
|
|
8779
|
+
* const Todo = types.model("Todo", {
|
|
8780
|
+
* id: types.identifierBigint,
|
|
8781
|
+
* title: types.string
|
|
8782
|
+
* })
|
|
8783
|
+
* ```
|
|
8784
|
+
*
|
|
8785
|
+
* @returns
|
|
8786
|
+
*/
|
|
8787
|
+
var identifierBigint = new IdentifierBigintType();
|
|
8428
8788
|
/**
|
|
8429
8789
|
* Returns if a given value represents an identifier type.
|
|
8430
8790
|
*
|
|
@@ -8446,14 +8806,14 @@
|
|
|
8446
8806
|
* @hidden
|
|
8447
8807
|
*/
|
|
8448
8808
|
function isValidIdentifier(id) {
|
|
8449
|
-
return typeof id === "string" || typeof id === "number";
|
|
8809
|
+
return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
|
|
8450
8810
|
}
|
|
8451
8811
|
/**
|
|
8452
8812
|
* @internal
|
|
8453
8813
|
* @hidden
|
|
8454
8814
|
*/
|
|
8455
8815
|
function assertIsValidIdentifier(id, argNumber) {
|
|
8456
|
-
assertArg(id, isValidIdentifier, "string or
|
|
8816
|
+
assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
|
|
8457
8817
|
}
|
|
8458
8818
|
|
|
8459
8819
|
/**
|
|
@@ -8614,12 +8974,14 @@
|
|
|
8614
8974
|
integer: integer,
|
|
8615
8975
|
float: float,
|
|
8616
8976
|
finite: finite,
|
|
8977
|
+
bigint: bigint,
|
|
8617
8978
|
Date: DatePrimitive,
|
|
8618
8979
|
map: map,
|
|
8619
8980
|
array: array,
|
|
8620
8981
|
frozen: frozen,
|
|
8621
8982
|
identifier: identifier,
|
|
8622
8983
|
identifierNumber: identifierNumber,
|
|
8984
|
+
identifierBigint: identifierBigint,
|
|
8623
8985
|
late: late,
|
|
8624
8986
|
lazy: lazy,
|
|
8625
8987
|
undefined: undefinedType,
|