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): lose activeEffectScope when effectScope(true) #5575

Merged
merged 3 commits into from Apr 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/reactivity/__tests__/effectScope.spec.ts
Expand Up @@ -6,7 +6,8 @@ import {
onScopeDispose,
computed,
ref,
ComputedRef
ComputedRef,
getCurrentScope
} from '../src'

describe('reactivity/effect/scope', () => {
Expand Down Expand Up @@ -263,4 +264,17 @@ describe('reactivity/effect/scope', () => {
expect(watchSpy).toHaveBeenCalledTimes(1)
expect(watchEffectSpy).toHaveBeenCalledTimes(2)
})

it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
const parentScope = new EffectScope()

parentScope.run(() => {
const currentScope = getCurrentScope()
expect(currentScope).toBeDefined()
const detachedScope = new EffectScope(true)
detachedScope.run(() => {})

expect(getCurrentScope()).toBe(currentScope)
})
})
})
9 changes: 8 additions & 1 deletion packages/reactivity/src/effectScope.ts
Expand Up @@ -8,7 +8,13 @@ export class EffectScope {
effects: ReactiveEffect[] = []
cleanups: (() => void)[] = []

/**
* only assinged by undetached scope
*/
parent: EffectScope | undefined
/**
* record undetached scopes
*/
scopes: EffectScope[] | undefined
/**
* track a child scope's index in its parent's scopes array for optimized
Expand All @@ -28,11 +34,12 @@ export class EffectScope {

run<T>(fn: () => T): T | undefined {
if (this.active) {
const currentEffectScope = activeEffectScope
try {
activeEffectScope = this
return fn()
} finally {
activeEffectScope = this.parent
activeEffectScope = currentEffectScope
}
} else if (__DEV__) {
warn(`cannot run an inactive effect scope.`)
Expand Down