@tanstack/angular-query-experimental 5.34.1 → 5.35.0
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/README.md +19 -2
- package/build/README.md +19 -2
- package/build/esm2022/create-base-query.mjs +3 -3
- package/build/esm2022/index.mjs +2 -2
- package/build/esm2022/infinite-query-options.mjs +9 -1
- package/build/esm2022/inject-infinite-query.mjs +11 -3
- package/build/esm2022/inject-is-fetching.mjs +11 -1
- package/build/esm2022/inject-is-mutating.mjs +10 -1
- package/build/esm2022/inject-mutation-state.mjs +8 -1
- package/build/esm2022/inject-mutation.mjs +13 -4
- package/build/esm2022/inject-queries.mjs +4 -1
- package/build/esm2022/inject-query-client.mjs +21 -3
- package/build/esm2022/inject-query.mjs +36 -3
- package/build/esm2022/providers.mjs +40 -1
- package/build/esm2022/query-options.mjs +23 -1
- package/build/esm2022/signal-proxy.mjs +2 -4
- package/build/esm2022/types.mjs +1 -1
- package/build/fesm2022/tanstack-angular-query-experimental.mjs +179 -14
- package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +1 -1
- package/build/rollup.d.ts +591 -0
- package/package.json +10 -6
- package/build/create-base-query.d.ts +0 -6
- package/build/index.d.ts +0 -14
- package/build/infinite-query-options.d.ts +0 -18
- package/build/inject-infinite-query.d.ts +0 -7
- package/build/inject-is-fetching.d.ts +0 -3
- package/build/inject-is-mutating.d.ts +0 -3
- package/build/inject-mutation-state.d.ts +0 -11
- package/build/inject-mutation.d.ts +0 -4
- package/build/inject-queries.d.ts +0 -71
- package/build/inject-query-client.d.ts +0 -13
- package/build/inject-query.d.ts +0 -7
- package/build/providers.d.ts +0 -3
- package/build/query-options.d.ts +0 -16
- package/build/signal-proxy.d.ts +0 -13
- package/build/types.d.ts +0 -44
- package/build/util/assert-injector/assert-injector.d.ts +0 -54
- package/build/util/create-injection-token/create-injection-token.d.ts +0 -52
- package/build/util/index.d.ts +0 -2
- package/build/util/lazy-init/lazy-init.d.ts +0 -1
- package/build/util/lazy-signal-initializer/lazy-signal-initializer.d.ts +0 -4
- package/src/__tests__/inject-infinite-query.test.ts +0 -64
- package/src/__tests__/inject-is-fetching.test.ts +0 -35
- package/src/__tests__/inject-is-mutating.test.ts +0 -39
- package/src/__tests__/inject-mutation-state.test-d.ts +0 -22
- package/src/__tests__/inject-mutation-state.test.ts +0 -177
- package/src/__tests__/inject-mutation.test-d.ts +0 -71
- package/src/__tests__/inject-mutation.test.ts +0 -458
- package/src/__tests__/inject-query.test-d.ts +0 -59
- package/src/__tests__/inject-query.test.ts +0 -349
- package/src/__tests__/query-options.test-d.ts +0 -127
- package/src/__tests__/signal-proxy.test.ts +0 -27
- package/src/__tests__/test-utils.ts +0 -131
- package/src/__tests__/util/lazy-init/lazy-init.test.ts +0 -126
- package/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts +0 -130
- package/src/create-base-query.ts +0 -116
- package/src/index.ts +0 -28
- package/src/infinite-query-options.ts +0 -94
- package/src/inject-infinite-query.ts +0 -93
- package/src/inject-is-fetching.ts +0 -39
- package/src/inject-is-mutating.ts +0 -39
- package/src/inject-mutation-state.ts +0 -97
- package/src/inject-mutation.ts +0 -109
- package/src/inject-queries.ts +0 -260
- package/src/inject-query-client.ts +0 -7
- package/src/inject-query.ts +0 -68
- package/src/providers.ts +0 -26
- package/src/query-options.ts +0 -50
- package/src/signal-proxy.ts +0 -48
- package/src/test-setup.ts +0 -12
- package/src/types.ts +0 -260
- package/src/util/assert-injector/assert-injector.test.ts +0 -74
- package/src/util/assert-injector/assert-injector.ts +0 -81
- package/src/util/create-injection-token/create-injection-token.test.ts +0 -32
- package/src/util/create-injection-token/create-injection-token.ts +0 -185
- package/src/util/index.ts +0 -13
- package/src/util/lazy-init/lazy-init.ts +0 -34
- package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +0 -28
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DestroyRef,
|
|
3
|
-
NgZone,
|
|
4
|
-
effect,
|
|
5
|
-
inject,
|
|
6
|
-
signal,
|
|
7
|
-
untracked,
|
|
8
|
-
} from '@angular/core'
|
|
9
|
-
import {
|
|
10
|
-
type DefaultError,
|
|
11
|
-
type Mutation,
|
|
12
|
-
type MutationCache,
|
|
13
|
-
type MutationFilters,
|
|
14
|
-
type MutationState,
|
|
15
|
-
notifyManager,
|
|
16
|
-
replaceEqualDeep,
|
|
17
|
-
} from '@tanstack/query-core'
|
|
18
|
-
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
19
|
-
import { injectQueryClient } from './inject-query-client'
|
|
20
|
-
import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
|
|
21
|
-
import type { Injector, Signal } from '@angular/core'
|
|
22
|
-
|
|
23
|
-
type MutationStateOptions<TResult = MutationState> = {
|
|
24
|
-
filters?: MutationFilters
|
|
25
|
-
select?: (
|
|
26
|
-
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
|
|
27
|
-
) => TResult
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function getResult<TResult = MutationState>(
|
|
31
|
-
mutationCache: MutationCache,
|
|
32
|
-
options: MutationStateOptions<TResult>,
|
|
33
|
-
): Array<TResult> {
|
|
34
|
-
return mutationCache
|
|
35
|
-
.findAll(options.filters)
|
|
36
|
-
.map(
|
|
37
|
-
(mutation): TResult =>
|
|
38
|
-
(options.select
|
|
39
|
-
? options.select(
|
|
40
|
-
mutation as Mutation<unknown, DefaultError, unknown, unknown>,
|
|
41
|
-
)
|
|
42
|
-
: mutation.state) as TResult,
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface InjectMutationStateOptions {
|
|
47
|
-
injector?: Injector
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function injectMutationState<TResult = MutationState>(
|
|
51
|
-
mutationStateOptionsFn: () => MutationStateOptions<TResult> = () => ({}),
|
|
52
|
-
options?: InjectMutationStateOptions,
|
|
53
|
-
): Signal<Array<TResult>> {
|
|
54
|
-
return assertInjector(injectMutationState, options?.injector, () => {
|
|
55
|
-
const destroyRef = inject(DestroyRef)
|
|
56
|
-
const queryClient = injectQueryClient()
|
|
57
|
-
const ngZone = inject(NgZone)
|
|
58
|
-
|
|
59
|
-
const mutationCache = queryClient.getMutationCache()
|
|
60
|
-
|
|
61
|
-
return lazySignalInitializer((injector) => {
|
|
62
|
-
const result = signal<Array<TResult>>(
|
|
63
|
-
getResult(mutationCache, mutationStateOptionsFn()),
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
effect(
|
|
67
|
-
() => {
|
|
68
|
-
const mutationStateOptions = mutationStateOptionsFn()
|
|
69
|
-
untracked(() => {
|
|
70
|
-
// Setting the signal from an effect because it's both 'computed' from options()
|
|
71
|
-
// and needs to be set imperatively in the mutationCache listener.
|
|
72
|
-
result.set(getResult(mutationCache, mutationStateOptions))
|
|
73
|
-
})
|
|
74
|
-
},
|
|
75
|
-
{ injector },
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
const unsubscribe = mutationCache.subscribe(
|
|
79
|
-
notifyManager.batchCalls(() => {
|
|
80
|
-
const nextResult = replaceEqualDeep(
|
|
81
|
-
result(),
|
|
82
|
-
getResult(mutationCache, mutationStateOptionsFn()),
|
|
83
|
-
)
|
|
84
|
-
if (result() !== nextResult) {
|
|
85
|
-
ngZone.run(() => {
|
|
86
|
-
result.set(nextResult)
|
|
87
|
-
})
|
|
88
|
-
}
|
|
89
|
-
}),
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
destroyRef.onDestroy(unsubscribe)
|
|
93
|
-
|
|
94
|
-
return result
|
|
95
|
-
})
|
|
96
|
-
})
|
|
97
|
-
}
|
package/src/inject-mutation.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DestroyRef,
|
|
3
|
-
Injector,
|
|
4
|
-
NgZone,
|
|
5
|
-
computed,
|
|
6
|
-
effect,
|
|
7
|
-
inject,
|
|
8
|
-
runInInjectionContext,
|
|
9
|
-
signal,
|
|
10
|
-
} from '@angular/core'
|
|
11
|
-
import { MutationObserver, notifyManager } from '@tanstack/query-core'
|
|
12
|
-
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
13
|
-
import { signalProxy } from './signal-proxy'
|
|
14
|
-
import { injectQueryClient } from './inject-query-client'
|
|
15
|
-
import { noop, shouldThrowError } from './util'
|
|
16
|
-
|
|
17
|
-
import { lazyInit } from './util/lazy-init/lazy-init'
|
|
18
|
-
import type {
|
|
19
|
-
DefaultError,
|
|
20
|
-
MutationObserverResult,
|
|
21
|
-
QueryClient,
|
|
22
|
-
} from '@tanstack/query-core'
|
|
23
|
-
import type {
|
|
24
|
-
CreateMutateFunction,
|
|
25
|
-
CreateMutationOptions,
|
|
26
|
-
CreateMutationResult,
|
|
27
|
-
} from './types'
|
|
28
|
-
|
|
29
|
-
export function injectMutation<
|
|
30
|
-
TData = unknown,
|
|
31
|
-
TError = DefaultError,
|
|
32
|
-
TVariables = void,
|
|
33
|
-
TContext = unknown,
|
|
34
|
-
>(
|
|
35
|
-
options: (
|
|
36
|
-
client: QueryClient,
|
|
37
|
-
) => CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
38
|
-
injector?: Injector,
|
|
39
|
-
): CreateMutationResult<TData, TError, TVariables, TContext> {
|
|
40
|
-
return assertInjector(injectMutation, injector, () => {
|
|
41
|
-
const queryClient = injectQueryClient()
|
|
42
|
-
const currentInjector = inject(Injector)
|
|
43
|
-
const destroyRef = inject(DestroyRef)
|
|
44
|
-
const ngZone = inject(NgZone)
|
|
45
|
-
|
|
46
|
-
return lazyInit(() =>
|
|
47
|
-
runInInjectionContext(currentInjector, () => {
|
|
48
|
-
const observer = new MutationObserver<
|
|
49
|
-
TData,
|
|
50
|
-
TError,
|
|
51
|
-
TVariables,
|
|
52
|
-
TContext
|
|
53
|
-
>(queryClient, options(queryClient))
|
|
54
|
-
const mutate: CreateMutateFunction<
|
|
55
|
-
TData,
|
|
56
|
-
TError,
|
|
57
|
-
TVariables,
|
|
58
|
-
TContext
|
|
59
|
-
> = (variables, mutateOptions) => {
|
|
60
|
-
observer.mutate(variables, mutateOptions).catch(noop)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
effect(() => {
|
|
64
|
-
observer.setOptions(options(queryClient))
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
const result = signal(observer.getCurrentResult())
|
|
68
|
-
|
|
69
|
-
const unsubscribe = observer.subscribe(
|
|
70
|
-
notifyManager.batchCalls(
|
|
71
|
-
(
|
|
72
|
-
state: MutationObserverResult<
|
|
73
|
-
TData,
|
|
74
|
-
TError,
|
|
75
|
-
TVariables,
|
|
76
|
-
TContext
|
|
77
|
-
>,
|
|
78
|
-
) => {
|
|
79
|
-
ngZone.run(() => {
|
|
80
|
-
if (
|
|
81
|
-
state.isError &&
|
|
82
|
-
shouldThrowError(observer.options.throwOnError, [state.error])
|
|
83
|
-
) {
|
|
84
|
-
throw state.error
|
|
85
|
-
}
|
|
86
|
-
result.set(state)
|
|
87
|
-
})
|
|
88
|
-
},
|
|
89
|
-
),
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
destroyRef.onDestroy(unsubscribe)
|
|
93
|
-
|
|
94
|
-
const resultSignal = computed(() => ({
|
|
95
|
-
...result(),
|
|
96
|
-
mutate,
|
|
97
|
-
mutateAsync: result().mutate,
|
|
98
|
-
}))
|
|
99
|
-
|
|
100
|
-
return signalProxy(resultSignal) as unknown as CreateMutationResult<
|
|
101
|
-
TData,
|
|
102
|
-
TError,
|
|
103
|
-
TVariables,
|
|
104
|
-
TContext
|
|
105
|
-
>
|
|
106
|
-
}),
|
|
107
|
-
)
|
|
108
|
-
})
|
|
109
|
-
}
|
package/src/inject-queries.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import { QueriesObserver, notifyManager } from '@tanstack/query-core'
|
|
2
|
-
import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
|
|
3
|
-
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
|
-
import { injectQueryClient } from './inject-query-client'
|
|
5
|
-
import type { Injector, Signal } from '@angular/core'
|
|
6
|
-
import type {
|
|
7
|
-
DefaultError,
|
|
8
|
-
OmitKeyof,
|
|
9
|
-
QueriesObserverOptions,
|
|
10
|
-
QueriesPlaceholderDataFunction,
|
|
11
|
-
QueryFunction,
|
|
12
|
-
QueryKey,
|
|
13
|
-
QueryObserverOptions,
|
|
14
|
-
QueryObserverResult,
|
|
15
|
-
SkipToken,
|
|
16
|
-
ThrowOnError,
|
|
17
|
-
} from '@tanstack/query-core'
|
|
18
|
-
|
|
19
|
-
// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
20
|
-
// `placeholderData` function does not have a parameter
|
|
21
|
-
type QueryObserverOptionsForCreateQueries<
|
|
22
|
-
TQueryFnData = unknown,
|
|
23
|
-
TError = DefaultError,
|
|
24
|
-
TData = TQueryFnData,
|
|
25
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
26
|
-
> = OmitKeyof<
|
|
27
|
-
QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
28
|
-
'placeholderData'
|
|
29
|
-
> & {
|
|
30
|
-
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Avoid TS depth-limit error in case of large array literal
|
|
34
|
-
type MAXIMUM_DEPTH = 20
|
|
35
|
-
|
|
36
|
-
type GetOptions<T> =
|
|
37
|
-
// Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
|
|
38
|
-
T extends {
|
|
39
|
-
queryFnData: infer TQueryFnData
|
|
40
|
-
error?: infer TError
|
|
41
|
-
data: infer TData
|
|
42
|
-
}
|
|
43
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
44
|
-
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
45
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
46
|
-
: T extends { data: infer TData; error?: infer TError }
|
|
47
|
-
? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
|
|
48
|
-
: // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
|
|
49
|
-
T extends [infer TQueryFnData, infer TError, infer TData]
|
|
50
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
51
|
-
: T extends [infer TQueryFnData, infer TError]
|
|
52
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
53
|
-
: T extends [infer TQueryFnData]
|
|
54
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData>
|
|
55
|
-
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
|
|
56
|
-
T extends {
|
|
57
|
-
queryFn?:
|
|
58
|
-
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
59
|
-
| SkipToken
|
|
60
|
-
select: (data: any) => infer TData
|
|
61
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
62
|
-
}
|
|
63
|
-
? QueryObserverOptionsForCreateQueries<
|
|
64
|
-
TQueryFnData,
|
|
65
|
-
TError,
|
|
66
|
-
TData,
|
|
67
|
-
TQueryKey
|
|
68
|
-
>
|
|
69
|
-
: T extends {
|
|
70
|
-
queryFn?:
|
|
71
|
-
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
72
|
-
| SkipToken
|
|
73
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
74
|
-
}
|
|
75
|
-
? QueryObserverOptionsForCreateQueries<
|
|
76
|
-
TQueryFnData,
|
|
77
|
-
TError,
|
|
78
|
-
TQueryFnData,
|
|
79
|
-
TQueryKey
|
|
80
|
-
>
|
|
81
|
-
: // Fallback
|
|
82
|
-
QueryObserverOptionsForCreateQueries
|
|
83
|
-
|
|
84
|
-
type GetResults<T> =
|
|
85
|
-
// Part 1: responsible for mapping explicit type parameter to function result, if object
|
|
86
|
-
T extends { queryFnData: any; error?: infer TError; data: infer TData }
|
|
87
|
-
? QueryObserverResult<TData, TError>
|
|
88
|
-
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
89
|
-
? QueryObserverResult<TQueryFnData, TError>
|
|
90
|
-
: T extends { data: infer TData; error?: infer TError }
|
|
91
|
-
? QueryObserverResult<TData, TError>
|
|
92
|
-
: // Part 2: responsible for mapping explicit type parameter to function result, if tuple
|
|
93
|
-
T extends [any, infer TError, infer TData]
|
|
94
|
-
? QueryObserverResult<TData, TError>
|
|
95
|
-
: T extends [infer TQueryFnData, infer TError]
|
|
96
|
-
? QueryObserverResult<TQueryFnData, TError>
|
|
97
|
-
: T extends [infer TQueryFnData]
|
|
98
|
-
? QueryObserverResult<TQueryFnData>
|
|
99
|
-
: // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
|
|
100
|
-
T extends {
|
|
101
|
-
queryFn?: QueryFunction<unknown, any>
|
|
102
|
-
select: (data: any) => infer TData
|
|
103
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
104
|
-
}
|
|
105
|
-
? QueryObserverResult<
|
|
106
|
-
TData,
|
|
107
|
-
unknown extends TError ? DefaultError : TError
|
|
108
|
-
>
|
|
109
|
-
: T extends {
|
|
110
|
-
queryFn?: QueryFunction<infer TQueryFnData, any>
|
|
111
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
112
|
-
}
|
|
113
|
-
? QueryObserverResult<
|
|
114
|
-
TQueryFnData,
|
|
115
|
-
unknown extends TError ? DefaultError : TError
|
|
116
|
-
>
|
|
117
|
-
: // Fallback
|
|
118
|
-
QueryObserverResult
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
122
|
-
*/
|
|
123
|
-
export type QueriesOptions<
|
|
124
|
-
T extends Array<any>,
|
|
125
|
-
TResult extends Array<any> = [],
|
|
126
|
-
TDepth extends ReadonlyArray<number> = [],
|
|
127
|
-
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
128
|
-
? Array<QueryObserverOptionsForCreateQueries>
|
|
129
|
-
: T extends []
|
|
130
|
-
? []
|
|
131
|
-
: T extends [infer Head]
|
|
132
|
-
? [...TResult, GetOptions<Head>]
|
|
133
|
-
: T extends [infer Head, ...infer Tail]
|
|
134
|
-
? QueriesOptions<
|
|
135
|
-
[...Tail],
|
|
136
|
-
[...TResult, GetOptions<Head>],
|
|
137
|
-
[...TDepth, 1]
|
|
138
|
-
>
|
|
139
|
-
: ReadonlyArray<unknown> extends T
|
|
140
|
-
? T
|
|
141
|
-
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
|
|
142
|
-
// use this to infer the param types in the case of Array.map() argument
|
|
143
|
-
T extends Array<
|
|
144
|
-
QueryObserverOptionsForCreateQueries<
|
|
145
|
-
infer TQueryFnData,
|
|
146
|
-
infer TError,
|
|
147
|
-
infer TData,
|
|
148
|
-
infer TQueryKey
|
|
149
|
-
>
|
|
150
|
-
>
|
|
151
|
-
? Array<
|
|
152
|
-
QueryObserverOptionsForCreateQueries<
|
|
153
|
-
TQueryFnData,
|
|
154
|
-
TError,
|
|
155
|
-
TData,
|
|
156
|
-
TQueryKey
|
|
157
|
-
>
|
|
158
|
-
>
|
|
159
|
-
: // Fallback
|
|
160
|
-
Array<QueryObserverOptionsForCreateQueries>
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* QueriesResults reducer recursively maps type param to results
|
|
164
|
-
*/
|
|
165
|
-
export type QueriesResults<
|
|
166
|
-
T extends Array<any>,
|
|
167
|
-
TResult extends Array<any> = [],
|
|
168
|
-
TDepth extends ReadonlyArray<number> = [],
|
|
169
|
-
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
170
|
-
? Array<QueryObserverResult>
|
|
171
|
-
: T extends []
|
|
172
|
-
? []
|
|
173
|
-
: T extends [infer Head]
|
|
174
|
-
? [...TResult, GetResults<Head>]
|
|
175
|
-
: T extends [infer Head, ...infer Tail]
|
|
176
|
-
? QueriesResults<
|
|
177
|
-
[...Tail],
|
|
178
|
-
[...TResult, GetResults<Head>],
|
|
179
|
-
[...TDepth, 1]
|
|
180
|
-
>
|
|
181
|
-
: T extends Array<
|
|
182
|
-
QueryObserverOptionsForCreateQueries<
|
|
183
|
-
infer TQueryFnData,
|
|
184
|
-
infer TError,
|
|
185
|
-
infer TData,
|
|
186
|
-
any
|
|
187
|
-
>
|
|
188
|
-
>
|
|
189
|
-
? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results
|
|
190
|
-
Array<
|
|
191
|
-
QueryObserverResult<
|
|
192
|
-
unknown extends TData ? TQueryFnData : TData,
|
|
193
|
-
unknown extends TError ? DefaultError : TError
|
|
194
|
-
>
|
|
195
|
-
>
|
|
196
|
-
: // Fallback
|
|
197
|
-
Array<QueryObserverResult>
|
|
198
|
-
|
|
199
|
-
export function injectQueries<
|
|
200
|
-
T extends Array<any>,
|
|
201
|
-
TCombinedResult = QueriesResults<T>,
|
|
202
|
-
>(
|
|
203
|
-
{
|
|
204
|
-
queries,
|
|
205
|
-
...options
|
|
206
|
-
}: {
|
|
207
|
-
queries: Signal<[...QueriesOptions<T>]>
|
|
208
|
-
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
209
|
-
},
|
|
210
|
-
injector?: Injector,
|
|
211
|
-
): Signal<TCombinedResult> {
|
|
212
|
-
return assertInjector(injectQueries, injector, () => {
|
|
213
|
-
const queryClient = injectQueryClient()
|
|
214
|
-
const destroyRef = inject(DestroyRef)
|
|
215
|
-
|
|
216
|
-
const defaultedQueries = computed(() => {
|
|
217
|
-
return queries().map((opts) => {
|
|
218
|
-
const defaultedOptions = queryClient.defaultQueryOptions(opts)
|
|
219
|
-
// Make sure the results are already in fetching state before subscribing or updating options
|
|
220
|
-
defaultedOptions._optimisticResults = 'optimistic'
|
|
221
|
-
|
|
222
|
-
return defaultedOptions as QueryObserverOptions<
|
|
223
|
-
unknown,
|
|
224
|
-
Error,
|
|
225
|
-
unknown,
|
|
226
|
-
unknown,
|
|
227
|
-
QueryKey
|
|
228
|
-
>
|
|
229
|
-
})
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
const observer = new QueriesObserver<TCombinedResult>(
|
|
233
|
-
queryClient,
|
|
234
|
-
defaultedQueries(),
|
|
235
|
-
options as QueriesObserverOptions<TCombinedResult>,
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
// Do not notify on updates because of changes in the options because
|
|
239
|
-
// these changes should already be reflected in the optimistic result.
|
|
240
|
-
effect(() => {
|
|
241
|
-
observer.setQueries(
|
|
242
|
-
defaultedQueries(),
|
|
243
|
-
options as QueriesObserverOptions<TCombinedResult>,
|
|
244
|
-
{ listeners: false },
|
|
245
|
-
)
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
249
|
-
defaultedQueries(),
|
|
250
|
-
(options as QueriesObserverOptions<TCombinedResult>).combine,
|
|
251
|
-
)
|
|
252
|
-
|
|
253
|
-
const result = signal(getCombinedResult() as any)
|
|
254
|
-
|
|
255
|
-
const unsubscribe = observer.subscribe(notifyManager.batchCalls(result.set))
|
|
256
|
-
destroyRef.onDestroy(unsubscribe)
|
|
257
|
-
|
|
258
|
-
return result
|
|
259
|
-
})
|
|
260
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { createNoopInjectionToken } from './util/create-injection-token/create-injection-token'
|
|
2
|
-
import type { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
|
|
4
|
-
const [injectQueryClient, provideQueryClient, QUERY_CLIENT] =
|
|
5
|
-
createNoopInjectionToken<QueryClient>('QueryClientToken')
|
|
6
|
-
|
|
7
|
-
export { injectQueryClient, provideQueryClient, QUERY_CLIENT }
|
package/src/inject-query.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { QueryObserver } from '@tanstack/query-core'
|
|
2
|
-
import { runInInjectionContext } from '@angular/core'
|
|
3
|
-
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
|
-
import { injectQueryClient } from './inject-query-client'
|
|
5
|
-
import { createBaseQuery } from './create-base-query'
|
|
6
|
-
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
|
|
7
|
-
import type { Injector } from '@angular/core'
|
|
8
|
-
import type {
|
|
9
|
-
CreateQueryOptions,
|
|
10
|
-
CreateQueryResult,
|
|
11
|
-
DefinedCreateQueryResult,
|
|
12
|
-
} from './types'
|
|
13
|
-
import type {
|
|
14
|
-
DefinedInitialDataOptions,
|
|
15
|
-
UndefinedInitialDataOptions,
|
|
16
|
-
} from './query-options'
|
|
17
|
-
|
|
18
|
-
export function injectQuery<
|
|
19
|
-
TQueryFnData = unknown,
|
|
20
|
-
TError = DefaultError,
|
|
21
|
-
TData = TQueryFnData,
|
|
22
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
23
|
-
>(
|
|
24
|
-
options: (
|
|
25
|
-
client: QueryClient,
|
|
26
|
-
) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
27
|
-
injector?: Injector,
|
|
28
|
-
): CreateQueryResult<TData, TError>
|
|
29
|
-
|
|
30
|
-
export function injectQuery<
|
|
31
|
-
TQueryFnData = unknown,
|
|
32
|
-
TError = DefaultError,
|
|
33
|
-
TData = TQueryFnData,
|
|
34
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
35
|
-
>(
|
|
36
|
-
options: (
|
|
37
|
-
client: QueryClient,
|
|
38
|
-
) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
39
|
-
injector?: Injector,
|
|
40
|
-
): DefinedCreateQueryResult<TData, TError>
|
|
41
|
-
|
|
42
|
-
export function injectQuery<
|
|
43
|
-
TQueryFnData = unknown,
|
|
44
|
-
TError = DefaultError,
|
|
45
|
-
TData = TQueryFnData,
|
|
46
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
47
|
-
>(
|
|
48
|
-
options: (
|
|
49
|
-
client: QueryClient,
|
|
50
|
-
) => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
51
|
-
injector?: Injector,
|
|
52
|
-
): CreateQueryResult<TData, TError>
|
|
53
|
-
|
|
54
|
-
export function injectQuery(
|
|
55
|
-
options: (client: QueryClient) => CreateQueryOptions,
|
|
56
|
-
injector?: Injector,
|
|
57
|
-
) {
|
|
58
|
-
const assertedInjector = assertInjector(injectQuery, injector)
|
|
59
|
-
return assertInjector(injectQuery, injector, () => {
|
|
60
|
-
const queryClient = injectQueryClient()
|
|
61
|
-
return createBaseQuery(
|
|
62
|
-
(client) =>
|
|
63
|
-
runInInjectionContext(assertedInjector, () => options(client)),
|
|
64
|
-
QueryObserver,
|
|
65
|
-
queryClient,
|
|
66
|
-
)
|
|
67
|
-
})
|
|
68
|
-
}
|
package/src/providers.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DestroyRef,
|
|
3
|
-
ENVIRONMENT_INITIALIZER,
|
|
4
|
-
inject,
|
|
5
|
-
makeEnvironmentProviders,
|
|
6
|
-
} from '@angular/core'
|
|
7
|
-
import { provideQueryClient } from './inject-query-client'
|
|
8
|
-
import type { EnvironmentProviders } from '@angular/core'
|
|
9
|
-
import type { QueryClient } from '@tanstack/query-core'
|
|
10
|
-
|
|
11
|
-
export function provideAngularQuery(
|
|
12
|
-
queryClient: QueryClient,
|
|
13
|
-
): EnvironmentProviders {
|
|
14
|
-
return makeEnvironmentProviders([
|
|
15
|
-
provideQueryClient(queryClient),
|
|
16
|
-
{
|
|
17
|
-
provide: ENVIRONMENT_INITIALIZER,
|
|
18
|
-
multi: true,
|
|
19
|
-
useValue: () => {
|
|
20
|
-
queryClient.mount()
|
|
21
|
-
// Unmount the query client on application destroy
|
|
22
|
-
inject(DestroyRef).onDestroy(() => queryClient.unmount())
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
])
|
|
26
|
-
}
|
package/src/query-options.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
|
|
2
|
-
import type { CreateQueryOptions } from './types'
|
|
3
|
-
|
|
4
|
-
export type UndefinedInitialDataOptions<
|
|
5
|
-
TQueryFnData = unknown,
|
|
6
|
-
TError = DefaultError,
|
|
7
|
-
TData = TQueryFnData,
|
|
8
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
9
|
-
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
10
|
-
initialData?: undefined
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type NonUndefinedGuard<T> = T extends undefined ? never : T
|
|
14
|
-
|
|
15
|
-
export type DefinedInitialDataOptions<
|
|
16
|
-
TQueryFnData = unknown,
|
|
17
|
-
TError = DefaultError,
|
|
18
|
-
TData = TQueryFnData,
|
|
19
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
20
|
-
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
21
|
-
initialData:
|
|
22
|
-
| NonUndefinedGuard<TQueryFnData>
|
|
23
|
-
| (() => NonUndefinedGuard<TQueryFnData>)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function queryOptions<
|
|
27
|
-
TQueryFnData = unknown,
|
|
28
|
-
TError = DefaultError,
|
|
29
|
-
TData = TQueryFnData,
|
|
30
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
31
|
-
>(
|
|
32
|
-
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
33
|
-
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
34
|
-
queryKey: DataTag<TQueryKey, TQueryFnData>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function queryOptions<
|
|
38
|
-
TQueryFnData = unknown,
|
|
39
|
-
TError = DefaultError,
|
|
40
|
-
TData = TQueryFnData,
|
|
41
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
42
|
-
>(
|
|
43
|
-
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
44
|
-
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
45
|
-
queryKey: DataTag<TQueryKey, TQueryFnData>
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function queryOptions(options: unknown) {
|
|
49
|
-
return options
|
|
50
|
-
}
|
package/src/signal-proxy.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { computed, untracked } from '@angular/core'
|
|
2
|
-
import type { Signal } from '@angular/core'
|
|
3
|
-
|
|
4
|
-
export type MapToSignals<T> = {
|
|
5
|
-
[K in keyof T]: T[K] extends Function ? T[K] : Signal<T[K]>
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Exposes fields of an object passed via an Angular `Signal` as `Computed` signals.
|
|
10
|
-
*
|
|
11
|
-
* Functions on the object are passed through as-is.
|
|
12
|
-
*
|
|
13
|
-
* @param inputSignal - `Signal` that must return an object.
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export function signalProxy<TInput extends Record<string | symbol, any>>(
|
|
17
|
-
inputSignal: Signal<TInput>,
|
|
18
|
-
) {
|
|
19
|
-
const internalState = {} as MapToSignals<TInput>
|
|
20
|
-
|
|
21
|
-
return new Proxy<MapToSignals<TInput>>(internalState, {
|
|
22
|
-
get(target, prop) {
|
|
23
|
-
// first check if we have it in our internal state and return it
|
|
24
|
-
const computedField = target[prop]
|
|
25
|
-
if (computedField) return computedField
|
|
26
|
-
|
|
27
|
-
// then, check if it's a function on the resultState and return it
|
|
28
|
-
const targetField = untracked(inputSignal)[prop]
|
|
29
|
-
if (typeof targetField === 'function') return targetField
|
|
30
|
-
|
|
31
|
-
// finally, create a computed field, store it and return it
|
|
32
|
-
// @ts-expect-error
|
|
33
|
-
return (target[prop] = computed(() => inputSignal()[prop]))
|
|
34
|
-
},
|
|
35
|
-
has(_, prop) {
|
|
36
|
-
return !!untracked(inputSignal)[prop]
|
|
37
|
-
},
|
|
38
|
-
ownKeys() {
|
|
39
|
-
return Reflect.ownKeys(untracked(inputSignal))
|
|
40
|
-
},
|
|
41
|
-
getOwnPropertyDescriptor() {
|
|
42
|
-
return {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
})
|
|
48
|
-
}
|
package/src/test-setup.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import '@analogjs/vite-plugin-angular/setup-vitest'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
BrowserDynamicTestingModule,
|
|
5
|
-
platformBrowserDynamicTesting,
|
|
6
|
-
} from '@angular/platform-browser-dynamic/testing'
|
|
7
|
-
import { getTestBed } from '@angular/core/testing'
|
|
8
|
-
|
|
9
|
-
getTestBed().initTestEnvironment(
|
|
10
|
-
BrowserDynamicTestingModule,
|
|
11
|
-
platformBrowserDynamicTesting(),
|
|
12
|
-
)
|