Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(baseHandlers): trigger will called two times when set the same proxy value #2904

Merged
merged 5 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
TrackOpTypes,
TriggerOpTypes,
DebuggerEvent,
markRaw
markRaw,
shallowReactive
} from '../src/index'
import { ITERATE_KEY } from '../src/effect'

Expand Down Expand Up @@ -811,4 +812,24 @@ describe('reactivity/effect', () => {
expect(dummy).toBe(0)
expect(record).toBeUndefined()
})

it('should trigger once effect when set the equal proxy', () => {
const obj = reactive({ foo: 1 })
const observed: any = reactive({ obj })
const fnSpy = jest.fn(() => observed.obj)

effect(fnSpy)

observed.obj = obj
expect(fnSpy).toHaveBeenCalledTimes(1)

const obj2 = reactive({ foo: 1 })
const observed2: any = shallowReactive({ obj2 })
const fnSpy2 = jest.fn(() => observed2.obj2)

effect(fnSpy2)

observed2.obj2 = obj2
expect(fnSpy2).toHaveBeenCalledTimes(1)
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function createSetter(shallow = false) {
if (target === toRaw(receiver)) {
if (!hadKey) {
trigger(target, TriggerOpTypes.ADD, key, value)
} else if (hasChanged(value, oldValue)) {
} else if (hasChanged(toRaw(value), toRaw(oldValue))) {
trigger(target, TriggerOpTypes.SET, key, value, oldValue)
}
}
Expand Down