* Executes a mutation with the given variables. Creates a new transaction if none is active, * or adds to the existing active transaction. The strategy controls when * the transaction is actually committed.
(variables: TVariables)
| 129 | * the transaction is actually committed. |
| 130 | */ |
| 131 | function mutate(variables: TVariables): Transaction<T> { |
| 132 | // Create a new transaction if we don't have an active one |
| 133 | if (!activeTransaction || activeTransaction.state !== `pending`) { |
| 134 | activeTransaction = createTransaction<T>({ |
| 135 | ...transactionConfig, |
| 136 | mutationFn, |
| 137 | autoCommit: false, |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | // Execute onMutate with variables to apply optimistic updates |
| 142 | activeTransaction.mutate(() => { |
| 143 | onMutate(variables) |
| 144 | }) |
| 145 | |
| 146 | // Save reference before calling strategy.execute |
| 147 | const txToReturn = activeTransaction |
| 148 | |
| 149 | // For queue strategy, pass a function that commits the captured transaction |
| 150 | // This prevents the error when commitCallback tries to access the cleared activeTransaction |
| 151 | if (strategy._type === `queue`) { |
| 152 | const capturedTx = activeTransaction |
| 153 | activeTransaction = null // Clear so next mutation creates a new transaction |
| 154 | strategy.execute(() => { |
| 155 | capturedTx.commit().catch(() => { |
| 156 | // Errors are handled via transaction.isPersisted.promise |
| 157 | }) |
| 158 | return capturedTx |
| 159 | }) |
| 160 | } else { |
| 161 | // For debounce/throttle, use commitCallback which manages activeTransaction |
| 162 | strategy.execute(commitCallback) |
| 163 | } |
| 164 | |
| 165 | return txToReturn |
| 166 | } |
| 167 | |
| 168 | return mutate |
| 169 | } |
no test coverage detected