95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
// Types
|
|
|
|
const Refs = Symbol('Forwarded refs');
|
|
|
|
/** Omit properties starting with P */
|
|
|
|
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) {
|
|
for (var _len = arguments.length, refs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
refs[_key - 1] = arguments[_key];
|
|
}
|
|
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.mjs.map
|