Skip to content

Commit ff5203d

Browse files
committed
refactor(reactivity): simplified isProxy judgment
1 parent 838f1ba commit ff5203d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/reactivity/__tests__/reactive.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { isRef, ref } from '../src/ref'
2-
import { isReactive, markRaw, reactive, toRaw } from '../src/reactive'
2+
import {
3+
isProxy,
4+
isReactive,
5+
markRaw,
6+
reactive,
7+
readonly,
8+
shallowReactive,
9+
shallowReadonly,
10+
toRaw,
11+
} from '../src/reactive'
312
import { computed } from '../src/computed'
413
import { effect } from '../src/effect'
514

@@ -302,4 +311,21 @@ describe('reactivity/reactive', () => {
302311
const observed = reactive(original)
303312
expect(isReactive(observed)).toBe(false)
304313
})
314+
315+
test('isProxy', () => {
316+
const foo = {}
317+
expect(isProxy(foo)).toBe(false)
318+
319+
const fooRe = reactive(foo)
320+
expect(isProxy(fooRe)).toBe(true)
321+
322+
const fooSRe = shallowReactive(foo)
323+
expect(isProxy(fooSRe)).toBe(true)
324+
325+
const fooRl = readonly(foo)
326+
expect(isProxy(fooRl)).toBe(true)
327+
328+
const fooSRl = shallowReadonly(foo)
329+
expect(isProxy(fooSRl)).toBe(true)
330+
})
305331
})

packages/reactivity/src/reactive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ export function isShallow(value: any): boolean {
329329
* @param value - The value to check.
330330
* @see {@link https://vuejs.org/api/reactivity-utilities.html#isproxy}
331331
*/
332-
export function isProxy(value: unknown): boolean {
333-
return isReactive(value) || isReadonly(value)
332+
export function isProxy(value: any): boolean {
333+
// see BaseReactiveHandler, createInstrumentationGetter
334+
return value ? !!value[ReactiveFlags.RAW] : false
334335
}
335336

336337
/**

0 commit comments

Comments
 (0)