mobx 6.3.2 → 6.3.6

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/mobx.esm.js CHANGED
@@ -512,7 +512,8 @@ function shallowComparer(a, b) {
512
512
  }
513
513
 
514
514
  function defaultComparer(a, b) {
515
- return Object.is(a, b);
515
+ if (Object.is) return Object.is(a, b);
516
+ return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
516
517
  }
517
518
 
518
519
  var comparer = {
@@ -672,7 +673,7 @@ function assertActionDescriptor(adm, _ref, key, _ref2) {
672
673
 
673
674
  function createActionDescriptor(adm, annotation, key, descriptor, // provides ability to disable safeDescriptors for prototypes
674
675
  safeDescriptors) {
675
- var _annotation$options_, _annotation$options_$, _annotation$options_2, _annotation$options_$2, _annotation$options_3;
676
+ var _annotation$options_, _annotation$options_$, _annotation$options_2, _annotation$options_$2, _annotation$options_3, _annotation$options_4, _adm$proxy_2;
676
677
 
677
678
  if (safeDescriptors === void 0) {
678
679
  safeDescriptors = globalState.safeDescriptors;
@@ -688,7 +689,8 @@ safeDescriptors) {
688
689
  }
689
690
 
690
691
  return {
691
- value: createAction((_annotation$options_$ = (_annotation$options_2 = annotation.options_) == null ? void 0 : _annotation$options_2.name) != null ? _annotation$options_$ : key.toString(), value, (_annotation$options_$2 = (_annotation$options_3 = annotation.options_) == null ? void 0 : _annotation$options_3.autoAction) != null ? _annotation$options_$2 : false),
692
+ value: createAction((_annotation$options_$ = (_annotation$options_2 = annotation.options_) == null ? void 0 : _annotation$options_2.name) != null ? _annotation$options_$ : key.toString(), value, (_annotation$options_$2 = (_annotation$options_3 = annotation.options_) == null ? void 0 : _annotation$options_3.autoAction) != null ? _annotation$options_$2 : false, // https://github.com/mobxjs/mobx/discussions/3140
693
+ ((_annotation$options_4 = annotation.options_) == null ? void 0 : _annotation$options_4.bound) ? (_adm$proxy_2 = adm.proxy_) != null ? _adm$proxy_2 : adm.target_ : undefined),
692
694
  // Non-configurable for classes
693
695
  // prevents accidental field redefinition in subclass
694
696
  configurable: safeDescriptors ? adm.isPlainObject_ : true,
@@ -957,6 +959,10 @@ function extend_$5(adm, key, descriptor, proxyTrap) {
957
959
  return observableAnnotation.extend_(adm, key, descriptor, proxyTrap);
958
960
  }
959
961
 
962
+ var OBSERVABLE = "observable";
963
+ var OBSERVABLE_REF = "observable.ref";
964
+ var OBSERVABLE_SHALLOW = "observable.shallow";
965
+ var OBSERVABLE_STRUCT = "observable.struct"; // Predefined bags of create observable options, to avoid allocating temporarily option objects
960
966
  // in the majority of cases
961
967
 
962
968
  var defaultCreateObservableOptions = {
@@ -969,14 +975,14 @@ Object.freeze(defaultCreateObservableOptions);
969
975
  function asCreateObservableOptions(thing) {
970
976
  return thing || defaultCreateObservableOptions;
971
977
  }
972
- var observableAnnotation = /*#__PURE__*/createObservableAnnotation("observable");
973
- var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation("observable.ref", {
978
+ var observableAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE);
979
+ var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_REF, {
974
980
  enhancer: referenceEnhancer
975
981
  });
976
- var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation("observable.shallow", {
982
+ var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_SHALLOW, {
977
983
  enhancer: shallowEnhancer
978
984
  });
979
- var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation("observable.struct", {
985
+ var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_STRUCT, {
980
986
  enhancer: refStructEnhancer
981
987
  });
982
988
  var observableDecoratorAnnotation = /*#__PURE__*/createDecoratorAnnotation(observableAnnotation);
@@ -1531,22 +1537,21 @@ var ComputedValue = /*#__PURE__*/function () {
1531
1537
  /* see #1208 */
1532
1538
  this.dependenciesState_ === IDerivationState_.NOT_TRACKING_;
1533
1539
  var newValue = this.computeValue_(true);
1534
-
1535
- if (process.env.NODE_ENV !== "production" && isSpyEnabled()) {
1536
- spyReport({
1537
- observableKind: "computed",
1538
- debugObjectName: this.name_,
1539
- object: this.scope_,
1540
- type: "update",
1541
- oldValue: this.value_,
1542
- newValue: newValue
1543
- });
1544
- }
1545
-
1546
1540
  var changed = wasSuspended || isCaughtException(oldValue) || isCaughtException(newValue) || !this.equals_(oldValue, newValue);
1547
1541
 
1548
1542
  if (changed) {
1549
1543
  this.value_ = newValue;
1544
+
1545
+ if (process.env.NODE_ENV !== "production" && isSpyEnabled()) {
1546
+ spyReport({
1547
+ observableKind: "computed",
1548
+ debugObjectName: this.name_,
1549
+ object: this.scope_,
1550
+ type: "update",
1551
+ oldValue: oldValue,
1552
+ newValue: newValue
1553
+ });
1554
+ }
1550
1555
  }
1551
1556
 
1552
1557
  return changed;
@@ -1581,6 +1586,10 @@ var ComputedValue = /*#__PURE__*/function () {
1581
1586
  if (!this.keepAlive_) {
1582
1587
  clearObserving(this);
1583
1588
  this.value_ = undefined; // don't hold on to computed value!
1589
+
1590
+ if (process.env.NODE_ENV !== "production" && this.isTracing_ !== TraceMode.NONE) {
1591
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' was suspended and it will recompute on the next access.");
1592
+ }
1584
1593
  }
1585
1594
  };
1586
1595
 
@@ -1614,16 +1623,12 @@ var ComputedValue = /*#__PURE__*/function () {
1614
1623
  _proto.warnAboutUntrackedRead_ = function warnAboutUntrackedRead_() {
1615
1624
  if (!(process.env.NODE_ENV !== "production")) return;
1616
1625
 
1617
- if (this.requiresReaction_ === true) {
1618
- die("[mobx] Computed value " + this.name_ + " is read outside a reactive context");
1619
- }
1620
-
1621
1626
  if (this.isTracing_ !== TraceMode.NONE) {
1622
- console.log("[mobx.trace] '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute");
1627
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1623
1628
  }
1624
1629
 
1625
- if (globalState.computedRequiresReaction) {
1626
- console.warn("[mobx] Computed value " + this.name_ + " is being read outside a reactive context. Doing a full recompute");
1630
+ if (globalState.computedRequiresReaction || this.requiresReaction_) {
1631
+ console.warn("[mobx] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1627
1632
  }
1628
1633
  };
1629
1634
 
@@ -1759,7 +1764,7 @@ function checkIfStateModificationsAreAllowed(atom) {
1759
1764
  }
1760
1765
  function checkIfStateReadsAreAllowed(observable) {
1761
1766
  if (process.env.NODE_ENV !== "production" && !globalState.allowStateReads && globalState.observableRequiresReaction) {
1762
- console.warn("[mobx] Observable " + observable.name_ + " being read outside a reactive context");
1767
+ console.warn("[mobx] Observable '" + observable.name_ + "' being read outside a reactive context.");
1763
1768
  }
1764
1769
  }
1765
1770
  /**
@@ -1804,7 +1809,7 @@ function warnAboutDerivationWithoutDependencies(derivation) {
1804
1809
  if (derivation.observing_.length !== 0) return;
1805
1810
 
1806
1811
  if (globalState.reactionRequiresObservable || derivation.requiresObservable_) {
1807
- console.warn("[mobx] Derivation " + derivation.name_ + " is created/updated without reading any observable value");
1812
+ console.warn("[mobx] Derivation '" + derivation.name_ + "' is created/updated without reading any observable value.");
1808
1813
  }
1809
1814
  }
1810
1815
  /**
@@ -1970,6 +1975,8 @@ var globalState = /*#__PURE__*/function () {
1970
1975
  if (global.__mobxGlobals && global.__mobxGlobals.version !== new MobXGlobals().version) canMergeGlobalState = false;
1971
1976
 
1972
1977
  if (!canMergeGlobalState) {
1978
+ // Because this is a IIFE we need to let isolateCalled a chance to change
1979
+ // so we run it after the event loop completed at least 1 iteration
1973
1980
  setTimeout(function () {
1974
1981
  if (!isolateCalled) {
1975
1982
  die(35);
@@ -2217,8 +2224,7 @@ function printDepTree(tree, lines, depth) {
2217
2224
  return;
2218
2225
  }
2219
2226
 
2220
- lines.push("" + new Array(depth).join("\t") + tree.name); // MWE: not the fastest, but the easiest way :)
2221
-
2227
+ lines.push("" + "\t".repeat(depth - 1) + tree.name);
2222
2228
  if (tree.dependencies) tree.dependencies.forEach(function (child) {
2223
2229
  return printDepTree(child, lines, depth + 1);
2224
2230
  });
@@ -2655,8 +2661,7 @@ function reaction(expression, effect, opts) {
2655
2661
  var firstTime = true;
2656
2662
  var isScheduled = false;
2657
2663
  var value;
2658
- var oldValue = undefined; // only an issue with fireImmediately
2659
-
2664
+ var oldValue;
2660
2665
  var equals = opts.compareStructural ? comparer.structural : opts.equals || comparer["default"];
2661
2666
  var r = new Reaction(name, function () {
2662
2667
  if (firstTime || runSync) {
@@ -2772,7 +2777,7 @@ function extendObservable(target, properties, annotations, options) {
2772
2777
  if (arguments.length > 4) die("'extendObservable' expected 2-4 arguments");
2773
2778
  if (typeof target !== "object") die("'extendObservable' expects an object as first argument");
2774
2779
  if (isObservableMap(target)) die("'extendObservable' should not be used on maps, use map.merge instead");
2775
- if (!isPlainObject(properties)) die("'extendObservabe' only accepts plain objects as second argument");
2780
+ if (!isPlainObject(properties)) die("'extendObservable' only accepts plain objects as second argument");
2776
2781
  if (isObservable(properties) || isObservable(annotations)) die("Extending an object with another observable (object) is not supported");
2777
2782
  } // Pull descriptors first, so we don't have to deal with props added by administration ($mobx)
2778
2783
 
@@ -2963,14 +2968,14 @@ function interceptProperty(thing, property, handler) {
2963
2968
  }
2964
2969
 
2965
2970
  function _isComputed(value, property) {
2966
- if (property !== undefined) {
2967
- if (isObservableObject(value) === false) return false;
2968
- if (!value[$mobx].values_.has(property)) return false;
2969
- var atom = getAtom(value, property);
2970
- return isComputedValue(atom);
2971
+ if (property === undefined) {
2972
+ return isComputedValue(value);
2971
2973
  }
2972
2974
 
2973
- return isComputedValue(value);
2975
+ if (isObservableObject(value) === false) return false;
2976
+ if (!value[$mobx].values_.has(property)) return false;
2977
+ var atom = getAtom(value, property);
2978
+ return isComputedValue(atom);
2974
2979
  }
2975
2980
  function isComputed(value) {
2976
2981
  if (process.env.NODE_ENV !== "production" && arguments.length > 1) return die("isComputed expects only 1 argument. Use isComputedProp to inspect the observability of a property");
@@ -3295,10 +3300,10 @@ function _when(predicate, effect, opts) {
3295
3300
  var timeoutHandle;
3296
3301
 
3297
3302
  if (typeof opts.timeout === "number") {
3303
+ var error = new Error("WHEN_TIMEOUT");
3298
3304
  timeoutHandle = setTimeout(function () {
3299
3305
  if (!disposer[$mobx].isDisposed_) {
3300
3306
  disposer();
3301
- var error = new Error("WHEN_TIMEOUT");
3302
3307
  if (opts.onError) opts.onError(error);else throw error;
3303
3308
  }
3304
3309
  }, opts.timeout);
@@ -3330,7 +3335,7 @@ function whenPromise(predicate, opts) {
3330
3335
 
3331
3336
  cancel = function cancel() {
3332
3337
  disposer();
3333
- reject("WHEN_CANCELLED");
3338
+ reject(new Error("WHEN_CANCELLED"));
3334
3339
  };
3335
3340
  });
3336
3341
  res.cancel = cancel;
@@ -3461,7 +3466,11 @@ function makeObservable(target, annotations, options) {
3461
3466
  try {
3462
3467
  var _annotations;
3463
3468
 
3464
- // Default to decorators
3469
+ if (process.env.NODE_ENV !== "production" && annotations && target[storedAnnotationsSymbol]) {
3470
+ die("makeObservable second arg must be nullish when using decorators. Mixing @decorator syntax with annotations is not supported.");
3471
+ } // Default to decorators
3472
+
3473
+
3465
3474
  (_annotations = annotations) != null ? _annotations : annotations = collectStoredAnnotations(target); // Annotate
3466
3475
 
3467
3476
  ownKeys(annotations).forEach(function (key) {
@@ -3622,7 +3631,7 @@ var ObservableArrayAdministration = /*#__PURE__*/function () {
3622
3631
  };
3623
3632
 
3624
3633
  _proto.setArrayLength_ = function setArrayLength_(newLength) {
3625
- if (typeof newLength !== "number" || newLength < 0) die("Out of range: " + newLength);
3634
+ if (typeof newLength !== "number" || isNaN(newLength) || newLength < 0) die("Out of range: " + newLength);
3626
3635
  var currentLength = this.values_.length;
3627
3636
  if (newLength === currentLength) return;else if (newLength > currentLength) {
3628
3637
  var newItems = new Array(newLength - currentLength);
@@ -4111,9 +4120,11 @@ var ObservableMap = /*#__PURE__*/function () {
4111
4120
 
4112
4121
  if (process.env.NODE_ENV !== "production" && notifySpy) spyReportStart(_change);
4113
4122
  transaction(function () {
4123
+ var _this2$hasMap_$get;
4124
+
4114
4125
  _this2.keysAtom_.reportChanged();
4115
4126
 
4116
- _this2.updateHasMapEntry_(key, false);
4127
+ (_this2$hasMap_$get = _this2.hasMap_.get(key)) == null ? void 0 : _this2$hasMap_$get.setNewValue_(false);
4117
4128
 
4118
4129
  var observable = _this2.data_.get(key);
4119
4130
 
@@ -4129,14 +4140,6 @@ var ObservableMap = /*#__PURE__*/function () {
4129
4140
  return false;
4130
4141
  };
4131
4142
 
4132
- _proto.updateHasMapEntry_ = function updateHasMapEntry_(key, value) {
4133
- var entry = this.hasMap_.get(key);
4134
-
4135
- if (entry) {
4136
- entry.setNewValue_(value);
4137
- }
4138
- };
4139
-
4140
4143
  _proto.updateValue_ = function updateValue_(key, newValue) {
4141
4144
  var observable = this.data_.get(key);
4142
4145
  newValue = observable.prepareNewValue_(newValue);
@@ -4165,13 +4168,15 @@ var ObservableMap = /*#__PURE__*/function () {
4165
4168
 
4166
4169
  checkIfStateModificationsAreAllowed(this.keysAtom_);
4167
4170
  transaction(function () {
4171
+ var _this3$hasMap_$get;
4172
+
4168
4173
  var observable = new ObservableValue(newValue, _this3.enhancer_, process.env.NODE_ENV !== "production" ? _this3.name_ + "." + stringifyKey(key) : "ObservableMap.key", false);
4169
4174
 
4170
4175
  _this3.data_.set(key, observable);
4171
4176
 
4172
4177
  newValue = observable.value_; // value might have been changed
4173
4178
 
4174
- _this3.updateHasMapEntry_(key, true);
4179
+ (_this3$hasMap_$get = _this3.hasMap_.get(key)) == null ? void 0 : _this3$hasMap_$get.setNewValue_(true);
4175
4180
 
4176
4181
  _this3.keysAtom_.reportChanged();
4177
4182
  });
@@ -5748,7 +5753,7 @@ function isAnnotation(thing) {
5748
5753
  * - utils/ Utility stuff.
5749
5754
  *
5750
5755
  */
5751
- ["Symbol", "Map", "Set", "Symbol"].forEach(function (m) {
5756
+ ["Symbol", "Map", "Set"].forEach(function (m) {
5752
5757
  var g = getGlobal();
5753
5758
 
5754
5759
  if (typeof g[m] === "undefined") {