mobx 6.10.0 → 6.10.1

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
@@ -423,12 +423,14 @@ var Atom = /*#__PURE__*/function () {
423
423
  this.isPendingUnobservation_ = false;
424
424
  this.isBeingObserved_ = false;
425
425
  this.observers_ = new Set();
426
+ this.batchId_ = void 0;
426
427
  this.diffValue_ = 0;
427
428
  this.lastAccessedBy_ = 0;
428
429
  this.lowestObserverState_ = IDerivationState_.NOT_TRACKING_;
429
430
  this.onBOL = void 0;
430
431
  this.onBUOL = void 0;
431
432
  this.name_ = name_;
433
+ this.batchId_ = globalState.inBatch ? globalState.batchId : NaN;
432
434
  }
433
435
  // onBecomeObservedListeners
434
436
  var _proto = Atom.prototype;
@@ -457,6 +459,12 @@ var Atom = /*#__PURE__*/function () {
457
459
  * Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
458
460
  */;
459
461
  _proto.reportChanged = function reportChanged() {
462
+ if (globalState.inBatch && this.batchId_ === globalState.batchId) {
463
+ // Called from the same batch this atom was created in.
464
+ return;
465
+ }
466
+ // Avoids the possibility of hitting the same globalState.batchId when it cycled through all integers (necessary?)
467
+ this.batchId_ = NaN;
460
468
  startBatch();
461
469
  propagateChanged(this);
462
470
  // We could update state version only at the end of batch,
@@ -990,7 +998,9 @@ var observableFactories = {
990
998
  return new ObservableSet(initialValues, getEnhancerFromOptions(o), o.name);
991
999
  },
992
1000
  object: function object(props, decorators, options) {
993
- return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1001
+ return initObservable(function () {
1002
+ return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1003
+ });
994
1004
  },
995
1005
  ref: /*#__PURE__*/createDecoratorAnnotation(observableRefAnnotation),
996
1006
  shallow: /*#__PURE__*/createDecoratorAnnotation(observableShallowAnnotation),
@@ -1821,6 +1831,7 @@ var MobXGlobals = function MobXGlobals() {
1821
1831
  this.runId = 0;
1822
1832
  this.mobxGuid = 0;
1823
1833
  this.inBatch = 0;
1834
+ this.batchId = Number.MIN_SAFE_INTEGER;
1824
1835
  this.pendingUnobservations = [];
1825
1836
  this.pendingReactions = [];
1826
1837
  this.isRunningReactions = false;
@@ -1960,6 +1971,9 @@ function queueForUnobservation(observable) {
1960
1971
  * Avoids unnecessary recalculations.
1961
1972
  */
1962
1973
  function startBatch() {
1974
+ if (globalState.inBatch === 0) {
1975
+ globalState.batchId = globalState.batchId < Number.MAX_SAFE_INTEGER ? globalState.batchId + 1 : Number.MIN_SAFE_INTEGER;
1976
+ }
1963
1977
  globalState.inBatch++;
1964
1978
  }
1965
1979
  function endBatch() {
@@ -2647,17 +2661,14 @@ function extendObservable(target, properties, annotations, options) {
2647
2661
  }
2648
2662
  // Pull descriptors first, so we don't have to deal with props added by administration ($mobx)
2649
2663
  var descriptors = getOwnPropertyDescriptors(properties);
2650
- var adm = asObservableObject(target, options)[$mobx];
2651
- startBatch();
2652
- try {
2664
+ initObservable(function () {
2665
+ var adm = asObservableObject(target, options)[$mobx];
2653
2666
  ownKeys(descriptors).forEach(function (key) {
2654
2667
  adm.extend_(key, descriptors[key],
2655
2668
  // must pass "undefined" for { key: undefined }
2656
2669
  !annotations ? true : key in annotations ? annotations[key] : true);
2657
2670
  });
2658
- } finally {
2659
- endBatch();
2660
- }
2671
+ });
2661
2672
  return target;
2662
2673
  }
2663
2674
 
@@ -3346,10 +3357,9 @@ function notifyListeners(listenable, change) {
3346
3357
  }
3347
3358
 
3348
3359
  function makeObservable(target, annotations, options) {
3349
- var adm = asObservableObject(target, options)[$mobx];
3350
- startBatch();
3351
- try {
3360
+ initObservable(function () {
3352
3361
  var _annotations;
3362
+ var adm = asObservableObject(target, options)[$mobx];
3353
3363
  if (process.env.NODE_ENV !== "production" && annotations && target[storedAnnotationsSymbol]) {
3354
3364
  die("makeObservable second arg must be nullish when using decorators. Mixing @decorator syntax with annotations is not supported.");
3355
3365
  }
@@ -3359,9 +3369,7 @@ function makeObservable(target, annotations, options) {
3359
3369
  ownKeys(annotations).forEach(function (key) {
3360
3370
  return adm.make_(key, annotations[key]);
3361
3371
  });
3362
- } finally {
3363
- endBatch();
3364
- }
3372
+ });
3365
3373
  return target;
3366
3374
  }
3367
3375
  // proto[keysSymbol] = new Set<PropertyKey>()
@@ -3380,26 +3388,23 @@ function makeAutoObservable(target, overrides, options) {
3380
3388
  if (isPlainObject(target)) {
3381
3389
  return extendObservable(target, target, overrides, options);
3382
3390
  }
3383
- var adm = asObservableObject(target, options)[$mobx];
3384
- // Optimization: cache keys on proto
3385
- // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3386
- if (!target[keysSymbol]) {
3387
- var proto = Object.getPrototypeOf(target);
3388
- var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3389
- keys["delete"]("constructor");
3390
- keys["delete"]($mobx);
3391
- addHiddenProp(proto, keysSymbol, keys);
3392
- }
3393
- startBatch();
3394
- try {
3391
+ initObservable(function () {
3392
+ var adm = asObservableObject(target, options)[$mobx];
3393
+ // Optimization: cache keys on proto
3394
+ // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3395
+ if (!target[keysSymbol]) {
3396
+ var proto = Object.getPrototypeOf(target);
3397
+ var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3398
+ keys["delete"]("constructor");
3399
+ keys["delete"]($mobx);
3400
+ addHiddenProp(proto, keysSymbol, keys);
3401
+ }
3395
3402
  target[keysSymbol].forEach(function (key) {
3396
3403
  return adm.make_(key,
3397
3404
  // must pass "undefined" for { key: undefined }
3398
3405
  !overrides ? true : key in overrides ? overrides[key] : true);
3399
3406
  });
3400
- } finally {
3401
- endBatch();
3402
- }
3407
+ });
3403
3408
  return target;
3404
3409
  }
3405
3410
 
@@ -3707,16 +3712,16 @@ function createObservableArray(initialValues, enhancer, name, owned) {
3707
3712
  owned = false;
3708
3713
  }
3709
3714
  assertProxies();
3710
- var adm = new ObservableArrayAdministration(name, enhancer, owned, false);
3711
- addHiddenFinalProp(adm.values_, $mobx, adm);
3712
- var proxy = new Proxy(adm.values_, arrayTraps);
3713
- adm.proxy_ = proxy;
3714
- if (initialValues && initialValues.length) {
3715
- var prev = allowStateChangesStart(true);
3716
- adm.spliceWithArray_(0, 0, initialValues);
3717
- allowStateChangesEnd(prev);
3718
- }
3719
- return proxy;
3715
+ return initObservable(function () {
3716
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, false);
3717
+ addHiddenFinalProp(adm.values_, $mobx, adm);
3718
+ var proxy = new Proxy(adm.values_, arrayTraps);
3719
+ adm.proxy_ = proxy;
3720
+ if (initialValues && initialValues.length) {
3721
+ adm.spliceWithArray_(0, 0, initialValues);
3722
+ }
3723
+ return proxy;
3724
+ });
3720
3725
  }
3721
3726
  // eslint-disable-next-line
3722
3727
  var arrayExtensions = {
@@ -3912,11 +3917,13 @@ var ObservableMap = /*#__PURE__*/function () {
3912
3917
  if (!isFunction(Map)) {
3913
3918
  die(18);
3914
3919
  }
3915
- this.keysAtom_ = createAtom(process.env.NODE_ENV !== "production" ? this.name_ + ".keys()" : "ObservableMap.keys()");
3916
- this.data_ = new Map();
3917
- this.hasMap_ = new Map();
3918
- allowStateChanges(true, function () {
3919
- _this.merge(initialData);
3920
+ initObservable(function () {
3921
+ _this.keysAtom_ = createAtom(process.env.NODE_ENV !== "production" ? _this.name_ + ".keys()" : "ObservableMap.keys()");
3922
+ _this.data_ = new Map();
3923
+ _this.hasMap_ = new Map();
3924
+ if (initialData) {
3925
+ _this.merge(initialData);
3926
+ }
3920
3927
  });
3921
3928
  }
3922
3929
  var _proto = ObservableMap.prototype;
@@ -4300,6 +4307,7 @@ _Symbol$iterator$1 = Symbol.iterator;
4300
4307
  _Symbol$toStringTag$1 = Symbol.toStringTag;
4301
4308
  var ObservableSet = /*#__PURE__*/function () {
4302
4309
  function ObservableSet(initialData, enhancer, name_) {
4310
+ var _this = this;
4303
4311
  if (enhancer === void 0) {
4304
4312
  enhancer = deepEnhancer;
4305
4313
  }
@@ -4318,13 +4326,15 @@ var ObservableSet = /*#__PURE__*/function () {
4318
4326
  if (!isFunction(Set)) {
4319
4327
  die(22);
4320
4328
  }
4321
- this.atom_ = createAtom(this.name_);
4322
4329
  this.enhancer_ = function (newV, oldV) {
4323
4330
  return enhancer(newV, oldV, name_);
4324
4331
  };
4325
- if (initialData) {
4326
- this.replace(initialData);
4327
- }
4332
+ initObservable(function () {
4333
+ _this.atom_ = createAtom(_this.name_);
4334
+ if (initialData) {
4335
+ _this.replace(initialData);
4336
+ }
4337
+ });
4328
4338
  }
4329
4339
  var _proto = ObservableSet.prototype;
4330
4340
  _proto.dehanceValue_ = function dehanceValue_(value) {
@@ -4334,12 +4344,12 @@ var ObservableSet = /*#__PURE__*/function () {
4334
4344
  return value;
4335
4345
  };
4336
4346
  _proto.clear = function clear() {
4337
- var _this = this;
4347
+ var _this2 = this;
4338
4348
  transaction(function () {
4339
4349
  untracked(function () {
4340
- for (var _iterator = _createForOfIteratorHelperLoose(_this.data_.values()), _step; !(_step = _iterator()).done;) {
4350
+ for (var _iterator = _createForOfIteratorHelperLoose(_this2.data_.values()), _step; !(_step = _iterator()).done;) {
4341
4351
  var value = _step.value;
4342
- _this["delete"](value);
4352
+ _this2["delete"](value);
4343
4353
  }
4344
4354
  });
4345
4355
  });
@@ -4351,7 +4361,7 @@ var ObservableSet = /*#__PURE__*/function () {
4351
4361
  }
4352
4362
  };
4353
4363
  _proto.add = function add(value) {
4354
- var _this2 = this;
4364
+ var _this3 = this;
4355
4365
  checkIfStateModificationsAreAllowed(this.atom_);
4356
4366
  if (hasInterceptors(this)) {
4357
4367
  var change = interceptChange(this, {
@@ -4368,8 +4378,8 @@ var ObservableSet = /*#__PURE__*/function () {
4368
4378
 
4369
4379
  if (!this.has(value)) {
4370
4380
  transaction(function () {
4371
- _this2.data_.add(_this2.enhancer_(value, undefined));
4372
- _this2.atom_.reportChanged();
4381
+ _this3.data_.add(_this3.enhancer_(value, undefined));
4382
+ _this3.atom_.reportChanged();
4373
4383
  });
4374
4384
  var notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4375
4385
  var notify = hasListeners(this);
@@ -4393,7 +4403,7 @@ var ObservableSet = /*#__PURE__*/function () {
4393
4403
  return this;
4394
4404
  };
4395
4405
  _proto["delete"] = function _delete(value) {
4396
- var _this3 = this;
4406
+ var _this4 = this;
4397
4407
  if (hasInterceptors(this)) {
4398
4408
  var change = interceptChange(this, {
4399
4409
  type: DELETE,
@@ -4418,8 +4428,8 @@ var ObservableSet = /*#__PURE__*/function () {
4418
4428
  spyReportStart(_change2);
4419
4429
  }
4420
4430
  transaction(function () {
4421
- _this3.atom_.reportChanged();
4422
- _this3.data_["delete"](value);
4431
+ _this4.atom_.reportChanged();
4432
+ _this4.data_["delete"](value);
4423
4433
  });
4424
4434
  if (notify) {
4425
4435
  notifyListeners(this, _change2);
@@ -4472,20 +4482,20 @@ var ObservableSet = /*#__PURE__*/function () {
4472
4482
  });
4473
4483
  };
4474
4484
  _proto.replace = function replace(other) {
4475
- var _this4 = this;
4485
+ var _this5 = this;
4476
4486
  if (isObservableSet(other)) {
4477
4487
  other = new Set(other);
4478
4488
  }
4479
4489
  transaction(function () {
4480
4490
  if (Array.isArray(other)) {
4481
- _this4.clear();
4491
+ _this5.clear();
4482
4492
  other.forEach(function (value) {
4483
- return _this4.add(value);
4493
+ return _this5.add(value);
4484
4494
  });
4485
4495
  } else if (isES6Set(other)) {
4486
- _this4.clear();
4496
+ _this5.clear();
4487
4497
  other.forEach(function (value) {
4488
- return _this4.add(value);
4498
+ return _this5.add(value);
4489
4499
  });
4490
4500
  } else if (other !== null && other !== undefined) {
4491
4501
  die("Cannot initialize set from " + other);
@@ -4749,6 +4759,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4749
4759
  if (proxyTrap === void 0) {
4750
4760
  proxyTrap = false;
4751
4761
  }
4762
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4752
4763
  try {
4753
4764
  startBatch();
4754
4765
  // Delete
@@ -4796,6 +4807,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4796
4807
  if (proxyTrap === void 0) {
4797
4808
  proxyTrap = false;
4798
4809
  }
4810
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4799
4811
  try {
4800
4812
  startBatch();
4801
4813
  // Delete
@@ -4847,6 +4859,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4847
4859
  if (proxyTrap === void 0) {
4848
4860
  proxyTrap = false;
4849
4861
  }
4862
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4850
4863
  try {
4851
4864
  startBatch();
4852
4865
  // Delete
@@ -4902,6 +4915,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4902
4915
  if (proxyTrap === void 0) {
4903
4916
  proxyTrap = false;
4904
4917
  }
4918
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4905
4919
  // No such prop
4906
4920
  if (!hasProp(this.target_, key)) {
4907
4921
  return true;
@@ -5131,6 +5145,17 @@ function assertAnnotable(adm, annotation, key) {
5131
5145
 
5132
5146
  // Bug in safari 9.* (or iOS 9 safari mobile). See #364
5133
5147
  var ENTRY_0 = /*#__PURE__*/createArrayEntryDescriptor(0);
5148
+ var safariPrototypeSetterInheritanceBug = /*#__PURE__*/function () {
5149
+ var v = false;
5150
+ var p = {};
5151
+ Object.defineProperty(p, "0", {
5152
+ set: function set() {
5153
+ v = true;
5154
+ }
5155
+ });
5156
+ /*#__PURE__*/Object.create(p)["0"] = 1;
5157
+ return v === false;
5158
+ }();
5134
5159
  /**
5135
5160
  * This array buffer contains two lists of properties, so that all arrays
5136
5161
  * can recycle their property definitions, which significantly improves performance of creating
@@ -5163,20 +5188,20 @@ var LegacyObservableArray = /*#__PURE__*/function (_StubArray, _Symbol$toStringT
5163
5188
  owned = false;
5164
5189
  }
5165
5190
  _this = _StubArray.call(this) || this;
5166
- var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5167
- adm.proxy_ = _assertThisInitialized(_this);
5168
- addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5169
- if (initialValues && initialValues.length) {
5170
- var prev = allowStateChangesStart(true);
5171
- // @ts-ignore
5172
- _this.spliceWithArray(0, 0, initialValues);
5173
- allowStateChangesEnd(prev);
5174
- }
5175
- {
5176
- // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5177
- // defined on the instance. After that it works fine, even if this property is deleted.
5178
- Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5179
- }
5191
+ initObservable(function () {
5192
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5193
+ adm.proxy_ = _assertThisInitialized(_this);
5194
+ addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5195
+ if (initialValues && initialValues.length) {
5196
+ // @ts-ignore
5197
+ _this.spliceWithArray(0, 0, initialValues);
5198
+ }
5199
+ if (safariPrototypeSetterInheritanceBug) {
5200
+ // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5201
+ // defined on the instance. After that it works fine, even if this property is deleted.
5202
+ Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5203
+ }
5204
+ });
5180
5205
  return _this;
5181
5206
  }
5182
5207
  var _proto = LegacyObservableArray.prototype;
@@ -5331,6 +5356,24 @@ function getDebugName(thing, property) {
5331
5356
  }
5332
5357
  return named.name_;
5333
5358
  }
5359
+ /**
5360
+ * Helper function for initializing observable structures, it applies:
5361
+ * 1. allowStateChanges so we don't violate enforceActions.
5362
+ * 2. untracked so we don't accidentaly subscribe to anything observable accessed during init in case the observable is created inside derivation.
5363
+ * 3. batch to avoid state version updates
5364
+ */
5365
+ function initObservable(cb) {
5366
+ var derivation = untrackedStart();
5367
+ var allowStateChanges = allowStateChangesStart(true);
5368
+ startBatch();
5369
+ try {
5370
+ return cb();
5371
+ } finally {
5372
+ endBatch();
5373
+ allowStateChangesEnd(allowStateChanges);
5374
+ untrackedEnd(derivation);
5375
+ }
5376
+ }
5334
5377
 
5335
5378
  var toString = objectPrototype.toString;
5336
5379
  function deepEqual(a, b, depth) {