wip: watcher cleanup
This commit is contained in:
parent
693938d956
commit
e5e56bb358
@ -7,6 +7,7 @@ function injectHook(
|
|||||||
) {
|
) {
|
||||||
if (target) {
|
if (target) {
|
||||||
// TODO inject a error-handling wrapped version of the hook
|
// TODO inject a error-handling wrapped version of the hook
|
||||||
|
// TODO also set currentInstance when calling the hook
|
||||||
;(target[name] || (target[name] = [])).push(hook)
|
;(target[name] || (target[name] = [])).push(hook)
|
||||||
} else {
|
} else {
|
||||||
// TODO warn
|
// TODO warn
|
||||||
|
@ -31,7 +31,7 @@ import {
|
|||||||
} from '@vue/observer'
|
} from '@vue/observer'
|
||||||
import { currentInstance } from './component'
|
import { currentInstance } from './component'
|
||||||
import { queueJob, queuePostFlushCb } from './scheduler'
|
import { queueJob, queuePostFlushCb } from './scheduler'
|
||||||
import { EMPTY_OBJ, isObject, isArray } from '@vue/shared'
|
import { EMPTY_OBJ, isObject, isArray, isFunction } from '@vue/shared'
|
||||||
|
|
||||||
// record effects created during a component's setup() so that they can be
|
// record effects created during a component's setup() so that they can be
|
||||||
// stopped when the component unmounts
|
// stopped when the component unmounts
|
||||||
@ -43,10 +43,10 @@ function recordEffect(effect: ReactiveEffect) {
|
|||||||
|
|
||||||
// a wrapped version of raw computed to tear it down at component unmount
|
// a wrapped version of raw computed to tear it down at component unmount
|
||||||
export function computed<T, C = null>(
|
export function computed<T, C = null>(
|
||||||
getter: (this: C, ctx: C) => T,
|
getter: () => T,
|
||||||
context?: C
|
setter?: (v: T) => void
|
||||||
): ComputedValue<T> {
|
): ComputedValue<T> {
|
||||||
const c = _computed(getter, context)
|
const c = _computed(getter, setter)
|
||||||
recordEffect(c.effect)
|
recordEffect(c.effect)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -77,12 +77,16 @@ export function watch<T>(
|
|||||||
const getter = options.deep ? () => traverse(baseGetter()) : baseGetter
|
const getter = options.deep ? () => traverse(baseGetter()) : baseGetter
|
||||||
|
|
||||||
let oldValue: any
|
let oldValue: any
|
||||||
|
let cleanup: any
|
||||||
const applyCb = cb
|
const applyCb = cb
|
||||||
? () => {
|
? () => {
|
||||||
const newValue = runner()
|
const newValue = runner()
|
||||||
if (options.deep || newValue !== oldValue) {
|
if (options.deep || newValue !== oldValue) {
|
||||||
try {
|
try {
|
||||||
cb(newValue, oldValue)
|
if (isFunction(cleanup)) {
|
||||||
|
cleanup()
|
||||||
|
}
|
||||||
|
cleanup = cb(newValue, oldValue)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// TODO handle error
|
// TODO handle error
|
||||||
// handleError(e, instance, ErrorTypes.WATCH_CALLBACK)
|
// handleError(e, instance, ErrorTypes.WATCH_CALLBACK)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user