1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { MonoTypeOperatorFunction, PartialObserver, TeardownLogic } from '../types';
5 import { noop } from '../util/noop';
6 import { isFunction } from '../util/isFunction';
8 /* tslint:disable:max-line-length */
9 /** @deprecated Use an observer instead of a complete callback */
10 export function tap<T>(next: null | undefined, error: null | undefined, complete: () => void): MonoTypeOperatorFunction<T>;
11 /** @deprecated Use an observer instead of an error callback */
12 export function tap<T>(next: null | undefined, error: (error: any) => void, complete?: () => void): MonoTypeOperatorFunction<T>;
13 /** @deprecated Use an observer instead of a complete callback */
14 export function tap<T>(next: (value: T) => void, error: null | undefined, complete: () => void): MonoTypeOperatorFunction<T>;
15 export function tap<T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void): MonoTypeOperatorFunction<T>;
16 export function tap<T>(observer: PartialObserver<T>): MonoTypeOperatorFunction<T>;
17 /* tslint:enable:max-line-length */
20 * Perform a side effect for every emission on the source Observable, but return
21 * an Observable that is identical to the source.
23 * <span class="informal">Intercepts each emission on the source and runs a
24 * function, but returns an output which is identical to the source as long as errors don't occur.</span>
28 * Returns a mirrored Observable of the source Observable, but modified so that
29 * the provided Observer is called to perform a side effect for every value,
30 * error, and completion emitted by the source. Any errors that are thrown in
31 * the aforementioned Observer or handlers are safely sent down the error path
32 * of the output Observable.
34 * This operator is useful for debugging your Observables for the correct values
35 * or performing other side effects.
37 * Note: this is different to a `subscribe` on the Observable. If the Observable
38 * returned by `tap` is not subscribed, the side effects specified by the
39 * Observer will never happen. `tap` therefore simply spies on existing
40 * execution, it does not trigger an execution to happen like `subscribe` does.
43 * Map every click to the clientX position of that click, while also logging the click event
45 * import { fromEvent } from 'rxjs';
46 * import { tap, map } from 'rxjs/operators';
48 * const clicks = fromEvent(document, 'click');
49 * const positions = clicks.pipe(
50 * tap(ev => console.log(ev)),
51 * map(ev => ev.clientX),
53 * positions.subscribe(x => console.log(x));
57 * @see {@link Observable#subscribe}
59 * @param {Observer|function} [nextOrObserver] A normal Observer object or a
60 * callback for `next`.
61 * @param {function} [error] Callback for errors in the source.
62 * @param {function} [complete] Callback for the completion of the source.
63 * @return {Observable} An Observable identical to the source, but runs the
64 * specified Observer or callback(s) for each item.
67 export function tap<T>(nextOrObserver?: PartialObserver<T> | ((x: T) => void),
68 error?: (e: any) => void,
69 complete?: () => void): MonoTypeOperatorFunction<T> {
70 return function tapOperatorFunction(source: Observable<T>): Observable<T> {
71 return source.lift(new DoOperator(nextOrObserver, error, complete));
75 class DoOperator<T> implements Operator<T, T> {
76 constructor(private nextOrObserver?: PartialObserver<T> | ((x: T) => void),
77 private error?: (e: any) => void,
78 private complete?: () => void) {
80 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
81 return source.subscribe(new TapSubscriber(subscriber, this.nextOrObserver, this.error, this.complete));
86 * We need this JSDoc comment for affecting ESDoc.
91 class TapSubscriber<T> extends Subscriber<T> {
92 private _context: any;
94 private _tapNext: ((value: T) => void) = noop;
96 private _tapError: ((err: any) => void) = noop;
98 private _tapComplete: (() => void) = noop;
100 constructor(destination: Subscriber<T>,
101 observerOrNext?: PartialObserver<T> | ((value: T) => void),
102 error?: (e?: any) => void,
103 complete?: () => void) {
105 this._tapError = error || noop;
106 this._tapComplete = complete || noop;
107 if (isFunction(observerOrNext)) {
108 this._context = this;
109 this._tapNext = observerOrNext;
110 } else if (observerOrNext) {
111 this._context = observerOrNext;
112 this._tapNext = observerOrNext.next || noop;
113 this._tapError = observerOrNext.error || noop;
114 this._tapComplete = observerOrNext.complete || noop;
120 this._tapNext.call(this._context, value);
122 this.destination.error(err);
125 this.destination.next(value);
130 this._tapError.call(this._context, err);
132 this.destination.error(err);
135 this.destination.error(err);
140 this._tapComplete.call(this._context, );
142 this.destination.error(err);
145 return this.destination.complete();