@@ -163,7 +163,6 @@ export class TransactionManager<T extends object = Record<string, unknown>> {
163163 updatedAt : new Date ( ) ,
164164 mutations,
165165 metadata : { } ,
166- isSynced : createDeferred ( ) ,
167166 isPersisted : createDeferred ( ) ,
168167 } as Transaction
169168 }
@@ -180,7 +179,7 @@ export class TransactionManager<T extends object = Record<string, unknown>> {
180179 }
181180
182181 /**
183- * Process a transaction through persist and awaitSync
182+ * Process a transaction through the mutation function
184183 *
185184 * @param transactionId - The ID of the transaction to process
186185 * @private
@@ -189,73 +188,32 @@ export class TransactionManager<T extends object = Record<string, unknown>> {
189188 const transaction = this . getTransaction ( transactionId )
190189 if ( ! transaction ) return
191190
192- // If no mutationFn is provided, throw an error
191+ // Throw an error if no mutation function is provided
193192 if ( ! this . collection . config . mutationFn ) {
194193 throw new Error (
195- `Cannot process transaction without a mutationFn in the collection config `
194+ `No mutation function provided for transaction ${ transactionId } `
196195 )
197196 }
198197
198+ // Set transaction state to persisting
199199 this . setTransactionState ( transactionId , `persisting` )
200200
201- this . collection . config . mutationFn
202- . persist ( {
203- transaction : this . createLiveTransactionReference ( transactionId ) ,
201+ // Create a live reference to the transaction that always returns the latest state
202+ const transactionRef = this . createLiveTransactionReference ( transactionId )
203+
204+ // Call the mutation function
205+ this . collection . config
206+ . mutationFn ( {
207+ transaction : transactionRef ,
204208 collection : this . collection ,
205209 } )
206- . then ( ( persistResult ) => {
210+ . then ( ( ) => {
207211 const tx = this . getTransaction ( transactionId )
208212 if ( ! tx ) return
209213
214+ // Mark as persisted
210215 tx . isPersisted ?. resolve ( true )
211- if ( this . collection . config . mutationFn ?. awaitSync ) {
212- this . setTransactionState ( transactionId , `persisted_awaiting_sync` )
213-
214- // Create a promise that rejects after 2 seconds
215- const timeoutPromise = new Promise < never > ( ( _ , reject ) => {
216- setTimeout ( ( ) => {
217- reject ( new Error ( `Sync operation timed out after 2 seconds` ) )
218- } , this . collection . config . mutationFn ?. awaitSyncTimeoutMs ?? 2000 )
219- } )
220-
221- // Race the awaitSync promise against the timeout
222- Promise . race ( [
223- this . collection . config . mutationFn . awaitSync ( {
224- transaction : this . createLiveTransactionReference ( transactionId ) ,
225- collection : this . collection ,
226- persistResult,
227- } ) ,
228- timeoutPromise ,
229- ] )
230- . then ( ( ) => {
231- const updatedTx = this . getTransaction ( transactionId )
232- if ( ! updatedTx ) return
233-
234- updatedTx . isSynced ?. resolve ( true )
235- this . setTransactionState ( transactionId , `completed` )
236- } )
237- // Catch awaitSync errors or timeout
238- . catch ( ( error ) => {
239- const updatedTx = this . getTransaction ( transactionId )
240- if ( ! updatedTx ) return
241-
242- // Update transaction with error information
243- updatedTx . error = {
244- message : error . message || `Error during sync` ,
245- error :
246- error instanceof Error ? error : new Error ( String ( error ) ) ,
247- }
248-
249- // Reject the isSynced promise
250- updatedTx . isSynced ?. reject ( error )
251-
252- // Set transaction state to failed
253- this . setTransaction ( updatedTx )
254- this . setTransactionState ( transactionId , `failed` )
255- } )
256- } else {
257- this . setTransactionState ( transactionId , `completed` )
258- }
216+ this . setTransactionState ( transactionId , `completed` )
259217 } )
260218 . catch ( ( error ) => {
261219 const tx = this . getTransaction ( transactionId )
@@ -267,9 +225,8 @@ export class TransactionManager<T extends object = Record<string, unknown>> {
267225 error : error instanceof Error ? error : new Error ( String ( error ) ) ,
268226 }
269227
270- // Reject both promises
228+ // Reject the promise
271229 tx . isPersisted ?. reject ( tx . error . error )
272- tx . isSynced ?. reject ( tx . error . error )
273230
274231 // Set transaction state to failed
275232 this . setTransactionState ( transactionId , `failed` )
0 commit comments