} 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
// 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
}
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)