File size: 1,055 Bytes
8d88d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
type Gen<T, TReturn> = AsyncGenerator<T, TReturn, undefined>;

type GenPromiseMap<T, TReturn> = Map<
	Gen<T, TReturn>,
	Promise<{ gen: Gen<T, TReturn> } & IteratorResult<T, TReturn>>
>;

/** Merges multiple async generators into a single async generator that yields values from all of them in parallel. */
export async function* mergeAsyncGenerators<T, TReturn>(
	generators: Gen<T, TReturn>[]
): Gen<T, TReturn[]> {
	const promises: GenPromiseMap<T, TReturn> = new Map();
	const results: Map<Gen<T, TReturn>, TReturn> = new Map();

	for (const gen of generators) {
		promises.set(
			gen,
			gen.next().then((result) => ({ gen, ...result }))
		);
	}

	while (promises.size) {
		const { gen, value, done } = await Promise.race(promises.values());
		if (done) {
			results.set(gen, value as TReturn);
			promises.delete(gen);
		} else {
			promises.set(
				gen,
				gen.next().then((result) => ({ gen, ...result }))
			);
			yield value as T;
		}
	}

	const orderedResults = generators.map((gen) => results.get(gen) as TReturn);
	return orderedResults;
}