refactor: extract getRawValue

This commit is contained in:
daiwei 2024-10-28 21:42:21 +08:00
parent a089d70993
commit 38f995763a
2 changed files with 27 additions and 40 deletions

View File

@ -46,6 +46,30 @@ function hasOwnProperty(this: object, key: unknown) {
return obj.hasOwnProperty(key as string)
}
export function getRawValue(
receiver: object,
isReadonly: boolean,
isShallow: boolean,
target: Target,
): Target | undefined {
if (
receiver ===
(isReadonly
? isShallow
? shallowReadonlyMap
: readonlyMap
: isShallow
? shallowReactiveMap
: reactiveMap
).get(target) ||
// receiver is not the reactive proxy, but has the same prototype
// this means the receiver is a user proxy of the reactive proxy
Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)
) {
return target
}
}
class BaseReactiveHandler implements ProxyHandler<Target> {
constructor(
protected readonly _isReadonly = false,
@ -62,24 +86,7 @@ class BaseReactiveHandler implements ProxyHandler<Target> {
} else if (key === ReactiveFlags.IS_SHALLOW) {
return isShallow
} else if (key === ReactiveFlags.RAW) {
if (
receiver ===
(isReadonly
? isShallow
? shallowReadonlyMap
: readonlyMap
: isShallow
? shallowReactiveMap
: reactiveMap
).get(target) ||
// receiver is not the reactive proxy, but has the same prototype
// this means the receiver is a user proxy of the reactive proxy
Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)
) {
return target
}
// early return undefined
return
return getRawValue(receiver, isReadonly, isShallow, target)
}
const targetIsArray = isArray(target)

View File

@ -2,10 +2,6 @@ import {
type Target,
isReadonly,
isShallow,
reactiveMap,
readonlyMap,
shallowReactiveMap,
shallowReadonlyMap,
toRaw,
toReactive,
toReadonly,
@ -21,6 +17,7 @@ import {
toRawType,
} from '@vue/shared'
import { warn } from './warning'
import { getRawValue } from './baseHandlers'
type CollectionTypes = IterableCollections | WeakCollections
@ -277,24 +274,7 @@ function createInstrumentationGetter(isReadonly: boolean, shallow: boolean) {
} else if (key === ReactiveFlags.IS_READONLY) {
return isReadonly
} else if (key === ReactiveFlags.RAW) {
if (
receiver ===
(isReadonly
? shallow
? shallowReadonlyMap
: readonlyMap
: shallow
? shallowReactiveMap
: reactiveMap
).get(target as Target) ||
// receiver is not the reactive proxy, but has the same prototype
// this means the reciever is a user proxy of the reactive proxy
Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)
) {
return target
}
// early return undefined
return
return getRawValue(receiver, isReadonly, shallow, target)
}
return Reflect.get(