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.
@@ -428,12 +428,14 @@
428
428
  this.isPendingUnobservation_ = false;
429
429
  this.isBeingObserved_ = false;
430
430
  this.observers_ = new Set();
431
+ this.batchId_ = void 0;
431
432
  this.diffValue_ = 0;
432
433
  this.lastAccessedBy_ = 0;
433
434
  this.lowestObserverState_ = IDerivationState_.NOT_TRACKING_;
434
435
  this.onBOL = void 0;
435
436
  this.onBUOL = void 0;
436
437
  this.name_ = name_;
438
+ this.batchId_ = globalState.inBatch ? globalState.batchId : NaN;
437
439
  }
438
440
  // onBecomeObservedListeners
439
441
  var _proto = Atom.prototype;
@@ -462,6 +464,12 @@
462
464
  * Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
463
465
  */;
464
466
  _proto.reportChanged = function reportChanged() {
467
+ if (globalState.inBatch && this.batchId_ === globalState.batchId) {
468
+ // Called from the same batch this atom was created in.
469
+ return;
470
+ }
471
+ // Avoids the possibility of hitting the same globalState.batchId when it cycled through all integers (necessary?)
472
+ this.batchId_ = NaN;
465
473
  startBatch();
466
474
  propagateChanged(this);
467
475
  // We could update state version only at the end of batch,
@@ -995,7 +1003,9 @@
995
1003
  return new ObservableSet(initialValues, getEnhancerFromOptions(o), o.name);
996
1004
  },
997
1005
  object: function object(props, decorators, options) {
998
- return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1006
+ return initObservable(function () {
1007
+ return extendObservable(globalState.useProxies === false || (options == null ? void 0 : options.proxy) === false ? asObservableObject({}, options) : asDynamicObservableObject({}, options), props, decorators);
1008
+ });
999
1009
  },
1000
1010
  ref: /*#__PURE__*/createDecoratorAnnotation(observableRefAnnotation),
1001
1011
  shallow: /*#__PURE__*/createDecoratorAnnotation(observableShallowAnnotation),
@@ -1817,6 +1827,7 @@
1817
1827
  this.runId = 0;
1818
1828
  this.mobxGuid = 0;
1819
1829
  this.inBatch = 0;
1830
+ this.batchId = Number.MIN_SAFE_INTEGER;
1820
1831
  this.pendingUnobservations = [];
1821
1832
  this.pendingReactions = [];
1822
1833
  this.isRunningReactions = false;
@@ -1956,6 +1967,9 @@
1956
1967
  * Avoids unnecessary recalculations.
1957
1968
  */
1958
1969
  function startBatch() {
1970
+ if (globalState.inBatch === 0) {
1971
+ globalState.batchId = globalState.batchId < Number.MAX_SAFE_INTEGER ? globalState.batchId + 1 : Number.MIN_SAFE_INTEGER;
1972
+ }
1959
1973
  globalState.inBatch++;
1960
1974
  }
1961
1975
  function endBatch() {
@@ -2631,17 +2645,14 @@
2631
2645
  }
2632
2646
  // Pull descriptors first, so we don't have to deal with props added by administration ($mobx)
2633
2647
  var descriptors = getOwnPropertyDescriptors(properties);
2634
- var adm = asObservableObject(target, options)[$mobx];
2635
- startBatch();
2636
- try {
2648
+ initObservable(function () {
2649
+ var adm = asObservableObject(target, options)[$mobx];
2637
2650
  ownKeys(descriptors).forEach(function (key) {
2638
2651
  adm.extend_(key, descriptors[key],
2639
2652
  // must pass "undefined" for { key: undefined }
2640
2653
  !annotations ? true : key in annotations ? annotations[key] : true);
2641
2654
  });
2642
- } finally {
2643
- endBatch();
2644
- }
2655
+ });
2645
2656
  return target;
2646
2657
  }
2647
2658
 
@@ -3327,10 +3338,9 @@
3327
3338
  }
3328
3339
 
3329
3340
  function makeObservable(target, annotations, options) {
3330
- var adm = asObservableObject(target, options)[$mobx];
3331
- startBatch();
3332
- try {
3341
+ initObservable(function () {
3333
3342
  var _annotations;
3343
+ var adm = asObservableObject(target, options)[$mobx];
3334
3344
  if ("development" !== "production" && annotations && target[storedAnnotationsSymbol]) {
3335
3345
  die("makeObservable second arg must be nullish when using decorators. Mixing @decorator syntax with annotations is not supported.");
3336
3346
  }
@@ -3340,9 +3350,7 @@
3340
3350
  ownKeys(annotations).forEach(function (key) {
3341
3351
  return adm.make_(key, annotations[key]);
3342
3352
  });
3343
- } finally {
3344
- endBatch();
3345
- }
3353
+ });
3346
3354
  return target;
3347
3355
  }
