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.
|
|
|
|
|
2020-05-09 12:34:47 +00:00
|
|
|
interface TaggedYieldedValue<T> {
|
2021-05-18 13:38:53 +00:00
|
|
|
iterator: AsyncIterator<T>;
|
2020-05-09 12:34:47 +00:00
|
|
|
value: T;
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2023-12-01 02:19:22 +00:00
|
|
|
* 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.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-05-22 05:08:36 +00:00
|
|
|
* @example Usage
|
2023-12-01 02:19:22 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { MuxAsyncIterator } from "@std/async/mux-async-iterator";
|
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";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* 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<number>();
|
|
|
|
* mux.add(gen123());
|
|
|
|
* mux.add(gen456());
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* const result = await Array.fromAsync(mux);
|
|
|
|
*
|
|
|
|
* assertEquals(result, [1, 4, 2, 5, 3, 6]);
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2024-05-22 00:40:43 +00:00
|
|
|
*
|
|
|
|
* @typeParam T The type of the provided async iterables and generated async iterable.
|
2020-05-09 12:34:47 +00:00
|
|
|
*/
|
|
|
|
export class MuxAsyncIterator<T> implements AsyncIterable<T> {
|
2022-05-27 12:27:13 +00:00
|
|
|
#iteratorCount = 0;
|
|
|
|
#yields: Array<TaggedYieldedValue<T>> = [];
|
2020-11-03 15:19:29 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2022-05-27 12:27:13 +00:00
|
|
|
#throws: any[] = [];
|
2023-11-10 03:31:16 +00:00
|
|
|
#signal = Promise.withResolvers<void>();
|
2020-05-09 12:34:47 +00:00
|
|
|
|
2024-05-22 00:40:43 +00:00
|
|
|
/**
|
|
|
|
* Add an async iterable to the stream.
|
|
|
|
*
|
|
|
|
* @param iterable The async iterable to add.
|
|
|
|
*
|
2024-05-22 05:08:36 +00:00
|
|
|
* @example Usage
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```ts
|
|
|
|
* import { MuxAsyncIterator } from "@std/async/mux-async-iterator";
|
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";
|
2024-05-22 00:40:43 +00:00
|
|
|
*
|
|
|
|
* async function* gen123(): AsyncIterableIterator<number> {
|
|
|
|
* yield 1;
|
|
|
|
* yield 2;
|
|
|
|
* yield 3;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const mux = new MuxAsyncIterator<number>();
|
|
|
|
* mux.add(gen123());
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* const result = await Array.fromAsync(mux.iterate());
|
|
|
|
*
|
|
|
|
* assertEquals(result, [1, 2, 3]);
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
add(iterable: AsyncIterable<T>) {
|
2022-05-27 12:27:13 +00:00
|
|
|
++this.#iteratorCount;
|
|
|
|
this.#callIteratorNext(iterable[Symbol.asyncIterator]());
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:27:13 +00:00
|
|
|
async #callIteratorNext(
|
2021-05-18 13:38:53 +00:00
|
|
|
iterator: AsyncIterator<T>,
|
2021-04-05 11:49:05 +00:00
|
|
|
) {
|
2020-06-15 16:03:07 +00:00
|
|
|
try {
|
|
|
|
const { value, done } = await iterator.next();
|
|
|
|
if (done) {
|
2022-05-27 12:27:13 +00:00
|
|
|
--this.#iteratorCount;
|
2020-06-15 16:03:07 +00:00
|
|
|
} else {
|
2022-05-27 12:27:13 +00:00
|
|
|
this.#yields.push({ iterator, value });
|
2020-06-15 16:03:07 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-05-27 12:27:13 +00:00
|
|
|
this.#throws.push(e);
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|
2022-05-27 12:27:13 +00:00
|
|
|
this.#signal.resolve();
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 00:40:43 +00:00
|
|
|
/**
|
|
|
|
* Returns an async iterator of the stream.
|
|
|
|
* @returns the async iterator for all the added async iterables.
|
|
|
|
*
|
2024-05-22 05:08:36 +00:00
|
|
|
* @example Usage
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```ts
|
|
|
|
* import { MuxAsyncIterator } from "@std/async/mux-async-iterator";
|
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";
|
2024-05-22 00:40:43 +00:00
|
|
|
*
|
|
|
|
* async function* gen123(): AsyncIterableIterator<number> {
|
|
|
|
* yield 1;
|
|
|
|
* yield 2;
|
|
|
|
* yield 3;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const mux = new MuxAsyncIterator<number>();
|
|
|
|
* mux.add(gen123());
|
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* const result = await Array.fromAsync(mux.iterate());
|
|
|
|
*
|
|
|
|
* assertEquals(result, [1, 2, 3]);
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2020-05-09 12:34:47 +00:00
|
|
|
async *iterate(): AsyncIterableIterator<T> {
|
2022-05-27 12:27:13 +00:00
|
|
|
while (this.#iteratorCount > 0) {
|
2020-05-09 12:34:47 +00:00
|
|
|
// Sleep until any of the wrapped iterators yields.
|
2023-11-10 03:31:16 +00:00
|
|
|
await this.#signal.promise;
|
2020-05-09 12:34:47 +00:00
|
|
|
|
|
|
|
// Note that while we're looping over `yields`, new items may be added.
|
2024-01-05 21:36:10 +00:00
|
|
|
for (const { iterator, value } of this.#yields) {
|
2020-05-09 12:34:47 +00:00
|
|
|
yield value;
|
2022-05-27 12:27:13 +00:00
|
|
|
this.#callIteratorNext(iterator);
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:27:13 +00:00
|
|
|
if (this.#throws.length) {
|
|
|
|
for (const e of this.#throws) {
|
2020-06-15 16:03:07 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 12:34:47 +00:00
|
|
|
// Clear the `yields` list and reset the `signal` promise.
|
2022-05-27 12:27:13 +00:00
|
|
|
this.#yields.length = 0;
|
2023-11-10 03:31:16 +00:00
|
|
|
this.#signal = Promise.withResolvers<void>();
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:40:43 +00:00
|
|
|
/**
|
|
|
|
* Implements an async iterator for the stream.
|
|
|
|
* @returns the async iterator for all the added async iterables.
|
|
|
|
*
|
2024-05-22 05:08:36 +00:00
|
|
|
* @example Usage
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```ts
|
|
|
|
* import { MuxAsyncIterator } from "@std/async/mux-async-iterator";
|
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";
|
2024-05-22 00:40:43 +00:00
|
|
|
*
|
|
|
|
* async function* gen123(): AsyncIterableIterator<number> {
|
|
|
|
* yield 1;
|
|
|
|
* yield 2;
|
|
|
|
* yield 3;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const mux = new MuxAsyncIterator<number>();
|
|
|
|
* mux.add(gen123());
|
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* const result = await Array.fromAsync(mux);
|
|
|
|
*
|
|
|
|
* assertEquals(result, [1, 2, 3]);
|
2024-05-22 00:40:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2021-05-18 13:38:53 +00:00
|
|
|
[Symbol.asyncIterator](): AsyncIterator<T> {
|
2020-05-09 12:34:47 +00:00
|
|
|
return this.iterate();
|
|
|
|
}
|
|
|
|
}
|