]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: disable tracking in all hooks
authorEvan You <yyx990803@gmail.com>
Wed, 4 Sep 2019 22:20:47 +0000 (18:20 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 4 Sep 2019 22:20:47 +0000 (18:20 -0400)
packages/reactivity/src/effect.ts
packages/reactivity/src/index.ts
packages/runtime-core/src/apiLifecycle.ts

index ce1a2f977cf44b250428d7ab08ab6931599dff8a..9c701f3e9e68ba4644ac1d31ae8281be51202096 100644 (file)
@@ -107,11 +107,24 @@ function cleanup(effect: ReactiveEffect) {
   }
 }
 
+let shouldTrack = true
+
+export function pauseTracking() {
+  shouldTrack = false
+}
+
+export function resumeTracking() {
+  shouldTrack = true
+}
+
 export function track(
   target: any,
   type: OperationTypes,
   key?: string | symbol
 ) {
+  if (!shouldTrack) {
+    return
+  }
   const effect = activeReactiveEffectStack[activeReactiveEffectStack.length - 1]
   if (effect) {
     if (type === OperationTypes.ITERATE) {
index 5b9824b801cee25fe0a56c04d12fdc336ba18ff9..f98f36a2ab7f3eb9570039398a0a2950dd7a9efd 100644 (file)
@@ -12,6 +12,8 @@ export { computed, ComputedRef, ComputedOptions } from './computed'
 export {
   effect,
   stop,
+  pauseTracking,
+  resumeTracking,
   ITERATE_KEY,
   ReactiveEffect,
   ReactiveEffectOptions,
index 60470d8280b63b76744a293c1a197267cdbb3f8d..a9f0918094c25981870df14e875f009e04335ada 100644 (file)
@@ -8,6 +8,7 @@ import {
 import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
 import { warn } from './warning'
 import { capitalize } from '@vue/shared'
+import { pauseTracking, resumeTracking } from '@vue/reactivity'
 
 function injectHook(
   type: LifecycleHooks,
@@ -16,12 +17,16 @@ function injectHook(
 ) {
   if (target) {
     ;(target[type] || (target[type] = [])).push((...args: any[]) => {
+      // disable tracking inside all lifecycle hooks
+      // since they can potentially be called inside effects.
+      pauseTracking()
       // Set currentInstance during hook invocation.
       // This assumes the hook does not synchronously trigger other hooks, which
       // can only be false when the user does something really funky.
       setCurrentInstance(target)
       const res = callWithAsyncErrorHandling(hook, target, type, args)
       setCurrentInstance(null)
+      resumeTracking()
       return res
     })
   } else if (__DEV__) {