@tanstack/angular-query-experimental 5.84.0 → 5.84.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/package.json +7 -43
  2. package/src/create-base-query.ts +0 -142
  3. package/src/index.ts +0 -64
  4. package/src/infinite-query-options.ts +0 -187
  5. package/src/inject-infinite-query.ts +0 -129
  6. package/src/inject-is-fetching.ts +0 -66
  7. package/src/inject-is-mutating.ts +0 -65
  8. package/src/inject-is-restoring.ts +0 -52
  9. package/src/inject-mutation-state.ts +0 -125
  10. package/src/inject-mutation.ts +0 -169
  11. package/src/inject-queries.ts +0 -271
  12. package/src/inject-query-client.ts +0 -23
  13. package/src/inject-query.ts +0 -230
  14. package/src/mutation-options.ts +0 -60
  15. package/src/providers.ts +0 -372
  16. package/src/query-options.ts +0 -178
  17. package/src/signal-proxy.ts +0 -46
  18. package/src/types.ts +0 -309
  19. package/src/util/is-dev-mode/is-dev-mode.ts +0 -3
  20. /package/{dist/create-base-query.d.ts → create-base-query.d.ts} +0 -0
  21. /package/{dist/create-base-query.mjs → create-base-query.mjs} +0 -0
  22. /package/{dist/create-base-query.mjs.map → create-base-query.mjs.map} +0 -0
  23. /package/{dist/index.d.ts → index.d.ts} +0 -0
  24. /package/{dist/index.mjs → index.mjs} +0 -0
  25. /package/{dist/index.mjs.map → index.mjs.map} +0 -0
  26. /package/{dist/infinite-query-options.d.ts → infinite-query-options.d.ts} +0 -0
  27. /package/{dist/infinite-query-options.mjs → infinite-query-options.mjs} +0 -0
  28. /package/{dist/infinite-query-options.mjs.map → infinite-query-options.mjs.map} +0 -0
  29. /package/{dist/inject-infinite-query.d.ts → inject-infinite-query.d.ts} +0 -0
  30. /package/{dist/inject-infinite-query.mjs → inject-infinite-query.mjs} +0 -0
  31. /package/{dist/inject-infinite-query.mjs.map → inject-infinite-query.mjs.map} +0 -0
  32. /package/{dist/inject-is-fetching.d.ts → inject-is-fetching.d.ts} +0 -0
  33. /package/{dist/inject-is-fetching.mjs → inject-is-fetching.mjs} +0 -0
  34. /package/{dist/inject-is-fetching.mjs.map → inject-is-fetching.mjs.map} +0 -0
  35. /package/{dist/inject-is-mutating.d.ts → inject-is-mutating.d.ts} +0 -0
  36. /package/{dist/inject-is-mutating.mjs → inject-is-mutating.mjs} +0 -0
  37. /package/{dist/inject-is-mutating.mjs.map → inject-is-mutating.mjs.map} +0 -0
  38. /package/{dist/inject-is-restoring.d.ts → inject-is-restoring.d.ts} +0 -0
  39. /package/{dist/inject-is-restoring.mjs → inject-is-restoring.mjs} +0 -0
  40. /package/{dist/inject-is-restoring.mjs.map → inject-is-restoring.mjs.map} +0 -0
  41. /package/{dist/inject-mutation-state.d.ts → inject-mutation-state.d.ts} +0 -0
  42. /package/{dist/inject-mutation-state.mjs → inject-mutation-state.mjs} +0 -0
  43. /package/{dist/inject-mutation-state.mjs.map → inject-mutation-state.mjs.map} +0 -0
  44. /package/{dist/inject-mutation.d.ts → inject-mutation.d.ts} +0 -0
  45. /package/{dist/inject-mutation.mjs → inject-mutation.mjs} +0 -0
  46. /package/{dist/inject-mutation.mjs.map → inject-mutation.mjs.map} +0 -0
  47. /package/{dist/inject-queries.d.ts → inject-queries.d.ts} +0 -0
  48. /package/{dist/inject-queries.mjs → inject-queries.mjs} +0 -0
  49. /package/{dist/inject-queries.mjs.map → inject-queries.mjs.map} +0 -0
  50. /package/{dist/inject-query-client.d.ts → inject-query-client.d.ts} +0 -0
  51. /package/{dist/inject-query-client.mjs → inject-query-client.mjs} +0 -0
  52. /package/{dist/inject-query-client.mjs.map → inject-query-client.mjs.map} +0 -0
  53. /package/{dist/inject-query.d.ts → inject-query.d.ts} +0 -0
  54. /package/{dist/inject-query.mjs → inject-query.mjs} +0 -0
  55. /package/{dist/inject-query.mjs.map → inject-query.mjs.map} +0 -0
  56. /package/{dist/mutation-options.d.ts → mutation-options.d.ts} +0 -0
  57. /package/{dist/mutation-options.mjs → mutation-options.mjs} +0 -0
  58. /package/{dist/mutation-options.mjs.map → mutation-options.mjs.map} +0 -0
  59. /package/{dist/providers.d.ts → providers.d.ts} +0 -0
  60. /package/{dist/providers.mjs → providers.mjs} +0 -0
  61. /package/{dist/providers.mjs.map → providers.mjs.map} +0 -0
  62. /package/{dist/query-options.d.ts → query-options.d.ts} +0 -0
  63. /package/{dist/query-options.mjs → query-options.mjs} +0 -0
  64. /package/{dist/query-options.mjs.map → query-options.mjs.map} +0 -0
  65. /package/{dist/signal-proxy.d.ts → signal-proxy.d.ts} +0 -0
  66. /package/{dist/signal-proxy.mjs → signal-proxy.mjs} +0 -0
  67. /package/{dist/signal-proxy.mjs.map → signal-proxy.mjs.map} +0 -0
  68. /package/{dist/types.d.ts → types.d.ts} +0 -0
  69. /package/{dist/util → util}/is-dev-mode/is-dev-mode.d.ts +0 -0
