import { AsyncIterableX } from '../asynciterablex.js'; import { OperatorAsyncFunction } from '../../interfaces.js'; import { returnAsyncIterator } from '../../util/returniterator.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class CatchWithAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _handler: ( error: any, signal?: AbortSignal ) => AsyncIterable | Promise>; constructor( source: AsyncIterable, handler: ( error: any, signal?: AbortSignal ) => AsyncIterable | Promise> ) { super(); this._source = source; this._handler = handler; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); let err: AsyncIterable | undefined; let hasError = false; const source = wrapWithAbort(this._source, signal); const it = source[Symbol.asyncIterator](); while (1) { let c = >{}; try { c = await it.next(); if (c.done) { await returnAsyncIterator(it); break; } } catch (e) { err = await this._handler(e, signal); hasError = true; await returnAsyncIterator(it); break; } yield c.value; } if (hasError) { for await (const item of wrapWithAbort(err!, signal)) { yield item; } } } } /** * Continues an async-iterable sequence that is terminated by an exception with the * async-iterable sequence produced by the handler. * * @template TSource The type of the elements in the source sequence. * @template TResult The type of elements from the handler function. * @param {(( * error: any, * signal?: AbortSignal * ) => AsyncIterable | Promise>)} handler Error handler function, producing another async-iterable sequence. * @returns {(OperatorAsyncFunction)} An operator which continues an async-iterable sequence that is terminated by * an exception with the specified handler. */ export function catchError( handler: ( error: any, signal?: AbortSignal ) => AsyncIterable | Promise> ): OperatorAsyncFunction { return function catchWithOperatorFunction( source: AsyncIterable ): AsyncIterableX { return new CatchWithAsyncIterable(source, handler); }; }