Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): cleanup subsHead in DEV and remove dep from depsMap #11971

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export class Dep {
*/
subsHead?: Link

/**
* Delete itself from depsMap
*/
cleanup?: () => void

constructor(public computed?: ComputedRefImpl | undefined) {
if (__DEV__) {
this.subsHead = undefined
Expand Down Expand Up @@ -249,6 +254,7 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
let dep = depsMap.get(key)
if (!dep) {
depsMap.set(key, (dep = new Dep()))
dep.cleanup = () => depsMap.delete(key)
}
if (__DEV__) {
dep.track({
Expand Down
16 changes: 12 additions & 4 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,22 @@ function removeSub(link: Link) {
dep.subs = prevSub
}

if (!dep.subs && dep.computed) {
if (!dep.subs) {
// last subscriber removed
// if computed, unsubscribe it from all its deps so this computed and its
// value can be GCed
dep.computed.flags &= ~EffectFlags.TRACKING
for (let l = dep.computed.deps; l; l = l.nextDep) {
removeSub(l)
if (dep.computed) {
dep.computed.flags &= ~EffectFlags.TRACKING
for (let l = dep.computed.deps; l; l = l.nextDep) {
removeSub(l)
}
}

// cleanup subsHead
if (__DEV__ && prevSub === undefined) dep.subsHead = undefined

// remove the dep from depsMap
if (dep.cleanup) dep.cleanup()
}
}

Expand Down