MCPcopy Create free account
hub / github.com/TanStack/query / injectMutation

Function injectMutation

packages/angular-query-experimental/src/inject-mutation.ts:174–328  ·  view source on GitHub ↗
(
  injectMutationFn: () => CreateMutationOptions<
    TData,
    TError,
    TVariables,
    TOnMutateResult
  >,
  options?: InjectMutationOptions,
)

Source from the content-addressed store, hash-verified

172 * ```
173 */
174export function injectMutation<
175 TData = unknown,
176 TError = DefaultError,
177 TVariables = void,
178 TOnMutateResult = unknown,
179>(
180 injectMutationFn: () => CreateMutationOptions<
181 TData,
182 TError,
183 TVariables,
184 TOnMutateResult
185 >,
186 options?: InjectMutationOptions,
187): CreateMutationResult<TData, TError, TVariables, TOnMutateResult> {
188 !options?.injector && assertInInjectionContext(injectMutation)
189 const injector = options?.injector ?? inject(Injector)
190 const ngZone = injector.get(NgZone)
191 const pendingTasks = injector.get(PENDING_TASKS)
192 const queryClient = injector.get(QueryClient)
193
194 /**
195 * computed() is used so signals can be inserted into the options
196 * making it reactive. Wrapping options in a function ensures embedded expressions
197 * are preserved and can keep being applied after signal changes
198 */
199 const optionsSignal = computed(injectMutationFn)
200
201 const observerSignal = (() => {
202 let instance: MutationObserver<
203 TData,
204 TError,
205 TVariables,
206 TOnMutateResult
207 > | null = null
208
209 return computed(() => {
210 return (instance ||= new MutationObserver(queryClient, optionsSignal()))
211 })
212 })()
213
214 const mutateFnSignal = computed<
215 CreateMutateFunction<TData, TError, TVariables, TOnMutateResult>
216 >(() => {
217 const observer = observerSignal()
218 return (
219 ...args: Parameters<
220 CreateMutateFunction<TData, TError, TVariables, TOnMutateResult>
221 >
222 ) => {
223 observer.mutate(args[0] as TVariables, args[1]).catch(noop)
224 }
225 })
226
227 /**
228 * Computed signal that gets result from mutation cache based on passed options
229 */
230 const resultFromInitialOptionsSignal = computed(() => {
231 const observer = observerSignal()

Callers 12

TestComponentClass · 0.85
PageClass · 0.85
FakeComponentClass · 0.85
PageClass · 0.85

Calls 9

shouldThrowErrorFunction · 0.90
signalProxyFunction · 0.90
getMethod · 0.80
unsubscribeFunction · 0.50
mutateMethod · 0.45
getCurrentResultMethod · 0.45
setOptionsMethod · 0.45
subscribeMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected