Skip to content

Commit

Permalink
fix(proxyRefs): When using proxyRefs, the internal variable compositi…
Browse files Browse the repository at this point in the history
…on-api.refKey is exposed on the object itself #817 (#818)

Co-authored-by: neiyichao03 <nieyichao03@kuaishou.com>
  • Loading branch information
edwardnyc and neiyichao03 committed Oct 5, 2021
1 parent 68b5d97 commit 92b7eb1
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -193,6 +193,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
12 changes: 11 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,15 +381,23 @@ 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)
Expand Down

0 comments on commit 92b7eb1

Please sign in to comment.