2021-06-25 05:44:32 +08:00
|
|
|
import { ReactiveEffect } from './effect'
|
2021-06-24 05:22:21 +08:00
|
|
|
import { Ref, trackRefValue, triggerRefValue } 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
|
|
|
}
|
|
|
|
|
2021-07-07 10:01:59 +08:00
|
|
|
type ComputedScheduler = (fn: () => void) => void
|
|
|
|
let scheduler: ComputedScheduler | undefined
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a scheduler for deferring computed computations
|
|
|
|
*/
|
|
|
|
export const setComputedScheduler = (s: ComputedScheduler | undefined) => {
|
|
|
|
scheduler = s
|
|
|
|
}
|
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
class ComputedRefImpl<T> {
|
2021-06-24 05:22:21 +08:00
|
|
|
public dep?: Set<ReactiveEffect> = undefined
|
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
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
|
|
|
|
) {
|
2021-07-07 10:01:59 +08:00
|
|
|
let deferFn: () => void
|
|
|
|
let scheduled = false
|
2021-06-25 05:44:32 +08:00
|
|
|
this.effect = new ReactiveEffect(getter, () => {
|
|
|
|
if (!this._dirty) {
|
|
|
|
this._dirty = true
|
2021-07-07 10:01:59 +08:00
|
|
|
if (scheduler) {
|
|
|
|
if (!scheduled) {
|
|
|
|
scheduled = true
|
|
|
|
scheduler(
|
|
|
|
deferFn ||
|
|
|
|
(deferFn = () => {
|
|
|
|
scheduled = false
|
|
|
|
if (this._dirty) {
|
|
|
|
this._dirty = false
|
|
|
|
const newValue = this.effect.run()!
|
|
|
|
if (this._value !== newValue) {
|
|
|
|
this._value = newValue
|
|
|
|
triggerRefValue(this)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
triggerRefValue(this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
triggerRefValue(this)
|
|
|
|
}
|
2020-08-22 01:47:41 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
this[ReactiveFlags.IS_READONLY] = isReadonly
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
2021-03-25 23:23:26 +08:00
|
|
|
// the computed ref may get wrapped by other proxies e.g. readonly() #3376
|
|
|
|
const self = toRaw(this)
|
|
|
|
if (self._dirty) {
|
2021-06-25 05:44:32 +08:00
|
|
|
self._value = self.effect.run()!
|
2021-03-25 23:23:26 +08:00
|
|
|
self._dirty = false
|
2020-08-22 01:47:41 +08:00
|
|
|
}
|
2021-06-25 05:44:32 +08:00
|
|
|
trackRefValue(self)
|
2021-03-25 23:23:26 +08:00
|
|
|
return self._value
|
2020-08-22 01:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|