2024-02-06 18:23:56 +08:00
|
|
|
import { type DebuggerOptions, ReactiveEffect } from './effect'
|
2021-06-24 05:22:21 +08:00
|
|
|
import { type Ref, trackRefValue, triggerRefValue } from './ref'
|
2023-10-27 22:25:09 +08:00
|
|
|
import { NOOP, hasChanged, isFunction } from '@vue/shared'
|
|
|
|
import { toRaw } from './reactive'
|
2021-07-08 02:37:28 +08:00
|
|
|
import type { Dep } from './dep'
|
2023-10-27 22:25:09 +08:00
|
|
|
import { DirtyLevels, ReactiveFlags } from './constants'
|
2024-02-13 17:38:26 +08:00
|
|
|
import { warn } from './warning'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2021-11-03 03:19:39 +08:00
|
|
|
declare const ComputedRefSymbol: unique symbol
|
2021-09-05 04:42:39 +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
|
2021-11-03 03:19:39 +08:00
|
|
|
[ComputedRefSymbol]: true
|
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
|
|
|
}
|
|
|
|
|
2023-10-31 22:19:40 +08:00
|
|
|
export type ComputedGetter<T> = (oldValue?: T) => T
|
|
|
|
export type ComputedSetter<T> = (newValue: T) => void
|
2019-10-22 01:57:20 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-13 17:38:26 +08:00
|
|
|
export const COMPUTED_SIDE_EFFECT_WARN =
|
|
|
|
`Computed is still dirty after getter evaluation,` +
|
|
|
|
` likely because a computed is mutating its own dependency in its getter.` +
|
|
|
|
` State mutations in computed getters should be avoided. ` +
|
|
|
|
` Check the docs for more details: https://vuejs.org/guide/essentials/computed.html#getters-should-be-side-effect-free`
|
|
|
|
|
2022-01-21 12:31:54 +08:00
|
|
|
export class ComputedRefImpl<T> {
|
2021-07-08 02:13:23 +08:00
|
|
|
public dep?: Dep = undefined
|
2021-06-24 05:22:21 +08:00
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
private _value!: T
|
|
|
|
public readonly effect: ReactiveEffect<T>
|
|
|
|
|
2021-07-20 06:24:18 +08:00
|
|
|
public readonly __v_isRef = true
|
2022-07-06 16:27:30 +08:00
|
|
|
public readonly [ReactiveFlags.IS_READONLY]: boolean = false
|
2020-08-22 01:47:41 +08:00
|
|
|
|
2022-01-21 12:31:54 +08:00
|
|
|
public _cacheable: boolean
|
|
|
|
|
2020-08-22 01:47:41 +08:00
|
|
|
constructor(
|
|
|
|
getter: ComputedGetter<T>,
|
|
|
|
private readonly _setter: ComputedSetter<T>,
|
2022-01-16 18:22:18 +08:00
|
|
|
isReadonly: boolean,
|
|
|
|
isSSR: boolean,
|
2020-08-22 01:47:41 +08:00
|
|
|
) {
|
2023-10-31 22:19:40 +08:00
|
|
|
this.effect = new ReactiveEffect(
|
|
|
|
() => getter(this._value),
|
2024-02-06 18:44:09 +08:00
|
|
|
() =>
|
|
|
|
triggerRefValue(
|
|
|
|
this,
|
|
|
|
this.effect._dirtyLevel === DirtyLevels.MaybeDirty_ComputedSideEffect
|
|
|
|
? DirtyLevels.MaybeDirty_ComputedSideEffect
|
|
|
|
: DirtyLevels.MaybeDirty,
|
|
|
|
),
|
2023-10-31 22:19:40 +08:00
|
|
|
)
|
2022-01-21 12:31:54 +08:00
|
|
|
this.effect.computed = this
|
|
|
|
this.effect.active = this._cacheable = !isSSR
|
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
|
2021-07-21 04:20:38 +08:00
|
|
|
const self = toRaw(this)
|
2024-02-06 18:23:56 +08:00
|
|
|
if (
|
|
|
|
(!self._cacheable || self.effect.dirty) &&
|
|
|
|
hasChanged(self._value, (self._value = self.effect.run()!))
|
|
|
|
) {
|
|
|
|
triggerRefValue(self, DirtyLevels.Dirty)
|
2021-07-21 04:20:38 +08:00
|
|
|
}
|
2024-01-13 15:53:06 +08:00
|
|
|
trackRefValue(self)
|
2024-02-06 18:44:09 +08:00
|
|
|
if (self.effect._dirtyLevel >= DirtyLevels.MaybeDirty_ComputedSideEffect) {
|
2024-02-13 17:38:26 +08:00
|
|
|
__DEV__ && warn(COMPUTED_SIDE_EFFECT_WARN)
|
2024-02-06 18:44:09 +08:00
|
|
|
triggerRefValue(self, DirtyLevels.MaybeDirty_ComputedSideEffect)
|
2024-01-18 10:46:57 +08:00
|
|
|
}
|
2021-07-21 04:20:38 +08:00
|
|
|
return self._value
|
2020-08-22 01:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
set value(newValue: T) {
|
|
|
|
this._setter(newValue)
|
|
|
|
}
|
2023-10-27 22:25:09 +08:00
|
|
|
|
|
|
|
// #region polyfill _dirty for backward compatibility third party code for Vue <= 3.3.x
|
|
|
|
get _dirty() {
|
|
|
|
return this.effect.dirty
|
|
|
|
}
|
|
|
|
|
|
|
|
set _dirty(v) {
|
|
|
|
this.effect.dirty = v
|
|
|
|
}
|
|
|
|
// #endregion
|
2020-08-22 01:47:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-31 17:06:10 +08:00
|
|
|
/**
|
|
|
|
* Takes a getter function and returns a readonly reactive ref object for the
|
|
|
|
* returned value from the getter. It can also take an object with get and set
|
|
|
|
* functions to create a writable ref object.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* // Creating a readonly computed ref:
|
|
|
|
* const count = ref(1)
|
|
|
|
* const plusOne = computed(() => count.value + 1)
|
|
|
|
*
|
|
|
|
* console.log(plusOne.value) // 2
|
|
|
|
* plusOne.value++ // error
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* // Creating a writable computed ref:
|
|
|
|
* const count = ref(1)
|
|
|
|
* const plusOne = computed({
|
|
|
|
* get: () => count.value + 1,
|
|
|
|
* set: (val) => {
|
|
|
|
* count.value = val - 1
|
|
|
|
* }
|
|
|
|
* })
|
|
|
|
*
|
|
|
|
* plusOne.value = 1
|
|
|
|
* console.log(count.value) // 0
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param getter - Function that produces the next value.
|
|
|
|
* @param debugOptions - For debugging. See {@link https://vuejs.org/guide/extras/reactivity-in-depth.html#computed-debugging}.
|
|
|
|
* @see {@link https://vuejs.org/api/reactivity-core.html#computed}
|
|
|
|
*/
|
2019-06-06 12:35:49 +08:00
|
|
|
export function computed<T>(
|
2021-07-09 05:09:23 +08:00
|
|
|
getter: ComputedGetter<T>,
|
|
|
|
debugOptions?: DebuggerOptions,
|
|
|
|
): ComputedRef<T>
|
|
|
|
export function computed<T>(
|
|
|
|
options: WritableComputedOptions<T>,
|
|
|
|
debugOptions?: DebuggerOptions,
|
2019-09-06 06:32:19 +08:00
|
|
|
): WritableComputedRef<T>
|
|
|
|
export function computed<T>(
|
2021-07-09 05:09:23 +08:00
|
|
|
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
|
2022-01-16 18:22:18 +08:00
|
|
|
debugOptions?: DebuggerOptions,
|
|
|
|
isSSR = false,
|
2019-10-22 23:26:48 +08:00
|
|
|
) {
|
2019-10-24 23:37:52 +08:00
|
|
|
let getter: ComputedGetter<T>
|
|
|
|
let setter: ComputedSetter<T>
|
|
|
|
|
2021-09-22 01:03:09 +08:00
|
|
|
const onlyGetter = isFunction(getterOrOptions)
|
|
|
|
if (onlyGetter) {
|
2019-10-24 23:37:52 +08:00
|
|
|
getter = getterOrOptions
|
|
|
|
setter = __DEV__
|
2019-10-10 00:22:08 +08:00
|
|
|
? () => {
|
2024-02-13 17:38:26 +08:00
|
|
|
warn('Write operation failed: computed value is readonly')
|
2019-10-10 00:20:53 +08:00
|
|
|
}
|
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
|
|
|
|
2022-01-16 18:22:18 +08:00
|
|
|
const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR)
|
2021-07-09 05:09:23 +08:00
|
|
|
|
2022-01-16 18:22:18 +08:00
|
|
|
if (__DEV__ && debugOptions && !isSSR) {
|
2021-07-09 05:09:23 +08:00
|
|
|
cRef.effect.onTrack = debugOptions.onTrack
|
|
|
|
cRef.effect.onTrigger = debugOptions.onTrigger
|
|
|
|
}
|
|
|
|
|
|
|
|
return cRef as any
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|