File tree 1 file changed +31
-1
lines changed
1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,35 @@ export async function* parallelMerge<I extends Array<AnyIterable<any>>>(
9
9
const concurrentWork = new Set ( )
10
10
const values = new Map ( )
11
11
12
+ type NullOrFunction = null | ( ( anything : any ) => void )
13
+
14
+ let lastError = null
15
+ let errCb : NullOrFunction = null
16
+ let valueCb : NullOrFunction = null
17
+
18
+ const notifyError = err => {
19
+ lastError = err
20
+ if ( errCb ) {
21
+ errCb ( err )
22
+ }
23
+ }
24
+
25
+ const notifyDone = value => {
26
+ if ( valueCb ) {
27
+ valueCb ( value )
28
+ }
29
+ }
30
+
31
+ const waitForQueue = ( ) =>
32
+ new Promise ( ( resolve , reject ) => {
33
+ if ( lastError ) {
34
+ reject ( lastError )
35
+ }
36
+ valueCb = resolve
37
+ errCb = reject
38
+ return this
39
+ } )
40
+
12
41
const queueNext = input => {
13
42
const nextVal = Promise . resolve ( input . next ( ) ) . then ( async ( { done, value } ) => {
14
43
if ( ! done ) {
@@ -17,6 +46,7 @@ export async function* parallelMerge<I extends Array<AnyIterable<any>>>(
17
46
concurrentWork . delete ( nextVal )
18
47
} )
19
48
concurrentWork . add ( nextVal )
49
+ nextVal . then ( notifyDone , notifyError )
20
50
}
21
51
22
52
for ( const input of inputs ) {
@@ -27,7 +57,7 @@ export async function* parallelMerge<I extends Array<AnyIterable<any>>>(
27
57
if ( concurrentWork . size === 0 ) {
28
58
return
29
59
}
30
- await Promise . race ( concurrentWork )
60
+ await waitForQueue ( )
31
61
for ( const [ input , value ] of values ) {
32
62
values . delete ( input )
33
63
yield value
You can’t perform that action at this time.
0 commit comments