From 92b7eb1d8eeb8b960df398bdbb491c34b826acf0 Mon Sep 17 00:00:00 2001 From: albumnyc <31278178+albumnyc@users.noreply.github.com> Date: Tue, 5 Oct 2021 08:46:59 +0800 Subject: [PATCH] fix(proxyRefs): When using proxyRefs, the internal variable composition-api.refKey is exposed on the object itself #817 (#818) Co-authored-by: neiyichao03 --- src/reactivity/ref.ts | 4 +++- test/v3/reactivity/ref.spec.ts | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/reactivity/ref.ts b/src/reactivity/ref.ts index 210d7f80..87b0ad77 100644 --- a/src/reactivity/ref.ts +++ b/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' @@ -193,6 +193,8 @@ export function proxyRefs( } const value: Record = reactive({ [RefKey]: objectWithRefs }) + def(value, RefKey, value[RefKey], false) + for (const key of Object.keys(objectWithRefs)) { proxy(value, key, { get() { diff --git a/test/v3/reactivity/ref.spec.ts b/test/v3/reactivity/ref.spec.ts index 5f5a3371..b4f9c958 100644 --- a/test/v3/reactivity/ref.spec.ts +++ b/test/v3/reactivity/ref.spec.ts @@ -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', () => { @@ -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 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)