2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
2021-05-18 13:39:12 +00:00
|
|
|
|
2024-06-14 10:15:05 +00:00
|
|
|
/**
|
|
|
|
* Utility for representing n-tuple. Used in {@linkcode tee}.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-12-01 02:19:22 +00:00
|
|
|
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;
|
2023-12-01 02:19:22 +00:00
|
|
|
|
2024-06-14 10:15:05 +00:00
|
|
|
/**
|
|
|
|
* Utility for representing n-tuple of. Used in {@linkcode Tuple}.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-12-01 02:19:22 +00:00
|
|
|
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
|
|
|
|
2021-10-20 05:18:58 +00:00
|
|
|
interface QueueNode<T> {
|
|
|
|
value: T;
|
|
|
|
next: QueueNode<T> | undefined;
|
|
|
|
}
|
2021-05-18 13:39:12 +00:00
|
|
|
|
2021-10-20 05:18:58 +00:00
|
|
|
class Queue<T> {
|
|
|
|
#source: AsyncIterator<T>;
|
|
|
|
#queue: QueueNode<T>;
|
|
|
|
head: QueueNode<T>;
|
2021-05-18 13:39:12 +00:00
|
|
|
|
2021-10-20 05:18:58 +00:00
|
|
|
done: boolean;
|
2021-05-18 13:39:12 +00:00
|
|
|
|
2021-10-20 05:18:58 +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
|
|
|
}
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
async next() {
|
2021-10-20 05:18:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-01 02:19:22 +00:00
|
|
|
* Branches the given async iterable into the `n` branches.
|
2021-05-18 13:39:12 +00:00
|
|
|
*
|
2024-05-22 05:08:36 +00:00
|
|
|
* @example Usage
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { tee } from "@std/async/tee";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-09-12 15:14:54 +00:00
|
|
|
*
|
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
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* const result1 = await Array.fromAsync(branch1);
|
|
|
|
* assertEquals(result1, [1, 2, 3]);
|
2021-05-18 13:39:12 +00:00
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* const result2 = await Array.fromAsync(branch2);
|
|
|
|
* assertEquals(result2, [1, 2, 3]);
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```
|
2024-05-22 00:40:43 +00:00
|
|
|
*
|
|
|
|
* @typeParam T The type of the provided async iterable and the returned async iterables.
|
|
|
|
* @typeParam N The amount of branches to tee into.
|
|
|
|
* @param iterable The iterable to tee.
|
|
|
|
* @param n The amount of branches to tee into.
|
|
|
|
* @returns The tuple where each element is an async iterable.
|
2021-05-18 13:39:12 +00:00
|
|
|
*/
|
|
|
|
export function tee<T, N extends number = 2>(
|
2021-10-20 05:18:58 +00:00
|
|
|
iterable: AsyncIterable<T>,
|
2021-05-18 13:39:12 +00:00
|
|
|
n: N = 2 as N,
|
|
|
|
): Tuple<AsyncIterable<T>, N> {
|
2021-10-20 05:18:58 +00:00
|
|
|
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) {
|
2021-10-20 05:18:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-10-20 05:18:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 02:19:22 +00:00
|
|
|
return Array.from({ length: n }).map(
|
2021-10-20 05:18:58 +00:00
|
|
|
() => generator(),
|
|
|
|
) as Tuple<
|
|
|
|
AsyncIterable<T>,
|
|
|
|
N
|
|
|
|
>;
|
2021-05-18 13:39:12 +00:00
|
|
|
}
|