mobx 6.15.3 → 6.16.0

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.
@@ -947,14 +947,21 @@
947
947
  var ann = this;
948
948
  var key = context.name,
949
949
  addInitializer = context.addInitializer;
950
+ // Defer ComputedValue creation until first access — avoids allocating
951
+ // ComputedValues for getters that are never read on a given instance.
952
+ // The factory is materialised by ObservableObjectAdministration on demand.
950
953
  addInitializer(function () {
954
+ var _adm$lazyComputedKeys;
951
955
  var adm = asObservableObject(this)[$mobx];
952
- var options = _extends({}, ann.options_, {
953
- get: get,
954
- context: this
956
+ var target = this;
957
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
958
+ var options = _extends({}, ann.options_, {
959
+ get: get,
960
+ context: target
961
+ });
962
+ options.name || (options.name = adm.name_ + "." + key.toString() );
963
+ return new ComputedValue(options);
955
964
  });
956
- options.name || (options.name = adm.name_ + "." + key.toString() );
957
- adm.values_.set(key, new ComputedValue(options));
958
965
  });
959
966
  return function () {
960
967
  return this[$mobx].getObservablePropValue_(key);
@@ -995,44 +1002,37 @@
995
1002
  var ann = this;
996
1003
  var kind = context.kind,
997
1004
  name = context.name;
998
- // The laziness here is not ideal... It's a workaround to how 2022.3 Decorators are implemented:
999
- // `addInitializer` callbacks are executed _before_ any accessors are defined (instead of the ideal-for-us right after each).
1000
- // This means that, if we were to do our stuff in an `addInitializer`, we'd attempt to read a private slot
1001
- // before it has been initialized. The runtime doesn't like that and throws a `Cannot read private member
1002
- // from an object whose class did not declare it` error.
1003
- // TODO: it seems that this will not be required anymore in the final version of the spec
1004
- // See TODO: link
1005
- var initializedObjects = new WeakSet();
1006
- function initializeObservable(target, value) {
1007
- var _ann$options_$enhance, _ann$options_;
1005
+ if (kind !== "accessor") {
1006
+ return;
1007
+ }
1008
+ // Defer ObservableValue construction until first access. The factory is
1009
+ // materialised by ObservableObjectAdministration on demand, so unused
1010
+ // fields on wide classes never pay the per-instance allocation cost.
1011
+ function registerLazy(target, value) {
1012
+ var _adm$lazyObservableKe;
1008
1013
  var adm = asObservableObject(target)[$mobx];
1009
- var observable = new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer) != null ? _ann$options_$enhance : deepEnhancer, adm.name_ + "." + name.toString() , false);
1010
- adm.values_.set(name, observable);
1011
- initializedObjects.add(target);
1012
- }
1013
- if (kind == "accessor") {
1014
- return {
1015
- get: function get() {
1016
- if (!initializedObjects.has(this)) {
1017
- initializeObservable(this, desc.get.call(this));
1018
- }
1019
- return this[$mobx].getObservablePropValue_(name);
1020
- },
1021
- set: function set(value) {
1022
- if (!initializedObjects.has(this)) {
1023
- initializeObservable(this, value);
1024
- }
1025
- return this[$mobx].setObservablePropValue_(name, value);
1026
- },
1027
- init: function init(value) {
1028
- if (!initializedObjects.has(this)) {
1029
- initializeObservable(this, value);
1030
- }
1031
- return value;
1032
- }
1033
- };
1014
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1015
+ var _ann$options_$enhance, _ann$options_;
1016
+ return new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer) != null ? _ann$options_$enhance : deepEnhancer, adm.name_ + "." + name.toString() , false);
1017
+ });
1018
+ return adm;
1034
1019
  }
1035
- return;
1020
+ return {
1021
+ get: function get() {
1022
+ var _this$$mobx;
1023
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1024
+ return adm.getObservablePropValue_(name);
1025
+ },
1026
+ set: function set(value) {
1027
+ var _this$$mobx2;
1028
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1029
+ return adm.setObservablePropValue_(name, value);
1030
+ },
1031
+ init: function init(value) {
1032
+ registerLazy(this, value);
1033
+ return value;
1034
+ }
1035
+ };
1036
1036
  }
1037
1037
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1038
1038
  var annotationType_ = _ref.annotationType_;
@@ -3220,13 +3220,18 @@
3220
3220
  }
3221
3221
 
