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