Skip to content

Commit

Permalink
chore: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Feb 13, 2023
1 parent a0e7dc3 commit a62da80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ export class EffectScope {
stop(fromParent?: boolean) {
if (this._active) {
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
// #5783
// effects will be changed when a watcher stoped.
// so we need to copy it for iteration.
const effectsToStop = this.effects.slice()
for (i = 0, l = effectsToStop.length; i < l; i++) {
effectsToStop[i].stop()
}
for (i = 0, l = this.cleanups.length; i < l; i++) {
this.cleanups[i]()
Expand Down
27 changes: 27 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,33 @@ describe('api: watch', () => {
expect(spy).toHaveBeenCalledTimes(2)
})

test('handle nested watcher stop properly', () => {
let instance: ComponentInternalInstance
const Comp = {
setup() {
instance = getCurrentInstance()!
watch(
() => 1,
(val, oldVal, onCleanup) => {
const stop = watch(
() => 2,
() => {}
)
onCleanup(stop)
},
{ immediate: true }
)
return () => ''
}
}
const root = nodeOps.createElement('div')
createApp(Comp).mount(root)
expect(instance!.scope.effects.length).toBe(3)

instance!.scope.stop()
expect(instance!.scope.effects[0].active).toBe(false)
})

it('watching sources: ref<any[]>', async () => {
const foo = ref([1])
const spy = vi.fn()
Expand Down

0 comments on commit a62da80

Please sign in to comment.