| 19 | * @template K The key type for S |
| 20 | */ |
| 21 | export function deepSignal<S, K extends keyof S>( |
| 22 | source: WritableSignal<S>, |
| 23 | prop: Signal<K>, |
| 24 | ): WritableSignal<S[K]> { |
| 25 | // Memoize the property. |
| 26 | const read = computed(() => source()[prop()]) as WritableSignal<S[K]>; |
| 27 | |
| 28 | read[SIGNAL] = source[SIGNAL]; |
| 29 | read.set = (value: S[K]) => { |
| 30 | if (Object.is(untracked(read), value)) { |
| 31 | return; |
| 32 | } |
| 33 | source.update((current) => valueForWrite(current, value, prop()) as S); |
| 34 | }; |
| 35 | |
| 36 | read.update = (fn: (current: S[K]) => S[K]) => { |
| 37 | read.set(fn(untracked(read))); |
| 38 | }; |
| 39 | read.asReadonly = () => read; |
| 40 | |
| 41 | return read; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Gets an updated root value to use when setting a value on a deepSignal with the given path. |