summaryrefslogtreecommitdiff
path: root/std/async/mux_async_iterator.ts
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-02-02 19:05:46 +0800
committerGitHub <noreply@github.com>2021-02-02 12:05:46 +0100
commit6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch)
treefd94c013a19fcb38954844085821ec1601c20e18 /std/async/mux_async_iterator.ts
parenta2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff)
chore: remove std directory (#9361)
This removes the std folder from the tree. Various parts of the tests are pretty tightly dependent on std (47 direct imports and 75 indirect imports, not counting the cli tests that use them as fixtures) so I've added std as a submodule for now.
Diffstat (limited to 'std/async/mux_async_iterator.ts')
-rw-r--r--std/async/mux_async_iterator.ts69
1 files changed, 0 insertions, 69 deletions
diff --git a/std/async/mux_async_iterator.ts b/std/async/mux_async_iterator.ts
deleted file mode 100644
index 0bda4f579..000000000
--- a/std/async/mux_async_iterator.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-import { Deferred, deferred } from "./deferred.ts";
-
-interface TaggedYieldedValue<T> {
- iterator: AsyncIterableIterator<T>;
- value: T;
-}
-
-/** The MuxAsyncIterator class multiplexes multiple async iterators into a
- * single stream. It currently makes an assumption:
- * - The final result (the value returned and not yielded from the iterator)
- * does not matter; if there is any, it is discarded.
- */
-export class MuxAsyncIterator<T> implements AsyncIterable<T> {
- private iteratorCount = 0;
- private yields: Array<TaggedYieldedValue<T>> = [];
- // deno-lint-ignore no-explicit-any
- private throws: any[] = [];
- private signal: Deferred<void> = deferred();
-
- add(iterator: AsyncIterableIterator<T>): void {
- ++this.iteratorCount;
- this.callIteratorNext(iterator);
- }
-
- private async callIteratorNext(
- iterator: AsyncIterableIterator<T>,
- ): Promise<void> {
- try {
- const { value, done } = await iterator.next();
- if (done) {
- --this.iteratorCount;
- } else {
- this.yields.push({ iterator, value });
- }
- } catch (e) {
- this.throws.push(e);
- }
- this.signal.resolve();
- }
-
- async *iterate(): AsyncIterableIterator<T> {
- while (this.iteratorCount > 0) {
- // Sleep until any of the wrapped iterators yields.
- await this.signal;
-
- // Note that while we're looping over `yields`, new items may be added.
- for (let i = 0; i < this.yields.length; i++) {
- const { iterator, value } = this.yields[i];
- yield value;
- this.callIteratorNext(iterator);
- }
-
- if (this.throws.length) {
- for (const e of this.throws) {
- throw e;
- }
- this.throws.length = 0;
- }
- // Clear the `yields` list and reset the `signal` promise.
- this.yields.length = 0;
- this.signal = deferred();
- }
- }
-
- [Symbol.asyncIterator](): AsyncIterableIterator<T> {
- return this.iterate();
- }
-}