( initialValue: T, options?: InputOptions<T, TransformT>, )
| 113 | * @param options Additional options for the input. e.g. a transform, or an alias. |
| 114 | */ |
| 115 | export function createInputSignal<T, TransformT>( |
| 116 | initialValue: T, |
| 117 | options?: InputOptions<T, TransformT>, |
| 118 | ): InputSignalWithTransform<T, TransformT> { |
| 119 | const node: InputSignalNode<T, TransformT> = Object.create(INPUT_SIGNAL_NODE); |
| 120 | |
| 121 | node.value = initialValue; |
| 122 | |
| 123 | // Perf note: Always set `transformFn` here to ensure that `node` always |
| 124 | // has the same v8 class shape, allowing monomorphic reads on input signals. |
| 125 | node.transformFn = options?.transform; |
| 126 | |
| 127 | function inputValueFn() { |
| 128 | // Record that someone looked at this signal. |
| 129 | producerAccessed(node); |
| 130 | |
| 131 | if (node.value === REQUIRED_UNSET_VALUE) { |
| 132 | let message: string | null = null; |
| 133 | if (ngDevMode) { |
| 134 | const name = options?.debugName ?? options?.alias; |
| 135 | message = `Input${name ? ` "${name}"` : ''} is required but no value is available yet.`; |
| 136 | } |
| 137 | throw new RuntimeError(RuntimeErrorCode.REQUIRED_INPUT_NO_VALUE, message); |
| 138 | } |
| 139 | |
| 140 | return node.value; |
| 141 | } |
| 142 | |
| 143 | (inputValueFn as any)[SIGNAL] = node; |
| 144 | |
| 145 | if (ngDevMode) { |
| 146 | inputValueFn.toString = () => `[Input Signal: ${inputValueFn()}]`; |
| 147 | node.debugName = options?.debugName; |
| 148 | } |
| 149 | |
| 150 | return inputValueFn as InputSignalWithTransform<T, TransformT>; |
| 151 | } |
no test coverage detected
searching dependent graphs…