Open History to see the last commit
1import { computed, isSignal } from '@angular/core'
2import type { Signal } from '@angular/core'
3
4/**
5 * A value that may be supplied to `injectChat` either as a static value, an
6 * Angular `Signal`, or a zero-arg getter. The getter form lets callers read
7 * other signals so the option stays reactive.
8 */
9export type ReactiveOption<T> = T | Signal<T> | (() => T)
10
11/**
12 * Normalize a {@link ReactiveOption} into a `Signal`-like getter `() => T`.
13 *
14 * - A `Signal` is returned as-is (already a getter that tracks reads).
15 * - A zero-arg function is wrapped in `computed` so reads inside it are tracked.
16 * - A plain value becomes a constant non-reactive fn (never re-fires).
17 */
18export function toReactive<T>(value: ReactiveOption<T>): () => T {
19 if (isSignal(value)) {
20 return value
21 }
22 if (typeof value === 'function') {
23 return computed(value as () => T)
24 }
25 return () => value
26}