@@ -146,7 +146,9 @@ describe('Watch Tests', () => {
146146 const updatesCount = 5 ;
147147 let receivedUpdatesCount = 0 ;
148148
149- const onUpdate = ( ) => receivedUpdatesCount ++ ;
149+ const onUpdate = ( ) => {
150+ receivedUpdatesCount ++ ;
151+ } ;
150152
151153 powersync . watch (
152154 'SELECT count() AS count FROM assets INNER JOIN customers ON customers.id = assets.customer_id' ,
@@ -216,7 +218,9 @@ describe('Watch Tests', () => {
216218 const assetsAbortController = new AbortController ( ) ;
217219
218220 let receivedAssetsUpdatesCount = 0 ;
219- const onWatchAssets = ( ) => receivedAssetsUpdatesCount ++ ;
221+ const onWatchAssets = ( ) => {
222+ receivedAssetsUpdatesCount ++ ;
223+ } ;
220224
221225 powersync . watch (
222226 'SELECT count() AS count FROM assets' ,
@@ -230,7 +234,9 @@ describe('Watch Tests', () => {
230234 const customersAbortController = new AbortController ( ) ;
231235
232236 let receivedCustomersUpdatesCount = 0 ;
233- const onWatchCustomers = ( ) => receivedCustomersUpdatesCount ++ ;
237+ const onWatchCustomers = ( ) => {
238+ receivedCustomersUpdatesCount ++ ;
239+ } ;
234240
235241 powersync . watch (
236242 'SELECT count() AS count FROM customers' ,
@@ -280,4 +286,39 @@ describe('Watch Tests', () => {
280286 await receivedError ;
281287 expect ( receivedErrorCount ) . equals ( 1 ) ;
282288 } ) ;
289+
290+ it ( 'should throttle watch callback overflow' , async ( ) => {
291+ const abortController = new AbortController ( ) ;
292+
293+ const updatesCount = 25 ;
294+
295+ let receivedWithManagedOverflowCount = 0 ;
296+ const onResultOverflow = ( ) => {
297+ receivedWithManagedOverflowCount ++ ;
298+ } ;
299+
300+ const overflowAbortController = new AbortController ( ) ;
301+ powersync . watch (
302+ 'SELECT count() AS count FROM assets' ,
303+ [ ] ,
304+ { onResult : onResultOverflow } ,
305+ { signal : overflowAbortController . signal , throttleMs : 1 }
306+ ) ;
307+
308+ // Allows us to count the number of updates received without the initial trigger
309+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 1 * throttleDuration ) ) ;
310+
311+ // Perform a large number of inserts to trigger overflow
312+ for ( let i = 0 ; i < updatesCount ; i ++ ) {
313+ powersync . execute ( 'INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)' , [ 'test' , uuid ( ) ] ) ;
314+ }
315+
316+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 1 * throttleDuration ) ) ;
317+
318+ abortController . abort ( ) ;
319+ overflowAbortController . abort ( ) ;
320+
321+ // Initial onResult plus two left after overflow was throttled for onChange triggers
322+ expect ( receivedWithManagedOverflowCount ) . toBe ( 3 ) ;
323+ } ) ;
283324} ) ;
0 commit comments