File size: 4,422 Bytes
1e92f2d |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import {
DestroyRef,
Injector,
NgZone,
assertInInjectionContext,
computed,
effect,
inject,
signal,
untracked,
} from '@angular/core'
import {
MutationObserver,
QueryClient,
noop,
notifyManager,
shouldThrowError,
} from '@tanstack/query-core'
import { signalProxy } from './signal-proxy'
import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
import type { CreateMutateFunction, CreateMutationResult } from './types'
import type { CreateMutationOptions } from './mutation-options'
export interface InjectMutationOptions {
/**
* The `Injector` in which to create the mutation.
*
* If this is not provided, the current injection context will be used instead (via `inject`).
*/
injector?: Injector
}
/**
* Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
*
* Unlike queries, mutations are not run automatically.
* @param injectMutationFn - A function that returns mutation options.
* @param options - Additional configuration
* @returns The mutation.
* @public
*/
export function injectMutation<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
>(
injectMutationFn: () => CreateMutationOptions<
TData,
TError,
TVariables,
TContext
>,
options?: InjectMutationOptions,
): CreateMutationResult<TData, TError, TVariables, TContext> {
!options?.injector && assertInInjectionContext(injectMutation)
const injector = options?.injector ?? inject(Injector)
const destroyRef = injector.get(DestroyRef)
const ngZone = injector.get(NgZone)
const queryClient = injector.get(QueryClient)
/**
* computed() is used so signals can be inserted into the options
* making it reactive. Wrapping options in a function ensures embedded expressions
* are preserved and can keep being applied after signal changes
*/
const optionsSignal = computed(injectMutationFn)
const observerSignal = (() => {
let instance: MutationObserver<TData, TError, TVariables, TContext> | null =
null
return computed(() => {
return (instance ||= new MutationObserver(queryClient, optionsSignal()))
})
})()
const mutateFnSignal = computed<
CreateMutateFunction<TData, TError, TVariables, TContext>
>(() => {
const observer = observerSignal()
return (variables, mutateOptions) => {
observer.mutate(variables, mutateOptions).catch(noop)
}
})
/**
* Computed signal that gets result from mutation cache based on passed options
*/
const resultFromInitialOptionsSignal = computed(() => {
const observer = observerSignal()
return observer.getCurrentResult()
})
/**
* Signal that contains result set by subscriber
*/
const resultFromSubscriberSignal = signal<MutationObserverResult<
TData,
TError,
TVariables,
TContext
> | null>(null)
effect(
() => {
const observer = observerSignal()
const observerOptions = optionsSignal()
untracked(() => {
observer.setOptions(observerOptions)
})
},
{
injector,
},
)
effect(
() => {
// observer.trackResult is not used as this optimization is not needed for Angular
const observer = observerSignal()
untracked(() => {
const unsubscribe = ngZone.runOutsideAngular(() =>
observer.subscribe(
notifyManager.batchCalls((state) => {
ngZone.run(() => {
if (
state.isError &&
shouldThrowError(observer.options.throwOnError, [state.error])
) {
ngZone.onError.emit(state.error)
throw state.error
}
resultFromSubscriberSignal.set(state)
})
}),
),
)
destroyRef.onDestroy(unsubscribe)
})
},
{
injector,
},
)
const resultSignal = computed(() => {
const resultFromSubscriber = resultFromSubscriberSignal()
const resultFromInitialOptions = resultFromInitialOptionsSignal()
const result = resultFromSubscriber ?? resultFromInitialOptions
return {
...result,
mutate: mutateFnSignal(),
mutateAsync: result.mutate,
}
})
return signalProxy(resultSignal) as CreateMutationResult<
TData,
TError,
TVariables,
TContext
>
}
|