onScopeDispose,
computed,
ref,
- ComputedRef
+ ComputedRef,
+ getCurrentScope
} from '../src'
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)
+ })
+ })
})
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
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.`)