1
1
/** A simple queue that holds promises. */
2
2
export class RequestBuffer < T > {
3
3
/** Internal set of queued Promises */
4
- private readonly buffer : Set < Promise < T > > = new Set ( ) ;
4
+ private readonly buffer : Array < Promise < T > > = [ ] ;
5
5
6
6
/**
7
7
* Add a promise to the queue.
@@ -10,16 +10,29 @@ export class RequestBuffer<T> {
10
10
* @returns The original promise.
11
11
*/
12
12
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 ) ) ;
15
17
return task ;
16
18
}
17
19
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
+
18
31
/**
19
32
* This function returns the number of unresolved promises in the queue.
20
33
*/
21
34
public length ( ) : number {
22
- return this . buffer . size ;
35
+ return this . buffer . length ;
23
36
}
24
37
25
38
/**
@@ -35,7 +48,7 @@ export class RequestBuffer<T> {
35
48
resolve ( false ) ;
36
49
}
37
50
} , timeout ) ;
38
- Promise . all ( this . buffer . values ( ) )
51
+ Promise . all ( this . buffer )
39
52
. then ( ( ) => {
40
53
clearTimeout ( capturedSetTimeout ) ;
41
54
resolve ( true ) ;
0 commit comments