mobx 6.15.4 → 6.16.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.
@@ -941,17 +941,44 @@ function decorate_20223_$3(get, context) {
941
941
  var ann = this;
942
942
  var key = context.name,
943
943
  addInitializer = context.addInitializer;
944
- addInitializer(function () {
945
- var adm = asObservableObject(this)[$mobx];
944
+ var computedValues;
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.
948
+ function createComputedValue(target, adm) {
946
949
  var options = _extends({}, ann.options_, {
947
950
  get: get,
948
- context: this
951
+ context: target
949
952
  });
950
953
  options.name || (options.name = adm.name_ + "." + key.toString() );
951
- adm.values_.set(key, new ComputedValue(options));
954
+ return new ComputedValue(options);
955
+ }
956
+ addInitializer(function () {
957
+ var _adm$lazyComputedKeys;
958
+ var adm = asObservableObject(this)[$mobx];
959
+ var target = this;
960
+ var observable = adm.values_.get(key);
961
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
962
+ adm.values_["delete"](key);
963
+ }
964
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
965
+ return createComputedValue(target, adm);
966
+ });
952
967
  });
953
968
  return function () {
954
- return this[$mobx].getObservablePropValue_(key);
969
+ var adm = this[$mobx];
970
+ var observable = adm.values_.get(key);
971
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
972
+ var _computedValues;
973
+ var computed = (_computedValues = computedValues) == null ? void 0 : _computedValues.get(this);
974
+ if (!computed) {
975
+ var _computedValues2;
976
+ computed = createComputedValue(this, adm);
977
+ ((_computedValues2 = computedValues) != null ? _computedValues2 : computedValues = new WeakMap()).set(this, computed);
978
+ }
979
+ return computed.get();
980
+ }
981
+ return adm.getObservablePropValue_(key);
955
982
  };
956
983
  }
957
984
  function assertComputedDescriptor(adm, _ref, key, _ref2) {
@@ -989,44 +1016,37 @@ function decorate_20223_$4(desc, context) {
989
1016
  var ann = this;
990
1017
  var kind = context.kind,
991
1018
  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_;
1019
+ if (kind !== "accessor") {
1020
+ return;
1021
+ }
1022
+ // Defer ObservableValue construction until first access. The factory is
1023
+ // materialised by ObservableObjectAdministration on demand, so unused
1024
+ // fields on wide classes never pay the per-instance allocation cost.
1025
+ function registerLazy(target, value) {
1026
+ var _adm$lazyObservableKe;
1002
1027
  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
- };
1028
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1029
+ var _ann$options_$enhance, _ann$options_;
1030
+ 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);
1031
+ });
1032
+ return adm;
1028
1033
  }
1029
- return;
1034
+ return {
1035
+ get: function get() {
1036
+ var _this$$mobx;
1037
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1038
+ return adm.getObservablePropValue_(name);
1039
+ },
1040
+ set: function set(value) {
1041
+ var _this$$mobx2;
1042
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1043
+ return adm.setObservablePropValue_(name, value);
1044
+ },
1045
+ init: function init(value) {
1046
+ registerLazy(this, value);
1047
+ return value;
1048
+ }
1049
+ };
1030
1050
  }
1031
1051
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1032
1052
  var annotationType_ = _ref.annotationType_;
@@ -3214,13 +3234,18 @@ function interceptProperty(thing, property, handler) {
3214
3234
  }
3215
3235
 
3216
3236
  function _isComputed(value, property) {
3237
+ var _adm$lazyComputedKeys;
3217
3238
  if (property === undefined) {
3218
3239
  return isComputedValue(value);
3219
3240
  }
3220
3241
  if (isObservableObject(value) === false) {
3221
3242
  return false;
3222
3243
  }
3223
- if (!value[$mobx].values_.has(property)) {
3244
+ var adm = value[$mobx];
3245
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3246
+ return true;
3247
+ }
3248
+ if (!adm.values_.has(property)) {
3224
3249
  return false;
3225
3250
  }
3226
3251
  var atom = getAtom(value, property);
@@ -3248,7 +3273,9 @@ function _isObservable(value, property) {
3248
3273
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3249
3274
  }
3250
3275
  if (isObservableObject(value)) {
3251
- return value[$mobx].values_.has(property);
3276
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3277
+ var adm = value[$mobx];
3278
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3252
3279
  }
3253
3280
  return false;
3254
3281
  }
@@ -4446,6 +4473,18 @@ var ObservableMap = /*#__PURE__*/function () {
4446
4473
  }
4447
4474
  return this.dehanceValue_(undefined);
4448
4475
  };
4476
+ _proto.getOrInsert = function getOrInsert(key, value) {
4477
+ if (!this.has(key)) {
4478
+ this.set(key, value);
4479
+ }
4480
+ return this.get(key);
4481
+ };
4482
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4483
+ if (!this.has(key)) {
4484
+ this.set(key, callback(key));
4485
+ }
4486
+ return this.get(key);
4487
+ };
4449
4488
  _proto.dehanceValue_ = function dehanceValue_(value) {
4450
4489
  if (this.dehancer !== undefined) {
4451
4490
  return this.dehancer(value);
@@ -4979,6 +5018,8 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4979
5018
  this.isPlainObject_ = void 0;
4980
5019
  this.appliedAnnotations_ = void 0;
4981
5020
  this.pendingKeys_ = void 0;
5021
+ this.lazyComputedKeys_ = void 0;
5022
+ this.lazyObservableKeys_ = void 0;
4982
5023
  this.target_ = target_;
4983
5024
  this.values_ = values_;
4984
5025
  this.name_ = name_;
@@ -4996,10 +5037,42 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
4996
5037
  }
4997
5038
  var _proto = ObservableObjectAdministration.prototype;
4998
5039
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
4999
- return this.values_.get(key).get();
5040
+ var _ref, _this$values_$get;
5041
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5042
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5043
+ return observable.get();
5044
+ };
5045
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5046
+ var _this$lazyComputedKey;
5047
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5048
+ if (!factory) {
5049
+ return undefined;
5050
+ }
5051
+ this.lazyComputedKeys_["delete"](key);
5052
+ if (this.lazyComputedKeys_.size === 0) {
5053
+ this.lazyComputedKeys_ = undefined;
5054
+ }
5055
+ var computed = factory();
5056
+ this.values_.set(key, computed);
5057
+ return computed;
5058
+ };
5059
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5060
+ var _this$lazyObservableK;
5061
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5062
+ if (!factory) {
5063
+ return undefined;
5064
+ }
5065
+ this.lazyObservableKeys_["delete"](key);
5066
+ if (this.lazyObservableKeys_.size === 0) {
5067
+ this.lazyObservableKeys_ = undefined;
5068
+ }
5069
+ var observable = factory();
5070
+ this.values_.set(key, observable);
5071
+ return observable;
5000
5072
  };
5001
5073
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5002
- var observable = this.values_.get(key);
5074
+ var _ref2, _this$values_$get2;
5075
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5003
5076
  if (observable instanceof ComputedValue) {
5004
5077
  observable.set(newValue);
5005
5078
  return true;
@@ -5720,10 +5793,12 @@ function getAtom(thing, property) {
5720
5793
  return observable;
5721
5794
  }
5722
5795
  if (isObservableObject(thing)) {
5796
+ var _ref, _adm$values_$get;
5723
5797
  if (!property) {
5724
5798
  return die(26);
5725
5799
  }
5726
- var _observable = thing[$mobx].values_.get(property);
5800
+ var adm = thing[$mobx];
5801
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5727
5802
  if (!_observable) {
5728
5803
  die(27, property, getDebugName(thing));
5729
5804
  }