Skip to content

Commit

Permalink
fix(reactive-vue): stop tracking if watcher is destroyed #3074 (#3075)
Browse files Browse the repository at this point in the history
* fix(reactive-vue): stop tracking if watcher is destroyed #3074

* fix(reactive-vue): add tests
  • Loading branch information
arcturus011 authored Apr 27, 2022
1 parent 43d0faa commit c2b7ba9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/reactive-vue/src/__tests__/observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,34 @@ test('observer: component scheduler', async () => {

wrapper.destroy()
})

test('observer: stop tracking if watcher is destroyed', async () => {
let count = 0
const model = observable<any>({
age: 10,
name: 'test',
})

const Component = observer({
name: 'test',
data() {
return {
model: model,
}
},
render() {
count++
return h('div', [this.model.name, this.model.age])
},
})

const wrapper = shallowMount(Component)

const childInst = wrapper.find({ name: 'test' })

expect(childInst.exists()).toBe(true)
;(childInst.vm as any)._isDestroyed = true
model.age++
wrapper.destroy()
expect(count).toEqual(1) // 不触发 reactiveRender
})
9 changes: 9 additions & 0 deletions packages/reactive-vue/src/observer/observerInVue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ function observer(Component: any, observerOptions?: IObserverOptions): any {
return this
}

reactiveRender.$vm = this

const tracker = new Tracker(() => {
if (
reactiveRender.$vm._isBeingDestroyed ||
reactiveRender.$vm._isDestroyed
) {
return tracker.dispose()
}

if (
observerOptions?.scheduler &&
typeof observerOptions.scheduler === 'function'
Expand Down

0 comments on commit c2b7ba9

Please sign in to comment.