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β
45export interface InjectMutationStateOptions {
46 /**
47 * The `Injector` in which to create the mutation state signal.
48 *
49 * If this is not provided, the current injection context will be used instead (via `inject`).
50 */
51 injector?: Injector
52}
53β
54/**
55 * Injects a signal that tracks the state of all mutations.
56 * @param injectMutationStateFn - A function that returns mutation state options.
57 * @param options - The Angular injector to use.
58 * @returns The signal that tracks the state of all mutations.
59 */
60export function injectMutationState<TResult = MutationState>(
61 injectMutationStateFn: () => MutationStateOptions<TResult> = () => ({}),
62 options?: InjectMutationStateOptions,
63): Signal<Array<TResult>> {
64 !options?.injector && assertInInjectionContext(injectMutationState)
65 const injector = options?.injector ?? inject(Injector)
66 const destroyRef = injector.get(DestroyRef)
67 const ngZone = injector.get(NgZone)
68 const queryClient = injector.get(QueryClient)
69 const mutationCache = queryClient.getMutationCache()
70β
71 /**
72 * Computed signal that gets result from mutation cache based on passed options
73 * First element is the result, second element is the time when the result was set
74 */
75 const resultFromOptionsSignal = computed(() => {
76 return [
77 getResult(mutationCache, injectMutationStateFn()),
78 performance.now(),
79 ] as const
80 })
81β
82 /**
83 * Signal that contains result set by subscriber
84 * First element is the result, second element is the time when the result was set
85 */
86 const resultFromSubscriberSignal = signal<[Array<TResult>, number] | null>(
87 null,
88 )
89β
90 /**
91 * Returns the last result by either subscriber or options
92 */
93 const effectiveResultSignal = computed(() => {
94 const optionsResult = resultFromOptionsSignal()
95 const subscriberResult = resultFromSubscriberSignal()
96 return subscriberResult && subscriberResult[1] > optionsResult[1]
97 ? subscriberResult[0]
98 : optionsResult[0]
99 })
100β
101 const unsubscribe = ngZone.runOutsideAngular(() =>
102 mutationCache.subscribe(
103 notifyManager.batchCalls(() => {
104 const [lastResult] = effectiveResultSignal()
105 const nextResult = replaceEqualDeep(
106 lastResult,
107 getResult(mutationCache, injectMutationStateFn()),
108 )
109 if (lastResult !== nextResult) {
110 ngZone.run(() => {
111 resultFromSubscriberSignal.set([nextResult, performance.now()])
112 })
113 }
114 }),
115 ),
116 )
117β
118 destroyRef.onDestroy(unsubscribe)
119β
120 return effectiveResultSignal
121}