@@ -146,7 +146,9 @@ describe('Watch Tests', () => {
146
146
const updatesCount = 5 ;
147
147
let receivedUpdatesCount = 0 ;
148
148
149
- const onUpdate = ( ) => receivedUpdatesCount ++ ;
149
+ const onUpdate = ( ) => {
150
+ receivedUpdatesCount ++ ;
151
+ } ;
150
152
151
153
powersync . watch (
152
154
'SELECT count() AS count FROM assets INNER JOIN customers ON customers.id = assets.customer_id' ,
@@ -216,7 +218,9 @@ describe('Watch Tests', () => {
216
218
const assetsAbortController = new AbortController ( ) ;
217
219
218
220
let receivedAssetsUpdatesCount = 0 ;
219
- const onWatchAssets = ( ) => receivedAssetsUpdatesCount ++ ;
221
+ const onWatchAssets = ( ) => {
222
+ receivedAssetsUpdatesCount ++ ;
223
+ } ;
220
224
221
225
powersync . watch (
222
226
'SELECT count() AS count FROM assets' ,
@@ -230,7 +234,9 @@ describe('Watch Tests', () => {
230
234
const customersAbortController = new AbortController ( ) ;
231
235
232
236
let receivedCustomersUpdatesCount = 0 ;
233
- const onWatchCustomers = ( ) => receivedCustomersUpdatesCount ++ ;
237
+ const onWatchCustomers = ( ) => {
238
+ receivedCustomersUpdatesCount ++ ;
239
+ } ;
234
240
235
241
powersync . watch (
236
242
'SELECT count() AS count FROM customers' ,
@@ -280,4 +286,39 @@ describe('Watch Tests', () => {
280
286
await receivedError ;
281
287
expect ( receivedErrorCount ) . equals ( 1 ) ;
282
288
} ) ;
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
+ } ) ;
283
324
} ) ;
0 commit comments