@@ -220,21 +220,6 @@ export interface FeedCollectionUtils extends UtilsRecord {
220
220
*/
221
221
refresh : ( ) => Promise < void >
222
222
223
- /**
224
- * Start polling if it was stopped
225
- */
226
- startPolling : ( ) => void
227
-
228
- /**
229
- * Stop polling
230
- */
231
- stopPolling : ( ) => void
232
-
233
- /**
234
- * Get the current polling status
235
- */
236
- isPolling : ( ) => boolean
237
-
238
223
/**
239
224
* Clear the seen items cache
240
225
*/
@@ -475,8 +460,12 @@ function createFeedCollectionOptions<
475
460
476
461
// State management
477
462
let seenItems = new Map < string , { id : string ; lastSeen : number } > ( )
478
- let isPolling = false
479
463
let pollingTimeoutId : NodeJS . Timeout | null = null
464
+ let syncParams :
465
+ | Parameters <
466
+ SyncConfig < ResolveType < TExplicit , TSchema , TFallback > , TKey > [ `sync`]
467
+ > [ 0 ]
468
+ | null = null
480
469
481
470
/**
482
471
* Clean up old seen items to prevent memory leaks
@@ -515,7 +504,7 @@ function createFeedCollectionOptions<
515
504
/**
516
505
* Refresh feed data
517
506
*/
518
- const refreshFeed = async ( syncParams : {
507
+ const refreshFeed = async ( params : {
519
508
begin : ( ) => void
520
509
write : ( message : {
521
510
type : `insert` | `update` | `delete`
@@ -546,7 +535,7 @@ function createFeedCollectionOptions<
546
535
throw new UnsupportedFeedFormatError ( feedUrl )
547
536
}
548
537
549
- const { begin, write, commit } = syncParams
538
+ const { begin, write, commit } = params
550
539
begin ( )
551
540
552
541
let newItemsCount = 0
@@ -608,42 +597,10 @@ function createFeedCollectionOptions<
608
597
}
609
598
}
610
599
611
- /**
612
- * Start polling
613
- */
614
- const startPollingFn = ( syncParams ?: any ) => {
615
- if ( isPolling ) {
616
- return // Already polling
617
- }
618
-
619
- isPolling = true
620
-
621
- const poll = async ( ) => {
622
- if ( ! isPolling ) {
623
- return // Polling was stopped
624
- }
625
-
626
- try {
627
- if ( syncParams ) {
628
- await refreshFeed ( syncParams )
629
- }
630
- } catch ( error ) {
631
- debug ( `Polling error: ${ error } ` )
632
- // Continue polling despite errors
633
- }
634
-
635
- // Schedule next poll
636
- pollingTimeoutId = setTimeout ( poll , pollingInterval )
637
- }
638
-
639
- poll ( )
640
- }
641
-
642
600
/**
643
601
* Stop polling
644
602
*/
645
- const stopPollingFn = ( ) => {
646
- isPolling = false
603
+ const stopPolling = ( ) => {
647
604
if ( pollingTimeoutId ) {
648
605
clearTimeout ( pollingTimeoutId )
649
606
pollingTimeoutId = null
@@ -657,14 +614,32 @@ function createFeedCollectionOptions<
657
614
sync : ( params ) => {
658
615
const { markReady } = params
659
616
617
+ // Store sync params for manual refresh
618
+ syncParams = params
619
+
620
+ // Polling function
621
+ const poll = async ( ) => {
622
+ try {
623
+ await refreshFeed ( syncParams ! )
624
+ } catch ( error ) {
625
+ debug ( `Polling error: ${ error } ` )
626
+ // Continue polling despite errors
627
+ }
628
+
629
+ // Schedule next poll if polling is enabled
630
+ if ( startPolling ) {
631
+ pollingTimeoutId = setTimeout ( poll , pollingInterval )
632
+ }
633
+ }
634
+
660
635
// Initial feed fetch
661
636
refreshFeed ( params )
662
637
. then ( ( ) => {
663
638
markReady ( )
664
639
665
640
// Start polling if configured to do so
666
641
if ( startPolling ) {
667
- startPollingFn ( params )
642
+ pollingTimeoutId = setTimeout ( poll , pollingInterval )
668
643
}
669
644
} )
670
645
. catch ( ( error ) => {
@@ -673,29 +648,26 @@ function createFeedCollectionOptions<
673
648
674
649
// Still start polling for retry attempts
675
650
if ( startPolling ) {
676
- startPollingFn ( params )
651
+ pollingTimeoutId = setTimeout ( poll , pollingInterval )
677
652
}
678
653
} )
679
654
680
655
// Return cleanup function
681
656
return ( ) => {
682
- stopPollingFn ( )
657
+ stopPolling ( )
658
+ syncParams = null
683
659
}
684
660
} ,
685
661
}
686
662
687
663
// Utils
688
664
const utils : FeedCollectionUtils = {
689
- refresh : ( ) => {
690
- // For manual refresh, we need access to sync params
691
- // This is a limitation - manual refresh without sync params
692
- return Promise . reject (
693
- new Error ( `Manual refresh not supported outside of sync context` )
694
- )
665
+ refresh : async ( ) => {
666
+ if ( ! syncParams ) {
667
+ throw new Error ( `Collection not synced yet - cannot refresh` )
668
+ }
669
+ await refreshFeed ( syncParams )
695
670
} ,
696
- startPolling : ( ) => startPollingFn ( ) ,
697
- stopPolling : stopPollingFn ,
698
- isPolling : ( ) => isPolling ,
699
671
clearSeenItems : ( ) => {
700
672
seenItems = new Map ( )
701
673
} ,
0 commit comments