2020-02-10 04:25:17 +08:00
|
|
|
import { effect, ReactiveEffect, trigger, track } from './effect'
|
|
|
|
import { TriggerOpTypes, TrackOpTypes } from './operations'
|
2020-02-26 08:44:06 +08:00
|
|
|
import { Ref } from './ref'
|
2019-10-10 00:22:08 +08:00
|
|
|
import { isFunction, NOOP } from '@vue/shared'
|
2020-08-22 01:47:41 +08:00
|
|
|
import { ReactiveFlags, toRaw } from './reactive'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-11-02 10:54:01 +08:00
|
|
|
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
|
2020-02-26 08:44:06 +08:00
|
|
|
readonly value: T
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
2019-10-05 22:10:37 +08:00
|
|
|
export interface WritableComputedRef<T> extends Ref<T> {
|
2019-10-14 23:02:49 +08:00
|
|
|
readonly effect: ReactiveEffect<T>
|
2019-09-06 06:32:19 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 12:22:25 +08:00
|
|
|
export type ComputedGetter<T> = (ctx?: any) => T
|
2019-10-22 01:57:20 +08:00
|
|
|
export type ComputedSetter<T> = (v: T) => void
|
|
|
|
|
2019-09-06 06:32:19 +08:00
|
|
|
export interface WritableComputedOptions<T> {
|
2019-10-22 01:57:20 +08:00
|
|
|
get: ComputedGetter<T>
|
|
|
|
set: ComputedSetter<T>
|
2019-08-20 21:45:28 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
class ComputedRefImpl<T> {
|
|
|
|
private _value!: T
|
|
|
|
private _dirty = true
|
|
|
|
|
|
|
|
public readonly effect: ReactiveEffect<T>
|
|
|
|
|
|
|
|
public readonly __v_isRef = true;
|
|
|
|
public readonly [ReactiveFlags.IS_READONLY]: boolean
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
getter: ComputedGetter<T>,
|
|
|
|
private readonly _setter: ComputedSetter<T>,
|
|
|
|
isReadonly: boolean
|
|
|
|
) {
|
|
|
|
this.effect = effect(getter, {
|
|
|
|
lazy: true,
|
|
|
|
scheduler: () => {
|
|
|
|
if (!this._dirty) {
|
|
|
|
this._dirty = true
|
|
|
|
trigger(toRaw(this), TriggerOpTypes.SET, 'value')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this[ReactiveFlags.IS_READONLY] = isReadonly
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
if (this._dirty) {
|
|
|
|
this._value = this.effect()
|
|
|
|
this._dirty = false
|
|
|
|
}
|
|
|
|
track(toRaw(this), TrackOpTypes.GET, 'value')
|
|
|
|
return this._value
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(newValue: T) {
|
|
|
|
this._setter(newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:57:20 +08:00
|
|
|
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
|
2019-06-06 12:35:49 +08:00
|
|
|
export function computed<T>(
|
2019-09-06 06:32:19 +08:00
|
|
|
options: WritableComputedOptions<T>
|
|
|
|
): WritableComputedRef<T>
|
|
|
|
export function computed<T>(
|
2019-10-22 01:57:20 +08:00
|
|
|
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
|
2019-10-22 23:26:48 +08:00
|
|
|
) {
|
2019-10-24 23:37:52 +08:00
|
|
|
let getter: ComputedGetter<T>
|
|
|
|
let setter: ComputedSetter<T>
|
|
|
|
|
|
|
|
if (isFunction(getterOrOptions)) {
|
|
|
|
getter = getterOrOptions
|
|
|
|
setter = __DEV__
|
2019-10-10 00:22:08 +08:00
|
|
|
? () => {
|
2019-10-10 00:20:53 +08:00
|
|
|
console.warn('Write operation failed: computed value is readonly')
|
|
|
|
}
|
2019-10-10 00:22:08 +08:00
|
|
|
: NOOP
|
2019-10-24 23:37:52 +08:00
|
|
|
} else {
|
|
|
|
getter = getterOrOptions.get
|
|
|
|
setter = getterOrOptions.set
|
|
|
|
}
|
2019-08-20 21:45:28 +08:00
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
return new ComputedRefImpl(
|
|
|
|
getter,
|
|
|
|
setter,
|
|
|
|
isFunction(getterOrOptions) || !getterOrOptions.set
|
|
|
|
) as any
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|