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.
@@ -791,7 +791,7 @@ function addDisposer(target, disposer) {
791
791
  }
792
792
  /**
793
793
  * Returns the environment of the current state tree, or throws. For more info on environments,
794
- * see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
794
+ * see [Dependency injection](/concepts/dependency-injection)
795
795
  *
796
796
  * Please note that in child nodes access to the root is only possible
797
797
  * once the `afterAttach` hook has fired
@@ -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,
@@ -2165,6 +2198,17 @@ var ObjectNode = /** @class */ (function (_super) {
2165
2198
  return this._internalEventsRegister(InternalEvents.Patch, handler);
2166
2199
  }
2167
2200
  });
2201
+ Object.defineProperty(ObjectNode.prototype, "hasPatchSubscribers", {
2202
+ enumerable: false,
2203
+ configurable: true,
2204
+ writable: true,
2205
+ value: function () {
2206
+ if (this._internalEventsHasSubscribers(InternalEvents.Patch)) {
2207
+ return true;
2208
+ }
2209
+ return this.parent ? this.parent.hasPatchSubscribers() : false;
2210
+ }
2211
+ });
2168
2212
  Object.defineProperty(ObjectNode.prototype, "emitPatch", {
2169
2213
  enumerable: false,
2170
2214
  configurable: true,
@@ -2377,6 +2421,7 @@ var TypeFlags;
2377
2421
  TypeFlags[TypeFlags["Lazy"] = 1048576] = "Lazy";
2378
2422
  TypeFlags[TypeFlags["Finite"] = 2097152] = "Finite";
2379
2423
  TypeFlags[TypeFlags["Float"] = 4194304] = "Float";
2424
+ TypeFlags[TypeFlags["BigInt"] = 8388608] = "BigInt";
2380
2425
  })(TypeFlags || (TypeFlags = {}));
2381
2426
  /**
2382
2427
  * @internal
@@ -2693,6 +2738,38 @@ var SimpleType = /** @class */ (function (_super) {
2693
2738
  });
2694
2739
  return SimpleType;
2695
2740
  }(BaseType));
2741
+ function canApplyDirectSnapshot(childType, childNode, newValue) {
2742
+ var reconciliationType = childNode.getReconciliationType();
2743
+ var expectedReconciliationType = resolveDirectApplyType(childType);
2744
+ // Only types that reconcile as the same transparent-wrapper-adjusted type can safely reuse
2745
+ // the existing child node. Wrappers with their own validation or snapshot semantics
2746
+ // intentionally keep a distinct reconciliation type so they fall back to the validated path.
2747
+ var hasMatchingReconciliationType = reconciliationType === expectedReconciliationType;
2748
+ return (childNode instanceof ObjectNode &&
2749
+ hasMatchingReconciliationType &&
2750
+ reconciliationType instanceof ComplexType &&
2751
+ isMutable(newValue) &&
2752
+ !isStateTreeNode(newValue) &&
2753
+ reconciliationType.isMatchingSnapshotId(childNode, newValue));
2754
+ }
2755
+ function resolveDirectApplyType(type) {
2756
+ var subTypes = type.getSubTypes();
2757
+ if (!subTypes ||
2758
+ Array.isArray(subTypes) ||
2759
+ subTypes === cannotDetermineSubtype ||
2760
+ !canUnwrapDirectApplyType(type)) {
2761
+ return type;
2762
+ }
2763
+ // Only unwrap wrappers that are transparent for direct apply. Wrappers with their own
2764
+ // validation or snapshot processing must keep their wrapper identity here.
2765
+ return resolveDirectApplyType(subTypes);
2766
+ }
2767
+ function canUnwrapDirectApplyType(type) {
2768
+ var transparentWrapperFlags = TypeFlags.Optional | TypeFlags.Late;
2769
+ var nonTransparentWrapperFlags = TypeFlags.Refinement | TypeFlags.SnapshotProcessor | TypeFlags.Union;
2770
+ return ((type.flags & transparentWrapperFlags) > 0 &&
2771
+ (type.flags & nonTransparentWrapperFlags) === 0);
2772
+ }
2696
2773
  /**
2697
2774
  * Returns if a given value represents a type.
2698
2775
  *
@@ -3184,9 +3261,10 @@ function runWithActionContext(context, fn) {
3184
3261
  var baseIsRunningAction = node._isRunningAction;
3185
3262
  node._isRunningAction = true;
3186
3263
  var previousContext = currentActionContext;
3264
+ var isRootActionContext = !previousContext;
3187
3265
  currentActionContext = context;
3188
3266
  try {
3189
- return runMiddleWares(node, context, fn);
3267
+ return runMiddleWares(node, context, fn, isRootActionContext);
3190
3268
  }
3191
3269
  finally {
3192
3270
  currentActionContext = previousContext;
@@ -3340,17 +3418,44 @@ var CollectedMiddlewares = /** @class */ (function () {
3340
3418
  });
