• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

arlac77 / aggregate-async-iterator / 15220868595

23 May 2025 11:23PM CUT coverage: 100.0%. Remained the same
15220868595

push

github

arlac77
chore(deps): lock

18 of 18 branches covered (100.0%)

Branch coverage included in aggregate %.

65 of 65 relevant lines covered (100.0%)

46.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/aggregate-async-iterator.mjs
1
/**
12✔
2
 * Aggregate items from sevaral async iterators into one.
12✔
3
 * Items are collected first in first out from the sources.
12✔
4
 * Whatever source comes first will be delivered first.
12✔
5
 * @param {AsyncIterator<any>[]} sources
12✔
6
 * @return {AsyncIterable<any>} items collected from all sources
12✔
7
 */
12✔
8
export async function* aggregateFifo(sources) {
12✔
9
  const queue = [];
6✔
10

6✔
11
  while (sources.length > 0) {
6✔
12
    queue.length = 0;
30✔
13

30✔
14
    await new Promise((resolve, reject) =>
30✔
15
      sources.map(s =>
30✔
16
        s
72✔
17
          .next()
72✔
18
          .then(r => {
72✔
19
            if (r.done) {
72✔
20
              const w = sources.indexOf(s);
24✔
21
              if (w >= 0) {
24✔
22
                sources.splice(w, 1);
18✔
23
              }
18✔
24
            } else {
72✔
25
              queue.push(r.value);
48✔
26
            }
48✔
27
            // @ts-ignore
72✔
28
            resolve();
72✔
29
          })
72✔
30
          .catch(f => reject(f))
72✔
31
      )
30✔
32
    );
30✔
33

30✔
34
    for (const r of queue) {
30✔
35
      yield r;
48✔
36
    }
48✔
37
  }
30✔
38
}
6✔
39

12✔
40
/**
12✔
41
 * Aggregate items from sevaral async iterators into one.
12✔
42
 * Items are collected round robin from the sources.
12✔
43
 * The 2nd. round of items will only be delivered after all sources
12✔
44
 * have delivered their 1st. round (or reached their end).
12✔
45
 * @param {AsyncIterator<any>[]} sources
12✔
46
 * @return {AsyncIterable<any>} items collected from all sources
12✔
47
 */
12✔
48
export async function* aggregateRoundRobin(sources) {
12✔
49
  do {
30✔
50
    const results = await Promise.all(sources.map(s => s.next()));
120✔
51

114✔
52
    for (const i in results) {
120✔
53
      const r = results[i];
162✔
54

162✔
55
      if (r.done) {
162✔
56
        // @ts-ignore
36✔
57
        sources.splice(i, 1);
36✔
58
      } else {
162✔
59
        yield r.value;
126✔
60
      }
126✔
61
    }
162✔
62
  } while (sources.length > 0);
30✔
63
}
30✔
64

12✔
65
export default aggregateFifo;
12✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc