mobx 7.0.4 → 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_;
@@ -1996,34 +2009,41 @@ function endBatch() {
1996
2009
  // the nested call instead of recursing; the outer loop re-reads list.length on
1997
2010
  // every iteration, so it picks up anything the nested dispose() pushes onto the
1998
2011
  // same pendingUnobservations array.
1999
- if (!globalState.isRunningUnobservations) {
2000
- globalState.isRunningUnobservations = true;
2001
- try {
2002
- const list = globalState.pendingUnobservations;
2003
- for (let i = 0; i < list.length; i++) {
2004
- const observable = list[i];
2005
- observable.isPendingUnobservation = false;
2006
- if (!observable.observers_ || observable.observers_.size === 0) {
2007
- if (observable.isBeingObserved) {
2008
- // if this observable had reactive observers, trigger the hooks
2009
- observable.isBeingObserved = false;
2010
- observable.onBUO();
2011
- }
2012
- if (observable instanceof ComputedValue) {
2013
- // computed values are automatically teared down when the last observer leaves
2014
- // this process happens recursively, this computed might be the last observabe of another, etc..
2015
- observable.suspend_();
2016
- }
2017
- }
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_();
2018
2038
  }
2019
- globalState.pendingUnobservations = [];
2020
- } finally {
2021
- // Always release the guard, even if an onBUO handler (user code) threw,
2022
- // otherwise every future endBatch() would see isRunningUnobservations
2023
- // stuck true and silently stop draining pendingUnobservations forever.
2024
- globalState.isRunningUnobservations = false;
2025
2039
  }
2026
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;
2027
2047
  }
2028
2048
  }
2029
2049
  /**
@@ -3314,8 +3334,13 @@ function hasInterceptors(interceptable) {
3314
3334
  return interceptable.interceptors_ !== undefined && interceptable.interceptors_.length > 0;
3315
3335
  }
3316
3336
  function registerInterceptor(interceptable, handler) {
3317
- const interceptors = interceptable.interceptors_ || (interceptable.interceptors_ = []);
3318
- 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
+ }
3319
3344
  return once(() => {
3320
3345
  const idx = interceptors.indexOf(handler);
3321
3346
  if (idx !== -1) {
@@ -3347,8 +3372,13 @@ function hasListeners(listenable) {
3347
3372
  return listenable.changeListeners_ !== undefined && listenable.changeListeners_.length > 0;
3348
3373
  }
3349
3374
  function registerListener(listenable, handler) {
3350
- const listeners = listenable.changeListeners_ || (listenable.changeListeners_ = []);
3351
- 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
+ }
3352
3382
  return once(() => {
3353
3383
  const idx = listeners.indexOf(handler);
3354
3384
  if (idx !== -1) {