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.
package/dist/mobx.esm.js CHANGED
@@ -942,17 +942,44 @@ function decorate_20223_$3(get, context) {
942
942
  var ann = this;
943
943
  var key = context.name,
944
944
  addInitializer = context.addInitializer;
945
- addInitializer(function () {
946
- var adm = asObservableObject(this)[$mobx];
945
+ var computedValues;
946
+ // Defer ComputedValue creation until first access — avoids allocating
947
+ // ComputedValues for getters that are never read on a given instance.
948
+ // The factory is materialised by ObservableObjectAdministration on demand.
949
+ function createComputedValue(target, adm) {
947
950
  var options = _extends({}, ann.options_, {
948
951
  get: get,
949
- context: this
952
+ context: target
950
953
  });
951
954
  options.name || (options.name = process.env.NODE_ENV !== "production" ? adm.name_ + "." + key.toString() : "ObservableObject." + key.toString());
952
- adm.values_.set(key, new ComputedValue(options));
955
+ return new ComputedValue(options);
956
+ }
957
+ addInitializer(function () {
958
+ var _adm$lazyComputedKeys;
959
+ var adm = asObservableObject(this)[$mobx];
960
+ var target = this;
961
+ var observable = adm.values_.get(key);
962
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
963
+ adm.values_["delete"](key);
964
+ }
965
+ ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null ? _adm$lazyComputedKeys : adm.lazyComputedKeys_ = new Map()).set(key, function () {
966
+ return createComputedValue(target, adm);
967
+ });
953
968
  });
954
969
  return function () {
955
- return this[$mobx].getObservablePropValue_(key);
970
+ var adm = this[$mobx];
971
+ var observable = adm.values_.get(key);
972
+ if (observable instanceof ComputedValue && observable.derivation !== get) {
973
+ var _computedValues;
974
+ var computed = (_computedValues = computedValues) == null ? void 0 : _computedValues.get(this);
975
+ if (!computed) {
976
+ var _computedValues2;
977
+ computed = createComputedValue(this, adm);
978
+ ((_computedValues2 = computedValues) != null ? _computedValues2 : computedValues = new WeakMap()).set(this, computed);
979
+ }
980
+ return computed.get();
981
+ }
982
+ return adm.getObservablePropValue_(key);
956
983
  };
957
984
  }
958
985
  function assertComputedDescriptor(adm, _ref, key, _ref2) {
@@ -990,44 +1017,37 @@ function decorate_20223_$4(desc, context) {
990
1017
  var ann = this;
991
1018
  var kind = context.kind,
992
1019
  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_;
1020
+ if (kind !== "accessor") {
1021
+ return;
1022
+ }
1023
+ // Defer ObservableValue construction until first access. The factory is
1024
+ // materialised by ObservableObjectAdministration on demand, so unused
1025
+ // fields on wide classes never pay the per-instance allocation cost.
1026
+ function registerLazy(target, value) {
1027
+ var _adm$lazyObservableKe;
1003
1028
  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
- };
1029
+ ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, function () {
1030
+ var _ann$options_$enhance, _ann$options_;
1031
+ 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);
1032
+ });
1033
+ return adm;
1029
1034
  }
1030
- return;
1035
+ return {
1036
+ get: function get() {
1037
+ var _this$$mobx;
1038
+ var adm = (_this$$mobx = this[$mobx]) != null ? _this$$mobx : registerLazy(this, desc.get.call(this));
1039
+ return adm.getObservablePropValue_(name);
1040
+ },
1041
+ set: function set(value) {
1042
+ var _this$$mobx2;
1043
+ var adm = (_this$$mobx2 = this[$mobx]) != null ? _this$$mobx2 : registerLazy(this, value);
1044
+ return adm.setObservablePropValue_(name, value);
1045
+ },
1046
+ init: function init(value) {
1047
+ registerLazy(this, value);
1048
+ return value;
1049
+ }
1050
+ };
1031
1051
  }
