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.
package/dist/mobx.esm.js CHANGED
@@ -942,14 +942,21 @@ function decorate_20223_$3(get, context) {
942
942
  var ann = this;
943
943
  var key = context.name,
944
944
  addInitializer = context.addInitializer;
945
+ // Defer ComputedValue creation until first access — avoids allocating
946
+ // ComputedValues for getters that are never read on a given instance.
947
+ // The factory is materialised by ObservableObjectAdministration on demand.
945
948
  addInitializer(function () {
949
+ var _adm$lazyComputedKeys;
946
950
  var adm = asObservableObject(this)[$mobx];
947
- var options = _extends({}, ann.options_, {
948
- get: get,
949
- context: this
951
+ var target = this;
952
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
953
+ var options = _extends({}, ann.options_, {
954
+ get: get,
955
+ context: target
956
+ });
957
+ options.name || (options.name = process.env.NODE_ENV !== "production" ? adm.name_ + "." + key.toString() : "ObservableObject." + key.toString());
958
+ return new ComputedValue(options);
950
959
  });
951
- options.name || (options.name = process.env.NODE_ENV !== "production" ? adm.name_ + "." + key.toString() : "ObservableObject." + key.toString());
952
- adm.values_.set(key, new ComputedValue(options));
953
960
  });
954
961
  return function () {
955
962
  return this[$mobx].getObservablePropValue_(key);
@@ -990,44 +997,37 @@ function decorate_20223_$4(desc, context) {
990
997
  var ann = this;
991
998
  var kind = context.kind,
992
999
  name = context.name;
993
- // The laziness here is not ideal... It's a workaround to how 2022.3 Decorators are implemented:
994
- // `addInitializer` callbacks are executed _before_ any accessors are defined (instead of the ideal-for-us right after each).
995
- // This means that, if we were to do our stuff in an `addInitializer`, we'd attempt to read a private slot
996
- // before it has been initialized. The runtime doesn't like that and throws a `Cannot read private member
997
- // from an object whose class did not declare it` error.
998
- // TODO: it seems that this will not be required anymore in the final version of the spec
999
- // See TODO: link
1000
- var initializedObjects = new WeakSet();
1001
- function initializeObservable(target, value) {
1002
- var _ann$options_$enhance, _ann$options_;
1000
+ if (kind !== "accessor") {
1001
+ return;
1002
+ }
1003
+ // Defer ObservableValue construction until first access. The factory is
1004
+ // materialised by ObservableObjectAdministration on demand, so unused
1005
+ // fields on wide classes never pay the per-instance allocation cost.
1006
+ function registerLazy(target, value) {
1007
+ var _adm$lazyObservableKe;
1003
1008
  var adm = asObservableObject(target)[$mobx];
1004
- var observable = new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer) != null ? _ann$options_$enhance : deepEnhancer, process.env.NODE_ENV !== "production" ? adm.name_ + "." + name.toString() : "ObservableObject." + name.toString(), false);
1005
- adm.values_.set(name, observable);
1006
- initializedObjects.add(target);
1007
- }
1008
- if (kind == "accessor") {
1009
- return {
1010
- get: function get() {
1011
- if (!initializedObjects.has(this)) {
1012
- initializeObservable(this, desc.get.call(this));
1013
- }
1014
- return this[$mobx].getObservablePropValue_(name);
1015
- },
1016
- set: function set(value) {
1017
- if (!initializedObjects.has(this)) {
1018
- initializeObservable(this, value);
1019
- }
1020
- return this[$mobx].setObservablePropValue_(name, value);
1021
- },
1022
- init: function init(value) {
1023
- if (!initializedObjects.has(this)) {
1024
- initializeObservable(this, value);
1025
- }
1026
- return value;
1027
- }
1028
- };
1009
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1010
+ var _ann$options_$enhance, _ann$options_;
1011
+ return new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer) != null ? _ann$options_$enhance : deepEnhancer, process.env.NODE_ENV !== "production" ? adm.name_ + "." + name.toString() : "ObservableObject." + name.toString(), false);
1012
+ });
1013
+ return adm;
1029
1014
  }
1030
- return;
1015
+ return {
1016
+ get: function get() {
1017
+ var _this$$mobx;
1018
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1019
+ return adm.getObservablePropValue_(name);
1020
+ },
1021
+ set: function set(value) {
1022
+ var _this$$mobx2;
1023
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1024
+ return adm.setObservablePropValue_(name, value);
1025
+ },
1026
+ init: function init(value) {
1027
+ registerLazy(this, value);
1028
+ return value;
1029
+ }
1030
+ };
1031
1031
  }
