-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathinject-mutation-state.ts
More file actions
99 lines (89 loc) 路 2.81 KB
/
Copy pathinject-mutation-state.ts
File metadata and controls
99 lines (89 loc) 路 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
DestroyRef,
NgZone,
effect,
inject,
signal,
untracked,
} from '@angular/core'
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { injectQueryClient } from './inject-query-client'
import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
import type { Injector, Signal } from '@angular/core'
import type {
Mutation,
MutationCache,
MutationFilters,
MutationState,
} from '@tanstack/query-core'
type MutationStateOptions<TResult = MutationState> = {
filters?: MutationFilters
select?: (mutation: Mutation) => TResult
}
function getResult<TResult = MutationState>(
mutationCache: MutationCache,
options: MutationStateOptions<TResult>,
): Array<TResult> {
return mutationCache
.findAll(options.filters)
.map(
(mutation): TResult =>
(options.select ? options.select(mutation) : mutation.state) as TResult,
)
}
/**
* @public
*/
export interface InjectMutationStateOptions {
injector?: Injector
}
/**
* Injects a signal that tracks the state of all mutations.
* @param mutationStateOptionsFn - A function that returns mutation state options.
* @param options - The Angular injector to use.
* @returns The signal that tracks the state of all mutations.
* @public
*/
export function injectMutationState<TResult = MutationState>(
mutationStateOptionsFn: () => MutationStateOptions<TResult> = () => ({}),
options?: InjectMutationStateOptions,
): Signal<Array<TResult>> {
return assertInjector(injectMutationState, options?.injector, () => {
const destroyRef = inject(DestroyRef)
const queryClient = injectQueryClient()
const ngZone = inject(NgZone)
const mutationCache = queryClient.getMutationCache()
return lazySignalInitializer((injector) => {
const result = signal<Array<TResult>>(
getResult(mutationCache, mutationStateOptionsFn()),
)
effect(
() => {
const mutationStateOptions = mutationStateOptionsFn()
untracked(() => {
// Setting the signal from an effect because it's both 'computed' from options()
// and needs to be set imperatively in the mutationCache listener.
result.set(getResult(mutationCache, mutationStateOptions))
})
},
{ injector },
)
const unsubscribe = mutationCache.subscribe(
notifyManager.batchCalls(() => {
const nextResult = replaceEqualDeep(
result(),
getResult(mutationCache, mutationStateOptionsFn()),
)
if (result() !== nextResult) {
ngZone.run(() => {
result.set(nextResult)
})
}
}),
)
destroyRef.onDestroy(unsubscribe)
return result
})
})
}