3348
3356
  // proto[keysSymbol] = new Set<PropertyKey>()
@@ -3361,26 +3369,23 @@
3361
3369
  if (isPlainObject(target)) {
3362
3370
  return extendObservable(target, target, overrides, options);
3363
3371
  }
3364
- var adm = asObservableObject(target, options)[$mobx];
3365
- // Optimization: cache keys on proto
3366
- // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3367
- if (!target[keysSymbol]) {
3368
- var proto = Object.getPrototypeOf(target);
3369
- var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3370
- keys["delete"]("constructor");
3371
- keys["delete"]($mobx);
3372
- addHiddenProp(proto, keysSymbol, keys);
3373
- }
3374
- startBatch();
3375
- try {
3372
+ initObservable(function () {
3373
+ var adm = asObservableObject(target, options)[$mobx];
3374
+ // Optimization: cache keys on proto
3375
+ // Assumes makeAutoObservable can be called only once per object and can't be used in subclass
3376
+ if (!target[keysSymbol]) {
3377
+ var proto = Object.getPrototypeOf(target);
3378
+ var keys = new Set([].concat(ownKeys(target), ownKeys(proto)));
3379
+ keys["delete"]("constructor");
3380
+ keys["delete"]($mobx);
3381
+ addHiddenProp(proto, keysSymbol, keys);
3382
+ }
3376
3383
  target[keysSymbol].forEach(function (key) {
3377
3384
  return adm.make_(key,
3378
3385
  // must pass "undefined" for { key: undefined }
3379
3386
  !overrides ? true : key in overrides ? overrides[key] : true);
3380
3387
  });
3381
- } finally {
3382
- endBatch();
3383
- }
3388
+ });
3384
3389
  return target;
3385
3390
  }
3386
3391
 
@@ -3688,16 +3693,16 @@
3688
3693
  owned = false;
3689
3694
  }
3690
3695
  assertProxies();
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
- var prev = allowStateChangesStart(true);
3697
- adm.spliceWithArray_(0, 0, initialValues);
3698
- allowStateChangesEnd(prev);
3699
- }
3700
- return proxy;
3696
+ return initObservable(function () {
3697
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, false);
3698
+ addHiddenFinalProp(adm.values_, $mobx, adm);
3699
+ var proxy = new Proxy(adm.values_, arrayTraps);
3700
+ adm.proxy_ = proxy;
3701
+ if (initialValues && initialValues.length) {
3702
+ adm.spliceWithArray_(0, 0, initialValues);
3703
+ }
3704
+ return proxy;
3705
+ });
3701
3706
  }
3702
3707
  // eslint-disable-next-line
