wip: update writable computed API to match latest spec
This commit is contained in:
parent
42559dc5fe
commit
36ab2ab980
@ -26,6 +26,7 @@ import {
|
|||||||
} from '@vue/reactivity'
|
} from '@vue/reactivity'
|
||||||
|
|
||||||
import { currentInstance } from './component'
|
import { currentInstance } from './component'
|
||||||
|
import { 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
|
||||||
@ -35,12 +36,20 @@ export function recordEffect(effect: ReactiveEffect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// a wrapped version of raw computed to tear it down at component unmount
|
interface ComputedOptions<T> {
|
||||||
export function computed<T, C = null>(
|
get: () => T
|
||||||
getter: () => T,
|
set: (v: T) => void
|
||||||
setter?: (v: T) => void
|
}
|
||||||
|
|
||||||
|
export function computed<T>(
|
||||||
|
getterOrOptions: (() => T) | ComputedOptions<T>
|
||||||
): ComputedRef<T> {
|
): ComputedRef<T> {
|
||||||
const c = _computed(getter, setter)
|
let c: ComputedRef<T>
|
||||||
|
if (isFunction(getterOrOptions)) {
|
||||||
|
c = _computed(getterOrOptions)
|
||||||
|
} else {
|
||||||
|
c = _computed(getterOrOptions.get, getterOrOptions.set)
|
||||||
|
}
|
||||||
recordEffect(c.effect)
|
recordEffect(c.effect)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
ReactiveEffectOptions
|
ReactiveEffectOptions
|
||||||
} from '@vue/reactivity'
|
} from '@vue/reactivity'
|
||||||
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'
|
||||||
import { recordEffect } from './apiState'
|
import { recordEffect } from './apiState'
|
||||||
|
|
||||||
export interface WatchOptions {
|
export interface WatchOptions {
|
||||||
@ -60,7 +60,7 @@ export function watch(
|
|||||||
| WatchOptions,
|
| WatchOptions,
|
||||||
options?: WatchOptions
|
options?: WatchOptions
|
||||||
): StopHandle {
|
): StopHandle {
|
||||||
if (typeof effectOrOptions === 'function') {
|
if (isFunction(effectOrOptions)) {
|
||||||
// effect callback as 2nd argument - this is a source watcher
|
// effect callback as 2nd argument - this is a source watcher
|
||||||
return doWatch(effectOrSource, effectOrOptions, options)
|
return doWatch(effectOrSource, effectOrOptions, options)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user