mobx 6.15.4 → 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.
@@ -941,14 +941,21 @@ function decorate_20223_$3(get, context) {
941
941
  var ann = this;
942
942
  var key = context.name,
943
943
  addInitializer = context.addInitializer;
944
+ // Defer ComputedValue creation until first access — avoids allocating
945
+ // ComputedValues for getters that are never read on a given instance.
946
+ // The factory is materialised by ObservableObjectAdministration on demand.
944
947
  addInitializer(function () {
948
+ var _adm$lazyComputedKeys;
945
949
  var adm = asObservableObject(this)[$mobx];
946
- var options = _extends({}, ann.options_, {
947
- get: get,
948
- context: this
950
+ var target = this;
951
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
952
+ var options = _extends({}, ann.options_, {
953
+ get: get,
954
+ context: target
955
+ });
956
+ options.name || (options.name = adm.name_ + "." + key.toString() );
957
+ return new ComputedValue(options);
949
958
  });
950
- options.name || (options.name = adm.name_ + "." + key.toString() );
951
- adm.values_.set(key, new ComputedValue(options));
952
959
  });
953
960
  return function () {
954
961
  return this[$mobx].getObservablePropValue_(key);
@@ -989,44 +996,37 @@ function decorate_20223_$4(desc, context) {
989
996
  var ann = this;
990
997
  var kind = context.kind,
991
998
  name = context.name;
992
- // The laziness here is not ideal... It's a workaround to how 2022.3 Decorators are implemented:
993
- // `addInitializer` callbacks are executed _before_ any accessors are defined (instead of the ideal-for-us right after each).
994
- // This means that, if we were to do our stuff in an `addInitializer`, we'd attempt to read a private slot
995
- // before it has been initialized. The runtime doesn't like that and throws a `Cannot read private member
996
- // from an object whose class did not declare it` error.
997
- // TODO: it seems that this will not be required anymore in the final version of the spec
998
- // See TODO: link
999
- var initializedObjects = new WeakSet();
1000
- function initializeObservable(target, value) {
1001
- var _ann$options_$enhance, _ann$options_;
999
+ if (kind !== "accessor") {
1000
+ return;
1001
+ }
1002
+ // Defer ObservableValue construction until first access. The factory is
1003
+ // materialised by ObservableObjectAdministration on demand, so unused
1004
+ // fields on wide classes never pay the per-instance allocation cost.
1005
+ function registerLazy(target, value) {
1006
+ var _adm$lazyObservableKe;
1002
1007
  var adm = asObservableObject(target)[$mobx];
1003
- 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);
1004
- adm.values_.set(name, observable);
1005
- initializedObjects.add(target);
1006
- }
1007
- if (kind == "accessor") {
1008
- return {
1009
- get: function get() {
1010
- if (!initializedObjects.has(this)) {
1011
- initializeObservable(this, desc.get.call(this));
1012
- }
1013
- return this[$mobx].getObservablePropValue_(name);
1014
- },
1015
- set: function set(value) {
1016
- if (!initializedObjects.has(this)) {
1017
- initializeObservable(this, value);
1018
- }
1019
- return this[$mobx].setObservablePropValue_(name, value);
1020
- },
1021
- init: function init(value) {
1022
- if (!initializedObjects.has(this)) {
1023
- initializeObservable(this, value);
1024
- }
1025
- return value;
1026
- }
1027
- };
1008
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1009
+ var _ann$options_$enhance, _ann$options_;
1010
+ 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);
1011
+ });
1012
+ return adm;
1028
1013
  }
1029
- return;
1014
+ return {
1015
+ get: function get() {
1016
+ var _this$$mobx;
1017
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1018
+ return adm.getObservablePropValue_(name);
1019
+ },
1020
+ set: function set(value) {
1021
+ var _this$$mobx2;
1022
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1023
+ return adm.setObservablePropValue_(name, value);
1024
+ },
1025
+ init: function init(value) {
1026
+ registerLazy(this, value);
1027
+ return value;
1028
+ }
1029
+ };
1030
1030
  }
1031
1031
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1032
1032
  var annotationType_ = _ref.annotationType_;
@@ -3214,13 +3214,18 @@ function interceptProperty(thing, property, handler) {
3214
3214
  }
3215
3215
 
