diff --git a/packages/reactivity/__tests__/effectScope.spec.ts b/packages/reactivity/__tests__/effectScope.spec.ts index c706f77c1c6..aef5e036300 100644 --- a/packages/reactivity/__tests__/effectScope.spec.ts +++ b/packages/reactivity/__tests__/effectScope.spec.ts @@ -625,6 +625,27 @@ describe('reactivity/effect/scope', () => { expect(scope.run(() => c.value)).toBe(value) expect(getter).toHaveBeenCalledTimes(1) }) + + it('should release a computed read outside of the scope it was created in', () => { + const src = reactive({ foo: 1 }) + const scope = effectScope() + const c = scope.run(() => computed(() => src.foo))! + + // e.g. in an event handler + expect(c.value).toBe(1) + src.foo = 2 + expect(c.value).toBe(2) + expect(getDepFromReactive(toRaw(src), 'foo')).toBeDefined() + + scope.stop() + expect(getDepFromReactive(toRaw(src), 'foo')).toBeUndefined() + + // e.g. in a timer that fires after the scope stopped + src.foo = 3 + expect(c.value).toBe(3) + expect(getDepFromReactive(toRaw(src), 'foo')).toBeUndefined() + expect((c as unknown as ComputedRefImpl).subs).toBeUndefined() + }) }) function getEffectsCount(scope: EffectScope): number { diff --git a/packages/reactivity/__tests__/gc.spec.ts b/packages/reactivity/__tests__/gc.spec.ts index ec2aec8b1b0..00673d57d0e 100644 --- a/packages/reactivity/__tests__/gc.spec.ts +++ b/packages/reactivity/__tests__/gc.spec.ts @@ -4,6 +4,7 @@ import { computed, effect, effectScope, + onScopeDispose, reactive, shallowRef as ref, toRaw, @@ -114,6 +115,46 @@ describe.skipIf(!global.gc)('reactivity/gc', () => { expect(c.deref()).toBeUndefined() }) + it('should release computed read outside of the effectScope it was created in', async () => { + const src = reactive({ foo: 1 }) + const scope = effectScope() + // @ts-expect-error ES2021 API + const c = new WeakRef(scope.run(() => computed(() => src.foo))!) + + c.deref().value + + expect(c.deref()).toBeDefined() + scope.stop() + await gc() + expect(c.deref()).toBeUndefined() + }) + + it('should not keep the cleanups of a stopped effectScope alive through its computed', async () => { + const src = reactive({ foo: 1 }) + // e.g. a composable that cleans up when its component unmounts + const useDisposable = () => { + const state = { disposed: false } + onScopeDispose(() => (state.disposed = true)) + // @ts-expect-error ES2021 API + return new WeakRef(state) + } + // e.g. a component that hands a computed to a long-lived owner + const mount = () => { + const scope = effectScope() + const result = scope.run(() => ({ + state: useDisposable(), + c: computed(() => src.foo), + }))! + scope.stop() + return result + } + + const { state, c } = mount() + await gc() + expect(c.value).toBe(1) + expect(state.deref()).toBeUndefined() + }) + // bytes that reach old space per step with only scavenges running: a major // GC would also free what a removed link keeps alive async function promotedPerStep(step: (i: number) => void) { diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index 5d48becb657..329681d8c19 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -2,7 +2,7 @@ import { hasChanged, isFunction } from '@vue/shared' import { ReactiveFlags, TrackOpTypes } from './constants' import { onTrack, setupOnTrigger } from './debug' import type { DebuggerEvent, DebuggerOptions } from './effect' -import { activeEffectScope } from './effectScope' +import { type EffectScope, activeEffectScope } from './effectScope' import type { Ref } from './ref' import { type Link, @@ -65,6 +65,11 @@ export class ComputedRefImpl implements ReactiveNode { depsTail: Link | undefined = undefined flags: SystemReactiveFlags = SystemReactiveFlags.Mutable | SystemReactiveFlags.Dirty + /** + * @internal + * the scope it was created in + */ + scope: EffectScope | undefined = activeEffectScope /** * @internal @@ -169,9 +174,15 @@ export class ComputedRefImpl implements ReactiveNode { }) } link(this, activeSub) - } else if (activeEffectScope !== undefined) { - if (activeEffectScope.active) { - link(this, activeEffectScope) + } else if ( + activeEffectScope !== undefined || + (this.subs === undefined && this.scope !== undefined) + ) { + // a read outside of any scope, e.g. in an event handler, counts as one + // in the scope it was created in unless something already keeps it + const scope = activeEffectScope || this.scope! + if (scope.active) { + link(this, scope) } else if (this.subs === undefined) { // a stopped scope never unlinks it, so nothing would release the deps // this read tracked and they would keep the computed alive @@ -283,9 +294,5 @@ export function computed( cRef.onTrigger = debugOptions.onTrigger } - if (isSSR && activeEffectScope !== undefined) { - link(cRef, activeEffectScope) - } - return cRef as any } diff --git a/packages/reactivity/src/effectScope.ts b/packages/reactivity/src/effectScope.ts index 8736e578105..7186ec8d2b1 100644 --- a/packages/reactivity/src/effectScope.ts +++ b/packages/reactivity/src/effectScope.ts @@ -80,6 +80,9 @@ export class EffectScope implements ReactiveNode { } this.flags = EffectFlags.STOP this.reset() + // drop the cleanups: they never run again, and a computed created in the + // scope may still reference it + this.cleanups = undefined const sub = this.subs if (sub !== undefined) { unlink(sub) diff --git a/packages/runtime-core/__tests__/apiLifecycle.spec.ts b/packages/runtime-core/__tests__/apiLifecycle.spec.ts index fdd18302720..e5974bc928a 100644 --- a/packages/runtime-core/__tests__/apiLifecycle.spec.ts +++ b/packages/runtime-core/__tests__/apiLifecycle.spec.ts @@ -485,4 +485,26 @@ describe('api: lifecycle hooks', () => { render(null, root) expect(double.deps).toBeUndefined() }) + + it('does not keep a computed read in an event handler subscribed after unmount', () => { + const store = reactive({ n: 0 }) + let double!: ComputedRefImpl + let onClick!: () => number + + const Comp = { + setup() { + double = computed(() => store.n * 2) as unknown as ComputedRefImpl + onClick = () => double.value + return () => h('div') + }, + } + + const root = nodeOps.createElement('div') + render(h(Comp), root) + expect(onClick()).toBe(0) + expect(double.deps).toBeDefined() + + render(null, root) + expect(double.deps).toBeUndefined() + }) })