routie dev init since i didn't adhere to any proper guidance up until now
This commit is contained in:
+94
@@ -0,0 +1,94 @@
|
||||
// Types
|
||||
|
||||
const Refs = Symbol('Forwarded refs');
|
||||
|
||||
/** Omit properties starting with P */
|
||||
|
||||
/** Omit keyof $props from T */
|
||||
|
||||
function getDescriptor(obj, key) {
|
||||
let currentObj = obj;
|
||||
while (currentObj) {
|
||||
const descriptor = Reflect.getOwnPropertyDescriptor(currentObj, key);
|
||||
if (descriptor) return descriptor;
|
||||
currentObj = Object.getPrototypeOf(currentObj);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
export function forwardRefs(target, ...refs) {
|
||||
target[Refs] = refs;
|
||||
return new Proxy(target, {
|
||||
get(target, key) {
|
||||
if (Reflect.has(target, key)) {
|
||||
return Reflect.get(target, key);
|
||||
}
|
||||
|
||||
// Skip internal properties
|
||||
if (typeof key === 'symbol' || key.startsWith('$') || key.startsWith('__')) return;
|
||||
for (const ref of refs) {
|
||||
if (ref.value && Reflect.has(ref.value, key)) {
|
||||
const val = Reflect.get(ref.value, key);
|
||||
return typeof val === 'function' ? val.bind(ref.value) : val;
|
||||
}
|
||||
}
|
||||
},
|
||||
has(target, key) {
|
||||
if (Reflect.has(target, key)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Skip internal properties
|
||||
if (typeof key === 'symbol' || key.startsWith('$') || key.startsWith('__')) return false;
|
||||
for (const ref of refs) {
|
||||
if (ref.value && Reflect.has(ref.value, key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
set(target, key, value) {
|
||||
if (Reflect.has(target, key)) {
|
||||
return Reflect.set(target, key, value);
|
||||
}
|
||||
|
||||
// Skip internal properties
|
||||
if (typeof key === 'symbol' || key.startsWith('$') || key.startsWith('__')) return false;
|
||||
for (const ref of refs) {
|
||||
if (ref.value && Reflect.has(ref.value, key)) {
|
||||
return Reflect.set(ref.value, key, value);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
getOwnPropertyDescriptor(target, key) {
|
||||
const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
|
||||
if (descriptor) return descriptor;
|
||||
|
||||
// Skip internal properties
|
||||
if (typeof key === 'symbol' || key.startsWith('$') || key.startsWith('__')) return;
|
||||
|
||||
// Check each ref's own properties
|
||||
for (const ref of refs) {
|
||||
if (!ref.value) continue;
|
||||
const descriptor = getDescriptor(ref.value, key) ?? ('_' in ref.value ? getDescriptor(ref.value._?.setupState, key) : undefined);
|
||||
if (descriptor) return descriptor;
|
||||
}
|
||||
|
||||
// Recursive search up each ref's prototype
|
||||
for (const ref of refs) {
|
||||
const childRefs = ref.value && ref.value[Refs];
|
||||
if (!childRefs) continue;
|
||||
const queue = childRefs.slice();
|
||||
while (queue.length) {
|
||||
const ref = queue.shift();
|
||||
const descriptor = getDescriptor(ref.value, key);
|
||||
if (descriptor) return descriptor;
|
||||
const childRefs = ref.value && ref.value[Refs];
|
||||
if (childRefs) queue.push(...childRefs);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
}
|
||||
//# sourceMappingURL=forwardRefs.js.map
|
||||
Reference in New Issue
Block a user