mobx 7.0.3 → 7.0.5

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.mjs CHANGED
@@ -819,6 +819,19 @@ function createAutoAnnotation(options) {
819
819
  extend_
820
820
  };
821
821
  }
822
+ // The auto annotation only depends on `deep` and `autoBind`, so share one instance per combination
823
+ // instead of allocating one for every object created with options
824
+ const autoAnnotations = [autoAnnotation];
825
+ function getAutoAnnotation(options) {
826
+ var _autoAnnotations$idx;
827
+ const deep = options.deep !== false;
828
+ const autoBind = !!options.autoBind;
829
+ const idx = (deep ? 0 : 1) | (autoBind ? 2 : 0);
830
+ return (_autoAnnotations$idx = autoAnnotations[idx]) != null ? _autoAnnotations$idx : autoAnnotations[idx] = createAutoAnnotation({
831
+ deep,
832
+ autoBind
833
+ });
834
+ }
822
835
  function make_$1(adm, key, descriptor, source) {
823
836
  var _this$options_3, _this$options_4;
824
837
  // getter -> computed
@@ -935,7 +948,7 @@ function getEnhancerFromOptions(options) {
935
948
  }
936
949
  function getAnnotationFromOptions(options) {
937
950
  var _options$defaultDecor;
938
- return options ? (_options$defaultDecor = options.defaultDecorator) != null ? _options$defaultDecor : createAutoAnnotation(options) : undefined;
951
+ return options ? (_options$defaultDecor = options.defaultDecorator) != null ? _options$defaultDecor : getAutoAnnotation(options) : undefined;
939
952
  }
940
953
  function getEnhancerFromAnnotation(annotation) {
941
954
  var _annotation$options_$, _annotation$options_;
@@ -1387,7 +1400,10 @@ class ComputedValue {
1387
1400
  reportObserved(this);
1388
1401
  if (shouldCompute(this)) {
1389
1402
  let prevTrackingContext = globalState.trackingContext;
1390
- if (this.keepAlive_ && !prevTrackingContext) {
1403
+ if (!prevTrackingContext && (this.keepAlive_ || this.isBeingObserved)) {
1404
+ // An observed or keep-alive computed can be recomputed by an untracked
1405
+ // read, for example from inside an action. Its dependencies are still
1406
+ // transitively observed and must fire their lifecycle hooks
1391
1407
  globalState.trackingContext = this;
1392
1408
  }
1393
1409
  if (this.trackAndCompute()) {
@@ -1993,34 +2009,41 @@ function endBatch() {
1993
2009
  // the nested call instead of recursing; the outer loop re-reads list.length on
1994
2010
  // every iteration, so it picks up anything the nested dispose() pushes onto the
1995
2011
  // same pendingUnobservations array.
1996
- if (!globalState.isRunningUnobservations) {
1997
- globalState.isRunningUnobservations = true;
1998
- try {
1999
- const list = globalState.pendingUnobservations;
2000
- for (let i = 0; i < list.length; i++) {
2001
- const observable = list[i];
2002
- observable.isPendingUnobservation = false;
2003
- if (!observable.observers_ || observable.observers_.size === 0) {
2004
- if (observable.isBeingObserved) {
2005
- // if this observable had reactive observers, trigger the hooks
2006
- observable.isBeingObserved = false;
2007
- observable.onBUO();
2008
- }
2009
- if (observable instanceof ComputedValue) {
2010
- // computed values are automatically teared down when the last observer leaves
2011
- // this process happens recursively, this computed might be the last observabe of another, etc..
2012
- observable.suspend_();
2013
- }
2014
- }
2012
+ if (!globalState.isRunningUnobservations && globalState.pendingUnobservations.length > 0) {
2013
+ runPendingUnobservations();
2014
+ }
2015
+ }
2016
+ }
2017
+ // Only called when there is something to unobserve, so the common endBatch() skips the
2018
+ // try/finally and the pendingUnobservations reallocation entirely
2019
+ function runPendingUnobservations() {
2020
+ globalState.isRunningUnobservations = true;
2021
+ try {
2022
+ const list = globalState.pendingUnobservations;
2023
+ for (let i = 0; i < list.length; i++) {
2024
+ const observable = list[i];
2025
+ observable.isPendingUnobservation = false;
2026
+ if (!observable.observers_ || observable.observers_.size === 0) {
2027
+ // release the empty Set so unobserved atoms don't keep paying for it
2028
+ observable.observers_ = null;
2029
+ if (observable.isBeingObserved) {
2030
+ // if this observable had reactive observers, trigger the hooks
2031
+ observable.isBeingObserved = false;
2032
+ observable.onBUO();
2033
+ }
2034
+ if (observable instanceof ComputedValue) {
2035
+ // computed values are automatically teared down when the last observer leaves
2036
+ // this process happens recursively, this computed might be the last observabe of another, etc..
2037
+ observable.suspend_();
2015
2038
  }
2016
- globalState.pendingUnobservations = [];
2017
- } finally {
2018
- // Always release the guard, even if an onBUO handler (user code) threw,
2019
- // otherwise every future endBatch() would see isRunningUnobservations
2020
- // stuck true and silently stop draining pendingUnobservations forever.
2021
- globalState.isRunningUnobservations = false;
2022
2039
  }
2023
2040
  }
2041
+ globalState.pendingUnobservations = [];
2042
+ } finally {
2043
+ // Always release the guard, even if an onBUO handler (user code) threw,
2044
+ // otherwise every future endBatch() would see isRunningUnobservations
2045
+ // stuck true and silently stop draining pendingUnobservations forever.
2046
+ globalState.isRunningUnobservations = false;
2024
2047
  }
2025
2048
  }
2026
2049
  /**
@@ -3311,8 +3334,13 @@ function hasInterceptors(interceptable) {
3311
3334
  return interceptable.interceptors_ !== undefined && interceptable.interceptors_.length > 0;
3312
3335
  }
3313
3336
  function registerInterceptor(interceptable, handler) {
3314
- const interceptors = interceptable.interceptors_ || (interceptable.interceptors_ = []);
3315
- interceptors.push(handler);
3337
+ let interceptors = interceptable.interceptors_;
3338
+ if (interceptors) {
3339
+ interceptors.push(handler);
3340
+ } else {
3341
+ // Allocate at the exact size: pushing onto an empty array over-allocates its backing store
3342
+ interceptors = interceptable.interceptors_ = [handler];
3343
+ }
3316
3344
  return once(() => {
3317
3345
  const idx = interceptors.indexOf(handler);
3318
3346
  if (idx !== -1) {
@@ -3344,8 +3372,13 @@ function hasListeners(listenable) {
3344
3372
  return listenable.changeListeners_ !== undefined && listenable.changeListeners_.length > 0;
3345
3373
  }
3346
3374
  function registerListener(listenable, handler) {
3347
- const listeners = listenable.changeListeners_ || (listenable.changeListeners_ = []);
3348
- listeners.push(handler);
3375
+ let listeners = listenable.changeListeners_;
3376
+ if (listeners) {
3377
+ listeners.push(handler);
3378
+ } else {
3379
+ // Allocate at the exact size: pushing onto an empty array over-allocates its backing store
3380
+ listeners = listenable.changeListeners_ = [handler];
3381
+ }
3349
3382
  return once(() => {
3350
3383
  const idx = listeners.indexOf(handler);
3351
3384
  if (idx !== -1) {