Open History to see the last commit
1import { DestroyRef, NgZone, computed, inject, signal } from '@angular/core'
2import {
3 QueryClient,
4 notifyManager,
5 replaceEqualDeep,
6} from '@tanstack/query-core'
7import { assertInjector } from './util/assert-injector/assert-injector'
8import type { Injector, Signal } from '@angular/core'
9import type {
10 Mutation,
11 MutationCache,
12 MutationFilters,
13 MutationState,
14} from '@tanstack/query-core'
15β
16type MutationStateOptions<TResult = MutationState> = {
17 filters?: MutationFilters
18 select?: (mutation: Mutation) => TResult
19}
20β
21function getResult<TResult = MutationState>(
22 mutationCache: MutationCache,
23 options: MutationStateOptions<TResult>,
24): Array<TResult> {
25 return mutationCache
26 .findAll(options.filters)
27 .map(
28 (mutation): TResult =>
29 (options.select ? options.select(mutation) : mutation.state) as TResult,
30 )
31}
32β
33/**
34 * @public
35 */
36export interface InjectMutationStateOptions {
37 injector?: Injector
38}
39β
40/**
41 * Injects a signal that tracks the state of all mutations.
42 * @param mutationStateOptionsFn - A function that returns mutation state options.
43 * @param options - The Angular injector to use.
44 * @returns The signal that tracks the state of all mutations.
45 * @public
46 */
47export function injectMutationState<TResult = MutationState>(
48 mutationStateOptionsFn: () => MutationStateOptions<TResult> = () => ({}),
49 options?: InjectMutationStateOptions,
50): Signal<Array<TResult>> {
51 return assertInjector(injectMutationState, options?.injector, () => {
52 const destroyRef = inject(DestroyRef)
53 const ngZone = inject(NgZone)
54 const queryClient = inject(QueryClient)
55β
56 const mutationCache = queryClient.getMutationCache()
57β
58 /**
59 * Computed signal that gets result from mutation cache based on passed options
60 * First element is the result, second element is the time when the result was set
61 */
62 const resultFromOptionsSignal = computed(() => {
63 return [
64 getResult(mutationCache, mutationStateOptionsFn()),
65 performance.now(),
66 ] as const
67 })
68β
69 /**
70 * Signal that contains result set by subscriber
71 * First element is the result, second element is the time when the result was set
72 */
73 const resultFromSubscriberSignal = signal<[Array<TResult>, number] | null>(
74 null,
75 )
76β
77 /**
78 * Returns the last result by either subscriber or options
79 */
80 const effectiveResultSignal = computed(() => {
81 const optionsResult = resultFromOptionsSignal()
82 const subscriberResult = resultFromSubscriberSignal()
83 return subscriberResult && subscriberResult[1] > optionsResult[1]
84 ? subscriberResult[0]
85 : optionsResult[0]
86 })
87β
88 const unsubscribe = ngZone.runOutsideAngular(() =>
89 mutationCache.subscribe(
90 notifyManager.batchCalls(() => {
91 const [lastResult] = effectiveResultSignal()
92 const nextResult = replaceEqualDeep(
93 lastResult,
94 getResult(mutationCache, mutationStateOptionsFn()),
95 )
96 if (lastResult !== nextResult) {
97 ngZone.run(() => {
98 resultFromSubscriberSignal.set([nextResult, performance.now()])
99 })
100 }
101 }),
102 ),
103 )
104β
105 destroyRef.onDestroy(unsubscribe)
106β
107 return effectiveResultSignal
108 })
109}