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