3703
3708
  var arrayExtensions = {
@@ -3893,11 +3898,13 @@
3893
3898
  if (!isFunction(Map)) {
3894
3899
  die(18);
3895
3900
  }
3896
- this.keysAtom_ = createAtom( this.name_ + ".keys()" );
3897
- this.data_ = new Map();
3898
- this.hasMap_ = new Map();
3899
- allowStateChanges(true, function () {
3900
- _this.merge(initialData);
3901
+ initObservable(function () {
3902
+ _this.keysAtom_ = createAtom("development" !== "production" ? _this.name_ + ".keys()" : "ObservableMap.keys()");
3903
+ _this.data_ = new Map();
3904
+ _this.hasMap_ = new Map();
3905
+ if (initialData) {
3906
+ _this.merge(initialData);
3907
+ }
3901
3908
  });
3902
3909
  }
3903
3910
  var _proto = ObservableMap.prototype;
@@ -4281,6 +4288,7 @@
4281
4288
  _Symbol$toStringTag$1 = Symbol.toStringTag;
4282
4289
  var ObservableSet = /*#__PURE__*/function () {
4283
4290
  function ObservableSet(initialData, enhancer, name_) {
4291
+ var _this = this;
4284
4292
  if (enhancer === void 0) {
4285
4293
  enhancer = deepEnhancer;
4286
4294
  }
@@ -4299,13 +4307,15 @@
4299
4307
  if (!isFunction(Set)) {
4300
4308
  die(22);
4301
4309
  }
4302
- this.atom_ = createAtom(this.name_);
4303
4310
  this.enhancer_ = function (newV, oldV) {
4304
4311
  return enhancer(newV, oldV, name_);
4305
4312
  };
4306
- if (initialData) {
4307
- this.replace(initialData);
4308
- }
4313
+ initObservable(function () {
4314
+ _this.atom_ = createAtom(_this.name_);
4315
+ if (initialData) {
4316
+ _this.replace(initialData);
4317
+ }
4318
+ });
4309
4319
  }
4310
4320
  var _proto = ObservableSet.prototype;
4311
4321
  _proto.dehanceValue_ = function dehanceValue_(value) {
@@ -4315,12 +4325,12 @@
4315
4325
  return value;
4316
4326
  };
4317
4327
  _proto.clear = function clear() {
4318
- var _this = this;
4328
+ var _this2 = this;
4319
4329
  transaction(function () {
4320
4330
  untracked(function () {
4321
- for (var _iterator = _createForOfIteratorHelperLoose(_this.data_.values()), _step; !(_step = _iterator()).done;) {
4331
+ for (var _iterator = _createForOfIteratorHelperLoose(_this2.data_.values()), _step; !(_step = _iterator()).done;) {
4322
4332
  var value = _step.value;
4323
- _this["delete"](value);
4333
+ _this2["delete"](value);
4324
4334
  }
4325
4335
  });
4326
4336
  });
@@ -4332,7 +4342,7 @@
4332
4342
  }
4333
4343
  };
4334
4344
  _proto.add = function add(value) {
4335
- var _this2 = this;
4345
+ var _this3 = this;
4336
4346
  checkIfStateModificationsAreAllowed(this.atom_);
4337
4347
  if (hasInterceptors(this)) {
4338
4348
  var change = interceptChange(this, {
@@ -4349,8 +4359,8 @@
4349
4359
 
4350
4360
  if (!this.has(value)) {
4351
4361
  transaction(function () {
4352
- _this2.data_.add(_this2.enhancer_(value, undefined));
4353
- _this2.atom_.reportChanged();
4362
+ _this3.data_.add(_this3.enhancer_(value, undefined));
4363
+ _this3.atom_.reportChanged();
4354
4364
  });
4355
4365
  var notifySpy = isSpyEnabled();
4356
4366
  var notify = hasListeners(this);
@@ -4374,7 +4384,7 @@
4374
4384
  return this;
4375
4385
  };
4376
4386
  _proto["delete"] = function _delete(value) {
4377
- var _this3 = this;
4387
+ var _this4 = this;
4378
4388
  if (hasInterceptors(this)) {
4379
4389
  var change = interceptChange(this, {
4380
4390
  type: DELETE,
@@ -4399,8 +4409,8 @@
4399
4409
  spyReportStart(_change2);
4400
4410
  }
4401
4411
  transaction(function () {
4402
- _this3.atom_.reportChanged();
4403
- _this3.data_["delete"](value);
4412
+ _this4.atom_.reportChanged();
4413
+ _this4.data_["delete"](value);
4404
4414
  });
4405
4415
  if (notify) {
4406
4416
  notifyListeners(this, _change2);
@@ -4453,20 +4463,20 @@
4453
4463
  });
4454
4464
  };
4455
4465
  _proto.replace = function replace(other) {
4456
- var _this4 = this;
4466
+ var _this5 = this;
4457
4467
  if (isObservableSet(other)) {
4458
4468
  other = new Set(other);
4459
4469
  }
4460
4470
  transaction(function () {
4461
4471
  if (Array.isArray(other)) {
4462
- _this4.clear();
4472
+ _this5.clear();
4463
4473
  other.forEach(function (value) {
4464
- return _this4.add(value);
4474
+ return _this5.add(value);
4465
4475
  });
4466
4476
  } else if (isES6Set(other)) {
4467
- _this4.clear();
4477
+ _this5.clear();
4468
4478
  other.forEach(function (value) {
4469
- return _this4.add(value);
4479
+ return _this5.add(value);
4470
4480
  });
4471
4481
  } else if (other !== null && other !== undefined) {
4472
4482
  die("Cannot initialize set from " + other);
@@ -4730,6 +4740,7 @@
4730
4740
  if (proxyTrap === void 0) {
4731
4741
  proxyTrap = false;
4732
4742
  }
4743
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4733
4744
  try {
4734
4745
  startBatch();
4735
4746
  // Delete
@@ -4777,6 +4788,7 @@
4777
4788
  if (proxyTrap === void 0) {
4778
4789
  proxyTrap = false;
4779
4790
  }
4791
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4780
4792
  try {
4781
4793
  startBatch();
4782
4794
  // Delete
@@ -4828,6 +4840,7 @@
4828
4840
  if (proxyTrap === void 0) {
4829
4841
  proxyTrap = false;
4830
4842
  }
4843
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4831
4844
  try {
4832
4845
  startBatch();
4833
4846
  // Delete
@@ -4883,6 +4896,7 @@
4883
4896
  if (proxyTrap === void 0) {
4884
4897
  proxyTrap = false;
4885
4898
  }
4899
+ checkIfStateModificationsAreAllowed(this.keysAtom_);
4886
4900
  // No such prop
4887
4901
  if (!hasProp(this.target_, key)) {
4888
4902
  return true;
@@ -5112,6 +5126,17 @@
5112
5126
 
5113
5127
  // Bug in safari 9.* (or iOS 9 safari mobile). See #364
5114
5128
  var ENTRY_0 = /*#__PURE__*/createArrayEntryDescriptor(0);
5129
+ var safariPrototypeSetterInheritanceBug = /*#__PURE__*/function () {
5130
+ var v = false;
5131
+ var p = {};
5132
+ Object.defineProperty(p, "0", {
5133
+ set: function set() {
5134
+ v = true;
5135
+ }
5136
+ });
5137
+ /*#__PURE__*/Object.create(p)["0"] = 1;
5138
+ return v === false;
5139
+ }();
5115
5140
  /**
5116
5141
  * This array buffer contains two lists of properties, so that all arrays
5117
5142
  * can recycle their property definitions, which significantly improves performance of creating
@@ -5144,20 +5169,20 @@
5144
5169
  owned = false;
5145
5170
  }
5146
5171
  _this = _StubArray.call(this) || this;
5147
- var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5148
- adm.proxy_ = _assertThisInitialized(_this);
5149
- addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5150
- if (initialValues && initialValues.length) {
5151
- var prev = allowStateChangesStart(true);
5152
- // @ts-ignore
5153
- _this.spliceWithArray(0, 0, initialValues);
5154
- allowStateChangesEnd(prev);
5155
- }
5156
- {
5157
- // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5158
- // defined on the instance. After that it works fine, even if this property is deleted.
5159
- Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5160
- }
5172
+ initObservable(function () {
5173
+ var adm = new ObservableArrayAdministration(name, enhancer, owned, true);
5174
+ adm.proxy_ = _assertThisInitialized(_this);
5175
+ addHiddenFinalProp(_assertThisInitialized(_this), $mobx, adm);
5176
+ if (initialValues && initialValues.length) {
5177
+ // @ts-ignore
5178
+ _this.spliceWithArray(0, 0, initialValues);
5179
+ }
5180
+ if (safariPrototypeSetterInheritanceBug) {
5181
+ // Seems that Safari won't use numeric prototype setter untill any * numeric property is
5182
+ // defined on the instance. After that it works fine, even if this property is deleted.
5183
+ Object.defineProperty(_assertThisInitialized(_this), "0", ENTRY_0);
5184
+ }
5185
+ });
5161
5186
  return _this;
5162
5187
  }
5163
5188
  var _proto = LegacyObservableArray.prototype;
@@ -5312,6 +5337,24 @@
5312
5337
  }
5313
5338
  return named.name_;
5314
5339
  }
5340
+ /**
5341
+ * Helper function for initializing observable structures, it applies:
5342
+ * 1. allowStateChanges so we don't violate enforceActions.
5343
+ * 2. untracked so we don't accidentaly subscribe to anything observable accessed during init in case the observable is created inside derivation.
5344
+ * 3. batch to avoid state version updates
5345
+ */
5346
+ function initObservable(cb) {
5347
+ var derivation = untrackedStart();
5348
+ var allowStateChanges = allowStateChangesStart(true);
5349
+ startBatch();
5350
+ try {
5351
+ return cb();
5352
+ } finally {
5353
+ endBatch();
5354
+ allowStateChangesEnd(allowStateChanges);
5355
+ untrackedEnd(derivation);
5356
+ }
5357
+ }
5315
5358
 
5316
5359
  var toString = objectPrototype.toString;
5317
5360
  function deepEqual(a, b, depth) {