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.
@@ -1542,6 +1542,12 @@ var ObjectNode = /** @class */ (function (_super) {
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 @@ var ObjectNode = /** @class */ (function (_super) {
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 @@ var ObjectNode = /** @class */ (function (_super) {
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 @@ function runWithActionContext(context, fn) {
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 @@ var CollectedMiddlewares = /** @class */ (function () {
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 @@ function runMiddleWares(node, baseCall, originalFn) {
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);
@@ -8133,7 +8197,7 @@ var BaseReferenceType = /** @class */ (function (_super) {
8133
8197
  value: function (value, context) {
8134
8198
  return isValidIdentifier(value)
8135
8199
  ? typeCheckSuccess()
8136
- : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a number");
8200
+ : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string, number or a bigint");
8137
8201
  }
8138
8202
  });
8139
8203
  Object.defineProperty(BaseReferenceType.prototype, "fireInvalidated", {
@@ -8222,6 +8286,23 @@ var BaseReferenceType = /** @class */ (function (_super) {
8222
8286
  onRefTargetDestroyedHookDisposer();
8223
8287
  }
8224
8288
  });
8289
+ var scheduleRetryAfterReconciliation = function () {
8290
+ var retry = function () {
8291
+ if (!storedRefNode.isAlive) {
8292
+ return;
8293
+ }
8294
+ if (_this.getSnapshot(storedRefNode) !== identifier) {
8295
+ return;
8296
+ }
8297
+ startWatching(false);
8298
+ };
8299
+ if (getCurrentActionContext()) {
8300
+ storedRefNode.root.enqueueEndOfAction(retry);
8301
+ }
8302
+ else {
8303
+ setImmediateWithFallback(retry);
8304
+ }
8305
+ };
8225
8306
  var startWatching = function (sync) {
8226
8307
  // re-create hook in case the stored ref gets reattached
8227
8308
  if (onRefTargetDestroyedHookDisposer) {
@@ -8239,12 +8320,13 @@ var BaseReferenceType = /** @class */ (function (_super) {
8239
8320
  refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
8240
8321
  }
8241
8322
  if (!refTargetNodeExists) {
8242
- // we cannot change the reference in sync mode
8243
- // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
8244
- // for those cases just let the wrong reference be assigned and fail upon usage
8245
- // (like current references do)
8246
- // this means that effectively this code will only run when it is created from a snapshot
8247
- if (!sync) {
8323
+ // if the tree is already attached we may still be in the middle of a reconcile/applySnapshot,
8324
+ // so wait until the current MST action finishes before deciding whether to invalidate or
8325
+ // attach a watcher to a target that appeared later in the same action.
8326
+ if (sync) {
8327
+ scheduleRetryAfterReconciliation();
8328
+ }
8329
+ else {
8248
8330
  _this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
8249
8331
  }
8250
8332
  }
@@ -8594,7 +8676,7 @@ var IdentifierNumberType = /** @class */ (function (_super) {
8594
8676
  * Inside a state tree, for each type can exist only one instance for each given identifier.
8595
8677
  * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
8596
8678
  * Identifier can be used only as type property of a model.
8597
- * This type accepts as parameter the value type of the identifier field that can be either string or number.
8679
+ * This type accepts as parameter the value type of the identifier field that can be either string, number or bigint.
8598
8680
  *
8599
8681
  * Example:
8600
8682
  * ```ts
@@ -8621,6 +8703,110 @@ var identifier = new IdentifierType();
8621
8703
  * @returns
8622
8704
  */
8623
8705
  var identifierNumber = new IdentifierNumberType();
8706
+ /**
8707
+ * @internal
8708
+ * @hidden
8709
+ * IdentifierBigintType uses SimpleType<bigint | string | number, string, bigint> so snapshots serialize to string (JSON-safe).
8710
+ */
8711
+ var IdentifierBigintType = /** @class */ (function (_super) {
8712
+ __extends(IdentifierBigintType, _super);
8713
+ function IdentifierBigintType() {
8714
+ var _this = _super.call(this, "identifierBigint") || this;
8715
+ Object.defineProperty(_this, "flags", {
8716
+ enumerable: true,
8717
+ configurable: true,
8718
+ writable: true,
8719
+ value: TypeFlags.Identifier
8720
+ });
8721
+ return _this;
8722
+ }
8723
+ Object.defineProperty(IdentifierBigintType.prototype, "createNewInstance", {
8724
+ enumerable: false,
8725
+ configurable: true,
8726
+ writable: true,
8727
+ value: function (snapshot) {
8728
+ if (typeof snapshot === "bigint")
8729
+ return snapshot;
8730
+ return BigInt(snapshot);
8731
+ }
8732
+ });
8733
+ Object.defineProperty(IdentifierBigintType.prototype, "instantiate", {
8734
+ enumerable: false,
8735
+ configurable: true,
8736
+ writable: true,
8737
+ value: function (parent, subpath, environment, initialValue) {
8738
+ if (!parent || !(parent.type instanceof ModelType))
8739
+ throw new MstError("Identifier types can only be instantiated as direct child of a model type");
8740
+ return createScalarNode(this, parent, subpath, environment, initialValue);
8741
+ }
8742
+ });
8743
+ Object.defineProperty(IdentifierBigintType.prototype, "reconcile", {
8744
+ enumerable: false,
8745
+ configurable: true,
8746
+ writable: true,
8747
+ value: function (current, newValue, parent, subpath) {
8748
+ var currentVal = current.storedValue;
8749
+ var newVal = typeof newValue === "bigint" ? newValue : BigInt(newValue);
8750
+ if (currentVal !== newVal)
8751
+ throw new MstError("Tried to change identifier from '".concat(currentVal, "' to '").concat(newVal, "'. Changing identifiers is not allowed."));
8752
+ current.setParent(parent, subpath);
8753
+ return current;
8754
+ }
8755
+ });
8756
+ Object.defineProperty(IdentifierBigintType.prototype, "isValidSnapshot", {
8757
+ enumerable: false,
8758
+ configurable: true,
8759
+ writable: true,
8760
+ value: function (value, context) {
8761
+ if (typeof value === "bigint") {
8762
+ return typeCheckSuccess();
8763
+ }
8764
+ if (typeof value === "string" || typeof value === "number") {
8765
+ try {
8766
+ // BigInt primitive constructor verifies whether the value is a valid integer
8767
+ BigInt(value);
8768
+ return typeCheckSuccess();
8769
+ }
8770
+ catch (e) {
8771
+ var errorMessage = e instanceof Error ? e.message : String(e);
8772
+ return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ": ").concat(errorMessage));
8773
+ }
8774
+ }
8775
+ return typeCheckFailure(context, value, "Value is not a valid ".concat(this.describe(), ", expected a bigint, a string or a number"));
8776
+ }
8777
+ });
8778
+ Object.defineProperty(IdentifierBigintType.prototype, "getSnapshot", {
8779
+ enumerable: false,
8780
+ configurable: true,
8781
+ writable: true,
8782
+ value: function (node) {
8783
+ return String(node.storedValue);
8784
+ }
8785
+ });
8786
+ Object.defineProperty(IdentifierBigintType.prototype, "describe", {
8787
+ enumerable: false,
8788
+ configurable: true,
8789
+ writable: true,
8790
+ value: function () {
8791
+ return "identifierBigint";
8792
+ }
8793
+ });
8794
+ return IdentifierBigintType;
8795
+ }(SimpleType));
8796
+ /**
8797
+ * `types.identifierBigint` - Similar to `types.identifier`. Snapshots serialize to string (JSON-safe) and deserialize from string, number or bigint.
8798
+ *
8799
+ * Example:
8800
+ * ```ts
8801
+ * const Todo = types.model("Todo", {
8802
+ * id: types.identifierBigint,
8803
+ * title: types.string
8804
+ * })
8805
+ * ```
8806
+ *
8807
+ * @returns
8808
+ */
8809
+ var identifierBigint = new IdentifierBigintType();
8624
8810
  /**
8625
8811
  * Returns if a given value represents an identifier type.
8626
8812
  *
@@ -8642,14 +8828,14 @@ function normalizeIdentifier(id) {
8642
8828
  * @hidden
8643
8829
  */
8644
8830
  function isValidIdentifier(id) {
8645
- return typeof id === "string" || typeof id === "number";
8831
+ return typeof id === "string" || typeof id === "number" || typeof id === "bigint";
8646
8832
  }
8647
8833
  /**
8648
8834
  * @internal
8649
8835
  * @hidden
8650
8836
  */
8651
8837
  function assertIsValidIdentifier(id, argNumber) {
8652
- assertArg(id, isValidIdentifier, "string or number (identifier)", argNumber);
8838
+ assertArg(id, isValidIdentifier, "string, number or bigint (identifier)", argNumber);
8653
8839
  }
8654
8840
 
8655
8841
  /**
@@ -8817,6 +9003,7 @@ var types = {
8817
9003
  frozen: frozen,
8818
9004
  identifier: identifier,
8819
9005
  identifierNumber: identifierNumber,
9006
+ identifierBigint: identifierBigint,
8820
9007
  late: late,
8821
9008
  lazy: lazy,
8822
9009
  undefined: undefinedType,