3341
3419
  return CollectedMiddlewares;
3342
3420
  }());
3343
- 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
+ }
3344
3449
  var middlewares = new CollectedMiddlewares(node, originalFn);
3345
3450
  // Short circuit
3346
3451
  if (middlewares.isEmpty)
3347
- return action(originalFn).apply(null, baseCall.args);
3452
+ return runInActionScope(baseCall, originalFn);
3348
3453
  var result = null;
3349
3454
  function runNextMiddleware(call) {
3350
3455
  var middleware = middlewares.getNextMiddleware();
3351
3456
  var handler = middleware && middleware.handler;
3352
3457
  if (!handler) {
3353
- return action(originalFn).apply(null, call.args);
3458
+ return runInActionScope(call, originalFn);
3354
3459
  }
3355
3460
  // skip hooks if asked to
3356
3461
  if (!middleware.includeHooks && Hook[call.name]) {
@@ -3386,6 +3491,9 @@ function runMiddleWares(node, baseCall, originalFn) {
3386
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."));
3387
3492
  }
3388
3493
  }
3494
+ if (abortInvoked && (isRootActionContext || !call.parentActionEvent)) {
3495
+ return runInActionScope(call, function () { return result; });
3496
+ }
3389
3497
  return result;
3390
3498
  }
3391
3499
  return runNextMiddleware(baseCall);
@@ -4042,6 +4150,7 @@ function isPrimitive(value, includeDate) {
4042
4150
  typeof value === "string" ||
4043
4151
  typeof value === "number" ||
4044
4152
  typeof value === "boolean" ||
4153
+ typeof value === "bigint" ||
4045
4154
  (includeDate && value instanceof Date));
4046
4155
  }
4047
4156
  /**
@@ -4926,6 +5035,8 @@ function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
4926
5035
  /**
4927
5036
  * `types.snapshotProcessor` - Runs a pre/post snapshot processor before/after serializing a given type.
4928
5037
  *
5038
+ * [See known issue with `applySnapshot` and `preProcessSnapshot`](https://github.com/mobxjs/mobx-state-tree/issues/1317)
5039
+ *
4929
5040
  * Example:
4930
5041
  * ```ts
4931
5042
  * const Todo1 = types.model({ text: types.string })
@@ -5376,23 +5487,33 @@ var MapType = /** @class */ (function (_super) {
5376
5487
  configurable: true,
5377
5488
  writable: true,
5378
5489
  value: function (node, snapshot) {
5379
- typecheckInternal(this, snapshot);
5490
+ if (!isPlainObject(snapshot)) {
5491
+ typecheckInternal(this, snapshot);
5492
+ return;
5493
+ }
5380
5494
  var target = node.storedValue;
5381
- var currentKeys = {};
5495
+ var childType = this.getChildType();
5382
5496
  Array.from(target.keys()).forEach(function (key) {
5383
- currentKeys[key] = false;
5384
- });
5385
- if (snapshot) {
5386
- // Don't use target.replace, as it will throw away all existing items first
5387
- for (var key in snapshot) {
5388
- target.set(key, snapshot[key]);
5389
- currentKeys["" + key] = true;
5390
- }
5391
- }
5392
- Object.keys(currentKeys).forEach(function (key) {
5393
- if (currentKeys[key] === false)
5497
+ if (!Object.prototype.hasOwnProperty.call(snapshot, key)) {
5394
5498
  target.delete(key);
5499
+ return;
5500
+ }
5501
+ var childNode = node.getChildNode(key);
5502
+ var newValue = snapshot[key];
5503
+ if (childNode.snapshot === newValue)
5504
+ return;
5505
+ if (canApplyDirectSnapshot(childType, childNode, newValue)) {
5506
+ childNode.applySnapshot(newValue);
5507
+ }
5508
+ else {
5509
+ target.set(key, newValue);
5510
+ }
5395
5511
  });
5512
+ for (var key in snapshot) {
5513
+ if (!Object.prototype.hasOwnProperty.call(snapshot, key) || target.has(key))
5514
+ continue;
5515
+ target.set(key, snapshot[key]);
5516
+ }
5396
5517
  }
5397
5518
  });
