Open History to see the last commit
1import {
2 DestroyRef,
3 Injector,
4 NgZone,
5 assertInInjectionContext,
6 computed,
7 inject,
8 signal,
9} from '@angular/core'
10import {
11 QueryClient,
12 notifyManager,
13 replaceEqualDeep,
14} from '@tanstack/query-core'
15import type { Signal } from '@angular/core'
16import type {
17 Mutation,
18 MutationCache,
19 MutationFilters,
20 MutationState,
21} from '@tanstack/query-core'
22β
23type MutationStateOptions<TResult = MutationState> = {
24 filters?: MutationFilters
25 select?: (mutation: Mutation) => TResult
26}
27β
28/**
29 *
30 * @param mutationCache
31 * @param options
32 */
33function 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 */
48export 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 */
64export 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}