Skip to content

Commit

Permalink
fix(reactivity): differentiate shallow/deep proxies of same target wh…
Browse files Browse the repository at this point in the history
…en nested in reactive

fix #5271
  • Loading branch information
yyx990803 committed Jan 18, 2022
1 parent 9fda941 commit 9c304bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/reactivity/__tests__/shallowReactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe('shallowReactive', () => {
expect(isShallow(shallowReadonly({}))).toBe(true)
})

// #5271
test('should respect shallow reactive nested inside reactive on reset', () => {
const r = reactive({ foo: shallowReactive({ bar: {} }) })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)

r.foo = shallowReactive({ bar: {} })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)
})

test('should respect shallow/deep versions of same target on access', () => {
const original = {}
const shallow = shallowReactive(original)
const deep = reactive(original)
const r = reactive({ shallow, deep })
expect(r.shallow).toBe(shallow)
expect(r.deep).toBe(deep)
})

describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())
Expand Down
9 changes: 6 additions & 3 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
reactiveMap,
shallowReactiveMap,
shallowReadonlyMap,
isReadonly
isReadonly,
isShallow
} from './reactive'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import {
Expand Down Expand Up @@ -150,8 +151,10 @@ function createSetter(shallow = false) {
): boolean {
let oldValue = (target as any)[key]
if (!shallow && !isReadonly(value)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
if (!isShallow(value)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
}
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
oldValue.value = value
return true
Expand Down

0 comments on commit 9c304bf

Please sign in to comment.