Open History to see the last commit
1import {
2 DestroyRef,
3 Injector,
4 NgZone,
5 computed,
6 effect,
7 inject,
8 runInInjectionContext,
9 signal,
10 untracked,
11} from '@angular/core'
12import { QueryClient, notifyManager } from '@tanstack/query-core'
13import { signalProxy } from './signal-proxy'
14import { shouldThrowError } from './util'
15import { lazyInit } from './util/lazy-init/lazy-init'
16import type {
17 QueryKey,
18 QueryObserver,
19 QueryObserverResult,
20} from '@tanstack/query-core'
21import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types'
22β
23/**
24 * Base implementation for `injectQuery` and `injectInfiniteQuery`.
25 */
26export function createBaseQuery<
27 TQueryFnData,
28 TError,
29 TData,
30 TQueryData,
31 TQueryKey extends QueryKey,
32>(
33 optionsFn: () => CreateBaseQueryOptions<
34 TQueryFnData,
35 TError,
36 TData,
37 TQueryData,
38 TQueryKey
39 >,
40 Observer: typeof QueryObserver,
41): CreateBaseQueryResult<TData, TError> {
42 const injector = inject(Injector)
43 return lazyInit(() => {
44 const ngZone = injector.get(NgZone)
45 const destroyRef = injector.get(DestroyRef)
46 const queryClient = injector.get(QueryClient)
47β
48 /**
49 * Signal that has the default options from query client applied
50 * computed() is used so signals can be inserted into the options
51 * making it reactive. Wrapping options in a function ensures embedded expressions
52 * are preserved and can keep being applied after signal changes
53 */
54 const defaultedOptionsSignal = computed(() => {
55 const options = runInInjectionContext(injector, () => optionsFn())
56 const defaultedOptions = queryClient.defaultQueryOptions(options)
57 defaultedOptions._optimisticResults = 'optimistic'
58 return defaultedOptions
59 })
60β
61 const observer = new Observer<
62 TQueryFnData,
63 TError,
64 TData,
65 TQueryData,
66 TQueryKey
67 >(queryClient, defaultedOptionsSignal())
68β
69 const resultSignal = signal(
70 observer.getOptimisticResult(defaultedOptionsSignal()),
71 )
72β
73 effect(
74 () => {
75 const defaultedOptions = defaultedOptionsSignal()
76 observer.setOptions(defaultedOptions, {
77 // Do not notify on updates because of changes in the options because
78 // these changes should already be reflected in the optimistic result.
79 listeners: false,
80 })
81 untracked(() => {
82 resultSignal.set(observer.getOptimisticResult(defaultedOptions))
83 })
84 },
85 {
86 injector,
87 },
88 )
89β
90 // observer.trackResult is not used as this optimization is not needed for Angular
91 const unsubscribe = observer.subscribe(
92 notifyManager.batchCalls((state: QueryObserverResult<TData, TError>) => {
93 ngZone.run(() => {
94 if (
95 state.isError &&
96 !state.isFetching &&
97 // !isRestoring() && // todo: enable when client persistence is implemented
98 shouldThrowError(observer.options.throwOnError, [
99 state.error,
100 observer.getCurrentQuery(),
101 ])
102 ) {
103 throw state.error
104 }
105 resultSignal.set(state)
106 })
107 }),
108 )
109 destroyRef.onDestroy(unsubscribe)
110β
111 return signalProxy(resultSignal) as CreateBaseQueryResult<TData, TError>
112 })
113}