@@ -1,65 +0,0 @@
1
- import {
2
- DestroyRef,
3
- Injector,
4
- NgZone,
5
- assertInInjectionContext,
6
- inject,
7
- signal,
8
- } from '@angular/core'
9
- import { QueryClient, notifyManager } from '@tanstack/query-core'
10
- import type { MutationFilters } from '@tanstack/query-core'
11
- import type { Signal } from '@angular/core'
12
-
13
- export interface InjectIsMutatingOptions {
14
- /**
15
- * The `Injector` in which to create the isMutating signal.
16
- *
17
- * If this is not provided, the current injection context will be used instead (via `inject`).
18
- */
19
- injector?: Injector
20
- }
21
-
22
- /**
23
- * Injects a signal that tracks the number of mutations that your application is fetching.
24
- *
25
- * Can be used for app-wide loading indicators
26
- * @param filters - The filters to apply to the query.
27
- * @param options - Additional configuration
28
- * @returns signal with number of fetching mutations.
29
- * @public
30
- */
31
- export function injectIsMutating(
32
- filters?: MutationFilters,
33
- options?: InjectIsMutatingOptions,
34
- ): Signal<number> {
35
- !options?.injector && assertInInjectionContext(injectIsMutating)
36
- const injector = options?.injector ?? inject(Injector)
37
- const destroyRef = injector.get(DestroyRef)
38
- const ngZone = injector.get(NgZone)
39
- const queryClient = injector.get(QueryClient)
40
-
41
- const cache = queryClient.getMutationCache()
42
- // isMutating is the prev value initialized on mount *
43
- let isMutating = queryClient.isMutating(filters)
44
-
45
- const result = signal(isMutating)
46
-
47
- const unsubscribe = ngZone.runOutsideAngular(() =>
48
- cache.subscribe(
49
- notifyManager.batchCalls(() => {
50
- const newIsMutating = queryClient.isMutating(filters)
51
- if (isMutating !== newIsMutating) {
52
- // * and update with each change
53
- isMutating = newIsMutating
54
- ngZone.run(() => {
55
- result.set(isMutating)
56
- })
57
- }
58
- }),
59
- ),
60
- )
61
-
62
- destroyRef.onDestroy(unsubscribe)
63
-
64
- return result
65
- }
@@ -1,52 +0,0 @@
1
- import {
2
- InjectionToken,
3
- Injector,
4
- assertInInjectionContext,
5
- inject,
6
- signal,
7
- } from '@angular/core'
8
- import type { Provider, Signal } from '@angular/core'
9
-
10
- const IS_RESTORING = new InjectionToken(
11
- typeof ngDevMode === 'undefined' || ngDevMode
12
- ? 'TANSTACK_QUERY_IS_RESTORING'
13
- : '',
14
- {
15
- // Default value when not provided
16
- factory: () => signal(false).asReadonly(),
17
- },
18
- )
19
-
20
- /**
21
- * The `Injector` in which to create the isRestoring signal.
22
- *
23
- * If this is not provided, the current injection context will be used instead (via `inject`).
24
- */
25
- interface InjectIsRestoringOptions {
26
- injector?: Injector
27
- }
28
-
29
- /**
30
- * Injects a signal that tracks whether a restore is currently in progress. {@link injectQuery} and friends also check this internally to avoid race conditions between the restore and initializing queries.
31
- * @param options - Options for injectIsRestoring.
32
- * @returns signal with boolean that indicates whether a restore is in progress.
33
- * @public
34
- */
35
- export function injectIsRestoring(options?: InjectIsRestoringOptions) {
36
- !options?.injector && assertInInjectionContext(injectIsRestoring)
37
- const injector = options?.injector ?? inject(Injector)
38
- return injector.get(IS_RESTORING)
39
- }
40
-
41
- /**
42
- * Used by TanStack Query Angular persist client plugin to provide the signal that tracks the restore state
43
- * @param isRestoring - a readonly signal that returns a boolean
44
- * @returns Provider for the `isRestoring` signal
45
- * @public
46
- */
47
- export function provideIsRestoring(isRestoring: Signal<boolean>): Provider {
48
- return {
49
- provide: IS_RESTORING,
50
- useValue: isRestoring,
51
- }
52
- }
@@ -1,125 +0,0 @@
1
- import {
2
- DestroyRef,
3
- Injector,
4
- NgZone,
5
- assertInInjectionContext,
6
- computed,
7
- inject,
8
- signal,
9
- } from '@angular/core'
10
- import {
11
- QueryClient,
12
- notifyManager,
13
- replaceEqualDeep,
14
- } from '@tanstack/query-core'
15
- import type { Signal } from '@angular/core'
16
- import type {
17
- Mutation,
18
- MutationCache,
19
- MutationFilters,
20
- MutationState,
21
- } from '@tanstack/query-core'
22
-
23
- type MutationStateOptions<TResult = MutationState> = {
24
- filters?: MutationFilters
25
- select?: (mutation: Mutation) => TResult
26
- }
27
-
28
- /**
29
- *
30
- * @param mutationCache
31
- * @param options
32
- */
33
- function getResult<TResult = MutationState>(
34
- mutationCache: MutationCache,
35
- options: MutationStateOptions<TResult>,
36
- ): Array<TResult> {
37
- return mutationCache
38
- .findAll(options.filters)
39
- .map(
40
- (mutation): TResult =>
41
- (options.select ? options.select(mutation) : mutation.state) as TResult,
42
- )
43
- }
44
-
45
- /**
46
- * @public
47
- */
48
- export interface InjectMutationStateOptions {
49
- /**
50
- * The `Injector` in which to create the mutation state signal.
51
- *
52
- * If this is not provided, the current injection context will be used instead (via `inject`).
53
- */
54
- injector?: Injector
55
- }
56
-
57
- /**
58
- * Injects a signal that tracks the state of all mutations.
59
- * @param injectMutationStateFn - A function that returns mutation state options.
60
- * @param options - The Angular injector to use.
61
- * @returns The signal that tracks the state of all mutations.
62
- * @public
63
- */
64
- export function injectMutationState<TResult = MutationState>(
65
- injectMutationStateFn: () => MutationStateOptions<TResult> = () => ({}),
66
- options?: InjectMutationStateOptions,
67
- ): Signal<Array<TResult>> {
68
- !options?.injector && assertInInjectionContext(injectMutationState)
69
- const injector = options?.injector ?? inject(Injector)
70
- const destroyRef = injector.get(DestroyRef)
71
- const ngZone = injector.get(NgZone)
72
- const queryClient = injector.get(QueryClient)
73
- const mutationCache = queryClient.getMutationCache()
74
-
75
- /**
76
- * Computed signal that gets result from mutation cache based on passed options
77
- * First element is the result, second element is the time when the result was set
78
- */
79
- const resultFromOptionsSignal = computed(() => {
80
- return [
81
- getResult(mutationCache, injectMutationStateFn()),
82
- performance.now(),
83
- ] as const
84
- })
85
-
86
- /**
87
- * Signal that contains result set by subscriber
88
- * First element is the result, second element is the time when the result was set
89
- */
90
- const resultFromSubscriberSignal = signal<[Array<TResult>, number] | null>(
91
- null,
92
- )
93
-
94
- /**
95
- * Returns the last result by either subscriber or options
96
- */
97
- const effectiveResultSignal = computed(() => {
98
- const optionsResult = resultFromOptionsSignal()
99
- const subscriberResult = resultFromSubscriberSignal()
100
- return subscriberResult && subscriberResult[1] > optionsResult[1]
101
- ? subscriberResult[0]
102
- : optionsResult[0]
103
- })
104
-
105
- const unsubscribe = ngZone.runOutsideAngular(() =>
106
- mutationCache.subscribe(
107
- notifyManager.batchCalls(() => {
108
- const [lastResult] = effectiveResultSignal()
109
- const nextResult = replaceEqualDeep(
110
- lastResult,
111
- getResult(mutationCache, injectMutationStateFn()),
112
- )
113
- if (lastResult !== nextResult) {
114
- ngZone.run(() => {
115
- resultFromSubscriberSignal.set([nextResult, performance.now()])
116
- })
117
- }
118
- }),
119
- ),
120
- )
121
-
122
- destroyRef.onDestroy(unsubscribe)
123
-
124
- return effectiveResultSignal
125
- }
@@ -1,169 +0,0 @@
1
- import {
2
- DestroyRef,
3
- Injector,
4
- NgZone,
5
- assertInInjectionContext,
6
- computed,
7
- effect,
8
- inject,
9
- signal,
10
- untracked,
11
- } from '@angular/core'
12
- import {
13
- MutationObserver,
14
- QueryClient,
15
- noop,
16
- notifyManager,
17
- shouldThrowError,
18
- } from '@tanstack/query-core'
19
- import { signalProxy } from './signal-proxy'
20
- import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
21
- import type { CreateMutateFunction, CreateMutationResult } from './types'
22
- import type { CreateMutationOptions } from './mutation-options'
23
-
24
- export interface InjectMutationOptions {
25
- /**
26
- * The `Injector` in which to create the mutation.
27
- *
28
- * If this is not provided, the current injection context will be used instead (via `inject`).
29
- */
30
- injector?: Injector
31
- }
32
-
33
- /**
34
- * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
35
- *
36
- * Unlike queries, mutations are not run automatically.
37
- * @param injectMutationFn - A function that returns mutation options.
38
- * @param options - Additional configuration
39
- * @returns The mutation.
40
- * @public
41
- */
42
- export function injectMutation<
43
- TData = unknown,
44
- TError = DefaultError,
45
- TVariables = void,
46
- TContext = unknown,
47
- >(
48
- injectMutationFn: () => CreateMutationOptions<
49
- TData,
50
- TError,
51
- TVariables,
52
- TContext
53
- >,
54
- options?: InjectMutationOptions,
55
- ): CreateMutationResult<TData, TError, TVariables, TContext> {
56
- !options?.injector && assertInInjectionContext(injectMutation)
57
- const injector = options?.injector ?? inject(Injector)
58
- const destroyRef = injector.get(DestroyRef)
59
- const ngZone = injector.get(NgZone)
60
- const queryClient = injector.get(QueryClient)
61
-
62
- /**
63
- * computed() is used so signals can be inserted into the options
64
- * making it reactive. Wrapping options in a function ensures embedded expressions
65
- * are preserved and can keep being applied after signal changes
66
- */
67
- const optionsSignal = computed(injectMutationFn)
68
-
69
- const observerSignal = (() => {
70
- let instance: MutationObserver<TData, TError, TVariables, TContext> | null =
71
- null
72
-
73
- return computed(() => {
74
- return (instance ||= new MutationObserver(queryClient, optionsSignal()))
75
- })
76
- })()
77
-
78
- const mutateFnSignal = computed<
79
- CreateMutateFunction<TData, TError, TVariables, TContext>
80
- >(() => {
81
- const observer = observerSignal()
82
- return (variables, mutateOptions) => {
83
- observer.mutate(variables, mutateOptions).catch(noop)
84
- }
85
- })
86
-
87
- /**
88
- * Computed signal that gets result from mutation cache based on passed options
89
- */
90
- const resultFromInitialOptionsSignal = computed(() => {
91
- const observer = observerSignal()
92
- return observer.getCurrentResult()
93
- })
94
-
95
- /**
96
- * Signal that contains result set by subscriber
97
- */
98
- const resultFromSubscriberSignal = signal<MutationObserverResult<
99
- TData,
100
- TError,
101
- TVariables,
102
- TContext
103
- > | null>(null)
104
-
105
- effect(
106
- () => {
107
- const observer = observerSignal()
108
- const observerOptions = optionsSignal()
109
-
110
- untracked(() => {
111
- observer.setOptions(observerOptions)
112
- })
113
- },
114
- {
115
- injector,
116
- },
117
- )
118
-
119
- effect(
120
- () => {
121
- // observer.trackResult is not used as this optimization is not needed for Angular
122
- const observer = observerSignal()
123
-
124
- untracked(() => {
125
- const unsubscribe = ngZone.runOutsideAngular(() =>
126
- observer.subscribe(
127
- notifyManager.batchCalls((state) => {
128
- ngZone.run(() => {
129
- if (
130
- state.isError &&
131
- shouldThrowError(observer.options.throwOnError, [state.error])
132
- ) {
133
- ngZone.onError.emit(state.error)
134
- throw state.error
135
- }
136
-
137
- resultFromSubscriberSignal.set(state)
138
- })
139
- }),
140
- ),
141
- )
142
- destroyRef.onDestroy(unsubscribe)
143
- })
144
- },
145
- {
146
- injector,
147
- },
148
- )
149
-
150
- const resultSignal = computed(() => {
151
- const resultFromSubscriber = resultFromSubscriberSignal()
152
- const resultFromInitialOptions = resultFromInitialOptionsSignal()
153
-
154
- const result = resultFromSubscriber ?? resultFromInitialOptions
155
-
156
- return {
157
- ...result,
158
- mutate: mutateFnSignal(),
159
- mutateAsync: result.mutate,
160
- }
161
- })
162
-
163
- return signalProxy(resultSignal) as CreateMutationResult<
164
- TData,
165
- TError,
166
- TVariables,
167
- TContext
168
- >
169
- }
@@ -1,271 +0,0 @@
1
- import {
2
- QueriesObserver,
3
- QueryClient,
4
- notifyManager,
5
- } from '@tanstack/query-core'
6
- import {
7
- DestroyRef,
8
- Injector,
9
- NgZone,
10
- assertInInjectionContext,
11
- computed,
12
- effect,
13
- inject,
14
- runInInjectionContext,
15
- signal,
16
- } from '@angular/core'
17
- import { injectIsRestoring } from './inject-is-restoring'
18
- import type { Signal } from '@angular/core'
19
- import type {
20
- DefaultError,
21
- OmitKeyof,
22
- QueriesObserverOptions,
23
- QueriesPlaceholderDataFunction,
24
- QueryFunction,
25
- QueryKey,
26
- QueryObserverOptions,
27
- QueryObserverResult,
28
- ThrowOnError,
29
- } from '@tanstack/query-core'
30
-
31
- // This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
32
- // `placeholderData` function does not have a parameter
33
- type QueryObserverOptionsForCreateQueries<
34
- TQueryFnData = unknown,
35
- TError = DefaultError,
36
- TData = TQueryFnData,
37
- TQueryKey extends QueryKey = QueryKey,
38
- > = OmitKeyof<
39
- QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
40
- 'placeholderData'
41
- > & {
42
- placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
43
- }
44
-
45
- // Avoid TS depth-limit error in case of large array literal
46
- type MAXIMUM_DEPTH = 20
47
-
48
- // Widen the type of the symbol to enable type inference even if skipToken is not immutable.
49
- type SkipTokenForUseQueries = symbol
50
-
51
- type GetOptions<T> =
52
- // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
53
- T extends {
54
- queryFnData: infer TQueryFnData
55
- error?: infer TError
56
- data: infer TData
57
- }
58
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
59
- : T extends { queryFnData: infer TQueryFnData; error?: infer TError }
60
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
61
- : T extends { data: infer TData; error?: infer TError }
62
- ? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
63
- : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
64
- T extends [infer TQueryFnData, infer TError, infer TData]
65
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
66
- : T extends [infer TQueryFnData, infer TError]
67
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
68
- : T extends [infer TQueryFnData]
69
- ? QueryObserverOptionsForCreateQueries<TQueryFnData>
70
- : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
71
- T extends {
72
- queryFn?:
73
- | QueryFunction<infer TQueryFnData, infer TQueryKey>
74
- | SkipTokenForUseQueries
75
- select: (data: any) => infer TData
76
- throwOnError?: ThrowOnError<any, infer TError, any, any>
77
- }
78
- ? QueryObserverOptionsForCreateQueries<
79
- TQueryFnData,
80
- unknown extends TError ? DefaultError : TError,
81
- unknown extends TData ? TQueryFnData : TData,
82
- TQueryKey
83
- >
84
- : // Fallback
85
- QueryObserverOptionsForCreateQueries
86
-
87
- type GetResults<T> =
88
- // Part 1: responsible for mapping explicit type parameter to function result, if object
89
- T extends { queryFnData: any; error?: infer TError; data: infer TData }
90
- ? QueryObserverResult<TData, TError>
91
- : T extends { queryFnData: infer TQueryFnData; error?: infer TError }
92
- ? QueryObserverResult<TQueryFnData, TError>
93
- : T extends { data: infer TData; error?: infer TError }
94
- ? QueryObserverResult<TData, TError>
95
- : // Part 2: responsible for mapping explicit type parameter to function result, if tuple
96
- T extends [any, infer TError, infer TData]
97
- ? QueryObserverResult<TData, TError>
98
- : T extends [infer TQueryFnData, infer TError]
99
- ? QueryObserverResult<TQueryFnData, TError>
100
- : T extends [infer TQueryFnData]
101
- ? QueryObserverResult<TQueryFnData>
102
- : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
103
- T extends {
104
- queryFn?:
105
- | QueryFunction<infer TQueryFnData, any>
106
- | SkipTokenForUseQueries
107
- select: (data: any) => infer TData
108
- throwOnError?: ThrowOnError<any, infer TError, any, any>
109
- }
110
- ? QueryObserverResult<
111
- unknown extends TData ? TQueryFnData : TData,
112
- unknown extends TError ? DefaultError : TError
113
- >
114
- : // Fallback
115
- QueryObserverResult
116
-
117
- /**
118
- * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
119
- * @public
120
- */
121
- export type QueriesOptions<
122
- T extends Array<any>,
123
- TResult extends Array<any> = [],
124
- TDepth extends ReadonlyArray<number> = [],
125
- > = TDepth['length'] extends MAXIMUM_DEPTH
126
- ? Array<QueryObserverOptionsForCreateQueries>
127
- : T extends []
128
- ? []
129
- : T extends [infer Head]
130
- ? [...TResult, GetOptions<Head>]
131
- : T extends [infer Head, ...infer Tail]
132
- ? QueriesOptions<
133
- [...Tail],
134
- [...TResult, GetOptions<Head>],
135
- [...TDepth, 1]
136
- >
137
- : ReadonlyArray<unknown> extends T
138
- ? T
139
- : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
140
- // use this to infer the param types in the case of Array.map() argument
141
- T extends Array<
142
- QueryObserverOptionsForCreateQueries<
143
- infer TQueryFnData,
144
- infer TError,
145
- infer TData,
146
- infer TQueryKey
147
- >
148
- >
149
- ? Array<
150
- QueryObserverOptionsForCreateQueries<
151
- TQueryFnData,
152
- TError,
153
- TData,
154
- TQueryKey
155
- >
156
- >
157
- : // Fallback
158
- Array<QueryObserverOptionsForCreateQueries>
159
-
160
- /**
161
- * QueriesResults reducer recursively maps type param to results
162
- * @public
163
- */
164
- export type QueriesResults<
165
- T extends Array<any>,
166
- TResult extends Array<any> = [],
167
- TDepth extends ReadonlyArray<number> = [],
168
- > = TDepth['length'] extends MAXIMUM_DEPTH
169
- ? Array<QueryObserverResult>
170
- : T extends []
171
- ? []
172
- : T extends [infer Head]
173
- ? [...TResult, GetResults<Head>]
174
- : T extends [infer Head, ...infer Tail]
175
- ? QueriesResults<
176
- [...Tail],
177
- [...TResult, GetResults<Head>],
178
- [...TDepth, 1]
179
- >
180
- : T extends Array<
181
- QueryObserverOptionsForCreateQueries<
182
- infer TQueryFnData,
183
- infer TError,
184
- infer TData,
185
- any
186
- >
187
- >
188
- ? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results
189
- Array<
190
- QueryObserverResult<
191
- unknown extends TData ? TQueryFnData : TData,
192
- unknown extends TError ? DefaultError : TError
193
- >
194
- >
195
- : // Fallback
196
- Array<QueryObserverResult>
197
-
198
- /**
199
- * @param root0
200
- * @param root0.queries
201
- * @param root0.combine
202
- * @param injector
203
- * @param injector
204
- * @public
205
- */
206
- export function injectQueries<
207
- T extends Array<any>,
208
- TCombinedResult = QueriesResults<T>,
209
- >(
210
- {
211
- queries,
212
- ...options
213
- }: {
214
- queries: Signal<[...QueriesOptions<T>]>
215
- combine?: (result: QueriesResults<T>) => TCombinedResult
216
- },
217
- injector?: Injector,
218
- ): Signal<TCombinedResult> {
219
- !injector && assertInInjectionContext(injectQueries)
220
- return runInInjectionContext(injector ?? inject(Injector), () => {
221
- const destroyRef = inject(DestroyRef)
222
- const ngZone = inject(NgZone)
223
- const queryClient = inject(QueryClient)
224
- const isRestoring = injectIsRestoring()
225
-
226
- const defaultedQueries = computed(() => {
227
- return queries().map((opts) => {
228
- const defaultedOptions = queryClient.defaultQueryOptions(opts)
229
- // Make sure the results are already in fetching state before subscribing or updating options
230
- defaultedOptions._optimisticResults = isRestoring()
231
- ? 'isRestoring'
232
- : 'optimistic'
233
-
234
- return defaultedOptions as QueryObserverOptions
235
- })
236
- })
237
-
238
- const observer = new QueriesObserver<TCombinedResult>(
239
- queryClient,
240
- defaultedQueries(),
241
- options as QueriesObserverOptions<TCombinedResult>,
242
- )
243
-
244
- // Do not notify on updates because of changes in the options because
245
- // these changes should already be reflected in the optimistic result.
246
- effect(() => {
247
- observer.setQueries(
248
- defaultedQueries(),
249
- options as QueriesObserverOptions<TCombinedResult>,
250
- )
251
- })
252
-
253
- const [, getCombinedResult] = observer.getOptimisticResult(
254
- defaultedQueries(),
255
- (options as QueriesObserverOptions<TCombinedResult>).combine,
256
- )
257
-
258
- const result = signal(getCombinedResult() as any)
259
-
260
- effect(() => {
261
- const unsubscribe = isRestoring()
262
- ? () => undefined
263
- : ngZone.runOutsideAngular(() =>
264
- observer.subscribe(notifyManager.batchCalls(result.set)),
265
- )
266
- destroyRef.onDestroy(unsubscribe)
267
- })
268
-
269
- return result
270
- })
271
- }
@@ -1,23 +0,0 @@
1
- import { Injector, inject } from '@angular/core'
2
- import { QueryClient } from '@tanstack/query-core'
3
- import type { InjectOptions } from '@angular/core'
4
-
5
- /**
6
- * Injects a `QueryClient` instance and allows passing a custom injector.
7
- * @param injectOptions - Type of the options argument to inject and optionally a custom injector.
8
- * @returns The `QueryClient` instance.
9
- * @public
10
- * @deprecated Use `inject(QueryClient)` instead.
11
- * If you need to get a `QueryClient` from a custom injector, use `injector.get(QueryClient)`.
12
- *
13
- *
14
- * **Example**
15
- * ```ts
16
- * const queryClient = injectQueryClient();
17
- * ```
18
- */
19
- export function injectQueryClient(
20
- injectOptions: InjectOptions & { injector?: Injector } = {},
21
- ) {
22
- return (injectOptions.injector ?? inject(Injector)).get(QueryClient)
23
- }