From: Evan You Date: Wed, 4 Sep 2019 22:20:47 +0000 (-0400) Subject: wip: disable tracking in all hooks X-Git-Tag: v3.0.0-alpha.0~824 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a6de6daa175172284888b0a4eedf7cffc74c1bce;p=thirdparty%2Fvuejs%2Fcore.git wip: disable tracking in all hooks --- diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index ce1a2f977c..9c701f3e9e 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -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) { diff --git a/packages/reactivity/src/index.ts b/packages/reactivity/src/index.ts index 5b9824b801..f98f36a2ab 100644 --- a/packages/reactivity/src/index.ts +++ b/packages/reactivity/src/index.ts @@ -12,6 +12,8 @@ export { computed, ComputedRef, ComputedOptions } from './computed' export { effect, stop, + pauseTracking, + resumeTracking, ITERATE_KEY, ReactiveEffect, ReactiveEffectOptions, diff --git a/packages/runtime-core/src/apiLifecycle.ts b/packages/runtime-core/src/apiLifecycle.ts index 60470d8280..a9f0918094 100644 --- a/packages/runtime-core/src/apiLifecycle.ts +++ b/packages/runtime-core/src/apiLifecycle.ts @@ -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__) {