All files / async / unstable_mux_async_iterator.ts

100.00% Branches 1/1
100.00% Lines 7/7
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
49
50
 
 
 
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
 
 
 
 
 
x2
x4
x4
x4
x2
















































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import { MuxAsyncIterator as MuxAsyncIterator_ } from "./mux_async_iterator.ts";

/**
 * Multiplexes multiple async iterators into a single stream. It currently
 * makes an assumption that the final result (the value returned and not
 * yielded from the iterator) does not matter; if there is any result, it is
 * discarded.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @example Usage
 * ```ts
 * import { MuxAsyncIterator } from "@std/async/unstable-mux-async-iterator";
 * import { assertEquals } from "@std/assert";
 *
 * async function* gen123(): AsyncIterableIterator<number> {
 *   yield 1;
 *   yield 2;
 *   yield 3;
 * }
 *
 * async function* gen456(): AsyncIterableIterator<number> {
 *   yield 4;
 *   yield 5;
 *   yield 6;
 * }
 *
 * const mux = new MuxAsyncIterator(gen123(), gen456());
 *
 * const result = await Array.fromAsync(mux);
 *
 * assertEquals(result, [1, 4, 2, 5, 3, 6]);
 * ```
 *
 * @typeParam T The type of the provided async iterables and generated async iterable.
 */
export class MuxAsyncIterator<T> extends MuxAsyncIterator_<T> {
  /**
   * Constructs a new {@linkcode MuxAsyncIterator} instance.
   *
   * @param iterables The async iterables to multiplex.
   */
  constructor(...iterables: AsyncIterable<T>[]) {
    super();
    for (const iterable of iterables) this.add(iterable);
  }
}