( inputSignal: Signal<TInput>, )
| 12 | * @returns A proxy object with the same fields as the input object, but with each field wrapped in a `Computed` signal. |
| 13 | */ |
| 14 | export function signalProxy<TInput extends Record<string | symbol, any>>( |
| 15 | inputSignal: Signal<TInput>, |
| 16 | ) { |
| 17 | const internalState = {} as MapToSignals<TInput> |
| 18 | |
| 19 | return new Proxy<MapToSignals<TInput>>(internalState, { |
| 20 | get(target, prop) { |
| 21 | // first check if we have it in our internal state and return it |
| 22 | const computedField = target[prop] |
| 23 | if (computedField) return computedField |
| 24 | |
| 25 | // then, check if it's a function on the resultState and return it |
| 26 | const targetField = untracked(inputSignal)[prop] |
| 27 | if (typeof targetField === 'function') return targetField |
| 28 | |
| 29 | // finally, create a computed field, store it and return it |
| 30 | // @ts-expect-error |
| 31 | return (target[prop] = computed(() => inputSignal()[prop])) |
| 32 | }, |
| 33 | has(_, prop) { |
| 34 | return !!untracked(inputSignal)[prop] |
| 35 | }, |
| 36 | ownKeys() { |
| 37 | return Reflect.ownKeys(untracked(inputSignal)) |
| 38 | }, |
| 39 | getOwnPropertyDescriptor() { |
| 40 | return { |
| 41 | enumerable: true, |
| 42 | configurable: true, |
| 43 | } |
| 44 | }, |
| 45 | }) |
| 46 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…