3222
3222
  function _isComputed(value, property) {
3223
+ var _adm$lazyComputedKeys;
3223
3224
  if (property === undefined) {
3224
3225
  return isComputedValue(value);
3225
3226
  }
3226
3227
  if (isObservableObject(value) === false) {
3227
3228
  return false;
3228
3229
  }
3229
- if (!value[$mobx].values_.has(property)) {
3230
+ var adm = value[$mobx];
3231
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3232
+ return true;
3233
+ }
3234
+ if (!adm.values_.has(property)) {
3230
3235
  return false;
3231
3236
  }
3232
3237
  var atom = getAtom(value, property);
@@ -3254,7 +3259,9 @@
3254
3259
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3255
3260
  }
3256
3261
  if (isObservableObject(value)) {
3257
- return value[$mobx].values_.has(property);
3262
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3263
+ var adm = value[$mobx];
3264
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3258
3265
  }
3259
3266
  return false;
3260
3267
  }
@@ -4452,6 +4459,18 @@
4452
4459
  }
4453
4460
  return this.dehanceValue_(undefined);
4454
4461
  };
4462
+ _proto.getOrInsert = function getOrInsert(key, value) {
4463
+ if (!this.has(key)) {
4464
+ this.set(key, value);
4465
+ }
4466
+ return this.get(key);
4467
+ };
4468
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4469
+ if (!this.has(key)) {
4470
+ this.set(key, callback(key));
4471
+ }
4472
+ return this.get(key);
4473
+ };
4455
4474
  _proto.dehanceValue_ = function dehanceValue_(value) {
4456
4475
  if (this.dehancer !== undefined) {
4457
4476
  return this.dehancer(value);
@@ -4985,6 +5004,8 @@
4985
5004
  this.isPlainObject_ = void 0;
4986
5005
  this.appliedAnnotations_ = void 0;
4987
5006
  this.pendingKeys_ = void 0;
5007
+ this.lazyComputedKeys_ = void 0;
5008
+ this.lazyObservableKeys_ = void 0;
4988
5009
  this.target_ = target_;
4989
5010
  this.values_ = values_;
4990
5011
  this.name_ = name_;
@@ -5002,10 +5023,42 @@
5002
5023
  }
5003
5024
  var _proto = ObservableObjectAdministration.prototype;
5004
5025
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
5005
- return this.values_.get(key).get();
5026
+ var _ref, _this$values_$get;
5027
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5028
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5029
+ return observable.get();
5030
+ };
5031
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5032
+ var _this$lazyComputedKey;
5033
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5034
+ if (!factory) {
5035
+ return undefined;
5036
+ }
5037
+ this.lazyComputedKeys_["delete"](key);
5038
+ if (this.lazyComputedKeys_.size === 0) {
5039
+ this.lazyComputedKeys_ = undefined;
5040
+ }
5041
+ var computed = factory();
5042
+ this.values_.set(key, computed);
5043
+ return computed;
5044
+ };
5045
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5046
+ var _this$lazyObservableK;
5047
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5048
+ if (!factory) {
5049
+ return undefined;
5050
+ }
5051
+ this.lazyObservableKeys_["delete"](key);
5052
+ if (this.lazyObservableKeys_.size === 0) {
5053
+ this.lazyObservableKeys_ = undefined;
5054
+ }
5055
+ var observable = factory();
5056
+ this.values_.set(key, observable);
5057
+ return observable;
5006
5058
  };
5007
5059
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5008
- var observable = this.values_.get(key);
5060
+ var _ref2, _this$values_$get2;
5061
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5009
5062
  if (observable instanceof ComputedValue) {
5010
5063
  observable.set(newValue);
5011
5064
  return true;
@@ -5726,10 +5779,12 @@
5726
5779
  return observable;
5727
5780
  }
5728
5781
  if (isObservableObject(thing)) {
5782
+ var _ref, _adm$values_$get;
5729
5783
  if (!property) {
5730
5784
  return die(26);
5731
5785
  }
5732
- var _observable = thing[$mobx].values_.get(property);
5786
+ var adm = thing[$mobx];
5787
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5733
5788
  if (!_observable) {
5734
5789
  die(27, property, getDebugName(thing));
5735
5790
  }
@@ -5950,9 +6005,10 @@
5950
6005
  return a;
5951
6006
  }
5952
6007
 
5953
- var _getGlobal$Iterator;
6008
+ var _global$Iterator;
5954
6009
  // safely get iterator prototype if available
5955
- var maybeIteratorPrototype = ((_getGlobal$Iterator = /*#__PURE__*/getGlobal().Iterator) == null ? void 0 : _getGlobal$Iterator.prototype) || {};
6010
+ var global$1 = /*#__PURE__*/getGlobal();
6011
+ var maybeIteratorPrototype = ((_global$Iterator = global$1.Iterator) == null ? void 0 : _global$Iterator.prototype) || {};
5956
6012
  function makeIterable(iterator) {
5957
6013
  iterator[Symbol.iterator] = getSelf;
5958
6014
  return Object.assign(Object.create(maybeIteratorPrototype), iterator);