(
optionsFn: () => CreateMutationOptions<
TData,
TError,
TVariables,
TOnMutateResult
>,
)
| 152 | * ``` |
| 153 | */ |
| 154 | export function injectMutation< |
| 155 | TData = unknown, |
| 156 | TError = DefaultError, |
| 157 | TVariables = void, |
| 158 | TOnMutateResult = unknown, |
| 159 | >( |
| 160 | optionsFn: () => CreateMutationOptions< |
| 161 | TData, |
| 162 | TError, |
| 163 | TVariables, |
| 164 | TOnMutateResult |
| 165 | >, |
| 166 | ): CreateMutationResult<TData, TError, TVariables, TOnMutateResult> { |
| 167 | if (typeof ngDevMode === 'undefined' || ngDevMode) { |
| 168 | assertInInjectionContext(injectMutation) |
| 169 | } |
| 170 | const outsideZone = injectQueryZone() |
| 171 | const queryClient = inject(QueryClient) |
| 172 | const lifecycle = injectPendingTasksLifecycle() |
| 173 | |
| 174 | const optionsSignal = computed(optionsFn) |
| 175 | |
| 176 | const observerSignal = computed( |
| 177 | () => new MutationObserver(queryClient, untracked(optionsSignal)), |
| 178 | ) |
| 179 | |
| 180 | // Configure the observer outside the result computation. Mutation-cache |
| 181 | // listeners can synchronously read the public result while setOptions emits. |
| 182 | effect(() => { |
| 183 | const options = optionsSignal() |
| 184 | outsideZone(() => untracked(() => observerSignal().setOptions(options))) |
| 185 | }) |
| 186 | |
| 187 | const mutationStateSignal = injectExternalStore(() => { |
| 188 | const observer = observerSignal() |
| 189 | return { |
| 190 | getSnapshot: () => observer.getCurrentResult(), |
| 191 | subscribe: (onStoreChange) => observer.subscribe(onStoreChange), |
| 192 | } |
| 193 | }) |
| 194 | |
| 195 | const mutate: CreateMutateFunction< |
| 196 | TData, |
| 197 | TError, |
| 198 | TVariables, |
| 199 | TOnMutateResult |
| 200 | > = (...args) => { |
| 201 | mutateAsync(...args).catch(noop) |
| 202 | } |
| 203 | |
| 204 | let pendingInvocations = 0 |
| 205 | |
| 206 | const mutateAsync: CreateMutateAsyncFunction< |
| 207 | TData, |
| 208 | TError, |
| 209 | TVariables, |
| 210 | TOnMutateResult |
| 211 | > = (...args) => { |
no test coverage detected