fix(ssr): make computed inactive during ssr, fix memory leak

fix #5208
This commit is contained in:
Evan You 2022-01-16 18:22:18 +08:00
parent 4d07ed809c
commit f4f0966b33
3 changed files with 15 additions and 5 deletions

View File

@ -36,7 +36,8 @@ class ComputedRefImpl<T> {
constructor( constructor(
getter: ComputedGetter<T>, getter: ComputedGetter<T>,
private readonly _setter: ComputedSetter<T>, private readonly _setter: ComputedSetter<T>,
isReadonly: boolean isReadonly: boolean,
isSSR: boolean
) { ) {
this.effect = new ReactiveEffect(getter, () => { this.effect = new ReactiveEffect(getter, () => {
if (!this._dirty) { if (!this._dirty) {
@ -44,6 +45,7 @@ class ComputedRefImpl<T> {
triggerRefValue(this) triggerRefValue(this)
} }
}) })
this.effect.active = !isSSR
this[ReactiveFlags.IS_READONLY] = isReadonly this[ReactiveFlags.IS_READONLY] = isReadonly
} }
@ -73,7 +75,8 @@ export function computed<T>(
): WritableComputedRef<T> ): WritableComputedRef<T>
export function computed<T>( export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>, getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
debugOptions?: DebuggerOptions debugOptions?: DebuggerOptions,
isSSR = false
) { ) {
let getter: ComputedGetter<T> let getter: ComputedGetter<T>
let setter: ComputedSetter<T> let setter: ComputedSetter<T>
@ -91,9 +94,9 @@ export function computed<T>(
setter = getterOrOptions.set setter = getterOrOptions.set
} }
const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter) const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR)
if (__DEV__ && debugOptions) { if (__DEV__ && debugOptions && !isSSR) {
cRef.effect.onTrack = debugOptions.onTrack cRef.effect.onTrack = debugOptions.onTrack
cRef.effect.onTrigger = debugOptions.onTrigger cRef.effect.onTrigger = debugOptions.onTrigger
} }

View File

@ -0,0 +1,7 @@
import { computed as _computed } from '@vue/reactivity'
import { isInSSRComponentSetup } from './component'
export const computed = ((getterOrOptions: any, debugOptions?: any) => {
// @ts-ignore
return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
}) as typeof _computed

View File

@ -3,7 +3,6 @@
export const version = __VERSION__ export const version = __VERSION__
export { export {
// core // core
computed,
reactive, reactive,
ref, ref,
readonly, readonly,
@ -34,6 +33,7 @@ export {
getCurrentScope, getCurrentScope,
onScopeDispose onScopeDispose
} from '@vue/reactivity' } from '@vue/reactivity'
export { computed } from './apiComputed'
export { export {
watch, watch,
watchEffect, watchEffect,