You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1005 B
43 lines
1005 B
import { isRef, reactive } from 'vue';
|
|
import type { Ref } from 'vue';
|
|
type MaybeRef<T> = T | Ref<T>;
|
|
/**
|
|
* Converts ref to reactive.
|
|
*
|
|
* @see https://vueuse.org/toReactive
|
|
* @param objectRef A ref of object
|
|
*/
|
|
export function toReactive<T extends object>(objectRef: MaybeRef<T>): T {
|
|
if (!isRef(objectRef)) return reactive(objectRef) as T;
|
|
|
|
const proxy = new Proxy(
|
|
{},
|
|
{
|
|
get(_, p, receiver) {
|
|
return Reflect.get(objectRef.value, p, receiver);
|
|
},
|
|
set(_, p, value) {
|
|
(objectRef.value as any)[p] = value;
|
|
return true;
|
|
},
|
|
deleteProperty(_, p) {
|
|
return Reflect.deleteProperty(objectRef.value, p);
|
|
},
|
|
has(_, p) {
|
|
return Reflect.has(objectRef.value, p);
|
|
},
|
|
ownKeys() {
|
|
return Object.keys(objectRef.value);
|
|
},
|
|
getOwnPropertyDescriptor() {
|
|
return {
|
|
enumerable: true,
|
|
configurable: true,
|
|
};
|
|
},
|
|
},
|
|
);
|
|
|
|
return reactive(proxy) as T;
|
|
}
|