]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: watcher cleanup
authorEvan You <yyx990803@gmail.com>
Thu, 6 Jun 2019 05:04:49 +0000 (13:04 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 6 Jun 2019 05:04:49 +0000 (13:04 +0800)
packages/runtime-core/src/componentLifecycle.ts
packages/runtime-core/src/reactivity.ts

index a0525e177126e5339bdfb2a41f5eb3b5a5736c90..ff8fdb849fbbd05b5c6567c3c5d946924494cab5 100644 (file)
@@ -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
index 30976be5c0365a9f2de06e5e3fb257191121f698..2b0e9f2f02e766d46fb0bb1cb57076aa690e2aad 100644 (file)
@@ -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<T, C = null>(
-  getter: (this: C, ctx: C) => T,
-  context?: C
+  getter: () => T,
+  setter?: (v: T) => void
 ): ComputedValue<T> {
-  const c = _computed(getter, context)
+  const c = _computed(getter, setter)
   recordEffect(c.effect)
   return c
 }
@@ -77,12 +77,16 @@ export function watch<T>(
   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)