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 type {
17 QueryClient,
18 QueryKey,
19 QueryObserver,
20 QueryObserverResult,
21} from '@tanstack/query-core'
22import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types'
23β
24/**
25 * Base implementation for `injectQuery` and `injectInfiniteQuery`.
26 */
27export function createBaseQuery<
28 TQueryFnData,
29 TError,
30 TData,
31 TQueryData,
32 TQueryKey extends QueryKey,
33>(
34 options: (
35 client: QueryClient,
36 ) => CreateBaseQueryOptions<
37 TQueryFnData,
38 TError,
39 TData,
40 TQueryData,
41 TQueryKey
42 >,
43 Observer: typeof QueryObserver,
44 queryClient: QueryClient,
45): CreateBaseQueryResult<TData, TError> {
46 const injector = inject(Injector)
47 const ngZone = inject(NgZone)
48β
49 return lazyInit(() => {
50 return runInInjectionContext(injector, () => {
51 const destroyRef = inject(DestroyRef)
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 defaultedOptions = queryClient.defaultQueryOptions(
60 options(queryClient),
61 )
62 defaultedOptions._optimisticResults = 'optimistic'
63 return defaultedOptions
64 })
65β
66 const observer = new Observer<
67 TQueryFnData,
68 TError,
69 TData,
70 TQueryData,
71 TQueryKey
72 >(queryClient, defaultedOptionsSignal())
73β
74 const resultSignal = signal(
75 observer.getOptimisticResult(defaultedOptionsSignal()),
76 )
77β
78 effect(() => {
79 const defaultedOptions = defaultedOptionsSignal()
80 observer.setOptions(defaultedOptions, {
81 // Do not notify on updates because of changes in the options because
82 // these changes should already be reflected in the optimistic result.
83 listeners: false,
84 })
85 untracked(() => {
86 resultSignal.set(observer.getOptimisticResult(defaultedOptions))
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(
93 (state: QueryObserverResult<TData, TError>) => {
94 ngZone.run(() => {
95 if (
96 state.isError &&
97 !state.isFetching &&
98 // !isRestoring() && // todo: enable when client persistence is implemented
99 shouldThrowError(observer.options.throwOnError, [
100 state.error,
101 observer.getCurrentQuery(),
102 ])
103 ) {
104 throw state.error
105 }
106 resultSignal.set(state)
107 })
108 },
109 ),
110 )
111 destroyRef.onDestroy(unsubscribe)
112β
113 return signalProxy(resultSignal) as CreateBaseQueryResult<TData, TError>
114 })
115 })
116}