(
injectMutationFn: () => CreateMutationOptions<
TData,
TError,
TVariables,
TOnMutateResult
>,
options?: InjectMutationOptions,
)
| 43 | * @returns The mutation. |
| 44 | */ |
| 45 | export function injectMutation< |
| 46 | TData = unknown, |
| 47 | TError = DefaultError, |
| 48 | TVariables = void, |
| 49 | TOnMutateResult = unknown, |
| 50 | >( |
| 51 | injectMutationFn: () => CreateMutationOptions< |
| 52 | TData, |
| 53 | TError, |
| 54 | TVariables, |
| 55 | TOnMutateResult |
| 56 | >, |
| 57 | options?: InjectMutationOptions, |
| 58 | ): CreateMutationResult<TData, TError, TVariables, TOnMutateResult> { |
| 59 | !options?.injector && assertInInjectionContext(injectMutation) |
| 60 | const injector = options?.injector ?? inject(Injector) |
| 61 | const ngZone = injector.get(NgZone) |
| 62 | const pendingTasks = injector.get(PENDING_TASKS) |
| 63 | const queryClient = injector.get(QueryClient) |
| 64 | |
| 65 | /** |
| 66 | * computed() is used so signals can be inserted into the options |
| 67 | * making it reactive. Wrapping options in a function ensures embedded expressions |
| 68 | * are preserved and can keep being applied after signal changes |
| 69 | */ |
| 70 | const optionsSignal = computed(injectMutationFn) |
| 71 | |
| 72 | const observerSignal = (() => { |
| 73 | let instance: MutationObserver< |
| 74 | TData, |
| 75 | TError, |
| 76 | TVariables, |
| 77 | TOnMutateResult |
| 78 | > | null = null |
| 79 | |
| 80 | return computed(() => { |
| 81 | return (instance ||= new MutationObserver(queryClient, optionsSignal())) |
| 82 | }) |
| 83 | })() |
| 84 | |
| 85 | const mutateFnSignal = computed< |
| 86 | CreateMutateFunction<TData, TError, TVariables, TOnMutateResult> |
| 87 | >(() => { |
| 88 | const observer = observerSignal() |
| 89 | return (variables, mutateOptions) => { |
| 90 | observer.mutate(variables, mutateOptions).catch(noop) |
| 91 | } |
| 92 | }) |
| 93 | |
| 94 | /** |
| 95 | * Computed signal that gets result from mutation cache based on passed options |
| 96 | */ |
| 97 | const resultFromInitialOptionsSignal = computed(() => { |
| 98 | const observer = observerSignal() |
| 99 | return observer.getCurrentResult() |
| 100 | }) |
| 101 | |
| 102 | /** |
no test coverage detected
searching dependent graphs…