|
1 |
| -import { DestroyRef, NgZone, computed, inject, signal } from '@angular/core' |
| 1 | +import { |
| 2 | + DestroyRef, |
| 3 | + Injector, |
| 4 | + NgZone, |
| 5 | + assertInInjectionContext, |
| 6 | + computed, |
| 7 | + inject, |
| 8 | + signal, |
| 9 | +} from '@angular/core' |
2 | 10 | import {
|
3 | 11 | QueryClient,
|
4 | 12 | notifyManager,
|
5 | 13 | replaceEqualDeep,
|
6 | 14 | } from '@tanstack/query-core'
|
7 |
| -import { assertInjector } from './util/assert-injector/assert-injector' |
8 |
| -import type { Injector, Signal } from '@angular/core' |
| 15 | +import type { Signal } from '@angular/core' |
9 | 16 | import type {
|
10 | 17 | Mutation,
|
11 | 18 | MutationCache,
|
@@ -58,62 +65,61 @@ export function injectMutationState<TResult = MutationState>(
|
58 | 65 | injectMutationStateFn: () => MutationStateOptions<TResult> = () => ({}),
|
59 | 66 | options?: InjectMutationStateOptions,
|
60 | 67 | ): Signal<Array<TResult>> {
|
61 |
| - return assertInjector(injectMutationState, options?.injector, () => { |
62 |
| - const destroyRef = inject(DestroyRef) |
63 |
| - const ngZone = inject(NgZone) |
64 |
| - const queryClient = inject(QueryClient) |
65 |
| - |
66 |
| - const mutationCache = queryClient.getMutationCache() |
| 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() |
67 | 74 |
|
68 |
| - /** |
69 |
| - * Computed signal that gets result from mutation cache based on passed options |
70 |
| - * First element is the result, second element is the time when the result was set |
71 |
| - */ |
72 |
| - const resultFromOptionsSignal = computed(() => { |
73 |
| - return [ |
74 |
| - getResult(mutationCache, injectMutationStateFn()), |
75 |
| - performance.now(), |
76 |
| - ] as const |
77 |
| - }) |
| 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 | + }) |
78 | 85 |
|
79 |
| - /** |
80 |
| - * Signal that contains result set by subscriber |
81 |
| - * First element is the result, second element is the time when the result was set |
82 |
| - */ |
83 |
| - const resultFromSubscriberSignal = signal<[Array<TResult>, number] | null>( |
84 |
| - null, |
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 | + ) |
86 | 93 |
|
87 |
| - /** |
88 |
| - * Returns the last result by either subscriber or options |
89 |
| - */ |
90 |
| - const effectiveResultSignal = computed(() => { |
91 |
| - const optionsResult = resultFromOptionsSignal() |
92 |
| - const subscriberResult = resultFromSubscriberSignal() |
93 |
| - return subscriberResult && subscriberResult[1] > optionsResult[1] |
94 |
| - ? subscriberResult[0] |
95 |
| - : optionsResult[0] |
96 |
| - }) |
| 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 | + }) |
97 | 104 |
|
98 |
| - const unsubscribe = ngZone.runOutsideAngular(() => |
99 |
| - mutationCache.subscribe( |
100 |
| - notifyManager.batchCalls(() => { |
101 |
| - const [lastResult] = effectiveResultSignal() |
102 |
| - const nextResult = replaceEqualDeep( |
103 |
| - lastResult, |
104 |
| - getResult(mutationCache, injectMutationStateFn()), |
105 |
| - ) |
106 |
| - if (lastResult !== nextResult) { |
107 |
| - ngZone.run(() => { |
108 |
| - resultFromSubscriberSignal.set([nextResult, performance.now()]) |
109 |
| - }) |
110 |
| - } |
111 |
| - }), |
112 |
| - ), |
113 |
| - ) |
| 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 | + ) |
114 | 121 |
|
115 |
| - destroyRef.onDestroy(unsubscribe) |
| 122 | + destroyRef.onDestroy(unsubscribe) |
116 | 123 |
|
117 |
| - return effectiveResultSignal |
118 |
| - }) |
| 124 | + return effectiveResultSignal |
119 | 125 | }
|
0 commit comments