wip: disable tracking in all hooks

This commit is contained in:
Evan You 2019-09-04 18:20:47 -04:00
parent 13298bc4fa
commit a6de6daa17
3 changed files with 20 additions and 0 deletions

View 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( export function track(
target: any, target: any,
type: OperationTypes, type: OperationTypes,
key?: string | symbol key?: string | symbol
) { ) {
if (!shouldTrack) {
return
}
const effect = activeReactiveEffectStack[activeReactiveEffectStack.length - 1] const effect = activeReactiveEffectStack[activeReactiveEffectStack.length - 1]
if (effect) { if (effect) {
if (type === OperationTypes.ITERATE) { if (type === OperationTypes.ITERATE) {

View File

@ -12,6 +12,8 @@ export { computed, ComputedRef, ComputedOptions } from './computed'
export { export {
effect, effect,
stop, stop,
pauseTracking,
resumeTracking,
ITERATE_KEY, ITERATE_KEY,
ReactiveEffect, ReactiveEffect,
ReactiveEffectOptions, ReactiveEffectOptions,

View File

@ -8,6 +8,7 @@ import {
import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling' import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
import { warn } from './warning' import { warn } from './warning'
import { capitalize } from '@vue/shared' import { capitalize } from '@vue/shared'
import { pauseTracking, resumeTracking } from '@vue/reactivity'
function injectHook( function injectHook(
type: LifecycleHooks, type: LifecycleHooks,
@ -16,12 +17,16 @@ function injectHook(
) { ) {
if (target) { if (target) {
;(target[type] || (target[type] = [])).push((...args: any[]) => { ;(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. // Set currentInstance during hook invocation.
// This assumes the hook does not synchronously trigger other hooks, which // This assumes the hook does not synchronously trigger other hooks, which
// can only be false when the user does something really funky. // can only be false when the user does something really funky.
setCurrentInstance(target) setCurrentInstance(target)
const res = callWithAsyncErrorHandling(hook, target, type, args) const res = callWithAsyncErrorHandling(hook, target, type, args)
setCurrentInstance(null) setCurrentInstance(null)
resumeTracking()
return res return res
}) })
} else if (__DEV__) { } else if (__DEV__) {