wip: make computed implementation consistent
This commit is contained in:
parent
b218678c66
commit
0519e10518
@ -1,17 +1,29 @@
|
|||||||
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
|
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
|
||||||
import { UnwrapNestedRefs, knownRefs } from './ref'
|
import { UnwrapNestedRefs, knownRefs } from './ref'
|
||||||
|
import { isFunction } from '@vue/shared'
|
||||||
|
|
||||||
export interface ComputedRef<T> {
|
export interface ComputedRef<T> {
|
||||||
readonly value: UnwrapNestedRefs<T>
|
readonly value: UnwrapNestedRefs<T>
|
||||||
readonly effect: ReactiveEffect
|
readonly effect: ReactiveEffect
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ComputedOptions<T> {
|
||||||
|
get: () => T
|
||||||
|
set: (v: T) => void
|
||||||
|
}
|
||||||
|
|
||||||
export function computed<T>(
|
export function computed<T>(
|
||||||
getter: () => T,
|
getterOrOptions: (() => T) | ComputedOptions<T>
|
||||||
setter?: (v: T) => void
|
|
||||||
): ComputedRef<T> {
|
): ComputedRef<T> {
|
||||||
|
const isReadonly = isFunction(getterOrOptions)
|
||||||
|
const getter = isReadonly
|
||||||
|
? (getterOrOptions as (() => T))
|
||||||
|
: (getterOrOptions as ComputedOptions<T>).get
|
||||||
|
const setter = isReadonly ? null : (getterOrOptions as ComputedOptions<T>).set
|
||||||
|
|
||||||
let dirty: boolean = true
|
let dirty: boolean = true
|
||||||
let value: any = undefined
|
let value: any = undefined
|
||||||
|
|
||||||
const runner = effect(getter, {
|
const runner = effect(getter, {
|
||||||
lazy: true,
|
lazy: true,
|
||||||
// mark effect as computed so that it gets priority during trigger
|
// mark effect as computed so that it gets priority during trigger
|
||||||
|
@ -8,7 +8,7 @@ export {
|
|||||||
markImmutable,
|
markImmutable,
|
||||||
markNonReactive
|
markNonReactive
|
||||||
} from './reactive'
|
} from './reactive'
|
||||||
export { computed, ComputedRef } from './computed'
|
export { computed, ComputedRef, ComputedOptions } from './computed'
|
||||||
export {
|
export {
|
||||||
effect,
|
effect,
|
||||||
stop,
|
stop,
|
||||||
|
@ -23,11 +23,11 @@ export {
|
|||||||
import {
|
import {
|
||||||
computed as _computed,
|
computed as _computed,
|
||||||
ComputedRef,
|
ComputedRef,
|
||||||
|
ComputedOptions,
|
||||||
ReactiveEffect
|
ReactiveEffect
|
||||||
} 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
|
||||||
@ -37,20 +37,10 @@ export function recordEffect(effect: ReactiveEffect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ComputedOptions<T> {
|
|
||||||
get: () => T
|
|
||||||
set: (v: T) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export function computed<T>(
|
export function computed<T>(
|
||||||
getterOrOptions: (() => T) | ComputedOptions<T>
|
getterOrOptions: (() => T) | ComputedOptions<T>
|
||||||
): ComputedRef<T> {
|
): ComputedRef<T> {
|
||||||
let c: ComputedRef<T>
|
const c = _computed(getterOrOptions)
|
||||||
if (isFunction(getterOrOptions)) {
|
|
||||||
c = _computed(getterOrOptions)
|
|
||||||
} else {
|
|
||||||
c = _computed(getterOrOptions.get, getterOrOptions.set)
|
|
||||||
}
|
|
||||||
recordEffect(c.effect)
|
recordEffect(c.effect)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user