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