Skip to content

Commit

Permalink
fix(reactivity): should not trigger when setting value to same proxy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 authored Mar 29, 2021
1 parent 4f26835 commit c61e767
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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)
})
})
3 changes: 2 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ function createSetter(shallow = false) {
value: unknown,
receiver: object
): boolean {
const oldValue = (target as any)[key]
let oldValue = (target as any)[key]
if (!shallow) {
value = toRaw(value)
oldValue = toRaw(oldValue)
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
oldValue.value = value
return true
Expand Down

0 comments on commit c61e767

Please sign in to comment.