mobx 6.10.0 → 6.10.2

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