5398
5519
  Object.defineProperty(MapType.prototype, "getChildType", {
@@ -5718,9 +5839,52 @@ var ArrayType = /** @class */ (function (_super) {
5718
5839
  configurable: true,
5719
5840
  writable: true,
5720
5841
  value: function (node, snapshot) {
5721
- typecheckInternal(this, snapshot);
5842
+ if (!isArray(snapshot)) {
5843
+ typecheckInternal(this, snapshot);
5844
+ return;
5845
+ }
5722
5846
  var target = node.storedValue;
5723
- target.replace(snapshot);
5847
+ var childNodes = node.getChildren();
5848
+ var oldLength = childNodes.length;
5849
+ var newLength = snapshot.length;
5850
+ var childType = this.getChildType();
5851
+ var minLength = Math.min(oldLength, newLength);
5852
+ var firstChangedIndex = findFirstChangedIndex(childNodes, snapshot, minLength);
5853
+ // If all array items are the same and the length did not change, there is nothing to do.
5854
+ if (firstChangedIndex === oldLength && firstChangedIndex === newLength) {
5855
+ return;
5856
+ }
5857
+ // Preserve the old "always replace" behavior for length changes and when patches are being
5858
+ // observed, since those cases are more sensitive to patch shape (for example, splice
5859
+ // would otherwise emit add/remove patch sequences) and benchmark variance.
5860
+ if (oldLength !== newLength || node.hasPatchSubscribers()) {
5861
+ target.replace(snapshot);
5862
+ return;
5863
+ }
5864
+ // When every changed entry can reuse its existing child node, update them in place
5865
+ // instead of going through array replacement/splice.
5866
+ if (canApplyDirectSnapshotsInRange(childType, childNodes, snapshot, firstChangedIndex)) {
5867
+ applyDirectSnapshotsInRange(childNodes, snapshot, firstChangedIndex);
5868
+ return;
5869
+ }
5870
+ var oldEnd = oldLength - 1;
5871
+ var newEnd = newLength - 1;
5872
+ while (oldEnd >= firstChangedIndex &&
5873
+ newEnd >= firstChangedIndex &&
5874
+ childNodes[oldEnd].snapshot === snapshot[newEnd]) {
5875
+ oldEnd--;
5876
+ newEnd--;
5877
+ }
5878
+ // Trim the unchanged suffix too, so we only replace the minimal changed window.
5879
+ var replacedCount = oldEnd >= firstChangedIndex ? oldEnd - firstChangedIndex + 1 : 0;
5880
+ var replacementSnapshots = newEnd >= firstChangedIndex
5881
+ ? snapshot.slice(firstChangedIndex, newEnd + 1)
5882
+ : EMPTY_ARRAY;
5883
+ if (replacedCount === 1 && replacementSnapshots.length === 1) {
5884
+ target[firstChangedIndex] = replacementSnapshots[0];
5885
+ return;
5886
+ }
5887
+ target.splice.apply(target, __spreadArray([firstChangedIndex, replacedCount], __read(replacementSnapshots), false));
5724
5888
  }
5725
5889
  });
5726
5890
  Object.defineProperty(ArrayType.prototype, "getChildType", {
@@ -5792,6 +5956,31 @@ function array(subtype) {
5792
5956
  assertIsType(subtype, 1);
5793
5957
  return new ArrayType("".concat(subtype.name, "[]"), subtype);
5794
5958
  }
5959
+ function findFirstChangedIndex(childNodes, snapshot, length) {
5960
+ var index = 0;
5961
+ while (index < length && childNodes[index].snapshot === snapshot[index]) {
5962
+ index++;
5963
+ }
5964
+ return index;
5965
+ }
5966
+ function canApplyDirectSnapshotsInRange(childType, childNodes, snapshot, startIndex) {
5967
+ for (var i = startIndex; i < childNodes.length; i++) {
5968
+ if (childNodes[i].snapshot === snapshot[i])
5969
+ continue;
5970
+ if (!canApplyDirectSnapshot(childType, childNodes[i], snapshot[i])) {
5971
+ return false;
5972
+ }
5973
+ }
5974
+ return true;
5975
+ }
5976
+ function applyDirectSnapshotsInRange(childNodes, snapshot, startIndex) {
5977
+ for (var i = startIndex; i < childNodes.length; i++) {
5978
+ var childNode = childNodes[i];
5979
+ if (childNode.snapshot === snapshot[i])
5980
+ continue;
5981
+ childNode.applySnapshot(snapshot[i]);
5982
+ }
5983
+ }
5795
5984
  function reconcileArrayChildren(parent, childType, oldNodes, newValues, newPaths) {
5796
5985
  var nothingChanged = true;
5797
5986
  for (var i = 0;; i++) {
@@ -6437,10 +6626,25 @@ var ModelType = /** @class */ (function (_super) {
6437
6626
  configurable: true,
6438
6627
  writable: true,
6439
6628
  value: function (node, snapshot) {
6440
- typecheckInternal(this, snapshot);
6441
6629
  var preProcessedSnapshot = this.applySnapshotPreProcessor(snapshot);
6442
- this.forAllProps(function (name) {
6443
- node.storedValue[name] = preProcessedSnapshot[name];
6630
+ var isPreProcessedSnapshotNonPlain = !isPlainObject(preProcessedSnapshot);
6631
+ // Fast-path plain-object snapshots, but still validate when preprocessing is required
6632
+ // or when preprocessing produces an invalid model snapshot.
6633
+ if (!isPlainObject(snapshot) || isPreProcessedSnapshotNonPlain) {
6634
+ typecheckInternal(this, snapshot);
6635
+ if (isPreProcessedSnapshotNonPlain)
6636
+ return;
6637
+ }
6638
+ this.forAllProps(function (name, childType) {
6639
+ var childNode = node.getChildNode(name);
6640
+ var newValue = preProcessedSnapshot[name];
6641
+ if (childNode.snapshot === newValue)
6642
+ return;
6643
+ if (canApplyDirectSnapshot(childType, childNode, newValue)) {
6644
+ childNode.applySnapshot(newValue);
6645
+ return;
6646
+ }
6647
+ node.storedValue[name] = newValue;
6444
6648
  });
6445
6649
  }
6446
6650
  });
@@ -6731,6 +6935,37 @@ var float = new CoreType("float", TypeFlags.Float, function (v) { return isFloat
6731
6935
  */
6732
6936
  // tslint:disable-next-line:variable-name
6733
6937
  var finite = new CoreType("finite", TypeFlags.Finite, function (v) { return isFinite(v); });
6938
+ var _BigIntPrimitive = new CoreType("bigint", TypeFlags.BigInt, function (v) {
6939
+ if (typeof v === "bigint") {
6940
+ return true;
6941
+ }
6942
+ if (typeof v === "string" || typeof v === "number") {
6943
+ try {
6944
+ // BigInt primitive constructor verifies whether the value is a valid integer
6945
+ BigInt(v);
6946
+ return true;
6947
+ }
6948
+ catch (_a) { }
6949
+ }
6950
+ return false;
6951
+ }, function (v) { return (typeof v === "bigint" ? v : BigInt(v)); });
6952
+ _BigIntPrimitive.getSnapshot = function (node) {
6953
+ return String(node.storedValue);
6954
+ };
6955
+ /**
6956
+ * `types.bigint` - Creates a type that can only contain a bigint value.
6957
+ * Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
6958
+ *
6959
+ * Example:
6960
+ * ```ts
6961
+ * const BigId = types.model({
6962
+ * id: types.identifier,
6963
+ * value: types.bigint
6964
+ * })
6965
+ * getSnapshot(store).value // "0" (string, JSON-safe)
6966
+ * ```
6967
+ */
6968
+ var bigint = _BigIntPrimitive;
6734
6969
  /**
6735
6970
  * `types.boolean` - Creates a type that can only contain a boolean value.
6736
6971
  * This type is used for boolean values by default
@@ -6782,6 +7017,8 @@ function getPrimitiveFactoryFromValue(value) {
6782
7017
  return number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
6783
7018
  case "boolean":
6784
7019
  return boolean;
7020
+ case "bigint":
7021
+ return bigint;
6785
7022
  case "object":
6786
7023
  if (value instanceof Date)
6787
7024
  return DatePrimitive;
@@ -6801,7 +7038,8 @@ function isPrimitiveType(type) {
6801
7038
  TypeFlags.Number |
6802
7039
  TypeFlags.Integer |
6803
7040
  TypeFlags.Boolean |
6804
- TypeFlags.Date)) >
7041
+ TypeFlags.Date |
7042
+ TypeFlags.BigInt)) >
6805
7043
  0);
6806
7044
  }
6807
7045
 
@@ -7955,7 +8193,7 @@ var BaseReferenceType = /** @class */ (function (_super) {
7955
8193
  value: function (value, context) {
7956
8194
  return isValidIdentifier(value)
7957
8195
  ? typeCheckSuccess()
7958
- : 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");
7959
8197
  }
7960
8198
  });
7961
8199
  Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
@@ -8044,6 +8282,23 @@ var BaseReferenceType = /** @class */ (function (_super) {
8044
8282
  onRefTargetDestroyedHookDisposer();
8045
8283
  }
8046
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
+ };
8047
8302
  var startWatching = function (sync) {
8048
8303
  // re-create hook in case the stored ref gets reattached
8049
8304
  if (onRefTargetDestroyedHookDisposer) {
@@ -8061,12 +8316,13 @@ var BaseReferenceType = /** @class */ (function (_super) {
8061
8316
  refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
8062
8317
  }
8063
8318
  if (!refTargetNodeExists) {
8064
- // we cannot change the reference in sync mode
8065
- // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
8066
- // for those cases just let the wrong reference be assigned and fail upon usage
8067
- // (like current references do)
8068
- // this means that effectively this code will only run when it is created from a snapshot
8069
- 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 {
8070
8326
  _this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
8071
8327
  }
8072
8328
  }
@@ -8416,7 +8672,7 @@ var IdentifierNumberType = /** @class */ (function (_super) {
8416
8672
  * Inside a state tree, for each type can exist only one instance for each given identifier.
8417
8673
  * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
8418
8674
  * Identifier can be used only as type property of a model.
8419
- * 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.
8420
8676
  *
8421
8677
  * Example:
8422
8678
  * ```ts
@@ -8443,6 +8699,110 @@ var identifier = new IdentifierType();
8443
8699
  * @returns
8444
8700
  */
8445
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();
8446
8806
  /**
8447
8807
  * Returns if a given value represents an identifier type.
8448
8808
  *
@@ -8464,14 +8824,14 @@ function normalizeIdentifier(id) {
8464
8824
  * @hidden
8465
8825
  */
8466
8826
  function isValidIdentifier(id) {
8467
- return typeof id === "string" || typeof id === "number";
8827
+ return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
8468
8828
  }
8469
8829
  /**
8470
8830
  * @internal
8471
8831
  * @hidden
8472
8832
  */
8473
8833
  function assertIsValidIdentifier(id, argNumber) {
8474
- assertArg(id, isValidIdentifier, "string or number (identifier)", argNumber);
8834
+ assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
8475
8835
  }
8476
8836
 
8477
8837
  /**
@@ -8632,12 +8992,14 @@ var types = {
8632
8992
  integer: integer,
8633
8993
  float: float,
8634
8994
  finite: finite,
8995
+ bigint: bigint,
8635
8996
  Date: DatePrimitive,
8636
8997
  map: map,
8637
8998
  array: array,
8638
8999
  frozen: frozen,
8639
9000
  identifier: identifier,
8640
9001
  identifierNumber: identifierNumber,
9002
+ identifierBigint: identifierBigint,
8641
9003
  late: late,
8642
9004
  lazy: lazy,
8643
9005
  undefined: undefinedType,