Skip to content

Commit

Permalink
fix: When using proxyRefs, the internal variable composition-api.refK…
Browse files Browse the repository at this point in the history
…ey is exposed on the object itself vuejs#817
  • Loading branch information
neiyichao03 committed Sep 24, 2021
1 parent 1a1cf2f commit 0614e82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/reactivity/ref.ts
@@ -1,5 +1,5 @@
import { RefKey } from '../utils/symbols'
import { proxy, isPlainObject, warn } from '../utils'
import { proxy, isPlainObject, warn, def } from '../utils'
import { reactive, isReactive, shallowReactive } from './reactive'
import { readonlySet } from '../utils/sets'

Expand Down Expand Up @@ -172,6 +172,8 @@ export function proxyRefs<T extends object>(
}
const value: Record<string, any> = reactive({ [RefKey]: objectWithRefs })

def(value, RefKey, value[RefKey], false)

for (const key of Object.keys(objectWithRefs)) {
proxy(value, key, {
get() {
Expand Down
13 changes: 12 additions & 1 deletion test/v3/reactivity/ref.spec.ts
Expand Up @@ -13,7 +13,9 @@ import {
isReactive,
shallowRef,
proxyRefs,
ShallowUnwrapRef,
} from '../../../src'
import { RefKey } from '../../../src/utils/symbols'

describe('reactivity/ref', () => {
it('should hold a value', () => {
Expand Down Expand Up @@ -379,19 +381,28 @@ describe('reactivity/ref', () => {
y: ref('foo'),
},
}
const p = proxyRefs(a)
const reactiveA = {
[RefKey]: a,
...a,
}
const p = proxyRefs(a) as ShallowUnwrapRef<typeof reactiveA>
expect(p.x).toBe(1)
expect(p.obj.y).toBe('foo')
expect(p[RefKey]).toBe(a)
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])

// @ts-expect-error
p.obj.y = 'bar'
p.x = 2
expect(a.x).toBe(2)
expect(a.obj.y).toBe('bar')
expect(p[RefKey]).toBe(a)
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])

const r = reactive({ k: 'v' })
const s = proxyRefs(r)
expect(s.k).toBe('v')
expect(s[RefKey])

r.k = 'k'
expect(s.k).toBe('k')
Expand Down

0 comments on commit 0614e82

Please sign in to comment.