Skip to content
Permalink
Browse files
fix(reactivity): differentiate shallow/deep proxies of same target wh…
…en nested in reactive

fix #5271
  • Loading branch information
yyx990803 committed Jan 18, 2022
1 parent 9fda941 commit 9c304bfe7942a20264235865b4bb5f6e53fdee0d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
@@ -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())
@@ -8,7 +8,8 @@ import {
reactiveMap,
shallowReactiveMap,
shallowReadonlyMap,
isReadonly
isReadonly,
isShallow
} from './reactive'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import {
@@ -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

0 comments on commit 9c304bf

Please sign in to comment.