-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan-in.js
48 lines (45 loc) · 1.1 KB
/
fan-in.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const createFanIn = (...asyncIterators) => {
const valuePool = []
const generatorPool = []
const pool = new Set()
let totalLive = asyncIterators.length
const addSource = function(iterator) {
const promise = iterator.next()
pool.add(promise)
promise
.then(({ value, done }) => {
valuePool.push(value)
if (!done) generatorPool.push(iterator)
else {
totalLive--
}
pool.delete(promise)
})
.catch(() => {
pool.delete(promise)
})
}
return {
async *fanInGenerator() {
asyncIterators.forEach(a => addSource(a))
while (true) {
try {
await Promise.race([...pool])
const val = valuePool.shift()
if (val !== undefined) {
yield val
}
const i = generatorPool.shift()
if (i) addSource(i)
} catch (error) {
console.log(`Iterator has error occurs ${totalLive}`,error) // eslint-disable-line
totalLive--
}
if (totalLive <= 0) break
}
},
}
}
module.exports = {
createFanIn,
}