11/** A simple queue that holds promises. */
22export class RequestBuffer < T > {
33 /** Internal set of queued Promises */
4- private readonly buffer : Set < Promise < T > > = new Set ( ) ;
4+ private readonly buffer : Array < Promise < T > > = [ ] ;
55
66 /**
77 * Add a promise to the queue.
@@ -10,16 +10,29 @@ export class RequestBuffer<T> {
1010 * @returns The original promise.
1111 */
1212 public async add ( task : Promise < T > ) : Promise < T > {
13- this . buffer . add ( task ) ;
14- task . then ( ( ) => this . buffer . delete ( task ) ) . catch ( ( ) => this . buffer . delete ( task ) ) ;
13+ if ( this . buffer . indexOf ( task ) === - 1 ) {
14+ this . buffer . push ( task ) ;
15+ }
16+ task . then ( async ( ) => this . remove ( task ) ) . catch ( async ( ) => this . remove ( task ) ) ;
1517 return task ;
1618 }
1719
20+ /**
21+ * Remove a promise to the queue.
22+ *
23+ * @param task Can be any Promise<T>
24+ * @returns Removed promise.
25+ */
26+ public async remove ( task : Promise < T > ) : Promise < T > {
27+ const removedTask = this . buffer . splice ( this . buffer . indexOf ( task ) , 1 ) [ 0 ] ;
28+ return removedTask ;
29+ }
30+
1831 /**
1932 * This function returns the number of unresolved promises in the queue.
2033 */
2134 public length ( ) : number {
22- return this . buffer . size ;
35+ return this . buffer . length ;
2336 }
2437
2538 /**
@@ -35,7 +48,7 @@ export class RequestBuffer<T> {
3548 resolve ( false ) ;
3649 }
3750 } , timeout ) ;
38- Promise . all ( this . buffer . values ( ) )
51+ Promise . all ( this . buffer )
3952 . then ( ( ) => {
4053 clearTimeout ( capturedSetTimeout ) ;
4154 resolve ( true ) ;
0 commit comments