std/async/tee.ts

102 lines
2.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
2021-05-18 13:39:12 +00:00
/** Utility for representing n-tuple. Used in {@linkcode tee}. */
export type Tuple<T, N extends number> = N extends N
2021-05-18 13:39:12 +00:00
? number extends N ? T[] : TupleOf<T, N, []>
: never;
/** Utility for representing n-tuple of. Used in {@linkcode Tuple}. */
export type TupleOf<T, N extends number, R extends unknown[]> =
R["length"] extends N ? R
: TupleOf<T, N, [T, ...R]>;
2021-05-18 13:39:12 +00:00
interface QueueNode<T> {
value: T;
next: QueueNode<T> | undefined;
}
2021-05-18 13:39:12 +00:00
class Queue<T> {
#source: AsyncIterator<T>;
#queue: QueueNode<T>;
head: QueueNode<T>;
2021-05-18 13:39:12 +00:00
done: boolean;
2021-05-18 13:39:12 +00:00
constructor(iterable: AsyncIterable<T>) {
this.#source = iterable[Symbol.asyncIterator]();
this.#queue = {
value: undefined!,
next: undefined,
};
this.head = this.#queue;
this.done = false;
2021-05-18 13:39:12 +00:00
}
async next() {
const result = await this.#source.next();
if (!result.done) {
const nextNode: QueueNode<T> = {
value: result.value,
next: undefined,
};
this.#queue.next = nextNode;
this.#queue = nextNode;
} else {
this.done = true;
}
2021-05-18 13:39:12 +00:00
}
}
/**
* Branches the given async iterable into the `n` branches.
2021-05-18 13:39:12 +00:00
*
2022-11-25 11:40:23 +00:00
* @example
* ```ts
2022-11-25 11:40:23 +00:00
* import { tee } from "https://deno.land/std@$STD_VERSION/async/tee.ts";
*
2022-11-25 11:40:23 +00:00
* const gen = async function* gen() {
* yield 1;
* yield 2;
* yield 3;
* };
2021-05-18 13:39:12 +00:00
*
2022-11-25 11:40:23 +00:00
* const [branch1, branch2] = tee(gen());
2021-05-18 13:39:12 +00:00
*
2022-11-25 11:40:23 +00:00
* for await (const n of branch1) {
* console.log(n); // => 1, 2, 3
* }
2021-05-18 13:39:12 +00:00
*
2022-11-25 11:40:23 +00:00
* for await (const n of branch2) {
* console.log(n); // => 1, 2, 3
* }
* ```
2021-05-18 13:39:12 +00:00
*/
export function tee<T, N extends number = 2>(
iterable: AsyncIterable<T>,
2021-05-18 13:39:12 +00:00
n: N = 2 as N,
): Tuple<AsyncIterable<T>, N> {
const queue = new Queue<T>(iterable);
async function* generator(): AsyncGenerator<T> {
let buffer = queue.head;
2021-05-18 13:39:12 +00:00
while (true) {
if (buffer.next) {
buffer = buffer.next;
yield buffer.value;
} else if (queue.done) {
return;
} else {
await queue.next();
2021-05-18 13:39:12 +00:00
}
}
}
return Array.from({ length: n }).map(
() => generator(),
) as Tuple<
AsyncIterable<T>,
N
>;
2021-05-18 13:39:12 +00:00
}