@@ -703,12 +703,9 @@ export class CollectionImpl<
703
703
this . optimisticDeletes . clear ( )
704
704
705
705
const activeTransactions : Array < Transaction < any > > = [ ]
706
- const completedTransactions : Array < Transaction < any > > = [ ]
707
706
708
707
for ( const transaction of this . transactions . values ( ) ) {
709
- if ( transaction . state === `completed` ) {
710
- completedTransactions . push ( transaction )
711
- } else if ( ! [ `completed` , `failed` ] . includes ( transaction . state ) ) {
708
+ if ( ! [ `completed` , `failed` ] . includes ( transaction . state ) ) {
712
709
activeTransactions . push ( transaction )
713
710
}
714
711
}
@@ -761,7 +758,6 @@ export class CollectionImpl<
761
758
// IMPORTANT: Skip complex filtering for user-triggered actions to prevent UI blocking
762
759
if ( this . pendingSyncedTransactions . length > 0 && ! triggeredByUserAction ) {
763
760
const pendingSyncKeys = new Set < TKey > ( )
764
- const completedTransactionMutations = new Set < string > ( )
765
761
766
762
// Collect keys from pending sync operations
767
763
for ( const transaction of this . pendingSyncedTransactions ) {
@@ -770,15 +766,6 @@ export class CollectionImpl<
770
766
}
771
767
}
772
768
773
- // Collect mutation IDs from completed transactions
774
- for ( const tx of completedTransactions ) {
775
- for ( const mutation of tx . mutations ) {
776
- if ( mutation . collection === this ) {
777
- completedTransactionMutations . add ( mutation . mutationId )
778
- }
779
- }
780
- }
781
-
782
769
// Only filter out delete events for keys that:
783
770
// 1. Have pending sync operations AND
784
771
// 2. Are from completed transactions (being cleaned up)
@@ -1290,6 +1277,30 @@ export class CollectionImpl<
1290
1277
}
1291
1278
}
1292
1279
1280
+ /**
1281
+ * Schedule cleanup of a transaction when it completes
1282
+ * @private
1283
+ */
1284
+ private scheduleTransactionCleanup ( transaction : Transaction < any > ) : void {
1285
+ // Only schedule cleanup for transactions that aren't already completed
1286
+ if ( transaction . state === `completed` ) {
1287
+ this . transactions . delete ( transaction . id )
1288
+ return
1289
+ }
1290
+
1291
+ // Schedule cleanup when the transaction completes
1292
+ transaction . isPersisted . promise
1293
+ . then ( ( ) => {
1294
+ // Transaction completed successfully, remove it immediately
1295
+ this . transactions . delete ( transaction . id )
1296
+ } )
1297
+ . catch ( ( ) => {
1298
+ // Transaction failed, but we want to keep failed transactions for reference
1299
+ // so don't remove it.
1300
+ // This empty catch block is necessary to prevent unhandled promise rejections.
1301
+ } )
1302
+ }
1303
+
1293
1304
private ensureStandardSchema ( schema : unknown ) : StandardSchema < T > {
1294
1305
// If the schema already implements the standard-schema interface, return it
1295
1306
if ( schema && `~standard` in ( schema as { } ) ) {
@@ -1650,6 +1661,7 @@ export class CollectionImpl<
1650
1661
ambientTransaction . applyMutations ( mutations )
1651
1662
1652
1663
this . transactions . set ( ambientTransaction . id , ambientTransaction )
1664
+ this . scheduleTransactionCleanup ( ambientTransaction )
1653
1665
this . recomputeOptimisticState ( true )
1654
1666
1655
1667
return ambientTransaction
@@ -1675,6 +1687,7 @@ export class CollectionImpl<
1675
1687
1676
1688
// Add the transaction to the collection's transactions store
1677
1689
this . transactions . set ( directOpTransaction . id , directOpTransaction )
1690
+ this . scheduleTransactionCleanup ( directOpTransaction )
1678
1691
this . recomputeOptimisticState ( true )
1679
1692
1680
1693
return directOpTransaction
@@ -1864,6 +1877,8 @@ export class CollectionImpl<
1864
1877
mutationFn : async ( ) => { } ,
1865
1878
} )
1866
1879
emptyTransaction . commit ( )
1880
+ // Schedule cleanup for empty transaction
1881
+ this . scheduleTransactionCleanup ( emptyTransaction )
1867
1882
return emptyTransaction
1868
1883
}
1869
1884
@@ -1872,6 +1887,7 @@ export class CollectionImpl<
1872
1887
ambientTransaction . applyMutations ( mutations )
1873
1888
1874
1889
this . transactions . set ( ambientTransaction . id , ambientTransaction )
1890
+ this . scheduleTransactionCleanup ( ambientTransaction )
1875
1891
this . recomputeOptimisticState ( true )
1876
1892
1877
1893
return ambientTransaction
@@ -1901,6 +1917,7 @@ export class CollectionImpl<
1901
1917
// Add the transaction to the collection's transactions store
1902
1918
1903
1919
this . transactions . set ( directOpTransaction . id , directOpTransaction )
1920
+ this . scheduleTransactionCleanup ( directOpTransaction )
1904
1921
this . recomputeOptimisticState ( true )
1905
1922
1906
1923
return directOpTransaction
@@ -1988,6 +2005,7 @@ export class CollectionImpl<
1988
2005
ambientTransaction . applyMutations ( mutations )
1989
2006
1990
2007
this . transactions . set ( ambientTransaction . id , ambientTransaction )
2008
+ this . scheduleTransactionCleanup ( ambientTransaction )
1991
2009
this . recomputeOptimisticState ( true )
1992
2010
1993
2011
return ambientTransaction
@@ -2014,6 +2032,7 @@ export class CollectionImpl<
2014
2032
directOpTransaction . commit ( )
2015
2033
2016
2034
this . transactions . set ( directOpTransaction . id , directOpTransaction )
2035
+ this . scheduleTransactionCleanup ( directOpTransaction )
2017
2036
this . recomputeOptimisticState ( true )
2018
2037
2019
2038
return directOpTransaction
0 commit comments