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.
@@ -422,12 +422,14 @@ var Atom = /*#__PURE__*/function () {
422
422
  this.isPendingUnobservation_ = false;
423
423
  this.isBeingObserved_ = false;
424
424
  this.observers_ = new Set();
425
+ this.batchId_ = void 0;
425
426
  this.diffValue_ = 0;
426
427
  this.lastAccessedBy_ = 0;
427
428
  this.lowestObserverState_ = IDerivationState_.NOT_TRACKING_;
428
429
  this.onBOL = void 0;
429
430
  this.onBUOL = void 0;
430
431
  this.name_ = name_;
432
+ this.batchId_ = globalState.inBatch ? globalState.batchId : NaN;
431
433
  }
432
434
  // onBecomeObservedListeners
433
435
  var _proto = Atom.prototype;
@@ -456,6 +458,12 @@ var Atom = /*#__PURE__*/function () {
456
458
  * Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
457
459
  */;
458
460
  _proto.reportChanged = function reportChanged() {
461
+ if (globalState.inBatch && this.batchId_ === globalState.batchId) {
462
+ // Called from the same batch this atom was created in.
463
+ return;
464
+ }
465
+ // Avoids the possibility of hitting the same globalState.batchId when it cycled through all integers (necessary?)
466
+ this.batchId_ = NaN;
459
467
  startBatch();
460
468
  propagateChanged(this);
461
469
  // We could update state version only at the end of batch,
@@ -989,7 +997,9 @@ var observableFactories = {
989
997
  return new ObservableSet(initialValues, getEnhancerFromOptions(o), o.name);
990
998
  },
991
999
  object: function object(props, decorators, options) {
992
- return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1000
+ return initObservable(function () {
1001
+ return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1002
+ });
993
1003
  },
994
1004
  ref: /*#__PURE__*/createDecoratorAnnotation(observableRefAnnotation),
995
1005
  shallow: /*#__PURE__*/createDecoratorAnnotation(observableShallowAnnotation),
@@ -1811,6 +1821,7 @@ var MobXGlobals = function MobXGlobals() {
1811
1821
  this.runId = 0;
1812
1822
  this.mobxGuid = 0;
1813
1823
  this.inBatch = 0;
1824
+ this.batchId = Number.MIN_SAFE_INTEGER;
1814
1825
  this.pendingUnobservations = [];
1815
1826
  this.pendingReactions = [];
1816
1827
  this.isRunningReactions = false;
@@ -1950,6 +1961,9 @@ function queueForUnobservation(observable) {
1950
1961
  * Avoids unnecessary recalculations.
1951
1962
  */
1952
1963
  function startBatch() {
1964
+ if (globalState.inBatch === 0) {
1965
+ globalState.batchId = globalState.batchId < Number.MAX_SAFE_INTEGER ? globalState.batchId + 1 : Number.MIN_SAFE_INTEGER;
1966
+ }
1953
1967
  globalState.inBatch++;
1954
1968
  }
1955
1969
  function endBatch() {
@@ -2625,17 +2639,14 @@ function extendObservable(target, properties, annotations, options) {
2625
2639
  }
2626
2640
  // Pull descriptors first, so we don't have to deal with props added by administration ($mobx)
2627
2641
  var descriptors = getOwnPropertyDescriptors(properties);
2628
- var adm = asObservableObject(target, options)[$mobx];
2629
- startBatch();
2630
- try {
2642
+ initObservable(function () {
2643
+ var adm = asObservableObject(target, options)[$mobx];
2631
2644
  ownKeys(descriptors).forEach(function (key) {
2632
2645
  adm.extend_(key, descriptors[key],
2633
2646
  // must pass "undefined" for { key: undefined }
2634
2647
  !annotations ? true : key in annotations ? annotations[key] : true);
2635
2648
  });
2636
- } finally {
2637
- endBatch();
2638
- }
2649
+ });
2639
2650
  return target;
2640
2651
  }
2641
2652
 
@@ -3321,10 +3332,9 @@ function notifyListeners(listenable, change) {
3321
3332
  }
3322
3333
 
3323
3334
  function makeObservable(target, annotations, options) {
3324
- var adm = asObservableObject(target, options)[$mobx];
3325
- startBatch();
3326
- try {
3335
+ initObservable(function () {
3327
3336
  var _annotations;
3337
+ var adm = asObservableObject(target, options)[$mobx];
3328
3338
  if ("development" !== "production" && annotations && target[storedAnnotationsSymbol]) {
3329
3339
  die("makeObservable second arg must be nullish when using decorators. Mixing @decorator syntax with annotations is not supported.");
3330
3340
  }
@@ -3334,9 +3344,7 @@ function makeObservable(target, annotations, options) {
3334
3344
  ownKeys(annotations).forEach(function (key) {
3335
3345
  return adm.make_(key, annotations[key]);
3336
3346
  });
3337
- } finally {
3338
- endBatch();
3339
- }
3347
+ });
3340
3348
  return target;
3341
3349
  }
3342
3350
  // proto[keysSymbol] = new Set<PropertyKey>()
@@ -3355,26 +3363,23 @@ function makeAutoObservable(target, overrides, options) {
3355
3363
  if (isPlainObject(target)) {
3356
3364
  return extendObservable(target, target, overrides, options);
3357
3365
  }
3358
- var adm = asObservableObject(target, options)[$mobx];
3359
- // Optimization: cache keys on proto
3360
- // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3361
- if (!target[keysSymbol]) {
3362
- var proto = Object.getPrototypeOf(target);
3363
- var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3364
- keys["delete"]("constructor");
3365
- keys["delete"]($mobx);
3366
- addHiddenProp(proto, keysSymbol, keys);
3367
- }
3368
- startBatch();
3369
- try {
3366
+ initObservable(function () {
3367
+ var adm = asObservableObject(target, options)[$mobx];
3368
+ // Optimization: cache keys on proto
3369
+ // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3370
+ if (!target[keysSymbol]) {
3371
+ var proto = Object.getPrototypeOf(target);
3372
+ var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3373
+ keys["delete"]("constructor");
3374
+ keys["delete"]($mobx);
3375
+ addHiddenProp(proto, keysSymbol, keys);
3376
+ }
3370
3377
  target[keysSymbol].forEach(function (key) {
3371
3378
  return adm.make_(key,
3372
3379
  // must pass "undefined" for { key: undefined }
3373
3380
  !overrides ? true : key in overrides ? overrides[key] : true);
3374
3381
  });
3375
- } finally {
3376
- endBatch();
3377
- }
3382
+ });
3378
3383
  return target;
3379
3384
  }
3380
3385
 
@@ -3682,16 +3687,16 @@ function createObservableArray(initialValues, enhancer, name, owned) {
3682
3687
  owned = false;
3683
3688
  }
3684
3689
  assertProxies();
3685
- var adm = new ObservableArrayAdministration(name, enhancer, owned, false);
3686
- addHiddenFinalProp(adm.values_, $mobx, adm);
3687
- var proxy = new Proxy(adm.values_, arrayTraps);
3688
- adm.proxy_ = proxy;
3689
- if (initialValues && initialValues.length) {
3690
- var prev = allowStateChangesStart(true);
3691
- adm.spliceWithArray_(0, 0, initialValues);
3692
- allowStateChangesEnd(prev);
3693
- }
3694
- return proxy;
3690
+ return initObservable(function () {
3691
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, false);
3692
+ addHiddenFinalProp(adm.values_, $mobx, adm);
3693
+ var proxy = new Proxy(adm.values_, arrayTraps);
3694
+ adm.proxy_ = proxy;
3695
+ if (initialValues && initialValues.length) {
3696
+ adm.spliceWithArray_(0, 0, initialValues);
3697
+ }
3698
+ return proxy;
3699
+ });
3695
3700
  }
3696
3701
  // eslint-disable-next-line
3697
3702
  var arrayExtensions = {
@@ -3887,11 +3892,13 @@ var ObservableMap = /*#__PURE__*/function () {
3887
3892
  if (!isFunction(Map)) {
3888
3893
  die(18);
3889
3894
  }
3890
- this.keysAtom_ = createAtom( this.name_ + ".keys()" );
3891
- this.data_ = new Map();
3892
- this.hasMap_ = new Map();
3893
- allowStateChanges(true, function () {
3894
- _this.merge(initialData);
3895
+ initObservable(function () {
3896
+ _this.keysAtom_ = createAtom("development" !== "production" ? _this.name_ + ".keys()" : "ObservableMap.keys()");
3897
+ _this.data_ = new Map();
3898
+ _this.hasMap_ = new Map();
3899
+ if (initialData) {
3900
+ _this.merge(initialData);
3901
+ }
3895
3902
  });
3896
3903
  }
3897
3904
  var _proto = ObservableMap.prototype;
@@ -4275,6 +4282,7 @@ _Symbol$iterator$1 = Symbol.iterator;
4275
4282
  _Symbol$toStringTag$1 = Symbol.toStringTag;
4276
4283
  var ObservableSet = /*#__PURE__*/function () {
4277
4284
  function ObservableSet(initialData, enhancer, name_) {
4285
+ var _this = this;
4278
4286
  if (enhancer === void 0) {
4279
4287
  enhancer = deepEnhancer;
4280
4288
  }
@@ -4293,13 +4301,15 @@ var ObservableSet = /*#__PURE__*/function () {
4293
4301
  if (!isFunction(Set)) {
4294
4302
  die(22);
4295
4303
  }
4296
- this.atom_ = createAtom(this.name_);
4297
4304
  this.enhancer_ = function (newV, oldV) {
4298
4305
  return enhancer(newV, oldV, name_);
4299
4306
  };
4300
- if (initialData) {
4301
- this.replace(initialData);
4302
- }
4307
+ initObservable(function () {
4308
+ _this.atom_ = createAtom(_this.name_);
4309
+ if (initialData) {
4310
+ _this.replace(initialData);
4311
+ }
4312
+ });
4303
4313
  }
4304
4314
  var _proto = ObservableSet.prototype;
4305
4315
  _proto.dehanceValue_ = function dehanceValue_(value) {
@@ -4309,12 +4319,12 @@ var ObservableSet = /*#__PURE__*/function () {
4309
4319
  return value;
4310
4320
  };
4311
4321
  _proto.clear = function clear() {
4312
- var _this = this;
4322
+ var _this2 = this;
4313
4323
  transaction(function () {
4314
4324
  untracked(function () {
4315
- for (var _iterator = _createForOfIteratorHelperLoose(_this.data_.values()), _step; !(_step = _iterator()).done;) {
4325
+ for (var _iterator = _createForOfIteratorHelperLoose(_this2.data_.values()), _step; !(_step = _iterator()).done;) {
4316
4326
  var value = _step.value;
4317
- _this["delete"](value);
4327
+ _this2["delete"](value);
4318
4328
  }
4319
4329
  });
4320
4330
  });
@@ -4326,7 +4336,7 @@ var ObservableSet = /*#__PURE__*/function () {
4326
4336
  }
4327
4337
  };
4328
4338
  _proto.add = function add(value) {
4329
- var _this2 = this;
4339
+ var _this3 = this;
4330
4340
  checkIfStateModificationsAreAllowed(this.atom_);
4331
4341
  if (hasInterceptors(this)) {
4332
4342
  var change = interceptChange(this, {
@@ -4343,8 +4353,8 @@ var ObservableSet = /*#__PURE__*/function () {
4343
4353
 
4344
4354
  if (!this.has(value)) {
4345
4355
  transaction(function () {
4346
- _this2.data_.add(_this2.enhancer_(value, undefined));
4347
- _this2.atom_.reportChanged();
4356
+ _this3.data_.add(_this3.enhancer_(value, undefined));
4357
+ _this3.atom_.reportChanged();
4348
4358
  });
4349
4359
  var notifySpy = isSpyEnabled();
4350
4360
  var notify = hasListeners(this);
@@ -4368,7 +4378,7 @@ var ObservableSet = /*#__PURE__*/function () {
4368
4378
  return this;
4369
4379
  };
4370
4380
  _proto["delete"] = function _delete(value) {
4371
- var _this3 = this;
4381
+ var _this4 = this;
4372
4382
  if (hasInterceptors(this)) {
4373
4383
  var change = interceptChange(this, {
4374
4384
  type: DELETE,
@@ -4393,8 +4403,8 @@ var ObservableSet = /*#__PURE__*/function () {
4393
4403
  spyReportStart(_change2);
4394
4404
  }
4395
4405
  transaction(function () {
4396
- _this3.atom_.reportChanged();
4397
- _this3.data_["delete"](value);
4406
+ _this4.atom_.reportChanged();
4407
+ _this4.data_["delete"](value);
4398
4408
  });
4399
4409
  if (notify) {
4400
4410
  notifyListeners(this, _change2);
@@ -4447,20 +4457,20 @@ var ObservableSet = /*#__PURE__*/function () {
4447
4457
  });
4448
4458
  };
4449
4459
  _proto.replace = function replace(other) {
4450
- var _this4 = this;
4460
+ var _this5 = this;
4451
4461
  if (isObservableSet(other)) {
4452
4462
  other = new Set(other);
4453
4463
  }
4454
4464
  transaction(function () {
4455
4465
  if (Array.isArray(other)) {
4456
- _this4.clear();
4466
+ _this5.clear();
4457
4467
  other.forEach(function (value) {
4458
- return _this4.add(value);
4468
+ return _this5.add(value);
4459
4469
  });
4460
4470
  } else if (isES6Set(other)) {
4461
- _this4.clear();
4471
+ _this5.clear();
4462
4472
  other.forEach(function (value) {
4463
- return _this4.add(value);
4473
+ return _this5.add(value);
4464
4474
  });
4465
4475
  } else if (other !== null && other !== undefined) {
4466
4476
  die("Cannot initialize set from " + other);
@@ -4724,6 +4734,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4724
4734
  if (proxyTrap === void 0) {
4725
4735
  proxyTrap = false;
4726
4736
  }
4737
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4727
4738
  try {
4728
4739
  startBatch();
4729
4740
  // Delete
@@ -4771,6 +4782,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4771
4782
  if (proxyTrap === void 0) {
4772
4783
  proxyTrap = false;
4773
4784
  }
4785
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4774
4786
  try {
4775
4787
  startBatch();
4776
4788
  // Delete
@@ -4822,6 +4834,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4822
4834
  if (proxyTrap === void 0) {
4823
4835
  proxyTrap = false;
4824
4836
  }
4837
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4825
4838
  try {
4826
4839
  startBatch();
4827
4840
  // Delete
@@ -4877,6 +4890,7 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4877
4890
  if (proxyTrap === void 0) {
4878
4891
  proxyTrap = false;
4879
4892
  }
4893
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4880
4894
  // No such prop
4881
4895
  if (!hasProp(this.target_, key)) {
4882
4896
  return true;
@@ -5106,6 +5120,17 @@ function assertAnnotable(adm, annotation, key) {
5106
5120
 
5107
5121
  // Bug in safari 9.* (or iOS 9 safari mobile). See #364
5108
5122
  var ENTRY_0 = /*#__PURE__*/createArrayEntryDescriptor(0);
5123
+ var safariPrototypeSetterInheritanceBug = /*#__PURE__*/function () {
5124
+ var v = false;
5125
+ var p = {};
5126
+ Object.defineProperty(p, "0", {
5127
+ set: function set() {
5128
+ v = true;
5129
+ }
5130
+ });
5131
+ /*#__PURE__*/Object.create(p)["0"] = 1;
5132
+ return v === false;
5133
+ }();
5109
5134
  /**
5110
5135
  * This array buffer contains two lists of properties, so that all arrays
5111
5136
  * can recycle their property definitions, which significantly improves performance of creating
@@ -5138,20 +5163,20 @@ var LegacyObservableArray = /*#__PURE__*/function (_StubArray, _Symbol$toStringT
5138
5163
  owned = false;
5139
5164
  }
5140
5165
  _this = _StubArray.call(this) || this;
5141
- var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5142
- adm.proxy_ = _assertThisInitialized(_this);
5143
- addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5144
- if (initialValues && initialValues.length) {
5145
- var prev = allowStateChangesStart(true);
5146
- // @ts-ignore
5147
- _this.spliceWithArray(0, 0, initialValues);
5148
- allowStateChangesEnd(prev);
5149
- }
5150
- {
5151
- // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5152
- // defined on the instance. After that it works fine, even if this property is deleted.
5153
- Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5154
- }
5166
+ initObservable(function () {
5167
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5168
+ adm.proxy_ = _assertThisInitialized(_this);
5169
+ addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5170
+ if (initialValues && initialValues.length) {
5171
+ // @ts-ignore
5172
+ _this.spliceWithArray(0, 0, initialValues);
5173
+ }
5174
+ if (safariPrototypeSetterInheritanceBug) {
5175
+ // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5176
+ // defined on the instance. After that it works fine, even if this property is deleted.
5177
+ Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5178
+ }
5179
+ });
5155
5180
  return _this;
5156
5181
  }
5157
5182
  var _proto = LegacyObservableArray.prototype;
@@ -5306,6 +5331,24 @@ function getDebugName(thing, property) {
5306
5331
  }
5307
5332
  return named.name_;
5308
5333
  }
5334
+ /**
5335
+ * Helper function for initializing observable structures, it applies:
5336
+ * 1. allowStateChanges so we don't violate enforceActions.
5337
+ * 2. untracked so we don't accidentaly subscribe to anything observable accessed during init in case the observable is created inside derivation.
5338
+ * 3. batch to avoid state version updates
5339
+ */
5340
+ function initObservable(cb) {
5341
+ var derivation = untrackedStart();
5342
+ var allowStateChanges = allowStateChangesStart(true);
5343
+ startBatch();
5344
+ try {
5345
+ return cb();
5346
+ } finally {
5347
+ endBatch();
5348
+ allowStateChangesEnd(allowStateChanges);
5349
+ untrackedEnd(derivation);
5350
+ }
5351
+ }
5309
5352
 
5310
5353
  var toString = objectPrototype.toString;
5311
5354
  function deepEqual(a, b, depth) {