mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-05-18 13:49:39 +01:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
// Copyright 2021 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
export type MaybeAsyncIterable<T> = Iterable<T> | AsyncIterable<T>;
|
|
|
|
export function concat<T>(
|
|
iterables: Iterable<MaybeAsyncIterable<T>>
|
|
): AsyncIterable<T> {
|
|
return new ConcatAsyncIterable(iterables);
|
|
}
|
|
|
|
class ConcatAsyncIterable<T> implements AsyncIterable<T> {
|
|
readonly #iterables: Iterable<MaybeAsyncIterable<T>>;
|
|
|
|
constructor(iterables: Iterable<MaybeAsyncIterable<T>>) {
|
|
this.#iterables = iterables;
|
|
}
|
|
|
|
async *[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
for (const iterable of this.#iterables) {
|
|
// oxlint-disable-next-line no-await-in-loop
|
|
for await (const value of iterable) {
|
|
yield value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function wrapPromise<T>(
|
|
promise: Promise<MaybeAsyncIterable<T>>
|
|
): AsyncIterable<T> {
|
|
return new WrapPromiseAsyncIterable(promise);
|
|
}
|
|
|
|
// oxlint-disable-next-line max-classes-per-file
|
|
class WrapPromiseAsyncIterable<T> implements AsyncIterable<T> {
|
|
readonly #promise: Promise<MaybeAsyncIterable<T>>;
|
|
|
|
constructor(promise: Promise<MaybeAsyncIterable<T>>) {
|
|
this.#promise = promise;
|
|
}
|
|
|
|
async *[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
for await (const value of await this.#promise) {
|
|
yield value;
|
|
}
|
|
}
|
|
}
|