UNPKG

ix

Version:

The Interactive Extensions for JavaScript

60 lines (58 loc) 2.66 kB
import { __asyncGenerator, __await } from "tslib"; import { AsyncIterableX } from '../asynciterablex.mjs'; import { toObserver } from '../../util/toobserver.mjs'; import { AbortError, throwIfAborted } from '../../aborterror.mjs'; import { returnAsyncIterator } from '../../util/returniterator.mjs'; /** @ignore */ export class TapAsyncIterable extends AsyncIterableX { constructor(source, observer) { super(); this._source = source; this._observer = observer; } [Symbol.asyncIterator](signal) { return __asyncGenerator(this, arguments, function* _a() { throwIfAborted(signal); const obs = this._observer; const it = this._source[Symbol.asyncIterator](signal); try { for (let res; !(res = yield __await(it.next())).done;) { if (obs.next) { yield __await(obs.next(res.value)); } yield yield __await(res.value); } if (obs.complete) { yield __await(obs.complete()); } } catch (e) { if (!(e instanceof AbortError) && obs.error) { yield __await(obs.error(e)); } throw e; } finally { yield __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<TSource> | ((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<TSource>} The source sequence with the side-effecting behavior applied. */ export function tap(observerOrNext, error, complete) { return function tapOperatorFunction(source) { return new TapAsyncIterable(source, toObserver(observerOrNext, error, complete)); }; } //# sourceMappingURL=tap.mjs.map