Skip to content

Commit

Permalink
fix(reactivity): cleanup subsHead in DEV + remove dep from depsMap du…
Browse files Browse the repository at this point in the history
…ring remove sub
  • Loading branch information
edison1105 committed Sep 20, 2024
1 parent e075dfa commit c1b1398
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit c1b1398

Please # to comment.