* A signal factory function in charge of creating a new computed signal capturing query * results. This centralized creation function is used by all types of queries (child / children, * required / optional). * * @param firstOnly indicates if all or only the first result should be returned * @p
(
firstOnly: boolean,
required: boolean,
opts?: {debugName?: string},
)
| 41 | * @returns a read-only signal with query results |
| 42 | */ |
| 43 | function createQuerySignalFn<V>( |
| 44 | firstOnly: boolean, |
| 45 | required: boolean, |
| 46 | opts?: {debugName?: string}, |
| 47 | ) { |
| 48 | let node: QuerySignalNode<V>; |
| 49 | const signalFn = createComputed(() => { |
| 50 | // A dedicated signal that increments its value every time a query changes its dirty status. By |
| 51 | // using this signal we can implement a query as computed and avoid creation of a specialized |
| 52 | // reactive node type. Please note that a query gets marked dirty under the following |
| 53 | // circumstances: |
| 54 | // - a view (where a query is active) finished its first creation pass; |
| 55 | // - a new view is inserted / deleted and it impacts query results. |
| 56 | node._dirtyCounter(); |
| 57 | |
| 58 | const value = refreshSignalQuery<V>(node, firstOnly); |
| 59 | |
| 60 | if (required && value === undefined) { |
| 61 | throw new RuntimeError( |
| 62 | RuntimeErrorCode.REQUIRED_QUERY_NO_VALUE, |
| 63 | ngDevMode && 'Child query result is required but no value is available.', |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return value; |
| 68 | }); |
| 69 | node = signalFn[SIGNAL] as QuerySignalNode<V>; |
| 70 | node._dirtyCounter = signal(0); |
| 71 | node._flatValue = undefined; |
| 72 | |
| 73 | if (ngDevMode) { |
| 74 | signalFn.toString = () => `[Query Signal]`; |
| 75 | node.debugName = opts?.debugName; |
| 76 | } |
| 77 | |
| 78 | return signalFn; |
| 79 | } |
| 80 | |
| 81 | export function createSingleResultOptionalQuerySignalFn<ReadT>(opts?: { |
| 82 | debugName?: string; |
no test coverage detected
searching dependent graphs…