Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions packages/reactivity/__tests__/gc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
computed,
effect,
effectScope,
onScopeDispose,
reactive,
shallowRef as ref,
toRaw,
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 15 additions & 8 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,6 +65,11 @@ export class ComputedRefImpl<T = any> implements ReactiveNode {
depsTail: Link | undefined = undefined
flags: SystemReactiveFlags =
SystemReactiveFlags.Mutable | SystemReactiveFlags.Dirty
/**
* @internal
* the scope it was created in
*/
scope: EffectScope | undefined = activeEffectScope

/**
* @internal
Expand Down Expand Up @@ -169,9 +174,15 @@ export class ComputedRefImpl<T = any> 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
Expand Down Expand Up @@ -283,9 +294,5 @@ export function computed<T>(
cRef.onTrigger = debugOptions.onTrigger
}

if (isSSR && activeEffectScope !== undefined) {
link(cRef, activeEffectScope)
}

return cRef as any
}
3 changes: 3 additions & 0 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime-core/__tests__/apiLifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Loading