@tanstack/angular-query-experimental 5.60.0 → 5.60.3
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.
- package/build/{rollup.d.ts → index.d.ts} +280 -361
- package/build/index.js +572 -0
- package/build/index.js.map +1 -0
- package/package.json +12 -15
- package/src/create-base-query.ts +117 -0
- package/src/index.ts +28 -0
- package/src/infinite-query-options.ts +125 -0
- package/src/inject-infinite-query.ts +119 -0
- package/src/inject-is-fetching.ts +49 -0
- package/src/inject-is-mutating.ts +48 -0
- package/src/inject-mutation-state.ts +102 -0
- package/src/inject-mutation.ts +121 -0
- package/src/inject-queries.ts +246 -0
- package/src/inject-query-client.ts +22 -0
- package/src/inject-query.ts +207 -0
- package/src/providers.ts +351 -0
- package/src/query-options.ts +125 -0
- package/src/signal-proxy.ts +46 -0
- package/src/test-setup.ts +12 -0
- package/src/types.ts +328 -0
- package/src/util/assert-injector/assert-injector.test.ts +74 -0
- package/src/util/assert-injector/assert-injector.ts +81 -0
- package/src/util/index.ts +13 -0
- package/src/util/is-dev-mode/is-dev-mode.ts +3 -0
- package/src/util/lazy-init/lazy-init.ts +34 -0
- package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +23 -0
- package/build/README.md +0 -133
- package/build/esm2022/create-base-query.mjs +0 -62
- package/build/esm2022/index.mjs +0 -16
- package/build/esm2022/infinite-query-options.mjs +0 -12
- package/build/esm2022/inject-infinite-query.mjs +0 -15
- package/build/esm2022/inject-is-fetching.mjs +0 -38
- package/build/esm2022/inject-is-mutating.mjs +0 -37
- package/build/esm2022/inject-mutation-state.mjs +0 -47
- package/build/esm2022/inject-mutation.mjs +0 -51
- package/build/esm2022/inject-queries.mjs +0 -33
- package/build/esm2022/inject-query-client.mjs +0 -23
- package/build/esm2022/inject-query.mjs +0 -44
- package/build/esm2022/providers.mjs +0 -206
- package/build/esm2022/query-options.mjs +0 -26
- package/build/esm2022/signal-proxy.mjs +0 -38
- package/build/esm2022/tanstack-angular-query-experimental.mjs +0 -5
- package/build/esm2022/types.mjs +0 -3
- package/build/esm2022/util/assert-injector/assert-injector.mjs +0 -21
- package/build/esm2022/util/create-injection-token/create-injection-token.mjs +0 -61
- package/build/esm2022/util/index.mjs +0 -9
- package/build/esm2022/util/is-dev-mode/is-dev-mode.mjs +0 -3
- package/build/esm2022/util/lazy-init/lazy-init.mjs +0 -31
- package/build/esm2022/util/lazy-signal-initializer/lazy-signal-initializer.mjs +0 -14
- package/build/fesm2022/tanstack-angular-query-experimental.mjs +0 -738
- package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +0 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DestroyRef, NgZone, inject, signal } from '@angular/core'
|
|
2
|
+
import { QueryClient, notifyManager } from '@tanstack/query-core'
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
|
+
import type { QueryFilters } from '@tanstack/query-core'
|
|
5
|
+
import type { Injector, Signal } from '@angular/core'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Injects a signal that tracks the number of queries that your application is loading or
|
|
9
|
+
* fetching in the background.
|
|
10
|
+
*
|
|
11
|
+
* Can be used for app-wide loading indicators
|
|
12
|
+
* @param filters - The filters to apply to the query.
|
|
13
|
+
* @param injector - The Angular injector to use.
|
|
14
|
+
* @returns signal with number of loading or fetching queries.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export function injectIsFetching(
|
|
18
|
+
filters?: QueryFilters,
|
|
19
|
+
injector?: Injector,
|
|
20
|
+
): Signal<number> {
|
|
21
|
+
return assertInjector(injectIsFetching, injector, () => {
|
|
22
|
+
const destroyRef = inject(DestroyRef)
|
|
23
|
+
const ngZone = inject(NgZone)
|
|
24
|
+
const queryClient = inject(QueryClient)
|
|
25
|
+
|
|
26
|
+
const cache = queryClient.getQueryCache()
|
|
27
|
+
// isFetching is the prev value initialized on mount *
|
|
28
|
+
let isFetching = queryClient.isFetching(filters)
|
|
29
|
+
|
|
30
|
+
const result = signal(isFetching)
|
|
31
|
+
|
|
32
|
+
const unsubscribe = cache.subscribe(
|
|
33
|
+
notifyManager.batchCalls(() => {
|
|
34
|
+
const newIsFetching = queryClient.isFetching(filters)
|
|
35
|
+
if (isFetching !== newIsFetching) {
|
|
36
|
+
// * and update with each change
|
|
37
|
+
isFetching = newIsFetching
|
|
38
|
+
ngZone.run(() => {
|
|
39
|
+
result.set(isFetching)
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
destroyRef.onDestroy(unsubscribe)
|
|
46
|
+
|
|
47
|
+
return result
|
|
48
|
+
})
|
|
49
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DestroyRef, NgZone, inject, signal } from '@angular/core'
|
|
2
|
+
import { QueryClient, notifyManager } from '@tanstack/query-core'
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
|
+
import type { MutationFilters } from '@tanstack/query-core'
|
|
5
|
+
import type { Injector, Signal } from '@angular/core'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Injects a signal that tracks the number of mutations that your application is fetching.
|
|
9
|
+
*
|
|
10
|
+
* Can be used for app-wide loading indicators
|
|
11
|
+
* @param filters - The filters to apply to the query.
|
|
12
|
+
* @param injector - The Angular injector to use.
|
|
13
|
+
* @returns signal with number of fetching mutations.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export function injectIsMutating(
|
|
17
|
+
filters?: MutationFilters,
|
|
18
|
+
injector?: Injector,
|
|
19
|
+
): Signal<number> {
|
|
20
|
+
return assertInjector(injectIsMutating, injector, () => {
|
|
21
|
+
const destroyRef = inject(DestroyRef)
|
|
22
|
+
const ngZone = inject(NgZone)
|
|
23
|
+
const queryClient = inject(QueryClient)
|
|
24
|
+
|
|
25
|
+
const cache = queryClient.getMutationCache()
|
|
26
|
+
// isMutating is the prev value initialized on mount *
|
|
27
|
+
let isMutating = queryClient.isMutating(filters)
|
|
28
|
+
|
|
29
|
+
const result = signal(isMutating)
|
|
30
|
+
|
|
31
|
+
const unsubscribe = cache.subscribe(
|
|
32
|
+
notifyManager.batchCalls(() => {
|
|
33
|
+
const newIsMutating = queryClient.isMutating(filters)
|
|
34
|
+
if (isMutating !== newIsMutating) {
|
|
35
|
+
// * and update with each change
|
|
36
|
+
isMutating = newIsMutating
|
|
37
|
+
ngZone.run(() => {
|
|
38
|
+
result.set(isMutating)
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
}),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
destroyRef.onDestroy(unsubscribe)
|
|
45
|
+
|
|
46
|
+
return result
|
|
47
|
+
})
|
|
48
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
NgZone,
|
|
4
|
+
effect,
|
|
5
|
+
inject,
|
|
6
|
+
signal,
|
|
7
|
+
untracked,
|
|
8
|
+
} from '@angular/core'
|
|
9
|
+
import {
|
|
10
|
+
QueryClient,
|
|
11
|
+
notifyManager,
|
|
12
|
+
replaceEqualDeep,
|
|
13
|
+
} from '@tanstack/query-core'
|
|
14
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
15
|
+
import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
|
|
16
|
+
import type { Injector, Signal } from '@angular/core'
|
|
17
|
+
import type {
|
|
18
|
+
Mutation,
|
|
19
|
+
MutationCache,
|
|
20
|
+
MutationFilters,
|
|
21
|
+
MutationState,
|
|
22
|
+
} from '@tanstack/query-core'
|
|
23
|
+
|
|
24
|
+
type MutationStateOptions<TResult = MutationState> = {
|
|
25
|
+
filters?: MutationFilters
|
|
26
|
+
select?: (mutation: Mutation) => TResult
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getResult<TResult = MutationState>(
|
|
30
|
+
mutationCache: MutationCache,
|
|
31
|
+
options: MutationStateOptions<TResult>,
|
|
32
|
+
): Array<TResult> {
|
|
33
|
+
return mutationCache
|
|
34
|
+
.findAll(options.filters)
|
|
35
|
+
.map(
|
|
36
|
+
(mutation): TResult =>
|
|
37
|
+
(options.select ? options.select(mutation) : mutation.state) as TResult,
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export interface InjectMutationStateOptions {
|
|
45
|
+
injector?: Injector
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Injects a signal that tracks the state of all mutations.
|
|
50
|
+
* @param mutationStateOptionsFn - A function that returns mutation state options.
|
|
51
|
+
* @param options - The Angular injector to use.
|
|
52
|
+
* @returns The signal that tracks the state of all mutations.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export function injectMutationState<TResult = MutationState>(
|
|
56
|
+
mutationStateOptionsFn: () => MutationStateOptions<TResult> = () => ({}),
|
|
57
|
+
options?: InjectMutationStateOptions,
|
|
58
|
+
): Signal<Array<TResult>> {
|
|
59
|
+
return assertInjector(injectMutationState, options?.injector, () => {
|
|
60
|
+
const destroyRef = inject(DestroyRef)
|
|
61
|
+
const ngZone = inject(NgZone)
|
|
62
|
+
const queryClient = inject(QueryClient)
|
|
63
|
+
|
|
64
|
+
const mutationCache = queryClient.getMutationCache()
|
|
65
|
+
|
|
66
|
+
return lazySignalInitializer((injector) => {
|
|
67
|
+
const result = signal<Array<TResult>>(
|
|
68
|
+
getResult(mutationCache, mutationStateOptionsFn()),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
effect(
|
|
72
|
+
() => {
|
|
73
|
+
const mutationStateOptions = mutationStateOptionsFn()
|
|
74
|
+
untracked(() => {
|
|
75
|
+
// Setting the signal from an effect because it's both 'computed' from options()
|
|
76
|
+
// and needs to be set imperatively in the mutationCache listener.
|
|
77
|
+
result.set(getResult(mutationCache, mutationStateOptions))
|
|
78
|
+
})
|
|
79
|
+
},
|
|
80
|
+
{ injector },
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const unsubscribe = mutationCache.subscribe(
|
|
84
|
+
notifyManager.batchCalls(() => {
|
|
85
|
+
const nextResult = replaceEqualDeep(
|
|
86
|
+
result(),
|
|
87
|
+
getResult(mutationCache, mutationStateOptionsFn()),
|
|
88
|
+
)
|
|
89
|
+
if (result() !== nextResult) {
|
|
90
|
+
ngZone.run(() => {
|
|
91
|
+
result.set(nextResult)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
destroyRef.onDestroy(unsubscribe)
|
|
98
|
+
|
|
99
|
+
return result
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
Injector,
|
|
4
|
+
NgZone,
|
|
5
|
+
computed,
|
|
6
|
+
effect,
|
|
7
|
+
inject,
|
|
8
|
+
runInInjectionContext,
|
|
9
|
+
signal,
|
|
10
|
+
} from '@angular/core'
|
|
11
|
+
import {
|
|
12
|
+
MutationObserver,
|
|
13
|
+
QueryClient,
|
|
14
|
+
notifyManager,
|
|
15
|
+
} from '@tanstack/query-core'
|
|
16
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
17
|
+
import { signalProxy } from './signal-proxy'
|
|
18
|
+
import { noop, shouldThrowError } from './util'
|
|
19
|
+
|
|
20
|
+
import { lazyInit } from './util/lazy-init/lazy-init'
|
|
21
|
+
import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
|
|
22
|
+
import type {
|
|
23
|
+
CreateMutateFunction,
|
|
24
|
+
CreateMutationOptions,
|
|
25
|
+
CreateMutationResult,
|
|
26
|
+
} from './types'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
|
|
30
|
+
*
|
|
31
|
+
* Unlike queries, mutations are not run automatically.
|
|
32
|
+
* @param optionsFn - A function that returns mutation options.
|
|
33
|
+
* @param injector - The Angular injector to use.
|
|
34
|
+
* @returns The mutation.
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export function injectMutation<
|
|
38
|
+
TData = unknown,
|
|
39
|
+
TError = DefaultError,
|
|
40
|
+
TVariables = void,
|
|
41
|
+
TContext = unknown,
|
|
42
|
+
>(
|
|
43
|
+
optionsFn: (
|
|
44
|
+
client: QueryClient,
|
|
45
|
+
) => CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
46
|
+
injector?: Injector,
|
|
47
|
+
): CreateMutationResult<TData, TError, TVariables, TContext> {
|
|
48
|
+
return assertInjector(injectMutation, injector, () => {
|
|
49
|
+
const currentInjector = inject(Injector)
|
|
50
|
+
const destroyRef = inject(DestroyRef)
|
|
51
|
+
const ngZone = inject(NgZone)
|
|
52
|
+
const queryClient = inject(QueryClient)
|
|
53
|
+
|
|
54
|
+
return lazyInit(() =>
|
|
55
|
+
runInInjectionContext(currentInjector, () => {
|
|
56
|
+
const observer = new MutationObserver<
|
|
57
|
+
TData,
|
|
58
|
+
TError,
|
|
59
|
+
TVariables,
|
|
60
|
+
TContext
|
|
61
|
+
>(queryClient, optionsFn(queryClient))
|
|
62
|
+
const mutate: CreateMutateFunction<
|
|
63
|
+
TData,
|
|
64
|
+
TError,
|
|
65
|
+
TVariables,
|
|
66
|
+
TContext
|
|
67
|
+
> = (variables, mutateOptions) => {
|
|
68
|
+
observer.mutate(variables, mutateOptions).catch(noop)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
effect(() => {
|
|
72
|
+
observer.setOptions(
|
|
73
|
+
runInInjectionContext(currentInjector, () =>
|
|
74
|
+
optionsFn(queryClient),
|
|
75
|
+
),
|
|
76
|
+
)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const result = signal(observer.getCurrentResult())
|
|
80
|
+
|
|
81
|
+
const unsubscribe = observer.subscribe(
|
|
82
|
+
notifyManager.batchCalls(
|
|
83
|
+
(
|
|
84
|
+
state: MutationObserverResult<
|
|
85
|
+
TData,
|
|
86
|
+
TError,
|
|
87
|
+
TVariables,
|
|
88
|
+
TContext
|
|
89
|
+
>,
|
|
90
|
+
) => {
|
|
91
|
+
ngZone.run(() => {
|
|
92
|
+
if (
|
|
93
|
+
state.isError &&
|
|
94
|
+
shouldThrowError(observer.options.throwOnError, [state.error])
|
|
95
|
+
) {
|
|
96
|
+
throw state.error
|
|
97
|
+
}
|
|
98
|
+
result.set(state)
|
|
99
|
+
})
|
|
100
|
+
},
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
destroyRef.onDestroy(unsubscribe)
|
|
105
|
+
|
|
106
|
+
const resultSignal = computed(() => ({
|
|
107
|
+
...result(),
|
|
108
|
+
mutate,
|
|
109
|
+
mutateAsync: result().mutate,
|
|
110
|
+
}))
|
|
111
|
+
|
|
112
|
+
return signalProxy(resultSignal) as unknown as CreateMutationResult<
|
|
113
|
+
TData,
|
|
114
|
+
TError,
|
|
115
|
+
TVariables,
|
|
116
|
+
TContext
|
|
117
|
+
>
|
|
118
|
+
}),
|
|
119
|
+
)
|
|
120
|
+
})
|
|
121
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
QueriesObserver,
|
|
3
|
+
QueryClient,
|
|
4
|
+
notifyManager,
|
|
5
|
+
} from '@tanstack/query-core'
|
|
6
|
+
import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
|
|
7
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
8
|
+
import type { Injector, Signal } from '@angular/core'
|
|
9
|
+
import type {
|
|
10
|
+
DefaultError,
|
|
11
|
+
OmitKeyof,
|
|
12
|
+
QueriesObserverOptions,
|
|
13
|
+
QueriesPlaceholderDataFunction,
|
|
14
|
+
QueryFunction,
|
|
15
|
+
QueryKey,
|
|
16
|
+
QueryObserverOptions,
|
|
17
|
+
QueryObserverResult,
|
|
18
|
+
ThrowOnError,
|
|
19
|
+
} from '@tanstack/query-core'
|
|
20
|
+
|
|
21
|
+
// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
22
|
+
// `placeholderData` function does not have a parameter
|
|
23
|
+
type QueryObserverOptionsForCreateQueries<
|
|
24
|
+
TQueryFnData = unknown,
|
|
25
|
+
TError = DefaultError,
|
|
26
|
+
TData = TQueryFnData,
|
|
27
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
28
|
+
> = OmitKeyof<
|
|
29
|
+
QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
30
|
+
'placeholderData'
|
|
31
|
+
> & {
|
|
32
|
+
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Avoid TS depth-limit error in case of large array literal
|
|
36
|
+
type MAXIMUM_DEPTH = 20
|
|
37
|
+
|
|
38
|
+
// Widen the type of the symbol to enable type inference even if skipToken is not immutable.
|
|
39
|
+
type SkipTokenForUseQueries = symbol
|
|
40
|
+
|
|
41
|
+
type GetOptions<T> =
|
|
42
|
+
// Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
|
|
43
|
+
T extends {
|
|
44
|
+
queryFnData: infer TQueryFnData
|
|
45
|
+
error?: infer TError
|
|
46
|
+
data: infer TData
|
|
47
|
+
}
|
|
48
|
+
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
49
|
+
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
50
|
+
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
51
|
+
: T extends { data: infer TData; error?: infer TError }
|
|
52
|
+
? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
|
|
53
|
+
: // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
|
|
54
|
+
T extends [infer TQueryFnData, infer TError, infer TData]
|
|
55
|
+
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
56
|
+
: T extends [infer TQueryFnData, infer TError]
|
|
57
|
+
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
58
|
+
: T extends [infer TQueryFnData]
|
|
59
|
+
? QueryObserverOptionsForCreateQueries<TQueryFnData>
|
|
60
|
+
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
|
|
61
|
+
T extends {
|
|
62
|
+
queryFn?:
|
|
63
|
+
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
64
|
+
| SkipTokenForUseQueries
|
|
65
|
+
select: (data: any) => infer TData
|
|
66
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
67
|
+
}
|
|
68
|
+
? QueryObserverOptionsForCreateQueries<
|
|
69
|
+
TQueryFnData,
|
|
70
|
+
unknown extends TError ? DefaultError : TError,
|
|
71
|
+
unknown extends TData ? TQueryFnData : TData,
|
|
72
|
+
TQueryKey
|
|
73
|
+
>
|
|
74
|
+
: // Fallback
|
|
75
|
+
QueryObserverOptionsForCreateQueries
|
|
76
|
+
|
|
77
|
+
type GetResults<T> =
|
|
78
|
+
// Part 1: responsible for mapping explicit type parameter to function result, if object
|
|
79
|
+
T extends { queryFnData: any; error?: infer TError; data: infer TData }
|
|
80
|
+
? QueryObserverResult<TData, TError>
|
|
81
|
+
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
82
|
+
? QueryObserverResult<TQueryFnData, TError>
|
|
83
|
+
: T extends { data: infer TData; error?: infer TError }
|
|
84
|
+
? QueryObserverResult<TData, TError>
|
|
85
|
+
: // Part 2: responsible for mapping explicit type parameter to function result, if tuple
|
|
86
|
+
T extends [any, infer TError, infer TData]
|
|
87
|
+
? QueryObserverResult<TData, TError>
|
|
88
|
+
: T extends [infer TQueryFnData, infer TError]
|
|
89
|
+
? QueryObserverResult<TQueryFnData, TError>
|
|
90
|
+
: T extends [infer TQueryFnData]
|
|
91
|
+
? QueryObserverResult<TQueryFnData>
|
|
92
|
+
: // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
|
|
93
|
+
T extends {
|
|
94
|
+
queryFn?:
|
|
95
|
+
| QueryFunction<infer TQueryFnData, any>
|
|
96
|
+
| SkipTokenForUseQueries
|
|
97
|
+
select: (data: any) => infer TData
|
|
98
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
99
|
+
}
|
|
100
|
+
? QueryObserverResult<
|
|
101
|
+
unknown extends TData ? TQueryFnData : TData,
|
|
102
|
+
unknown extends TError ? DefaultError : TError
|
|
103
|
+
>
|
|
104
|
+
: // Fallback
|
|
105
|
+
QueryObserverResult
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export type QueriesOptions<
|
|
112
|
+
T extends Array<any>,
|
|
113
|
+
TResult extends Array<any> = [],
|
|
114
|
+
TDepth extends ReadonlyArray<number> = [],
|
|
115
|
+
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
116
|
+
? Array<QueryObserverOptionsForCreateQueries>
|
|
117
|
+
: T extends []
|
|
118
|
+
? []
|
|
119
|
+
: T extends [infer Head]
|
|
120
|
+
? [...TResult, GetOptions<Head>]
|
|
121
|
+
: T extends [infer Head, ...infer Tail]
|
|
122
|
+
? QueriesOptions<
|
|
123
|
+
[...Tail],
|
|
124
|
+
[...TResult, GetOptions<Head>],
|
|
125
|
+
[...TDepth, 1]
|
|
126
|
+
>
|
|
127
|
+
: ReadonlyArray<unknown> extends T
|
|
128
|
+
? T
|
|
129
|
+
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
|
|
130
|
+
// use this to infer the param types in the case of Array.map() argument
|
|
131
|
+
T extends Array<
|
|
132
|
+
QueryObserverOptionsForCreateQueries<
|
|
133
|
+
infer TQueryFnData,
|
|
134
|
+
infer TError,
|
|
135
|
+
infer TData,
|
|
136
|
+
infer TQueryKey
|
|
137
|
+
>
|
|
138
|
+
>
|
|
139
|
+
? Array<
|
|
140
|
+
QueryObserverOptionsForCreateQueries<
|
|
141
|
+
TQueryFnData,
|
|
142
|
+
TError,
|
|
143
|
+
TData,
|
|
144
|
+
TQueryKey
|
|
145
|
+
>
|
|
146
|
+
>
|
|
147
|
+
: // Fallback
|
|
148
|
+
Array<QueryObserverOptionsForCreateQueries>
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* QueriesResults reducer recursively maps type param to results
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
export type QueriesResults<
|
|
155
|
+
T extends Array<any>,
|
|
156
|
+
TResult extends Array<any> = [],
|
|
157
|
+
TDepth extends ReadonlyArray<number> = [],
|
|
158
|
+
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
159
|
+
? Array<QueryObserverResult>
|
|
160
|
+
: T extends []
|
|
161
|
+
? []
|
|
162
|
+
: T extends [infer Head]
|
|
163
|
+
? [...TResult, GetResults<Head>]
|
|
164
|
+
: T extends [infer Head, ...infer Tail]
|
|
165
|
+
? QueriesResults<
|
|
166
|
+
[...Tail],
|
|
167
|
+
[...TResult, GetResults<Head>],
|
|
168
|
+
[...TDepth, 1]
|
|
169
|
+
>
|
|
170
|
+
: T extends Array<
|
|
171
|
+
QueryObserverOptionsForCreateQueries<
|
|
172
|
+
infer TQueryFnData,
|
|
173
|
+
infer TError,
|
|
174
|
+
infer TData,
|
|
175
|
+
any
|
|
176
|
+
>
|
|
177
|
+
>
|
|
178
|
+
? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results
|
|
179
|
+
Array<
|
|
180
|
+
QueryObserverResult<
|
|
181
|
+
unknown extends TData ? TQueryFnData : TData,
|
|
182
|
+
unknown extends TError ? DefaultError : TError
|
|
183
|
+
>
|
|
184
|
+
>
|
|
185
|
+
: // Fallback
|
|
186
|
+
Array<QueryObserverResult>
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @public
|
|
190
|
+
*/
|
|
191
|
+
export function injectQueries<
|
|
192
|
+
T extends Array<any>,
|
|
193
|
+
TCombinedResult = QueriesResults<T>,
|
|
194
|
+
>(
|
|
195
|
+
{
|
|
196
|
+
queries,
|
|
197
|
+
...options
|
|
198
|
+
}: {
|
|
199
|
+
queries: Signal<[...QueriesOptions<T>]>
|
|
200
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
201
|
+
},
|
|
202
|
+
injector?: Injector,
|
|
203
|
+
): Signal<TCombinedResult> {
|
|
204
|
+
return assertInjector(injectQueries, injector, () => {
|
|
205
|
+
const queryClient = inject(QueryClient)
|
|
206
|
+
const destroyRef = inject(DestroyRef)
|
|
207
|
+
|
|
208
|
+
const defaultedQueries = computed(() => {
|
|
209
|
+
return queries().map((opts) => {
|
|
210
|
+
const defaultedOptions = queryClient.defaultQueryOptions(opts)
|
|
211
|
+
// Make sure the results are already in fetching state before subscribing or updating options
|
|
212
|
+
defaultedOptions._optimisticResults = 'optimistic'
|
|
213
|
+
|
|
214
|
+
return defaultedOptions as QueryObserverOptions
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
const observer = new QueriesObserver<TCombinedResult>(
|
|
219
|
+
queryClient,
|
|
220
|
+
defaultedQueries(),
|
|
221
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
// Do not notify on updates because of changes in the options because
|
|
225
|
+
// these changes should already be reflected in the optimistic result.
|
|
226
|
+
effect(() => {
|
|
227
|
+
observer.setQueries(
|
|
228
|
+
defaultedQueries(),
|
|
229
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
230
|
+
{ listeners: false },
|
|
231
|
+
)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
235
|
+
defaultedQueries(),
|
|
236
|
+
(options as QueriesObserverOptions<TCombinedResult>).combine,
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
const result = signal(getCombinedResult() as any)
|
|
240
|
+
|
|
241
|
+
const unsubscribe = observer.subscribe(notifyManager.batchCalls(result.set))
|
|
242
|
+
destroyRef.onDestroy(unsubscribe)
|
|
243
|
+
|
|
244
|
+
return result
|
|
245
|
+
})
|
|
246
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
*
|
|
8
|
+
* You can also use `inject(QueryClient)` if you don't need to provide a custom injector.
|
|
9
|
+
*
|
|
10
|
+
* **Example**
|
|
11
|
+
* ```ts
|
|
12
|
+
* const queryClient = injectQueryClient();
|
|
13
|
+
* ```
|
|
14
|
+
* @param injectOptions - Type of the options argument to inject and optionally a custom injector.
|
|
15
|
+
* @returns The `QueryClient` instance.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export function injectQueryClient(
|
|
19
|
+
injectOptions: InjectOptions & { injector?: Injector } = {},
|
|
20
|
+
) {
|
|
21
|
+
return (injectOptions.injector ?? inject(Injector)).get(QueryClient)
|
|
22
|
+
}
|