import { AsyncIterableX } from '../asynciterablex.js'; import { PartialAsyncObserver } from '../../observer.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { toObserver } from '../../util/toobserver.js'; import { AbortError, throwIfAborted } from '../../aborterror.js'; import { returnAsyncIterator } from '../../util/returniterator.js'; /** @ignore */ export class TapAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _observer: PartialAsyncObserver; constructor(source: AsyncIterable, observer: PartialAsyncObserver) { super(); this._source = source; this._observer = observer; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const obs = this._observer; const it = this._source[Symbol.asyncIterator](signal); try { for (let res: IteratorResult; !(res = await it.next()).done; ) { if (obs.next) { await obs.next(res.value); } yield res.value; } if (obs.complete) { await obs.complete(); } } catch (e) { if (!(e instanceof AbortError) && obs.error) { await obs.error(e); } throw e; } finally { await returnAsyncIterator(it); } } } /** * Invokes an action for each element in the async-iterable sequence, and propagates all observer * messages through the result sequence. This method can be used for debugging, logging, etc. by * intercepting the message stream to run arbitrary actions for messages on the pipeline. * * @template TSource The type of the elements in the source sequence. * @param {PartialAsyncObserver} observer Observer whose methods to invoke as part of the source sequence's observation. * @returns {MonoTypeOperatorAsyncFunction} The source sequence with the side-effecting behavior applied. */ export function tap( observer: PartialAsyncObserver ): MonoTypeOperatorAsyncFunction; /** * Invokes an action for each element in the async-iterable sequence, and propagates all observer * messages through the result sequence. This method can be used for debugging, logging, etc. by * intercepting the message stream to run arbitrary actions for messages on the pipeline. * * @template TSource The type of the elements in the source sequence. * @param {(((value: TSource) => any) | null)} [next] Function to invoke for each element in the async-iterable sequence. * @param {(((err: any) => any) | null)} [error] Function to invoke upon exceptional termination of the async-iterable sequence. * @param {((() => any) | null)} [complete] Function to invoke upon graceful termination of the async-iterable sequence. * @returns {MonoTypeOperatorAsyncFunction} The source sequence with the side-effecting behavior applied. */ export function tap( next?: ((value: TSource) => any) | null, error?: ((err: any) => any) | null, complete?: (() => any) | null ): MonoTypeOperatorAsyncFunction; /** * Invokes an action for each element in the async-iterable sequence, and propagates all observer * messages through the result sequence. This method can be used for debugging, logging, etc. by * intercepting the message stream to run arbitrary actions for messages on the pipeline. * * @template TSource The type of the elements in the source sequence. * @param {(PartialAsyncObserver | ((value: TSource) => any) | null)} [observerOrNext] Observer whose methods to invoke as * part of the source sequence's observation or a function to invoke for each element in the async-iterable sequence. * @param {(((err: any) => any) | null)} [error] Function to invoke upon exceptional termination of the async-iterable sequence. * @param {((() => any) | null)} [complete] Function to invoke upon graceful termination of the async-iterable sequence. * @returns {MonoTypeOperatorAsyncFunction} The source sequence with the side-effecting behavior applied. */ export function tap( observerOrNext?: PartialAsyncObserver | ((value: TSource) => any) | null, error?: ((err: any) => any) | null, complete?: (() => any) | null ): MonoTypeOperatorAsyncFunction { return function tapOperatorFunction(source: AsyncIterable): AsyncIterableX { return new TapAsyncIterable(source, toObserver(observerOrNext, error, complete)); }; }