mobx-state-tree 7.1.0 → 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.
@@ -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 number"));
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 action(originalFn).apply(null, baseCall.args);
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 action(originalFn).apply(null, call.args);
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);
@@ -8129,7 +8193,7 @@ var BaseReferenceType = /** @class */ (function (_super) {
8129
8193
  value: function (value, context) {
8130
8194
  return isValidIdentifier(value)
8131
8195
  ? typeCheckSuccess()
8132
- : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a number");
8196
+ : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string, number or a bigint");
8133
8197
  }
8134
8198
  });
8135
8199
  Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
@@ -8218,6 +8282,23 @@ var BaseReferenceType = /** @class */ (function (_super) {
8218
8282
  onRefTargetDestroyedHookDisposer();
8219
8283
  }
8220
8284
  });
8285
+ var scheduleRetryAfterReconciliation = function () {
8286
+ var retry = function () {
8287
+ if (!storedRefNode.isAlive) {
8288
+ return;
8289
+ }
8290
+ if (_this.getSnapshot(storedRefNode) !== identifier) {
8291
+ return;
8292
+ }
8293
+ startWatching(false);
8294
+ };
8295
+ if (getCurrentActionContext()) {
8296
+ storedRefNode.root.enqueueEndOfAction(retry);
8297
+ }
8298
+ else {
8299
+ setImmediateWithFallback(retry);
8300
+ }
8301
+ };
8221
8302
  var startWatching = function (sync) {
8222
8303
  // re-create hook in case the stored ref gets reattached
8223
8304
  if (onRefTargetDestroyedHookDisposer) {
@@ -8235,12 +8316,13 @@ var BaseReferenceType = /** @class */ (function (_super) {
8235
8316
  refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
8236
8317
  }
8237
8318
  if (!refTargetNodeExists) {
8238
- // we cannot change the reference in sync mode
8239
- // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
8240
- // for those cases just let the wrong reference be assigned and fail upon usage
8241
- // (like current references do)
8242
- // this means that effectively this code will only run when it is created from a snapshot
8243
- if (!sync) {
8319
+ // if the tree is already attached we may still be in the middle of a reconcile/applySnapshot,
8320
+ // so wait until the current MST action finishes before deciding whether to invalidate or
8321
+ // attach a watcher to a target that appeared later in the same action.
8322
+ if (sync) {
8323
+ scheduleRetryAfterReconciliation();
8324
+ }
8325
+ else {
8244
8326
  _this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
8245
8327
  }
8246
8328
  }
@@ -8590,7 +8672,7 @@ var IdentifierNumberType = /** @class */ (function (_super) {
8590
8672
  * Inside a state tree, for each type can exist only one instance for each given identifier.
8591
8673
  * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
8592
8674
  * 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 number.
8675
+ * This type accepts as parameter the value type of the identifier field that can be either string, number or bigint.
8594
8676
  *
8595
8677
  * Example:
8596
8678
  * ```ts
@@ -8617,6 +8699,110 @@ var identifier = new IdentifierType();
8617
8699
  * @returns
8618
8700
  */
8619
8701
  var identifierNumber = new IdentifierNumberType();
8702
+ /**
8703
+ * @internal
8704
+ * @hidden
8705
+ * IdentifierBigintType uses SimpleType<bigint | string | number, string, bigint> so snapshots serialize to string (JSON-safe).
8706
+ */
8707
+ var IdentifierBigintType = /** @class */ (function (_super) {
8708
+ __extends(IdentifierBigintType, _super);
8709
+ function IdentifierBigintType() {
8710
+ var _this = _super.call(this, "identifierBigint") || this;
8711
+ Object.defineProperty(_this, "flags", {
8712
+ enumerable: true,
8713
+ configurable: true,
8714
+ writable: true,
8715
+ value: TypeFlags.Identifier
8716
+ });
8717
+ return _this;
8718
+ }
8719
+ Object.defineProperty(IdentifierBigintType.prototype, "createNewInstance", {
8720
+ enumerable: false,
8721
+ configurable: true,
8722
+ writable: true,
8723
+ value: function (snapshot) {
8724
+ if (typeof snapshot === "bigint")
8725
+ return snapshot;
8726
+ return BigInt(snapshot);
8727
+ }
8728
+ });
8729
+ Object.defineProperty(IdentifierBigintType.prototype, "instantiate", {
8730
+ enumerable: false,
8731
+ configurable: true,
8732
+ writable: true,
8733
+ value: function (parent, subpath, environment, initialValue) {
8734
+ if (!parent || !(parent.type instanceof ModelType))
8735
+ throw new MstError("Identifier types can only be instantiated as direct child of a model type");
8736
+ return createScalarNode(this, parent, subpath, environment, initialValue);
8737
+ }
8738
+ });
8739
+ Object.defineProperty(IdentifierBigintType.prototype, "reconcile", {
8740
+ enumerable: false,
8741
+ configurable: true,
8742
+ writable: true,
8743
+ value: function (current, newValue, parent, subpath) {
8744
+ var currentVal = current.storedValue;
8745
+ var newVal = typeof newValue === "bigint" ? newValue : BigInt(newValue);
8746
+ if (currentVal !== newVal)
8747
+ throw new MstError("Tried to change identifier from '".concat(currentVal, "' to '").concat(newVal, "'. Changing identifiers is not allowed."));
8748
+ current.setParent(parent, subpath);
8749
+ return current;
8750
+ }
8751
+ });
8752
+ Object.defineProperty(IdentifierBigintType.prototype, "isValidSnapshot", {
8753
+ enumerable: false,
8754
+ configurable: true,
8755
+ writable: true,
8756
+ value: function (value, context) {
8757
+ if (typeof value === "bigint") {
8758
+ return typeCheckSuccess();
8759
+ }
8760
+ if (typeof value === "string" || typeof value === "number") {
8761
+ try {
8762
+ // BigInt primitive constructor verifies whether the value is a valid integer
8763
+ BigInt(value);
8764
+ return typeCheckSuccess();
8765
+ }
8766
+ catch (e) {
8767
+ var errorMessage = e instanceof Error ? e.message : String(e);
8768
+ return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ": ").concat(errorMessage));
8769
+ }
8770
+ }
8771
+ return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ", expected a bigint, a string or a number"));
8772
+ }
8773
+ });
8774
+ Object.defineProperty(IdentifierBigintType.prototype, "getSnapshot", {
8775
+ enumerable: false,
8776
+ configurable: true,
8777
+ writable: true,
8778
+ value: function (node) {
8779
+ return String(node.storedValue);
8780
+ }
8781
+ });
8782
+ Object.defineProperty(IdentifierBigintType.prototype, "describe", {
8783
+ enumerable: false,
8784
+ configurable: true,
8785
+ writable: true,
8786
+ value: function () {
8787
+ return "identifierBigint";
8788
+ }
8789
+ });
8790
+ return IdentifierBigintType;
8791
+ }(SimpleType));
8792
+ /**
8793
+ * `types.identifierBigint` - Similar to `types.identifier`. Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
8794
+ *
8795
+ * Example:
8796
+ * ```ts
8797
+ * const Todo = types.model("Todo", {
8798
+ * id: types.identifierBigint,
8799
+ * title: types.string
8800
+ * })
8801
+ * ```
8802
+ *
8803
+ * @returns
8804
+ */
8805
+ var identifierBigint = new IdentifierBigintType();
8620
8806
  /**
8621
8807
  * Returns if a given value represents an identifier type.
8622
8808
  *
@@ -8638,14 +8824,14 @@ function normalizeIdentifier(id) {
8638
8824
  * @hidden
8639
8825
  */
8640
8826
  function isValidIdentifier(id) {
8641
- return typeof id === "string" || typeof id === "number";
8827
+ return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
8642
8828
  }
8643
8829
  /**
8644
8830
  * @internal
8645
8831
  * @hidden
8646
8832
  */
8647
8833
  function assertIsValidIdentifier(id, argNumber) {
8648
- assertArg(id, isValidIdentifier, "string or number (identifier)", argNumber);
8834
+ assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
8649
8835
  }
8650
8836
 
8651
8837
  /**
@@ -8813,6 +8999,7 @@ var types = {
8813
8999
  frozen: frozen,
8814
9000
  identifier: identifier,
8815
9001
  identifierNumber: identifierNumber,
9002
+ identifierBigint: identifierBigint,
8816
9003
  late: late,
8817
9004
  lazy: lazy,
8818
9005
  undefined: undefinedType,
@@ -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 number"));
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,
@@ -3232,9 +3265,10 @@
3232
3265
  var baseIsRunningAction = node._isRunningAction;
3233
3266
  node._isRunningAction = true;
3234
3267
  var previousContext = currentActionContext;
3268
+ var isRootActionContext = !previousContext;
3235
3269
  currentActionContext = context;
3236
3270
  try {
3237
- return runMiddleWares(node, context, fn);
3271
+ return runMiddleWares(node, context, fn, isRootActionContext);
3238
3272
  }
3239
3273
  finally {
3240
3274
  currentActionContext = previousContext;
@@ -3388,17 +3422,44 @@
3388
3422
  });
3389
3423
  return CollectedMiddlewares;
3390
3424
  }());
3391
- 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
+ }
3392
3453
  var middlewares = new CollectedMiddlewares(node, originalFn);
3393
3454
  // Short circuit
3394
3455
  if (middlewares.isEmpty)
3395
- return mobx.action(originalFn).apply(null, baseCall.args);
3456
+ return runInActionScope(baseCall, originalFn);
3396
3457
  var result = null;
3397
3458
  function runNextMiddleware(call) {
3398
3459
  var middleware = middlewares.getNextMiddleware();
3399
3460
  var handler = middleware && middleware.handler;
3400
3461
  if (!handler) {
3401
- return mobx.action(originalFn).apply(null, call.args);
3462
+ return runInActionScope(call, originalFn);
3402
3463
  }
3403
3464
  // skip hooks if asked to
3404
3465
  if (!middleware.includeHooks && Hook[call.name]) {
@@ -3434,6 +3495,9 @@
3434
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."));
3435
3496
  }
3436
3497
  }
3498
+ if (abortInvoked && (isRootActionContext || !call.parentActionEvent)) {
3499
+ return runInActionScope(call, function () { return result; });
3500
+ }
3437
3501
  return result;
3438
3502
  }
3439
3503
  return runNextMiddleware(baseCall);
@@ -8111,7 +8175,7 @@
8111
8175
  value: function (value, context) {
8112
8176
  return isValidIdentifier(value)
8113
8177
  ? typeCheckSuccess()
8114
- : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a number");
8178
+ : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string, number or a bigint");
8115
8179
  }
8116
8180
  });
8117
8181
  Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
@@ -8200,6 +8264,23 @@
8200
8264
  onRefTargetDestroyedHookDisposer();
8201
8265
  }
8202
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
+ };
8203
8284
  var startWatching = function (sync) {
8204
8285
  // re-create hook in case the stored ref gets reattached
8205
8286
  if (onRefTargetDestroyedHookDisposer) {
@@ -8217,12 +8298,13 @@
8217
8298
  refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
8218
8299
  }
8219
8300
  if (!refTargetNodeExists) {
8220
- // we cannot change the reference in sync mode
8221
- // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
8222
- // for those cases just let the wrong reference be assigned and fail upon usage
8223
- // (like current references do)
8224
- // this means that effectively this code will only run when it is created from a snapshot
8225
- if (!sync) {
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 {
8226
8308
  _this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
8227
8309
  }
8228
8310
  }
@@ -8572,7 +8654,7 @@
8572
8654
  * Inside a state tree, for each type can exist only one instance for each given identifier.
8573
8655
  * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
8574
8656
  * Identifier can be used only as type property of a model.
8575
- * This type accepts as parameter the value type of the identifier field that can be either string or number.
8657
+ * This type accepts as parameter the value type of the identifier field that can be either string, number or bigint.
8576
8658
  *
8577
8659
  * Example:
8578
8660
  * ```ts
@@ -8599,6 +8681,110 @@
8599
8681
  * @returns
8600
8682
  */
8601
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();
8602
8788
  /**
8603
8789
  * Returns if a given value represents an identifier type.
8604
8790
  *
@@ -8620,14 +8806,14 @@
8620
8806
  * @hidden
8621
8807
  */
8622
8808
  function isValidIdentifier(id) {
8623
- return typeof id === "string" || typeof id === "number";
8809
+ return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
8624
8810
  }
8625
8811
  /**
8626
8812
  * @internal
8627
8813
  * @hidden
8628
8814
  */
8629
8815
  function assertIsValidIdentifier(id, argNumber) {
8630
- assertArg(id, isValidIdentifier, "string or number (identifier)", argNumber);
8816
+ assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
8631
8817
  }
8632
8818
 
8633
8819
  /**
@@ -8795,6 +8981,7 @@
8795
8981
  frozen: frozen,
8796
8982
  identifier: identifier,
8797
8983
  identifierNumber: identifierNumber,
8984
+ identifierBigint: identifierBigint,
8798
8985
  late: late,
8799
8986
  lazy: lazy,
8800
8987
  undefined: undefinedType,