import { OperatorAsyncFunction, UnaryFunction } from '../interfaces.js'; import { Observable } from '../observer.js'; import { bindCallback } from '../util/bindcallback.js'; import { identityAsync } from '../util/identity.js'; import { isReadableNodeStream, isWritableNodeStream, isIterable, isAsyncIterable, isArrayLike, isIterator, isPromise, isObservable, } from '../util/isiterable.js'; import { toLength } from '../util/tolength.js'; import { AbortError, throwIfAborted } from '../aborterror.js'; /** * This class serves as the base for all operations which support [Symbol.asyncIterator]. */ export abstract class AsyncIterableX implements AsyncIterable { abstract [Symbol.asyncIterator](signal?: AbortSignal): AsyncIterator; /** @nocollapse */ async forEach( projection: (value: T, index: number, signal?: AbortSignal) => void | Promise, thisArg?: any, signal?: AbortSignal ): Promise { const source = signal ? new WithAbortAsyncIterable(this, signal) : this; let i = 0; for await (const item of source) { await projection.call(thisArg, item, i++, signal); } } /** @nocollapse */ pipe(...operations: UnaryFunction, R>[]): R; pipe(...operations: OperatorAsyncFunction[]): AsyncIterableX; pipe(writable: R, options?: { end?: boolean }): R; pipe(...args: any[]) { let i = -1; const n = args.length; let acc: any = this; while (++i < n) { acc = args[i](AsyncIterableX.as(acc)); } return acc; } /** @nocollapse */ static from( source: AsyncIterableInput, selector: (value: TSource, index: number) => TResult | Promise = identityAsync, thisArg?: any ): AsyncIterableX { const fn = bindCallback(selector, thisArg, 2); if (isIterable(source) || isAsyncIterable(source)) { return new FromAsyncIterable(source, fn); } if (isPromise(source)) { return new FromPromiseIterable(source, fn); } if (isObservable(source)) { return new FromObservableAsyncIterable(source, fn); } if (isArrayLike(source)) { return new FromArrayIterable(source, fn); } if (isIterator(source)) { return new FromAsyncIterable({ [Symbol.asyncIterator]: () => source }, fn); } throw new TypeError('Input type not supported'); } /** * Converts an existing string into an async-iterable of characters. * * @param {string} source The string to convert to an async-iterable. * @returns {AsyncIterableX} An async-iterable stream of characters from the source. */ static as(source: string): AsyncIterableX; /** * Converts the AsyncIterable-like input or single element into an AsyncIterable. * * @template T The type of elements in the async-iterable like sequence. * @param {AsyncIterableInput} source The async-iterable like input to convert to an async-iterable. * @returns {AsyncIterableX} An async-iterable stream from elements in the async-iterable like sequence. */ static as(source: AsyncIterableInput | T): AsyncIterableX; /** * Converts the single element into an async-iterable sequence. * * @template T The type of the input to turn into an async-iterable sequence. * @param {T} source The single element to turn into an async-iterable sequence. * @returns {AsyncIterableX} An async-iterable sequence which contains the single element. */ static as(source: T): AsyncIterableX; /** * Converts the input into an async-iterable sequence. * * @param {*} source The source to convert to an async-iterable sequence. * @returns {AsyncIterableX<*>} An async-iterable containing the input. */ /** @nocollapse */ static as(source: any): AsyncIterableX { if (source instanceof AsyncIterableX) { return source; } if (typeof source === 'string') { return new FromArrayIterable([source], identityAsync); } if (isIterable(source) || isAsyncIterable(source)) { return new FromAsyncIterable(source, identityAsync); } if (isPromise(source)) { return new FromPromiseIterable(source, identityAsync); } if (isObservable(source)) { return new FromObservableAsyncIterable(source, identityAsync); } if (isArrayLike(source)) { return new FromArrayIterable(source, identityAsync); } return new FromArrayIterable([source], identityAsync); } } (AsyncIterableX.prototype)[Symbol.toStringTag] = 'AsyncIterableX'; Object.defineProperty(AsyncIterableX, Symbol.hasInstance, { writable: true, configurable: true, value(inst: any) { return !!(inst && inst[Symbol.toStringTag] === 'AsyncIterableX'); }, }); const ARRAY_VALUE = 'value'; const ARRAY_ERROR = 'error'; interface AsyncSinkItem { type: string; value?: T; error?: any; } interface AsyncResolver { resolve: (value: T | PromiseLike) => void; reject: (reason?: any) => void; } /** @ignore */ /** @ignore */ export class AsyncSink implements AsyncIterableIterator { private _ended: boolean; private _values: AsyncSinkItem[]; private _resolvers: AsyncResolver>[]; constructor() { this._ended = false; this._values = []; this._resolvers = []; } [Symbol.asyncIterator]() { return this; } write(value: TSource) { this._push({ type: ARRAY_VALUE, value }); } error(error: any) { this._push({ type: ARRAY_ERROR, error }); } private _push(item: AsyncSinkItem) { if (this._ended) { throw new Error('AsyncSink already ended'); } if (this._resolvers.length > 0) { const { resolve, reject } = this._resolvers.shift()!; if (item.type === ARRAY_ERROR) { reject(item.error!); } else { resolve({ done: false, value: item.value! }); } } else { this._values.push(item); } } next() { if (this._values.length > 0) { const { type, value, error } = this._values.shift()!; if (type === ARRAY_ERROR) { return Promise.reject(error); } else { return Promise.resolve({ done: false, value } as IteratorResult); } } if (this._ended) { return Promise.resolve({ done: true } as IteratorResult); } return new Promise>((resolve, reject) => { this._resolvers.push({ resolve, reject }); }); } end() { while (this._resolvers.length > 0) { this._resolvers.shift()!.resolve({ done: true } as IteratorResult); } this._ended = true; } } /** @ignore */ export class FromArrayIterable extends AsyncIterableX { private _source: ArrayLike; private _selector: (value: TSource, index: number) => TResult | Promise; constructor( source: ArrayLike, selector: (value: TSource, index: number) => TResult | Promise ) { super(); this._source = source; this._selector = selector; } async *[Symbol.asyncIterator]() { let i = 0; const length = toLength((>this._source).length); while (i < length) { yield await this._selector(this._source[i], i++); } } } /** @ignore */ export class FromAsyncIterable extends AsyncIterableX { private _source: Iterable> | AsyncIterable; private _selector: (value: TSource, index: number) => TResult | Promise; constructor( source: Iterable> | AsyncIterable, selector: (value: TSource, index: number) => TResult | Promise ) { super(); this._source = source; this._selector = selector; } async *[Symbol.asyncIterator](signal?: AbortSignal) { let i = 0; if (signal && this._source instanceof AsyncIterableX) { for await (const item of new WithAbortAsyncIterable(this._source, signal)) { yield await this._selector(item, i++); } } else { throwIfAborted(signal); for await (const item of this._source) { throwIfAborted(signal); const value = await this._selector(item, i++); throwIfAborted(signal); yield value; } } } } /** @ignore */ export class FromPromiseIterable extends AsyncIterableX { private _source: PromiseLike; private _selector: (value: TSource, index: number) => TResult | Promise; constructor( source: PromiseLike, selector: (value: TSource, index: number) => TResult | Promise ) { super(); this._source = source; this._selector = selector; } async *[Symbol.asyncIterator]() { const item = await this._source; yield await this._selector(item, 0); } } /** @ignore */ export class FromObservableAsyncIterable extends AsyncIterableX< TResult > { private _observable: Observable; private _selector: (value: TSource, index: number) => TResult | Promise; constructor( observable: Observable, selector: (value: TSource, index: number) => TResult | Promise ) { super(); this._observable = observable; this._selector = selector; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const sink: AsyncSink = new AsyncSink(); const subscription = this._observable.subscribe({ next(value: TSource) { sink.write(value); }, error(err: any) { sink.error(err); }, complete() { sink.end(); }, }); function onAbort() { sink.error(new AbortError()); } if (signal) { signal.addEventListener('abort', onAbort); } let i = 0; try { for (let next; !(next = await sink.next()).done; ) { throwIfAborted(signal); yield await this._selector(next.value!, i++); } } finally { if (signal) { signal.removeEventListener('abort', onAbort); } subscription.unsubscribe(); } } } class WithAbortAsyncIterable implements AsyncIterable { private _source: AsyncIterable; private _signal: AbortSignal; constructor(source: AsyncIterable, signal: AbortSignal) { this._source = source; this._signal = signal; } [Symbol.asyncIterator](): AsyncIterator { // @ts-ignore return this._source[Symbol.asyncIterator](this._signal); } } /** @ignore */ export type AsyncIterableInput = | AsyncIterable | AsyncIterator | Iterable> | ArrayLike | PromiseLike | Observable; type WritableOrOperatorAsyncFunction = | NodeJS.WritableStream | NodeJS.ReadWriteStream | OperatorAsyncFunction; declare module '../asynciterable/asynciterablex' { interface AsyncIterableX { pipe(): AsyncIterableX; pipe(op1: OperatorAsyncFunction): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction, op5: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction, op5: OperatorAsyncFunction, op6: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction, op5: OperatorAsyncFunction, op6: OperatorAsyncFunction, op7: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction, op5: OperatorAsyncFunction, op6: OperatorAsyncFunction, op7: OperatorAsyncFunction, op8: OperatorAsyncFunction ): AsyncIterableX; pipe( op1: OperatorAsyncFunction, op2: OperatorAsyncFunction, op3: OperatorAsyncFunction, op4: OperatorAsyncFunction, op5: OperatorAsyncFunction, op6: OperatorAsyncFunction, op7: OperatorAsyncFunction, op8: OperatorAsyncFunction, op9: OperatorAsyncFunction ): AsyncIterableX; pipe(...operations: OperatorAsyncFunction[]): AsyncIterableX; pipe(op1: A, options?: { end?: boolean }): A; } } try { ((isBrowser) => { if (isBrowser) { return; } AsyncIterableX.prototype['pipe'] = nodePipe; const readableOpts = (x: any, opts = x._writableState || { objectMode: true }) => opts; function nodePipe(this: AsyncIterableX, ...args: any[]) { let i = -1; let end: boolean; const n = args.length; let prev: any = this; let next: WritableOrOperatorAsyncFunction; while (++i < n) { next = args[i]; if (typeof next === 'function') { prev = next(AsyncIterableX.as(prev)); } else if (isWritableNodeStream(next)) { ({ end = true } = args[i + 1] || {}); // prettier-ignore return isReadableNodeStream(prev) ? prev.pipe(next, { end }) : AsyncIterableX.as(prev).toNodeStream(readableOpts(next)).pipe(next, { end }); } } return prev; } })(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9); } catch (e) { /* */ } export const as = AsyncIterableX.as; export const from = AsyncIterableX.from;