3216
3216
  function _isComputed(value, property) {
3217
+ var _adm$lazyComputedKeys;
3217
3218
  if (property === undefined) {
3218
3219
  return isComputedValue(value);
3219
3220
  }
3220
3221
  if (isObservableObject(value) === false) {
3221
3222
  return false;
3222
3223
  }
3223
- if (!value[$mobx].values_.has(property)) {
3224
+ var adm = value[$mobx];
3225
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3226
+ return true;
3227
+ }
3228
+ if (!adm.values_.has(property)) {
3224
3229
  return false;
3225
3230
  }
3226
3231
  var atom = getAtom(value, property);
@@ -3248,7 +3253,9 @@ function _isObservable(value, property) {
3248
3253
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3249
3254
  }
3250
3255
  if (isObservableObject(value)) {
3251
- return value[$mobx].values_.has(property);
3256
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3257
+ var adm = value[$mobx];
3258
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3252
3259
  }
3253
3260
  return false;
3254
3261
  }
@@ -4446,6 +4453,18 @@ var ObservableMap = /*#__PURE__*/function () {
4446
4453
  }
4447
4454
  return this.dehanceValue_(undefined);
4448
4455
  };
4456
+ _proto.getOrInsert = function getOrInsert(key, value) {
4457
+ if (!this.has(key)) {
4458
+ this.set(key, value);
4459
+ }
4460
+ return this.get(key);
4461
+ };
4462
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4463
+ if (!this.has(key)) {
4464
+ this.set(key, callback(key));
4465
+ }
4466
+ return this.get(key);
4467
+ };
4449
4468
  _proto.dehanceValue_ = function dehanceValue_(value) {
4450
4469
  if (this.dehancer !== undefined) {
4451
4470
  return this.dehancer(value);
@@ -4979,6 +4998,8 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4979
4998
  this.isPlainObject_ = void 0;
4980
4999
  this.appliedAnnotations_ = void 0;
4981
5000
  this.pendingKeys_ = void 0;
5001
+ this.lazyComputedKeys_ = void 0;
5002
+ this.lazyObservableKeys_ = void 0;
4982
5003
  this.target_ = target_;
4983
5004
  this.values_ = values_;
4984
5005
  this.name_ = name_;
@@ -4996,10 +5017,42 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4996
5017
  }
4997
5018
  var _proto = ObservableObjectAdministration.prototype;
4998
5019
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
4999
- return this.values_.get(key).get();
5020
+ var _ref, _this$values_$get;
5021
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5022
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5023
+ return observable.get();
5024
+ };
5025
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5026
+ var _this$lazyComputedKey;
5027
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5028
+ if (!factory) {
5029
+ return undefined;
5030
+ }
5031
+ this.lazyComputedKeys_["delete"](key);
5032
+ if (this.lazyComputedKeys_.size === 0) {
5033
+ this.lazyComputedKeys_ = undefined;
5034
+ }
5035
+ var computed = factory();
5036
+ this.values_.set(key, computed);
5037
+ return computed;
5038
+ };
5039
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5040
+ var _this$lazyObservableK;
5041
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5042
+ if (!factory) {
5043
+ return undefined;
5044
+ }
5045
+ this.lazyObservableKeys_["delete"](key);
5046
+ if (this.lazyObservableKeys_.size === 0) {
5047
+ this.lazyObservableKeys_ = undefined;
5048
+ }
5049
+ var observable = factory();
5050
+ this.values_.set(key, observable);
5051
+ return observable;
5000
5052
  };
5001
5053
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5002
- var observable = this.values_.get(key);
5054
+ var _ref2, _this$values_$get2;
5055
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5003
5056
  if (observable instanceof ComputedValue) {
5004
5057
  observable.set(newValue);
5005
5058
  return true;
@@ -5720,10 +5773,12 @@ function getAtom(thing, property) {
5720
5773
  return observable;
5721
5774
  }
5722
5775
  if (isObservableObject(thing)) {
5776
+ var _ref, _adm$values_$get;
5723
5777
  if (!property) {
5724
5778
  return die(26);
5725
5779
  }
5726
- var _observable = thing[$mobx].values_.get(property);
5780
+ var adm = thing[$mobx];
5781
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5727
5782
  if (!_observable) {
5728
5783
  die(27, property, getDebugName(thing));
5729
5784
  }