mobx-state-tree 7.1.0 → 7.3.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/type/errorFormatting.d.ts +60 -0
- package/dist/index.d.ts +1 -1
- package/dist/internal.d.ts +1 -0
- package/dist/mobx-state-tree.js +408 -29
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +407 -30
- package/dist/mobx-state-tree.umd.js +408 -29
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/utility-types/identifier.d.ts +18 -4
- package/package.json +1 -1
|
@@ -1538,6 +1538,12 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1538
1538
|
writable: true,
|
|
1539
1539
|
value: false
|
|
1540
1540
|
});
|
|
1541
|
+
Object.defineProperty(_this, "_endOfActionCallbacks", {
|
|
1542
|
+
enumerable: true,
|
|
1543
|
+
configurable: true,
|
|
1544
|
+
writable: true,
|
|
1545
|
+
value: void 0
|
|
1546
|
+
});
|
|
1541
1547
|
Object.defineProperty(_this, "_observableInstanceState", {
|
|
1542
1548
|
enumerable: true,
|
|
1543
1549
|
configurable: true,
|
|
@@ -1608,8 +1614,8 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1608
1614
|
id = childNode.value;
|
|
1609
1615
|
}
|
|
1610
1616
|
}
|
|
1611
|
-
if (typeof id !== "string" && typeof id !== "number") {
|
|
1612
|
-
throw new MstError("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string or a
|
|
1617
|
+
if (typeof id !== "string" && typeof id !== "number" && typeof id !== "bigint") {
|
|
1618
|
+
throw new MstError("Instance identifier '".concat(_this.identifierAttribute, "' for type '").concat(_this.type.name, "' must be a string, a number or a bigint"));
|
|
1613
1619
|
}
|
|
1614
1620
|
// normalize internal identifier to string
|
|
1615
1621
|
_this.identifier = normalizeIdentifier(id);
|
|
@@ -1747,6 +1753,33 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1747
1753
|
enumerable: false,
|
|
1748
1754
|
configurable: true
|
|
1749
1755
|
});
|
|
1756
|
+
Object.defineProperty(ObjectNode.prototype, "enqueueEndOfAction", {
|
|
1757
|
+
enumerable: false,
|
|
1758
|
+
configurable: true,
|
|
1759
|
+
writable: true,
|
|
1760
|
+
value: function (callback) {
|
|
1761
|
+
var root = this.root;
|
|
1762
|
+
if (!root._endOfActionCallbacks) {
|
|
1763
|
+
root._endOfActionCallbacks = [];
|
|
1764
|
+
}
|
|
1765
|
+
root._endOfActionCallbacks.push(callback);
|
|
1766
|
+
}
|
|
1767
|
+
});
|
|
1768
|
+
Object.defineProperty(ObjectNode.prototype, "flushEndOfActionCallbacks", {
|
|
1769
|
+
enumerable: false,
|
|
1770
|
+
configurable: true,
|
|
1771
|
+
writable: true,
|
|
1772
|
+
value: function () {
|
|
1773
|
+
var root = this.root;
|
|
1774
|
+
while (root._endOfActionCallbacks && root._endOfActionCallbacks.length > 0) {
|
|
1775
|
+
var callbacks = root._endOfActionCallbacks;
|
|
1776
|
+
root._endOfActionCallbacks = undefined;
|
|
1777
|
+
callbacks.forEach(function (callback) {
|
|
1778
|
+
callback();
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
});
|
|
1750
1783
|
Object.defineProperty(ObjectNode.prototype, "clearParent", {
|
|
1751
1784
|
enumerable: false,
|
|
1752
1785
|
configurable: true,
|
|
@@ -3228,9 +3261,10 @@ function runWithActionContext(context, fn) {
|
|
|
3228
3261
|
var baseIsRunningAction = node._isRunningAction;
|
|
3229
3262
|
node._isRunningAction = true;
|
|
3230
3263
|
var previousContext = currentActionContext;
|
|
3264
|
+
var isRootActionContext = !previousContext;
|
|
3231
3265
|
currentActionContext = context;
|
|
3232
3266
|
try {
|
|
3233
|
-
return runMiddleWares(node, context, fn);
|
|
3267
|
+
return runMiddleWares(node, context, fn, isRootActionContext);
|
|
3234
3268
|
}
|
|
3235
3269
|
finally {
|
|
3236
3270
|
currentActionContext = previousContext;
|
|
@@ -3384,17 +3418,44 @@ var CollectedMiddlewares = /** @class */ (function () {
|
|
|
3384
3418
|
});
|
|
3385
3419
|
return CollectedMiddlewares;
|
|
3386
3420
|
}());
|
|
3387
|
-
function runMiddleWares(node, baseCall, originalFn) {
|
|
3421
|
+
function runMiddleWares(node, baseCall, originalFn, isRootActionContext) {
|
|
3422
|
+
function runInActionScope(call, fn) {
|
|
3423
|
+
var execute = function () {
|
|
3424
|
+
var result;
|
|
3425
|
+
var error;
|
|
3426
|
+
try {
|
|
3427
|
+
result = fn.apply(null, call.args);
|
|
3428
|
+
}
|
|
3429
|
+
catch (e) {
|
|
3430
|
+
error = e;
|
|
3431
|
+
}
|
|
3432
|
+
if (isRootActionContext || !call.parentActionEvent) {
|
|
3433
|
+
try {
|
|
3434
|
+
node.root.flushEndOfActionCallbacks();
|
|
3435
|
+
}
|
|
3436
|
+
catch (flushError) {
|
|
3437
|
+
if (!error) {
|
|
3438
|
+
error = flushError;
|
|
3439
|
+
}
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
if (error) {
|
|
3443
|
+
throw error;
|
|
3444
|
+
}
|
|
3445
|
+
return result;
|
|
3446
|
+
};
|
|
3447
|
+
return call.name ? action(call.name, execute)() : action(execute)();
|
|
3448
|
+
}
|
|
3388
3449
|
var middlewares = new CollectedMiddlewares(node, originalFn);
|
|
3389
3450
|
// Short circuit
|
|
3390
3451
|
if (middlewares.isEmpty)
|
|
3391
|
-
return
|
|
3452
|
+
return runInActionScope(baseCall, originalFn);
|
|
3392
3453
|
var result = null;
|
|
3393
3454
|
function runNextMiddleware(call) {
|
|
3394
3455
|
var middleware = middlewares.getNextMiddleware();
|
|
3395
3456
|
var handler = middleware && middleware.handler;
|
|
3396
3457
|
if (!handler) {
|
|
3397
|
-
return
|
|
3458
|
+
return runInActionScope(call, originalFn);
|
|
3398
3459
|
}
|
|
3399
3460
|
// skip hooks if asked to
|
|
3400
3461
|
if (!middleware.includeHooks && Hook[call.name]) {
|
|
@@ -3430,6 +3491,9 @@ function runMiddleWares(node, baseCall, originalFn) {
|
|
|
3430
3491
|
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."));
|
|
3431
3492
|
}
|
|
3432
3493
|
}
|
|
3494
|
+
if (abortInvoked && (isRootActionContext || !call.parentActionEvent)) {
|
|
3495
|
+
return runInActionScope(call, function () { return result; });
|
|
3496
|
+
}
|
|
3433
3497
|
return result;
|
|
3434
3498
|
}
|
|
3435
3499
|
return runNextMiddleware(baseCall);
|
|
@@ -3471,30 +3535,207 @@ function isActionContextThisOrChildOf(actionContext, parentOrThis) {
|
|
|
3471
3535
|
return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
|
|
3472
3536
|
}
|
|
3473
3537
|
|
|
3474
|
-
|
|
3538
|
+
var defaultOptions = {
|
|
3539
|
+
enabled: false,
|
|
3540
|
+
indent: 2,
|
|
3541
|
+
maxStringLength: 100,
|
|
3542
|
+
maxArrayLength: 10,
|
|
3543
|
+
maxPropertyCount: 30,
|
|
3544
|
+
maxDepth: 5
|
|
3545
|
+
};
|
|
3546
|
+
var currentOptions = __assign({}, defaultOptions);
|
|
3547
|
+
/**
|
|
3548
|
+
* Configures how snapshots/values and type shapes are formatted inside
|
|
3549
|
+
* type-checking error messages. This is **opt-in**: by default MST keeps its
|
|
3550
|
+
* original single-line error formatting, so enabling this does not change
|
|
3551
|
+
* behavior for anyone who doesn't call it.
|
|
3552
|
+
*
|
|
3553
|
+
* The given options are merged over the current ones, so you can set only the
|
|
3554
|
+
* fields you care about.
|
|
3555
|
+
*
|
|
3556
|
+
* @example
|
|
3557
|
+
* // pretty-print large snapshots across multiple lines and truncate them
|
|
3558
|
+
* setErrorFormatting({ enabled: true })
|
|
3559
|
+
*
|
|
3560
|
+
* @example
|
|
3561
|
+
* // only bound the message size, without reflowing it onto multiple lines
|
|
3562
|
+
* setErrorFormatting({ enabled: true, indent: 0 })
|
|
3563
|
+
*
|
|
3564
|
+
* @example
|
|
3565
|
+
* // tweak the truncation limits
|
|
3566
|
+
* setErrorFormatting({ enabled: true, maxArrayLength: 3, maxStringLength: 40 })
|
|
3567
|
+
*
|
|
3568
|
+
* @param options A partial set of {@link ErrorFormattingOptions} to apply.
|
|
3569
|
+
*/
|
|
3570
|
+
function setErrorFormatting(options) {
|
|
3571
|
+
currentOptions = __assign(__assign({}, currentOptions), options);
|
|
3572
|
+
}
|
|
3573
|
+
/**
|
|
3574
|
+
* Returns a copy of the current error formatting options (see
|
|
3575
|
+
* {@link setErrorFormatting}). Useful for temporarily overriding and then
|
|
3576
|
+
* restoring the configuration.
|
|
3577
|
+
*
|
|
3578
|
+
* @returns The current {@link ErrorFormattingOptions}.
|
|
3579
|
+
*/
|
|
3580
|
+
function getErrorFormatting() {
|
|
3581
|
+
return __assign({}, currentOptions);
|
|
3582
|
+
}
|
|
3583
|
+
|
|
3584
|
+
function safeStringify(value, indent) {
|
|
3475
3585
|
try {
|
|
3476
|
-
return JSON.stringify(value);
|
|
3586
|
+
return JSON.stringify(value, null, indent);
|
|
3477
3587
|
}
|
|
3478
3588
|
catch (e) {
|
|
3479
3589
|
// istanbul ignore next
|
|
3480
3590
|
return "<Unserializable: ".concat(e, ">");
|
|
3481
3591
|
}
|
|
3482
3592
|
}
|
|
3593
|
+
function shortenPrintValue(valueInString) {
|
|
3594
|
+
return valueInString.length < 280
|
|
3595
|
+
? valueInString
|
|
3596
|
+
: "".concat(valueInString.substring(0, 272), "......").concat(valueInString.substring(valueInString.length - 8));
|
|
3597
|
+
}
|
|
3598
|
+
/**
|
|
3599
|
+
* Returns a clone of `value` in which overly long strings are clipped and large
|
|
3600
|
+
* arrays/objects (or values nested too deeply) are summarized, so the result is
|
|
3601
|
+
* safe to print in an error message without flooding the screen.
|
|
3602
|
+
*/
|
|
3603
|
+
function truncateForDisplay(value, depth, options) {
|
|
3604
|
+
if (typeof value === "string") {
|
|
3605
|
+
return value.length > options.maxStringLength
|
|
3606
|
+
? "".concat(value.slice(0, options.maxStringLength), "\u2026 (").concat(value.length - options.maxStringLength, " more characters)")
|
|
3607
|
+
: value;
|
|
3608
|
+
}
|
|
3609
|
+
if (Array.isArray(value)) {
|
|
3610
|
+
if (depth >= options.maxDepth)
|
|
3611
|
+
return "[…]";
|
|
3612
|
+
var items = value
|
|
3613
|
+
.slice(0, options.maxArrayLength)
|
|
3614
|
+
.map(function (item) { return truncateForDisplay(item, depth + 1, options); });
|
|
3615
|
+
if (value.length > options.maxArrayLength) {
|
|
3616
|
+
items.push("\u2026 ".concat(value.length - options.maxArrayLength, " more items"));
|
|
3617
|
+
}
|
|
3618
|
+
return items;
|
|
3619
|
+
}
|
|
3620
|
+
if (isPlainObject(value)) {
|
|
3621
|
+
if (depth >= options.maxDepth)
|
|
3622
|
+
return "{…}";
|
|
3623
|
+
var result_1 = {};
|
|
3624
|
+
var keys = Object.keys(value);
|
|
3625
|
+
keys.slice(0, options.maxPropertyCount).forEach(function (key) {
|
|
3626
|
+
result_1[key] = truncateForDisplay(value[key], depth + 1, options);
|
|
3627
|
+
});
|
|
3628
|
+
if (keys.length > options.maxPropertyCount) {
|
|
3629
|
+
result_1["…"] = "".concat(keys.length - options.maxPropertyCount, " more keys");
|
|
3630
|
+
}
|
|
3631
|
+
return result_1;
|
|
3632
|
+
}
|
|
3633
|
+
return value;
|
|
3634
|
+
}
|
|
3483
3635
|
/**
|
|
3484
3636
|
* @internal
|
|
3485
3637
|
* @hidden
|
|
3486
3638
|
*/
|
|
3487
3639
|
function prettyPrintValue(value) {
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3640
|
+
if (typeof value === "function") {
|
|
3641
|
+
return "<function".concat(value.name ? " " + value.name : "", ">");
|
|
3642
|
+
}
|
|
3643
|
+
if (isStateTreeNode(value)) {
|
|
3644
|
+
return "<".concat(value, ">");
|
|
3645
|
+
}
|
|
3646
|
+
var options = getErrorFormatting();
|
|
3647
|
+
if (!options.enabled) {
|
|
3648
|
+
// Default behavior: serialize the value compactly on a single line.
|
|
3649
|
+
// JSON.stringify returns `undefined` for values like `undefined` itself,
|
|
3650
|
+
// which the template literal coerces back to a string.
|
|
3651
|
+
return "`".concat(safeStringify(value), "`");
|
|
3652
|
+
}
|
|
3653
|
+
// Opt-in behavior: clip long strings, big arrays/objects and deep nesting, and
|
|
3654
|
+
// (when indent > 0) pretty-print the result across multiple lines.
|
|
3655
|
+
var truncated = truncateForDisplay(value, 0, options);
|
|
3656
|
+
return "`".concat(safeStringify(truncated, options.indent || undefined), "`");
|
|
3493
3657
|
}
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3658
|
+
/**
|
|
3659
|
+
* Re-indents a type description (as produced by `IType.describe()`) across
|
|
3660
|
+
* multiple lines, by breaking after the `{`, `}` and `;` separators used in
|
|
3661
|
+
* model shapes (while leaving union `|` and array `[]` parts inline).
|
|
3662
|
+
* Characters inside string literals (e.g. literal types like `"a;b"`) are left
|
|
3663
|
+
* untouched.
|
|
3664
|
+
*
|
|
3665
|
+
* `indentSize` is the number of spaces per nesting level; when it is `0` (or
|
|
3666
|
+
* negative) the description is returned unchanged on a single line. As this runs
|
|
3667
|
+
* while formatting an error that is already being thrown, it must never throw
|
|
3668
|
+
* itself: any unexpected input falls back to the original, unformatted
|
|
3669
|
+
* description.
|
|
3670
|
+
*
|
|
3671
|
+
* @internal
|
|
3672
|
+
* @hidden
|
|
3673
|
+
*/
|
|
3674
|
+
function prettyPrintDescription(description, indentSize) {
|
|
3675
|
+
if (indentSize <= 0) {
|
|
3676
|
+
return description;
|
|
3677
|
+
}
|
|
3678
|
+
try {
|
|
3679
|
+
var step_1 = " ".repeat(indentSize);
|
|
3680
|
+
var result_2 = "";
|
|
3681
|
+
var depth_1 = 0;
|
|
3682
|
+
var stringDelimiter = null;
|
|
3683
|
+
var escaped = false;
|
|
3684
|
+
var newline = function () {
|
|
3685
|
+
// drop any trailing spaces (e.g. the "{ " / "; " separators) before breaking,
|
|
3686
|
+
// and never let a malformed (over-closed) shape produce a negative indent
|
|
3687
|
+
result_2 = result_2.replace(/[ \t]+$/, "");
|
|
3688
|
+
result_2 += "\n" + step_1.repeat(Math.max(0, depth_1));
|
|
3689
|
+
};
|
|
3690
|
+
for (var i = 0; i < description.length; i++) {
|
|
3691
|
+
var char = description[i];
|
|
3692
|
+
if (stringDelimiter) {
|
|
3693
|
+
result_2 += char;
|
|
3694
|
+
if (escaped) {
|
|
3695
|
+
escaped = false;
|
|
3696
|
+
}
|
|
3697
|
+
else if (char === "\\") {
|
|
3698
|
+
escaped = true;
|
|
3699
|
+
}
|
|
3700
|
+
else if (char === stringDelimiter) {
|
|
3701
|
+
stringDelimiter = null;
|
|
3702
|
+
}
|
|
3703
|
+
continue;
|
|
3704
|
+
}
|
|
3705
|
+
switch (char) {
|
|
3706
|
+
case '"':
|
|
3707
|
+
case "'":
|
|
3708
|
+
stringDelimiter = char;
|
|
3709
|
+
result_2 += char;
|
|
3710
|
+
break;
|
|
3711
|
+
case "{":
|
|
3712
|
+
depth_1++;
|
|
3713
|
+
result_2 += "{";
|
|
3714
|
+
newline();
|
|
3715
|
+
while (description[i + 1] === " ")
|
|
3716
|
+
i++;
|
|
3717
|
+
break;
|
|
3718
|
+
case "}":
|
|
3719
|
+
depth_1--;
|
|
3720
|
+
newline();
|
|
3721
|
+
result_2 += "}";
|
|
3722
|
+
break;
|
|
3723
|
+
case ";":
|
|
3724
|
+
result_2 += ";";
|
|
3725
|
+
newline();
|
|
3726
|
+
while (description[i + 1] === " ")
|
|
3727
|
+
i++;
|
|
3728
|
+
break;
|
|
3729
|
+
default:
|
|
3730
|
+
result_2 += char;
|
|
3731
|
+
}
|
|
3732
|
+
}
|
|
3733
|
+
return result_2;
|
|
3734
|
+
}
|
|
3735
|
+
catch (e) {
|
|
3736
|
+
// istanbul ignore next - defensive: never let formatting hide the real error
|
|
3737
|
+
return description;
|
|
3738
|
+
}
|
|
3498
3739
|
}
|
|
3499
3740
|
function toErrorString(error) {
|
|
3500
3741
|
var value = error.value;
|
|
@@ -3513,12 +3754,18 @@ function toErrorString(error) {
|
|
|
3513
3754
|
? "value"
|
|
3514
3755
|
: "snapshot";
|
|
3515
3756
|
var isSnapshotCompatible = type && isStateTreeNode(value) && type.is(getStateTreeNode(value).snapshot);
|
|
3757
|
+
// When error formatting is enabled, the type shape is re-indented using the
|
|
3758
|
+
// same indent setting as values; otherwise it's left on a single line.
|
|
3759
|
+
var formatting = getErrorFormatting();
|
|
3760
|
+
var describeType = function (t) {
|
|
3761
|
+
return formatting.enabled ? prettyPrintDescription(t.describe(), formatting.indent) : t.describe();
|
|
3762
|
+
};
|
|
3516
3763
|
return ("".concat(pathPrefix).concat(currentTypename, " ").concat(prettyPrintValue(value), " is not assignable ").concat(type ? "to type: `".concat(type.name, "`") : "") +
|
|
3517
3764
|
(error.message ? " (".concat(error.message, ")") : "") +
|
|
3518
3765
|
(type
|
|
3519
3766
|
? isPrimitiveType(type) || isPrimitive(value)
|
|
3520
3767
|
? "."
|
|
3521
|
-
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(type
|
|
3768
|
+
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(describeType(type), "` instead.") +
|
|
3522
3769
|
(isSnapshotCompatible
|
|
3523
3770
|
? " (Note that a snapshot of the provided value is compatible with the targeted type)"
|
|
3524
3771
|
: "")
|
|
@@ -3581,7 +3828,14 @@ function validationErrorsToString(type, value, errors) {
|
|
|
3581
3828
|
if (errors.length === 0) {
|
|
3582
3829
|
return undefined;
|
|
3583
3830
|
}
|
|
3584
|
-
|
|
3831
|
+
// When formatting is disabled, keep the original behavior of capping the
|
|
3832
|
+
// header value's length; when enabled, truncation already bounds its size.
|
|
3833
|
+
var printedValue = prettyPrintValue(value);
|
|
3834
|
+
var headerValue = getErrorFormatting().enabled
|
|
3835
|
+
? printedValue
|
|
3836
|
+
: shortenPrintValue(printedValue);
|
|
3837
|
+
return ("Error while converting ".concat(headerValue, " to `").concat(type.name, "`:\n\n ") +
|
|
3838
|
+
errors.map(toErrorString).join("\n "));
|
|
3585
3839
|
}
|
|
3586
3840
|
|
|
3587
3841
|
var identifierCacheId = 0;
|
|
@@ -8129,7 +8383,7 @@ var BaseReferenceType = /** @class */ (function (_super) {
|
|
|
8129
8383
|
value: function (value, context) {
|
|
8130
8384
|
return isValidIdentifier(value)
|
|
8131
8385
|
? typeCheckSuccess()
|
|
8132
|
-
: typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a
|
|
8386
|
+
: typeCheckFailure(context, value, "Value is not a valid identifier, which is a string, number or a bigint");
|
|
8133
8387
|
}
|
|
8134
8388
|
});
|
|
8135
8389
|
Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
|
|
@@ -8218,6 +8472,23 @@ var BaseReferenceType = /** @class */ (function (_super) {
|
|
|
8218
8472
|
onRefTargetDestroyedHookDisposer();
|
|
8219
8473
|
}
|
|
8220
8474
|
});
|
|
8475
|
+
var scheduleRetryAfterReconciliation = function () {
|
|
8476
|
+
var retry = function () {
|
|
8477
|
+
if (!storedRefNode.isAlive) {
|
|
8478
|
+
return;
|
|
8479
|
+
}
|
|
8480
|
+
if (_this.getSnapshot(storedRefNode) !== identifier) {
|
|
8481
|
+
return;
|
|
8482
|
+
}
|
|
8483
|
+
startWatching(false);
|
|
8484
|
+
};
|
|
8485
|
+
if (getCurrentActionContext()) {
|
|
8486
|
+
storedRefNode.root.enqueueEndOfAction(retry);
|
|
8487
|
+
}
|
|
8488
|
+
else {
|
|
8489
|
+
setImmediateWithFallback(retry);
|
|
8490
|
+
}
|
|
8491
|
+
};
|
|
8221
8492
|
var startWatching = function (sync) {
|
|
8222
8493
|
// re-create hook in case the stored ref gets reattached
|
|
8223
8494
|
if (onRefTargetDestroyedHookDisposer) {
|
|
@@ -8235,12 +8506,13 @@ var BaseReferenceType = /** @class */ (function (_super) {
|
|
|
8235
8506
|
refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
|
|
8236
8507
|
}
|
|
8237
8508
|
if (!refTargetNodeExists) {
|
|
8238
|
-
// we
|
|
8239
|
-
//
|
|
8240
|
-
//
|
|
8241
|
-
|
|
8242
|
-
|
|
8243
|
-
|
|
8509
|
+
// if the tree is already attached we may still be in the middle of a reconcile/applySnapshot,
|
|
8510
|
+
// so wait until the current MST action finishes before deciding whether to invalidate or
|
|
8511
|
+
// attach a watcher to a target that appeared later in the same action.
|
|
8512
|
+
if (sync) {
|
|
8513
|
+
scheduleRetryAfterReconciliation();
|
|
8514
|
+
}
|
|
8515
|
+
else {
|
|
8244
8516
|
_this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
|
|
8245
8517
|
}
|
|
8246
8518
|
}
|
|
@@ -8590,7 +8862,7 @@ var IdentifierNumberType = /** @class */ (function (_super) {
|
|
|
8590
8862
|
* Inside a state tree, for each type can exist only one instance for each given identifier.
|
|
8591
8863
|
* For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
|
|
8592
8864
|
* Identifier can be used only as type property of a model.
|
|
8593
|
-
* This type accepts as parameter the value type of the identifier field that can be either string or
|
|
8865
|
+
* This type accepts as parameter the value type of the identifier field that can be either string, number or bigint.
|
|
8594
8866
|
*
|
|
8595
8867
|
* Example:
|
|
8596
8868
|
* ```ts
|
|
@@ -8617,6 +8889,110 @@ var identifier = new IdentifierType();
|
|
|
8617
8889
|
* @returns
|
|
8618
8890
|
*/
|
|
8619
8891
|
var identifierNumber = new IdentifierNumberType();
|
|
8892
|
+
/**
|
|
8893
|
+
* @internal
|
|
8894
|
+
* @hidden
|
|
8895
|
+
* IdentifierBigintType uses SimpleType<bigint | string | number, string, bigint> so snapshots serialize to string (JSON-safe).
|
|
8896
|
+
*/
|
|
8897
|
+
var IdentifierBigintType = /** @class */ (function (_super) {
|
|
8898
|
+
__extends(IdentifierBigintType, _super);
|
|
8899
|
+
function IdentifierBigintType() {
|
|
8900
|
+
var _this = _super.call(this, "identifierBigint") || this;
|
|
8901
|
+
Object.defineProperty(_this, "flags", {
|
|
8902
|
+
enumerable: true,
|
|
8903
|
+
configurable: true,
|
|
8904
|
+
writable: true,
|
|
8905
|
+
value: TypeFlags.Identifier
|
|
8906
|
+
});
|
|
8907
|
+
return _this;
|
|
8908
|
+
}
|
|
8909
|
+
Object.defineProperty(IdentifierBigintType.prototype, "createNewInstance", {
|
|
8910
|
+
enumerable: false,
|
|
8911
|
+
configurable: true,
|
|
8912
|
+
writable: true,
|
|
8913
|
+
value: function (snapshot) {
|
|
8914
|
+
if (typeof snapshot === "bigint")
|
|
8915
|
+
return snapshot;
|
|
8916
|
+
return BigInt(snapshot);
|
|
8917
|
+
}
|
|
8918
|
+
});
|
|
8919
|
+
Object.defineProperty(IdentifierBigintType.prototype, "instantiate", {
|
|
8920
|
+
enumerable: false,
|
|
8921
|
+
configurable: true,
|
|
8922
|
+
writable: true,
|
|
8923
|
+
value: function (parent, subpath, environment, initialValue) {
|
|
8924
|
+
if (!parent || !(parent.type instanceof ModelType))
|
|
8925
|
+
throw new MstError("Identifier types can only be instantiated as direct child of a model type");
|
|
8926
|
+
return createScalarNode(this, parent, subpath, environment, initialValue);
|
|
8927
|
+
}
|
|
8928
|
+
});
|
|
8929
|
+
Object.defineProperty(IdentifierBigintType.prototype, "reconcile", {
|
|
8930
|
+
enumerable: false,
|
|
8931
|
+
configurable: true,
|
|
8932
|
+
writable: true,
|
|
8933
|
+
value: function (current, newValue, parent, subpath) {
|
|
8934
|
+
var currentVal = current.storedValue;
|
|
8935
|
+
var newVal = typeof newValue === "bigint" ? newValue : BigInt(newValue);
|
|
8936
|
+
if (currentVal !== newVal)
|
|
8937
|
+
throw new MstError("Tried to change identifier from '".concat(currentVal, "' to '").concat(newVal, "'. Changing identifiers is not allowed."));
|
|
8938
|
+
current.setParent(parent, subpath);
|
|
8939
|
+
return current;
|
|
8940
|
+
}
|
|
8941
|
+
});
|
|
8942
|
+
Object.defineProperty(IdentifierBigintType.prototype, "isValidSnapshot", {
|
|
8943
|
+
enumerable: false,
|
|
8944
|
+
configurable: true,
|
|
8945
|
+
writable: true,
|
|
8946
|
+
value: function (value, context) {
|
|
8947
|
+
if (typeof value === "bigint") {
|
|
8948
|
+
return typeCheckSuccess();
|
|
8949
|
+
}
|
|
8950
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
8951
|
+
try {
|
|
8952
|
+
// BigInt primitive constructor verifies whether the value is a valid integer
|
|
8953
|
+
BigInt(value);
|
|
8954
|
+
return typeCheckSuccess();
|
|
8955
|
+
}
|
|
8956
|
+
catch (e) {
|
|
8957
|
+
var errorMessage = e instanceof Error ? e.message : String(e);
|
|
8958
|
+
return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ": ").concat(errorMessage));
|
|
8959
|
+
}
|
|
8960
|
+
}
|
|
8961
|
+
return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ", expected a bigint, a string or a number"));
|
|
8962
|
+
}
|
|
8963
|
+
});
|
|
8964
|
+
Object.defineProperty(IdentifierBigintType.prototype, "getSnapshot", {
|
|
8965
|
+
enumerable: false,
|
|
8966
|
+
configurable: true,
|
|
8967
|
+
writable: true,
|
|
8968
|
+
value: function (node) {
|
|
8969
|
+
return String(node.storedValue);
|
|
8970
|
+
}
|
|
8971
|
+
});
|
|
8972
|
+
Object.defineProperty(IdentifierBigintType.prototype, "describe", {
|
|
8973
|
+
enumerable: false,
|
|
8974
|
+
configurable: true,
|
|
8975
|
+
writable: true,
|
|
8976
|
+
value: function () {
|
|
8977
|
+
return "identifierBigint";
|
|
8978
|
+
}
|
|
8979
|
+
});
|
|
8980
|
+
return IdentifierBigintType;
|
|
8981
|
+
}(SimpleType));
|
|
8982
|
+
/**
|
|
8983
|
+
* `types.identifierBigint` - Similar to `types.identifier`. Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
|
|
8984
|
+
*
|
|
8985
|
+
* Example:
|
|
8986
|
+
* ```ts
|
|
8987
|
+
* const Todo = types.model("Todo", {
|
|
8988
|
+
* id: types.identifierBigint,
|
|
8989
|
+
* title: types.string
|
|
8990
|
+
* })
|
|
8991
|
+
* ```
|
|
8992
|
+
*
|
|
8993
|
+
* @returns
|
|
8994
|
+
*/
|
|
8995
|
+
var identifierBigint = new IdentifierBigintType();
|
|
8620
8996
|
/**
|
|
8621
8997
|
* Returns if a given value represents an identifier type.
|
|
8622
8998
|
*
|
|
@@ -8638,14 +9014,14 @@ function normalizeIdentifier(id) {
|
|
|
8638
9014
|
* @hidden
|
|
8639
9015
|
*/
|
|
8640
9016
|
function isValidIdentifier(id) {
|
|
8641
|
-
return typeof id === "string" || typeof id === "number";
|
|
9017
|
+
return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
|
|
8642
9018
|
}
|
|
8643
9019
|
/**
|
|
8644
9020
|
* @internal
|
|
8645
9021
|
* @hidden
|
|
8646
9022
|
*/
|
|
8647
9023
|
function assertIsValidIdentifier(id, argNumber) {
|
|
8648
|
-
assertArg(id, isValidIdentifier, "string or
|
|
9024
|
+
assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
|
|
8649
9025
|
}
|
|
8650
9026
|
|
|
8651
9027
|
/**
|
|
@@ -8813,6 +9189,7 @@ var types = {
|
|
|
8813
9189
|
frozen: frozen,
|
|
8814
9190
|
identifier: identifier,
|
|
8815
9191
|
identifierNumber: identifierNumber,
|
|
9192
|
+
identifierBigint: identifierBigint,
|
|
8816
9193
|
late: late,
|
|
8817
9194
|
lazy: lazy,
|
|
8818
9195
|
undefined: undefinedType,
|
|
@@ -8820,4 +9197,4 @@ var types = {
|
|
|
8820
9197
|
snapshotProcessor: snapshotProcessor
|
|
8821
9198
|
};
|
|
8822
9199
|
|
|
8823
|
-
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasEnv, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setLivelinessChecking, setLivelynessChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|
|
9200
|
+
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getErrorFormatting, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasEnv, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setErrorFormatting, setLivelinessChecking, setLivelynessChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|