1032
1052
  function assertObservableDescriptor(adm, _ref, key, descriptor) {
1033
1053
  var annotationType_ = _ref.annotationType_;
@@ -3236,13 +3256,18 @@ function interceptProperty(thing, property, handler) {
3236
3256
  }
3237
3257
 
3238
3258
  function _isComputed(value, property) {
3259
+ var _adm$lazyComputedKeys;
3239
3260
  if (property === undefined) {
3240
3261
  return isComputedValue(value);
3241
3262
  }
3242
3263
  if (isObservableObject(value) === false) {
3243
3264
  return false;
3244
3265
  }
3245
- if (!value[$mobx].values_.has(property)) {
3266
+ var adm = value[$mobx];
3267
+ if ((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) {
3268
+ return true;
3269
+ }
3270
+ if (!adm.values_.has(property)) {
3246
3271
  return false;
3247
3272
  }
3248
3273
  var atom = getAtom(value, property);
@@ -3270,7 +3295,9 @@ function _isObservable(value, property) {
3270
3295
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
3271
3296
  }
3272
3297
  if (isObservableObject(value)) {
3273
- return value[$mobx].values_.has(property);
3298
+ var _adm$lazyComputedKeys, _adm$lazyObservableKe;
3299
+ var adm = value[$mobx];
3300
+ return adm.values_.has(property) || !!((_adm$lazyComputedKeys = adm.lazyComputedKeys_) != null && _adm$lazyComputedKeys.has(property)) || !!((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null && _adm$lazyObservableKe.has(property));
3274
3301
  }
3275
3302
  return false;
3276
3303
  }
@@ -4471,6 +4498,18 @@ var ObservableMap = /*#__PURE__*/function () {
4471
4498
  }
4472
4499
  return this.dehanceValue_(undefined);
4473
4500
  };
4501
+ _proto.getOrInsert = function getOrInsert(key, value) {
4502
+ if (!this.has(key)) {
4503
+ this.set(key, value);
4504
+ }
4505
+ return this.get(key);
4506
+ };
4507
+ _proto.getOrInsertComputed = function getOrInsertComputed(key, callback) {
4508
+ if (!this.has(key)) {
4509
+ this.set(key, callback(key));
4510
+ }
4511
+ return this.get(key);
4512
+ };
4474
4513
  _proto.dehanceValue_ = function dehanceValue_(value) {
4475
4514
  if (this.dehancer !== undefined) {
4476
4515
  return this.dehancer(value);
@@ -5004,6 +5043,8 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
5004
5043
  this.isPlainObject_ = void 0;
5005
5044
  this.appliedAnnotations_ = void 0;
5006
5045
  this.pendingKeys_ = void 0;
5046
+ this.lazyComputedKeys_ = void 0;
5047
+ this.lazyObservableKeys_ = void 0;
5007
5048
  this.target_ = target_;
5008
5049
  this.values_ = values_;
5009
5050
  this.name_ = name_;
@@ -5021,10 +5062,42 @@ var ObservableObjectAdministration = /*#__PURE__*/function () {
5021
5062
  }
5022
5063
  var _proto = ObservableObjectAdministration.prototype;
5023
5064
  _proto.getObservablePropValue_ = function getObservablePropValue_(key) {
5024
- return this.values_.get(key).get();
5065
+ var _ref, _this$values_$get;
5066
+ // Hot path: single map lookup. Lazy entries (rare) take the materialise branch.
5067
+ var observable = (_ref = (_this$values_$get = this.values_.get(key)) != null ? _this$values_$get : this.materializeLazyComputed_(key)) != null ? _ref : this.materializeLazyObservable_(key);
5068
+ return observable.get();
5069
+ };
5070
+ _proto.materializeLazyComputed_ = function materializeLazyComputed_(key) {
5071
+ var _this$lazyComputedKey;
5072
+ var factory = (_this$lazyComputedKey = this.lazyComputedKeys_) == null ? void 0 : _this$lazyComputedKey.get(key);
5073
+ if (!factory) {
5074
+ return undefined;
5075
+ }
5076
+ this.lazyComputedKeys_["delete"](key);
5077
+ if (this.lazyComputedKeys_.size === 0) {
5078
+ this.lazyComputedKeys_ = undefined;
5079
+ }
5080
+ var computed = factory();
5081
+ this.values_.set(key, computed);
5082
+ return computed;
5083
+ };
5084
+ _proto.materializeLazyObservable_ = function materializeLazyObservable_(key) {
5085
+ var _this$lazyObservableK;
5086
+ var factory = (_this$lazyObservableK = this.lazyObservableKeys_) == null ? void 0 : _this$lazyObservableK.get(key);
5087
+ if (!factory) {
5088
+ return undefined;
5089
+ }
5090
+ this.lazyObservableKeys_["delete"](key);
5091
+ if (this.lazyObservableKeys_.size === 0) {
5092
+ this.lazyObservableKeys_ = undefined;
5093
+ }
5094
+ var observable = factory();
5095
+ this.values_.set(key, observable);
5096
+ return observable;
5025
5097
  };
5026
5098
  _proto.setObservablePropValue_ = function setObservablePropValue_(key, newValue) {
5027
- var observable = this.values_.get(key);
5099
+ var _ref2, _this$values_$get2;
5100
+ var observable = (_ref2 = (_this$values_$get2 = this.values_.get(key)) != null ? _this$values_$get2 : this.materializeLazyComputed_(key)) != null ? _ref2 : this.materializeLazyObservable_(key);
5028
5101
  if (observable instanceof ComputedValue) {
5029
5102
  observable.set(newValue);
5030
5103
  return true;
@@ -5745,10 +5818,12 @@ function getAtom(thing, property) {
5745
5818
  return observable;
5746
5819
  }
5747
5820
  if (isObservableObject(thing)) {
5821
+ var _ref, _adm$values_$get;
5748
5822
  if (!property) {
5749
5823
  return die(26);
5750
5824
  }
5751
- var _observable = thing[$mobx].values_.get(property);
5825
+ var adm = thing[$mobx];
5826
+ var _observable = (_ref = (_adm$values_$get = adm.values_.get(property)) != null ? _adm$values_$get : adm.materializeLazyComputed_(property)) != null ? _ref : adm.materializeLazyObservable_(property);
5752
5827
  if (!_observable) {
5753
5828
  die(27, property, getDebugName(thing));
5754
5829
  }