Skip to content

Commit

Permalink
Fix "should release computed that untrack by effectScope"
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 13, 2024
1 parent 983d29f commit 4114a12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './effect'
import type { Ref } from './ref'
import { warn } from './warning'
import { activeEffectScope } from './effectScope'

declare const ComputedRefSymbol: unique symbol
declare const WritableComputedRefSymbol: unique symbol
Expand Down Expand Up @@ -135,6 +136,8 @@ export class ComputedRefImpl<T = any> implements IComputed {
}
Dependency.link(this, System.activeSub!)
}
} else if (activeEffectScope !== undefined) {
Dependency.link(this, activeEffectScope)
}
return this._value!
}
Expand Down
23 changes: 21 additions & 2 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { PauseLevels, type ReactiveEffect } from './effect'
import {
DirtyLevels,
type Link,
PauseLevels,
type ReactiveEffect,
Subscriber,
System,
} from './effect'
import { warn } from './warning'

export let activeEffectScope: EffectScope | undefined

export class EffectScope {
export class EffectScope implements Subscriber {
// Subscriber: In order to collect orphans computeds
deps: Link | undefined = undefined
depsTail: Link | undefined = undefined
trackId: number = ++System.lastTrackId
dirtyLevel: DirtyLevels = DirtyLevels.Dirty
canPropagate = false

/**
* @internal
*/
Expand Down Expand Up @@ -111,6 +125,11 @@ export class EffectScope {

stop(fromParent?: boolean): void {
if (this.active) {
if (this.deps !== undefined) {
Subscriber.clearTrack(this.deps)
this.deps = undefined
this.depsTail = undefined
}
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
Expand Down

0 comments on commit 4114a12

Please sign in to comment.