1- import { MAX_REQUESTS_PER_SECOND } from "src/config/api" ;
2-
31type PollingTask < T = unknown > = {
42 id : string ;
53 apiCall : ( signal : AbortSignal ) => Promise < T > ;
@@ -14,9 +12,6 @@ class PollingManager {
1412 private tasks : Map < string , PollingTask < unknown > > = new Map ( ) ;
1513 private isRunning = false ;
1614 private isPaused = false ;
17- private maxRequestsPerSecond = MAX_REQUESTS_PER_SECOND ;
18- private minInterval = 1000 / this . maxRequestsPerSecond ;
19- private lastRequestTime = 0 ;
2015 private visibilityChangeListener : ( ( ) => void ) | null = null ;
2116
2217 constructor ( ) {
@@ -57,59 +52,45 @@ class PollingManager {
5752 }
5853 }
5954
60- private async executeNextTask ( ) : Promise < void > {
55+ private async executeDueTasks ( ) : Promise < void > {
6156 if ( this . isPaused ) {
6257 return ;
6358 }
6459
6560 const now = Date . now ( ) ;
6661
67- // Find the next task that should be executed
68- let nextTask : PollingTask < unknown > | null = null ;
69- let earliestDue = Infinity ;
62+ // Find all tasks that are due for execution
63+ const dueTasks : PollingTask < unknown > [ ] = [ ] ;
7064
7165 for ( const task of this . tasks . values ( ) ) {
7266 const timeSinceLastExecution = now - task . lastExecuted ;
7367 if ( timeSinceLastExecution >= task . pollingInterval ) {
74- const dueTime = task . lastExecuted + task . pollingInterval ;
75- if ( dueTime < earliestDue ) {
76- earliestDue = dueTime ;
77- nextTask = task ;
78- }
68+ dueTasks . push ( task ) ;
7969 }
8070 }
8171
82- if ( ! nextTask ) return ;
83-
84- // Respect rate limiting
85- const timeSinceLastRequest = now - this . lastRequestTime ;
86- if ( timeSinceLastRequest < this . minInterval ) {
87- const delay = this . minInterval - timeSinceLastRequest ;
88- await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
89- }
90-
91- if ( this . isPaused ) {
92- return ;
93- }
72+ if ( dueTasks . length === 0 ) return ;
9473
95- // Execute the task
96- try {
97- this . lastRequestTime = Date . now ( ) ;
98- nextTask . lastExecuted = this . lastRequestTime ;
74+ // Execute all due tasks in parallel
75+ const promises = dueTasks . map ( async ( task ) => {
76+ try {
77+ task . lastExecuted = Date . now ( ) ;
78+ const result = await task . apiCall ( task . abortController . signal ) ;
79+ task . onSuccess ( result ) ;
80+ } catch ( error ) {
81+ task . onError ( error ) ;
82+ }
83+ } ) ;
9984
100- const result = await nextTask . apiCall ( nextTask . abortController . signal ) ;
101- nextTask . onSuccess ( result ) ;
102- } catch ( error ) {
103- nextTask . onError ( error ) ;
104- }
85+ await Promise . all ( promises ) ;
10586 }
10687
10788 private async runPollingLoop ( ) : Promise < void > {
10889 while ( this . isRunning && this . tasks . size > 0 ) {
109- await this . executeNextTask ( ) ;
90+ await this . executeDueTasks ( ) ;
11091
11192 // Delay to prevent tight loop
112- const delay = this . isPaused ? 1000 : 50 ;
93+ const delay = this . isPaused ? 1000 : 100 ;
11394 await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
11495 }
11596 this . isRunning = false ;
@@ -183,4 +164,4 @@ class PollingManager {
183164 }
184165}
185166
186- export default PollingManager ;
167+ export default new PollingManager ( ) ;
0 commit comments