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.
@@ -822,6 +822,19 @@
822
822
  extend_
823
823
  };
824
824
  }
825
+ // The auto annotation only depends on `deep` and `autoBind`, so share one instance per combination
826
+ // instead of allocating one for every object created with options
827
+ const autoAnnotations = [autoAnnotation];
828
+ function getAutoAnnotation(options) {
829
+ var _autoAnnotations$idx;
830
+ const deep = options.deep !== false;
831
+ const autoBind = !!options.autoBind;
832
+ const idx = (deep ? 0 : 1) | (autoBind ? 2 : 0);
833
+ return (_autoAnnotations$idx = autoAnnotations[idx]) != null ? _autoAnnotations$idx : autoAnnotations[idx] = createAutoAnnotation({
834
+ deep,
835
+ autoBind
836
+ });
837
+ }
825
838
  function make_$1(adm, key, descriptor, source) {
826
839
  var _this$options_3, _this$options_4;
827
840
  // getter -> computed
@@ -938,7 +951,7 @@
938
951
  }
939
952
  function getAnnotationFromOptions(options) {
940
953
  var _options$defaultDecor;
941
- return options ? (_options$defaultDecor = options.defaultDecorator) != null ? _options$defaultDecor : createAutoAnnotation(options) : undefined;
954
+ return options ? (_options$defaultDecor = options.defaultDecorator) != null ? _options$defaultDecor : getAutoAnnotation(options) : undefined;
942
955
  }
943
956
  function getEnhancerFromAnnotation(annotation) {
944
957
  var _annotation$options_$, _annotation$options_;
@@ -1990,34 +2003,41 @@
1990
2003
  // the nested call instead of recursing; the outer loop re-reads list.length on
1991
2004
  // every iteration, so it picks up anything the nested dispose() pushes onto the
1992
2005
  // same pendingUnobservations array.
1993
- if (!globalState.isRunningUnobservations) {
1994
- globalState.isRunningUnobservations = true;
1995
- try {
1996
- const list = globalState.pendingUnobservations;
1997
- for (let i = 0; i < list.length; i++) {
1998
- const observable = list[i];
1999
- observable.isPendingUnobservation = false;
2000
- if (!observable.observers_ || observable.observers_.size === 0) {
2001
- if (observable.isBeingObserved) {
2002
- // if this observable had reactive observers, trigger the hooks
2003
- observable.isBeingObserved = false;
2004
- observable.onBUO();
2005
- }
2006
- if (observable instanceof ComputedValue) {
2007
- // computed values are automatically teared down when the last observer leaves
2008
- // this process happens recursively, this computed might be the last observabe of another, etc..
2009
- observable.suspend_();
2010
- }
2011
- }
2006
+ if (!globalState.isRunningUnobservations && globalState.pendingUnobservations.length > 0) {
2007
+ runPendingUnobservations();
2008
+ }
2009
+ }
2010
+ }
2011
+ // Only called when there is something to unobserve, so the common endBatch() skips the
2012
+ // try/finally and the pendingUnobservations reallocation entirely
2013
+ function runPendingUnobservations() {
2014
+ globalState.isRunningUnobservations = true;
2015
+ try {
2016
+ const list = globalState.pendingUnobservations;
2017
+ for (let i = 0; i < list.length; i++) {
2018
+ const observable = list[i];
2019
+ observable.isPendingUnobservation = false;
2020
+ if (!observable.observers_ || observable.observers_.size === 0) {
2021
+ // release the empty Set so unobserved atoms don't keep paying for it
2022
+ observable.observers_ = null;
2023
+ if (observable.isBeingObserved) {
2024
+ // if this observable had reactive observers, trigger the hooks
2025
+ observable.isBeingObserved = false;
2026
+ observable.onBUO();
2027
+ }
2028
+ if (observable instanceof ComputedValue) {
2029
+ // computed values are automatically teared down when the last observer leaves
2030
+ // this process happens recursively, this computed might be the last observabe of another, etc..
2031
+ observable.suspend_();
2012
2032
  }
2013
- globalState.pendingUnobservations = [];
2014
- } finally {
2015
- // Always release the guard, even if an onBUO handler (user code) threw,
2016
- // otherwise every future endBatch() would see isRunningUnobservations
2017
- // stuck true and silently stop draining pendingUnobservations forever.
2018
- globalState.isRunningUnobservations = false;
2019
2033
  }
2020
2034
  }
2035
+ globalState.pendingUnobservations = [];
2036
+ } finally {
2037
+ // Always release the guard, even if an onBUO handler (user code) threw,
2038
+ // otherwise every future endBatch() would see isRunningUnobservations
2039
+ // stuck true and silently stop draining pendingUnobservations forever.
2040
+ globalState.isRunningUnobservations = false;
2021
2041
  }
2022
2042
  }
2023
2043
  /**
@@ -3296,8 +3316,13 @@
3296
3316
  return interceptable.interceptors_ !== undefined && interceptable.interceptors_.length > 0;
3297
3317
  }
3298
3318
  function registerInterceptor(interceptable, handler) {
3299
- const interceptors = interceptable.interceptors_ || (interceptable.interceptors_ = []);
3300
- interceptors.push(handler);
3319
+ let interceptors = interceptable.interceptors_;
3320
+ if (interceptors) {
3321
+ interceptors.push(handler);
3322
+ } else {
3323
+ // Allocate at the exact size: pushing onto an empty array over-allocates its backing store
3324
+ interceptors = interceptable.interceptors_ = [handler];
3325
+ }
3301
3326
  return once(() => {
3302
3327
  const idx = interceptors.indexOf(handler);
3303
3328
  if (idx !== -1) {
@@ -3329,8 +3354,13 @@
3329
3354
  return listenable.changeListeners_ !== undefined && listenable.changeListeners_.length > 0;
3330
3355
  }
3331
3356
  function registerListener(listenable, handler) {
3332
- const listeners = listenable.changeListeners_ || (listenable.changeListeners_ = []);
3333
- listeners.push(handler);
3357
+ let listeners = listenable.changeListeners_;
3358
+ if (listeners) {
3359
+ listeners.push(handler);
3360
+ } else {
3361
+ // Allocate at the exact size: pushing onto an empty array over-allocates its backing store
3362
+ listeners = listenable.changeListeners_ = [handler];
3363
+ }
3334
3364
  return once(() => {
3335
3365
  const idx = listeners.indexOf(handler);
3336
3366
  if (idx !== -1) {