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): ensure multiple effectScope on() and off() calls maintains correct active scope #12641

Merged
merged 3 commits into from
Jan 8, 2025
Merged
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
13 changes: 13 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ describe('reactivity/effect/scope', () => {
})
})

it('calling on() and off() multiple times inside an active scope should not break currentScope', () => {
const parentScope = effectScope()
parentScope.run(() => {
const childScope = effectScope(true)
childScope.on()
childScope.on()
childScope.off()
childScope.off()
childScope.off()
expect(getCurrentScope()).toBe(parentScope)
})
})

it('should pause/resume EffectScope', async () => {
const counter = reactive({ num: 0 })
const fnSpy = vi.fn(() => counter.num)
Expand Down
15 changes: 12 additions & 3 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class EffectScope {
* @internal
*/
private _active = true
/**
* @internal track `on` calls, allow `on` call multiple times
*/
private _on = 0
/**
* @internal
*/
Expand Down Expand Up @@ -105,16 +109,21 @@ export class EffectScope {
* @internal
*/
on(): void {
this.prevScope = activeEffectScope
activeEffectScope = this
if (++this._on === 1) {
this.prevScope = activeEffectScope
activeEffectScope = this
}
}

/**
* This should only be called on non-detached scopes
* @internal
*/
off(): void {
activeEffectScope = this.prevScope
if (this._on > 0 && --this._on === 0) {
activeEffectScope = this.prevScope
this.prevScope = undefined
}
}

stop(fromParent?: boolean): void {
Expand Down
Loading