@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
50 lines (48 loc) • 1.72 kB
JavaScript
import { AsyncIterableX } from '../asynciterablex';
import { isAsyncIterable } from '../../util/isiterable';
import { wrapWithAbort } from './withabort';
import { throwIfAborted } from '../../aborterror';
export class FlattenAsyncIterable extends AsyncIterableX {
constructor(source, depth) {
super();
this._source = source;
this._depth = depth;
}
// eslint-disable-next-line consistent-return
async *_flatten(source, depth, signal) {
if (depth === 0) {
for await (const item of wrapWithAbort(source, signal)) {
yield item;
}
return undefined;
}
for await (const item of wrapWithAbort(source, signal)) {
if (isAsyncIterable(item)) {
for await (const innerItem of this._flatten(item, depth - 1, signal)) {
yield innerItem;
}
}
else {
yield item;
}
}
}
[Symbol.asyncIterator](signal) {
throwIfAborted(signal);
return this._flatten(this._source, this._depth, signal)[Symbol.asyncIterator]();
}
}
/**
* Flattens the nested async-iterable by the given depth.
*
* @export
* @template T The type of elements in the source sequence.
* @param {number} [depth=Infinity] The depth to flatten the async-iterable sequence if specified, otherwise infinite.
* @returns {MonoTypeOperatorAsyncFunction<T>} An operator that flattens the async-iterable sequence.
*/
export function flat(depth = Infinity) {
return function flattenOperatorFunction(source) {
return new FlattenAsyncIterable(source, depth);
};
}
//# sourceMappingURL=flat.mjs.map