@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
39 lines (37 loc) • 1.15 kB
JavaScript
import { AsyncIterableX } from '../asynciterablex';
import { isAsyncIterable } from '../../util/isiterable';
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) {
if (depth === 0) {
for await (const item of source) {
yield item;
}
return undefined;
}
for await (const item of source) {
if (isAsyncIterable(item)) {
for await (const innerItem of this._flatten(item, depth - 1)) {
yield innerItem;
}
}
else {
yield item;
}
}
}
[Symbol.asyncIterator]() {
return this._flatten(this._source, this._depth)[Symbol.asyncIterator]();
}
}
export function flat(depth = Infinity) {
return function flattenOperatorFunction(source) {
return new FlattenAsyncIterable(source, depth);
};
}
//# sourceMappingURL=flat.mjs.map