From: Evan You Date: Thu, 6 Jun 2019 05:04:49 +0000 (+0800) Subject: wip: watcher cleanup X-Git-Tag: v3.0.0-alpha.0~947 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e5e56bb358ca8151263c085662f3df253d373c43;p=thirdparty%2Fvuejs%2Fcore.git wip: watcher cleanup --- diff --git a/packages/runtime-core/src/componentLifecycle.ts b/packages/runtime-core/src/componentLifecycle.ts index a0525e1771..ff8fdb849f 100644 --- a/packages/runtime-core/src/componentLifecycle.ts +++ b/packages/runtime-core/src/componentLifecycle.ts @@ -7,6 +7,7 @@ function injectHook( ) { if (target) { // TODO inject a error-handling wrapped version of the hook + // TODO also set currentInstance when calling the hook ;(target[name] || (target[name] = [])).push(hook) } else { // TODO warn diff --git a/packages/runtime-core/src/reactivity.ts b/packages/runtime-core/src/reactivity.ts index 30976be5c0..2b0e9f2f02 100644 --- a/packages/runtime-core/src/reactivity.ts +++ b/packages/runtime-core/src/reactivity.ts @@ -31,7 +31,7 @@ import { } from '@vue/observer' import { currentInstance } from './component' 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 // 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 export function computed( - getter: (this: C, ctx: C) => T, - context?: C + getter: () => T, + setter?: (v: T) => void ): ComputedValue { - const c = _computed(getter, context) + const c = _computed(getter, setter) recordEffect(c.effect) return c } @@ -77,12 +77,16 @@ export function watch( const getter = options.deep ? () => traverse(baseGetter()) : baseGetter let oldValue: any + let cleanup: any const applyCb = cb ? () => { const newValue = runner() if (options.deep || newValue !== oldValue) { try { - cb(newValue, oldValue) + if (isFunction(cleanup)) { + cleanup() + } + cleanup = cb(newValue, oldValue) } catch (e) { // TODO handle error // handleError(e, instance, ErrorTypes.WATCH_CALLBACK)