Skip to content

fix(reactivity): fix shallowReactive map "unwraps" the nested refs #8502

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ describe('reactivity/collections', () => {
const observed = reactive(map)
const value = reactive({})
observed.set('key', value)
expect(map.get('key')).not.toBe(value)
expect(map.get('key')).toBe(toRaw(value))
expect(map.get('key')).toBe(value)
expect(toRaw(map.get('key'))).toBe(toRaw(value))
})

it('should return observable versions of contained values', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/__tests__/collections/Set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('reactivity/collections', () => {
const value = reactive({})
observed.add(value)
expect(observed.has(value)).toBe(true)
expect(set.has(value)).toBe(false)
expect(set.has(value)).toBe(true)
})

it('should observe nested values in iterations (forEach)', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/__tests__/collections/WeakMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('reactivity/collections', () => {
const key = {}
const value = reactive({})
observed.set(key, value)
expect(map.get(key)).not.toBe(value)
expect(map.get(key)).toBe(toRaw(value))
expect(map.get(key)).toBe(value)
expect(toRaw(map.get(key))).toBe(toRaw(value))
})

it('should return observable versions of contained values', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/__tests__/collections/WeakSet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('reactivity/collections', () => {
const value = reactive({})
observed.add(value)
expect(observed.has(value)).toBe(true)
expect(set.has(value)).toBe(false)
expect(set.has(value)).toBe(true)
})

it('should return proxy from WeakSet.add call', () => {
Expand Down
16 changes: 13 additions & 3 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { toRaw, ReactiveFlags, toReactive, toReadonly } from './reactive'
import {
toRaw,
ReactiveFlags,
toReactive,
toReadonly,
isProxy
} from './reactive'
import { track, trigger, ITERATE_KEY, MAP_KEY_ITERATE_KEY } from './effect'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import { capitalize, hasOwn, hasChanged, toRawType, isMap } from '@vue/shared'
Expand Down Expand Up @@ -67,7 +73,9 @@ function size(target: IterableCollections, isReadonly = false) {
}

function add(this: SetTypes, value: unknown) {
value = toRaw(value)
if (!isProxy(value)) {
value = toRaw(value)
}
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, value)
Expand All @@ -79,7 +87,9 @@ function add(this: SetTypes, value: unknown) {
}

function set(this: MapTypes, key: unknown, value: unknown) {
value = toRaw(value)
if (!isProxy(value)) {
value = toRaw(value)
}
const target = toRaw(this)
const { has, get } = getProto(target)

Expand Down