@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
59 lines (57 loc) • 2.41 kB
JavaScript
import { AsyncIterableX } from '../asynciterablex.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 {
_source;
_observer;
constructor(source, observer) {
super();
this._source = source;
this._observer = observer;
}
async *[Symbol.asyncIterator](signal) {
throwIfAborted(signal);
const obs = this._observer;
const it = this._source[Symbol.asyncIterator](signal);
try {
for (let res; !(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<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.js.map