mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 20:28:54 +00:00
fix(effectScope): calling off() of a detached scope should not break currentScope
fix #12825
This commit is contained in:
parent
5960f05c69
commit
800207c473
@ -16,9 +16,7 @@ export class EffectScope {
|
||||
* @internal
|
||||
*/
|
||||
cleanups: (() => void)[] = []
|
||||
|
||||
/**
|
||||
* only assigned by undetached scope
|
||||
* @internal
|
||||
*/
|
||||
parent: EffectScope | undefined
|
||||
@ -38,9 +36,9 @@ export class EffectScope {
|
||||
*/
|
||||
private index: number | undefined
|
||||
|
||||
constructor(detached = false) {
|
||||
if (!detached && activeEffectScope) {
|
||||
constructor(public detached = false) {
|
||||
this.parent = activeEffectScope
|
||||
if (!detached && activeEffectScope) {
|
||||
this.index =
|
||||
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
||||
this
|
||||
@ -93,7 +91,7 @@ export class EffectScope {
|
||||
}
|
||||
}
|
||||
// nested scope, dereference from parent to avoid memory leaks
|
||||
if (this.parent && !fromParent) {
|
||||
if (!this.detached && this.parent && !fromParent) {
|
||||
// optimized O(1) removal
|
||||
const last = this.parent.scopes!.pop()
|
||||
if (last && last !== this) {
|
||||
@ -101,6 +99,7 @@ export class EffectScope {
|
||||
last.index = this.index!
|
||||
}
|
||||
}
|
||||
this.parent = undefined
|
||||
this.active = false
|
||||
}
|
||||
}
|
||||
|
@ -279,4 +279,15 @@ describe('reactivity/effectScope', () => {
|
||||
expect(getCurrentScope()).toBe(currentScope)
|
||||
})
|
||||
})
|
||||
|
||||
it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
|
||||
const parentScope = new EffectScope()
|
||||
|
||||
parentScope.run(() => {
|
||||
const childScope = new EffectScope(true)
|
||||
childScope.on()
|
||||
childScope.off()
|
||||
expect(getCurrentScope()).toBe(parentScope)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user