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.
@@ -947,17 +947,44 @@
947
947
  var ann = this;
948
948
  var key = context.name,
949
949
  addInitializer = context.addInitializer;
950
- addInitializer(function () {
951
- var adm = asObservableObject(this)[$mobx];
950
+ var computedValues;
951
+ // Defer ComputedValue creation until first access — avoids allocating
952
+ // ComputedValues for getters that are never read on a given instance.
953
+ // The factory is materialised by ObservableObjectAdministration on demand.
954
+ function createComputedValue(target, adm) {
952
955
  var options = _extends({}, ann.options_, {
953
956
  get: get,
954
- context: this
957
+ context: target
955
958
  });
956
959
  options.name || (options.name = adm.name_ + "." + key.toString() );
957
- adm.values_.set(key, new ComputedValue(options));
960
+ return new ComputedValue(options);
961
+ }
962
+ addInitializer(function () {
963
+ var _adm$lazyComputedKeys;
964
+ var adm = asObservableObject(this)[$mobx];
965
+ var target = this;
966
+ var observable = adm.values_.get(key);
967
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
968
+ adm.values_["delete"](key);
969
+ }
970
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
971
+ return createComputedValue(target, adm);
972
+ });
958
973
  });
959
974
  return function () {
960
- return this[$mobx].getObservablePropValue_(key);
975
+ var adm = this[$mobx];
976
+ var observable = adm.values_.get(key);
977
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
978
+ var _computedValues;
979
+ var computed = (_computedValues = computedValues) == null ? void 0 : _computedValues.get(this);
980
+ if (!computed) {
981
+ var _computedValues2;
982
+ computed = createComputedValue(this, adm);
983
+ ((_computedValues2 = computedValues) != null ? _computedValues2 : computedValues = new WeakMap()).set(this, computed);
984
+ }
985
+ return computed.get();
986
+ }
987
+ return adm.getObservablePropValue_(key);
961
988
  };
962
989
  }
963
990
  function assertComputedDescriptor(adm, _ref, key, _ref2) {
@@ -995,44 +1022,37 @@
995
1022
  var ann = this;
996
1023
  var kind = context.kind,
997
1024
  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_;
1025
+ if (kind !== "accessor") {
1026
+ return;
1027
+ }
1028
+ // Defer ObservableValue construction until first access. The factory is
1029
+ // materialised by ObservableObjectAdministration on demand, so unused
1030
+ // fields on wide classes never pay the per-instance allocation cost.
1031
+ function registerLazy(target, value) {
1032
+ var _adm$lazyObservableKe;
1008
1033
  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
- };
1034
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1035
+ var _ann$options_$enhance, _ann$options_;
1036
+ 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);
1037
+ });
1038
+ return adm;
1034
1039
  }
1035
- return;
1040
+ return {
1041
+ get: function get() {
1042
+ var _this$$mobx;
1043
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1044
+ return adm.getObservablePropValue_(name);
1045
+ },
1046
+ set: function set(value) {
1047
+ var _this$$mobx2;
1048
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1049
+ return adm.setObservablePropValue_(name, value);
1050
+ },
1051
+ init: function init(value) {
1052
+ registerLazy(this, value);
1053
+ return value;
1054
+ }
1055
+ };
1036
1056
  }
1037
1057
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1038
1058
  var annotationType_ = _ref.annotationType_;
@@ -3220,13 +3240,18 @@
3220
3240
  }
3221
3241
 
3222
3242
  function _isComputed(value, property) {
3243
+ var _adm$lazyComputedKeys;
3223
3244
  if (property === undefined) {
3224
3245
  return isComputedValue(value);
3225
3246
  }
3226
3247
  if (isObservableObject(value) === false) {
3227
3248
  return false;
3228
3249
  }
3229
- if (!value[$mobx].values_.has(property)) {
3250
+ var adm = value[$mobx];
3251
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3252
+ return true;
3253
+ }
3254
+ if (!adm.values_.has(property)) {
3230
3255
  return false;
3231
3256
  }
3232
3257
  var atom = getAtom(value, property);
@@ -3254,7 +3279,9 @@
3254
3279
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3255
3280
  }
3256
3281
  if (isObservableObject(value)) {
3257
- return value[$mobx].values_.has(property);
3282
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3283
+ var adm = value[$mobx];
3284
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3258
3285
  }
3259
3286
  return false;
3260
3287
  }
@@ -4452,6 +4479,18 @@
4452
4479
  }
4453
4480
  return this.dehanceValue_(undefined);
4454
4481
  };
4482
+ _proto.getOrInsert = function getOrInsert(key, value) {
4483
+ if (!this.has(key)) {
4484
+ this.set(key, value);
4485
+ }
4486
+ return this.get(key);
4487
+ };
4488
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4489
+ if (!this.has(key)) {
4490
+ this.set(key, callback(key));
4491
+ }
4492
+ return this.get(key);
4493
+ };
4455
4494
  _proto.dehanceValue_ = function dehanceValue_(value) {
4456
4495
  if (this.dehancer !== undefined) {
4457
4496
  return this.dehancer(value);
@@ -4985,6 +5024,8 @@
4985
5024
  this.isPlainObject_ = void 0;
4986
5025
  this.appliedAnnotations_ = void 0;
4987
5026
  this.pendingKeys_ = void 0;
5027
+ this.lazyComputedKeys_ = void 0;
5028
+ this.lazyObservableKeys_ = void 0;
4988
5029
  this.target_ = target_;
4989
5030
  this.values_ = values_;
4990
5031
  this.name_ = name_;
@@ -5002,10 +5043,42 @@
5002
5043
  }
5003
5044
  var _proto = ObservableObjectAdministration.prototype;
5004
5045
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
5005
- return this.values_.get(key).get();
5046
+ var _ref, _this$values_$get;
5047
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5048
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5049
+ return observable.get();
5050
+ };
5051
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5052
+ var _this$lazyComputedKey;
5053
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5054
+ if (!factory) {
5055
+ return undefined;
5056
+ }
5057
+ this.lazyComputedKeys_["delete"](key);
5058
+ if (this.lazyComputedKeys_.size === 0) {
5059
+ this.lazyComputedKeys_ = undefined;
5060
+ }
5061
+ var computed = factory();
5062
+ this.values_.set(key, computed);
5063
+ return computed;
5064
+ };
5065
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5066
+ var _this$lazyObservableK;
5067
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5068
+ if (!factory) {
5069
+ return undefined;
5070
+ }
5071
+ this.lazyObservableKeys_["delete"](key);
5072
+ if (this.lazyObservableKeys_.size === 0) {
5073
+ this.lazyObservableKeys_ = undefined;
5074
+ }
5075
+ var observable = factory();
5076
+ this.values_.set(key, observable);
5077
+ return observable;
5006
5078
  };
5007
5079
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5008
- var observable = this.values_.get(key);
5080
+ var _ref2, _this$values_$get2;
5081
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5009
5082
  if (observable instanceof ComputedValue) {
5010
5083
  observable.set(newValue);
5011
5084
  return true;
@@ -5726,10 +5799,12 @@
5726
5799
  return observable;
5727
5800
  }
5728
5801
  if (isObservableObject(thing)) {
5802
+ var _ref, _adm$values_$get;
5729
5803
  if (!property) {
5730
5804
  return die(26);
5731
5805
  }
5732
- var _observable = thing[$mobx].values_.get(property);
5806
+ var adm = thing[$mobx];
5807
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5733
5808
  if (!_observable) {
5734
5809
  die(27, property, getDebugName(thing));
5735
5810
  }