Skip to content

Commit 378f978

Browse files
authored
fix(reactivity): handle effect removal during scope stop (#15084)
close #15083
1 parent e0d2723 commit 378f978

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎packages/reactivity/__tests__/effectScope.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,40 @@ describe('reactivity/effect/scope', () => {
435435
expect(rs.value.stage).toBe(1)
436436
expect(renderCount).toBe(5)
437437
})
438+
439+
it('should stop child scopes when cleanup stops a sibling scope', () => {
440+
const parent = effectScope()
441+
let sibling!: EffectScope
442+
443+
parent.run(() => {
444+
effectScope().run(() => {
445+
onScopeDispose(() => sibling.stop())
446+
})
447+
sibling = effectScope()
448+
})
449+
450+
expect(() => parent.stop()).not.toThrow()
451+
expect(sibling.active).toBe(false)
452+
})
453+
454+
it('should resume effects when a watcher stops a sibling watcher', () => {
455+
const count = ref(0)
456+
const scope = effectScope()
457+
let stopSecond = () => {}
458+
const firstSpy = vi.fn(() => stopSecond())
459+
const secondSpy = vi.fn()
460+
461+
scope.run(() => {
462+
watch(count, firstSpy, { flush: 'sync' })
463+
stopSecond = watch(count, secondSpy, { flush: 'sync' })
464+
})
465+
466+
scope.pause()
467+
count.value++
468+
469+
expect(() => scope.resume()).not.toThrow()
470+
expect(firstSpy).toHaveBeenCalledTimes(1)
471+
expect(secondSpy).not.toHaveBeenCalled()
472+
expect(scope.effects).toHaveLength(1)
473+
})
438474
})

‎packages/reactivity/src/effectScope.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ export class EffectScope {
7070
this._isPaused = true
7171
let i, l
7272
if (this.scopes) {
73-
for (i = 0, l = this.scopes.length; i < l; i++) {
74-
this.scopes[i].pause()
73+
const scopes = this.scopes.slice()
74+
for (i = 0, l = scopes.length; i < l; i++) {
75+
scopes[i].pause()
7576
}
7677
}
7778
for (i = 0, l = this.effects.length; i < l; i++) {
@@ -89,12 +90,14 @@ export class EffectScope {
8990
this._isPaused = false
9091
let i, l
9192
if (this.scopes) {
92-
for (i = 0, l = this.scopes.length; i < l; i++) {
93-
this.scopes[i].resume()
93+
const scopes = this.scopes.slice()
94+
for (i = 0, l = scopes.length; i < l; i++) {
95+
scopes[i].resume()
9496
}
9597
}
96-
for (i = 0, l = this.effects.length; i < l; i++) {
97-
this.effects[i].resume()
98+
const effects = this.effects.slice()
99+
for (i = 0, l = effects.length; i < l; i++) {
100+
effects[i].resume()
98101
}
99102
}
100103
}
@@ -171,8 +174,9 @@ export class EffectScope {
171174
this.cleanups.length = 0
172175

173176
if (this.scopes) {
174-
for (i = 0, l = this.scopes.length; i < l; i++) {
175-
this.scopes[i].stop(true)
177+
const scopes = this.scopes.slice()
178+
for (i = 0, l = scopes.length; i < l; i++) {
179+
scopes[i].stop(true)
176180
}
177181
this.scopes.length = 0
178182
}

0 commit comments

Comments
 (0)