2020-02-09 20:25:17 +00:00
|
|
|
import { effect, ReactiveEffect, trigger, track } from './effect'
|
|
|
|
import { TriggerOpTypes, TrackOpTypes } from './operations'
|
2020-02-26 00:44:06 +00:00
|
|
|
import { Ref } from './ref'
|
2019-10-09 16:22:08 +00:00
|
|
|
import { isFunction, NOOP } from '@vue/shared'
|
2018-09-19 15:35:38 +00:00
|
|
|
|
2019-11-02 02:54:01 +00:00
|
|
|
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
|
2020-02-26 00:44:06 +00:00
|
|
|
readonly value: T
|
2018-09-19 15:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 14:10:37 +00:00
|
|
|
export interface WritableComputedRef<T> extends Ref<T> {
|
2019-10-14 15:02:49 +00:00
|
|
|
readonly effect: ReactiveEffect<T>
|
2019-09-05 22:32:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 04:22:25 +00:00
|
|
|
export type ComputedGetter<T> = (ctx?: any) => T
|
2019-10-21 17:57:20 +00:00
|
|
|
export type ComputedSetter<T> = (v: T) => void
|
|
|
|
|
2019-09-05 22:32:19 +00:00
|
|
|
export interface WritableComputedOptions<T> {
|
2019-10-21 17:57:20 +00:00
|
|
|
get: ComputedGetter<T>
|
|
|
|
set: ComputedSetter<T>
|
2019-08-20 13:45:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 17:57:20 +00:00
|
|
|
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
|
2019-06-06 04:35:49 +00:00
|
|
|
export function computed<T>(
|
2019-09-05 22:32:19 +00:00
|
|
|
options: WritableComputedOptions<T>
|
|
|
|
): WritableComputedRef<T>
|
|
|
|
export function computed<T>(
|
2019-10-21 17:57:20 +00:00
|
|
|
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
|
2019-10-22 15:26:48 +00:00
|
|
|
) {
|
2019-10-24 15:37:52 +00:00
|
|
|
let getter: ComputedGetter<T>
|
|
|
|
let setter: ComputedSetter<T>
|
|
|
|
|
|
|
|
if (isFunction(getterOrOptions)) {
|
|
|
|
getter = getterOrOptions
|
|
|
|
setter = __DEV__
|
2019-10-09 16:22:08 +00:00
|
|
|
? () => {
|
2019-10-09 16:20:53 +00:00
|
|
|
console.warn('Write operation failed: computed value is readonly')
|
|
|
|
}
|
2019-10-09 16:22:08 +00:00
|
|
|
: NOOP
|
2019-10-24 15:37:52 +00:00
|
|
|
} else {
|
|
|
|
getter = getterOrOptions.get
|
|
|
|
setter = getterOrOptions.set
|
|
|
|
}
|
2019-08-20 13:45:28 +00:00
|
|
|
|
2019-10-06 15:22:32 +00:00
|
|
|
let dirty = true
|
|
|
|
let value: T
|
2020-02-09 20:25:17 +00:00
|
|
|
let computed: ComputedRef<T>
|
2019-08-20 13:45:28 +00:00
|
|
|
|
2019-06-06 04:35:49 +00:00
|
|
|
const runner = effect(getter, {
|
2018-09-19 15:35:38 +00:00
|
|
|
lazy: true,
|
2019-05-29 15:44:59 +00:00
|
|
|
// mark effect as computed so that it gets priority during trigger
|
|
|
|
computed: true,
|
2018-09-19 15:35:38 +00:00
|
|
|
scheduler: () => {
|
2020-02-09 20:25:17 +00:00
|
|
|
if (!dirty) {
|
|
|
|
dirty = true
|
|
|
|
trigger(computed, TriggerOpTypes.SET, 'value')
|
|
|
|
}
|
2018-09-19 15:35:38 +00:00
|
|
|
}
|
|
|
|
})
|
2020-02-09 20:25:17 +00:00
|
|
|
computed = {
|
2019-10-17 01:36:17 +00:00
|
|
|
_isRef: true,
|
2019-05-29 10:44:50 +00:00
|
|
|
// expose effect so computed can be stopped
|
|
|
|
effect: runner,
|
|
|
|
get value() {
|
|
|
|
if (dirty) {
|
|
|
|
value = runner()
|
|
|
|
dirty = false
|
|
|
|
}
|
2020-02-09 20:25:17 +00:00
|
|
|
track(computed, TrackOpTypes.GET, 'value')
|
2019-05-29 10:44:50 +00:00
|
|
|
return value
|
2019-06-06 04:35:49 +00:00
|
|
|
},
|
2019-10-05 19:48:05 +00:00
|
|
|
set value(newValue: T) {
|
|
|
|
setter(newValue)
|
2019-05-29 10:44:50 +00:00
|
|
|
}
|
2019-11-08 18:29:43 +00:00
|
|
|
} as any
|
2020-02-09 20:25:17 +00:00
|
|
|
return computed
|
2018-09-19 15:35:38 +00:00
|
|
|
}
|