From 36ab2ab9806d85653f928446316158538f0c840c Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 19 Aug 2019 12:05:07 -0400 Subject: [PATCH] wip: update writable computed API to match latest spec --- packages/runtime-core/src/apiState.ts | 19 ++++++++++++++----- packages/runtime-core/src/apiWatch.ts | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/runtime-core/src/apiState.ts b/packages/runtime-core/src/apiState.ts index a40df59c..f50e14da 100644 --- a/packages/runtime-core/src/apiState.ts +++ b/packages/runtime-core/src/apiState.ts @@ -26,6 +26,7 @@ import { } from '@vue/reactivity' import { currentInstance } from './component' +import { isFunction } from '@vue/shared' // record effects created during a component's setup() so that they can be // stopped when the component unmounts @@ -35,12 +36,20 @@ export function recordEffect(effect: ReactiveEffect) { } } -// a wrapped version of raw computed to tear it down at component unmount -export function computed( - getter: () => T, - setter?: (v: T) => void +interface ComputedOptions { + get: () => T + set: (v: T) => void +} + +export function computed( + getterOrOptions: (() => T) | ComputedOptions ): ComputedRef { - const c = _computed(getter, setter) + let c: ComputedRef + if (isFunction(getterOrOptions)) { + c = _computed(getterOrOptions) + } else { + c = _computed(getterOrOptions.get, getterOrOptions.set) + } recordEffect(c.effect) return c } diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 0f9e5186..c396452b 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -6,7 +6,7 @@ import { ReactiveEffectOptions } from '@vue/reactivity' import { queueJob, queuePostFlushCb } from './scheduler' -import { EMPTY_OBJ, isObject, isArray } from '@vue/shared' +import { EMPTY_OBJ, isObject, isArray, isFunction } from '@vue/shared' import { recordEffect } from './apiState' export interface WatchOptions { @@ -60,7 +60,7 @@ export function watch( | WatchOptions, options?: WatchOptions ): StopHandle { - if (typeof effectOrOptions === 'function') { + if (isFunction(effectOrOptions)) { // effect callback as 2nd argument - this is a source watcher return doWatch(effectOrSource, effectOrOptions, options) } else {