forwardRefs.mjs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Types
  2. const Refs = Symbol('Forwarded refs');
  3. /** Omit properties starting with P */
  4. function getDescriptor(obj, key) {
  5. let currentObj = obj;
  6. while (currentObj) {
  7. const descriptor = Reflect.getOwnPropertyDescriptor(currentObj, key);
  8. if (descriptor) return descriptor;
  9. currentObj = Object.getPrototypeOf(currentObj);
  10. }
  11. return undefined;
  12. }
  13. export function forwardRefs(target) {
  14. for (var _len = arguments.length, refs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  15. refs[_key - 1] = arguments[_key];
  16. }
  17. target[Refs] = refs;
  18. return new Proxy(target, {
  19. get(target, key) {
  20. if (Reflect.has(target, key)) {
  21. return Reflect.get(target, key);
  22. }
  23. // Skip internal properties
  24. if (typeof key === 'symbol' || key.startsWith('__')) return;
  25. for (const ref of refs) {
  26. if (ref.value && Reflect.has(ref.value, key)) {
  27. const val = Reflect.get(ref.value, key);
  28. return typeof val === 'function' ? val.bind(ref.value) : val;
  29. }
  30. }
  31. },
  32. has(target, key) {
  33. if (Reflect.has(target, key)) {
  34. return true;
  35. }
  36. // Skip internal properties
  37. if (typeof key === 'symbol' || key.startsWith('__')) return false;
  38. for (const ref of refs) {
  39. if (ref.value && Reflect.has(ref.value, key)) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. },
  45. set(target, key, value) {
  46. if (Reflect.has(target, key)) {
  47. return Reflect.set(target, key, value);
  48. }
  49. // Skip internal properties
  50. if (typeof key === 'symbol' || key.startsWith('__')) return false;
  51. for (const ref of refs) {
  52. if (ref.value && Reflect.has(ref.value, key)) {
  53. return Reflect.set(ref.value, key, value);
  54. }
  55. }
  56. return false;
  57. },
  58. getOwnPropertyDescriptor(target, key) {
  59. const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
  60. if (descriptor) return descriptor;
  61. // Skip internal properties
  62. if (typeof key === 'symbol' || key.startsWith('__')) return;
  63. // Check each ref's own properties
  64. for (const ref of refs) {
  65. if (!ref.value) continue;
  66. const descriptor = getDescriptor(ref.value, key) ?? ('_' in ref.value ? getDescriptor(ref.value._?.setupState, key) : undefined);
  67. if (descriptor) return descriptor;
  68. }
  69. // Recursive search up each ref's prototype
  70. for (const ref of refs) {
  71. const childRefs = ref.value && ref.value[Refs];
  72. if (!childRefs) continue;
  73. const queue = childRefs.slice();
  74. while (queue.length) {
  75. const ref = queue.shift();
  76. const descriptor = getDescriptor(ref.value, key);
  77. if (descriptor) return descriptor;
  78. const childRefs = ref.value && ref.value[Refs];
  79. if (childRefs) queue.push(...childRefs);
  80. }
  81. }
  82. return undefined;
  83. }
  84. });
  85. }
  86. //# sourceMappingURL=forwardRefs.mjs.map