1032
1032
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1033
1033
  var annotationType_ = _ref.annotationType_;
@@ -3236,13 +3236,18 @@ function interceptProperty(thing, property, handler) {
3236
3236
  }
3237
3237
 
3238
3238
  function _isComputed(value, property) {
3239
+ var _adm$lazyComputedKeys;
3239
3240
  if (property === undefined) {
3240
3241
  return isComputedValue(value);
3241
3242
  }
3242
3243
  if (isObservableObject(value) === false) {
3243
3244
  return false;
3244
3245
  }
3245
- if (!value[$mobx].values_.has(property)) {
3246
+ var adm = value[$mobx];
3247
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3248
+ return true;
3249
+ }
3250
+ if (!adm.values_.has(property)) {
3246
3251
  return false;
3247
3252
  }
3248
3253
  var atom = getAtom(value, property);
@@ -3270,7 +3275,9 @@ function _isObservable(value, property) {
3270
3275
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3271
3276
  }
3272
3277
  if (isObservableObject(value)) {
3273
- return value[$mobx].values_.has(property);
3278
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3279
+ var adm = value[$mobx];
3280
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3274
3281
  }
3275
3282
  return false;
3276
3283
  }
@@ -4471,6 +4478,18 @@ var ObservableMap = /*#__PURE__*/function () {
4471
4478
  }
4472
4479
  return this.dehanceValue_(undefined);
4473
4480
  };
4481
+ _proto.getOrInsert = function getOrInsert(key, value) {
4482
+ if (!this.has(key)) {
4483
+ this.set(key, value);
4484
+ }
4485
+ return this.get(key);
4486
+ };
4487
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4488
+ if (!this.has(key)) {
4489
+ this.set(key, callback(key));
4490
+ }
4491
+ return this.get(key);
4492
+ };
4474
4493
  _proto.dehanceValue_ = function dehanceValue_(value) {
4475
4494
  if (this.dehancer !== undefined) {
4476
4495
  return this.dehancer(value);
@@ -5004,6 +5023,8 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
5004
5023
  this.isPlainObject_ = void 0;
5005
5024
  this.appliedAnnotations_ = void 0;
5006
5025
  this.pendingKeys_ = void 0;
5026
+ this.lazyComputedKeys_ = void 0;
5027
+ this.lazyObservableKeys_ = void 0;
5007
5028
  this.target_ = target_;
5008
5029
  this.values_ = values_;
5009
5030
  this.name_ = name_;
@@ -5021,10 +5042,42 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
5021
5042
  }
5022
5043
  var _proto = ObservableObjectAdministration.prototype;
5023
5044
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
5024
- return this.values_.get(key).get();
5045
+ var _ref, _this$values_$get;
5046
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5047
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5048
+ return observable.get();
5049
+ };
5050
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5051
+ var _this$lazyComputedKey;
5052
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5053
+ if (!factory) {
5054
+ return undefined;
5055
+ }
5056
+ this.lazyComputedKeys_["delete"](key);
5057
+ if (this.lazyComputedKeys_.size === 0) {
5058
+ this.lazyComputedKeys_ = undefined;
5059
+ }
5060
+ var computed = factory();
5061
+ this.values_.set(key, computed);
5062
+ return computed;
5063
+ };
5064
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5065
+ var _this$lazyObservableK;
5066
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5067
+ if (!factory) {
5068
+ return undefined;
5069
+ }
5070
+ this.lazyObservableKeys_["delete"](key);
5071
+ if (this.lazyObservableKeys_.size === 0) {
5072
+ this.lazyObservableKeys_ = undefined;
5073
+ }
5074
+ var observable = factory();
5075
+ this.values_.set(key, observable);
5076
+ return observable;
5025
5077
  };
5026
5078
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5027
- var observable = this.values_.get(key);
5079
+ var _ref2, _this$values_$get2;
5080
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5028
5081
  if (observable instanceof ComputedValue) {
5029
5082
  observable.set(newValue);
5030
5083
  return true;
@@ -5745,10 +5798,12 @@ function getAtom(thing, property) {
5745
5798
  return observable;
5746
5799
  }
5747
5800
  if (isObservableObject(thing)) {
5801
+ var _ref, _adm$values_$get;
5748
5802
  if (!property) {
5749
5803
  return die(26);
5750
5804
  }
5751
- var _observable = thing[$mobx].values_.get(property);
5805
+ var adm = thing[$mobx];
5806
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5752
5807
  if (!_observable) {
5753
5808
  die(27, property, getDebugName(thing));
5754
5809
  }