source: trip-planner-front/node_modules/rxjs/src/internal/operators/tap.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.3 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { Observable } from '../Observable';
4import { MonoTypeOperatorFunction, PartialObserver, TeardownLogic } from '../types';
5import { noop } from '../util/noop';
6import { isFunction } from '../util/isFunction';
7
8/* tslint:disable:max-line-length */
9/** @deprecated Use an observer instead of a complete callback */
10export function tap<T>(next: null | undefined, error: null | undefined, complete: () => void): MonoTypeOperatorFunction<T>;
11/** @deprecated Use an observer instead of an error callback */
12export function tap<T>(next: null | undefined, error: (error: any) => void, complete?: () => void): MonoTypeOperatorFunction<T>;
13/** @deprecated Use an observer instead of a complete callback */
14export function tap<T>(next: (value: T) => void, error: null | undefined, complete: () => void): MonoTypeOperatorFunction<T>;
15export function tap<T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void): MonoTypeOperatorFunction<T>;
16export function tap<T>(observer: PartialObserver<T>): MonoTypeOperatorFunction<T>;
17/* tslint:enable:max-line-length */
18
19/**
20 * Perform a side effect for every emission on the source Observable, but return
21 * an Observable that is identical to the source.
22 *
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>
25 *
26 * ![](do.png)
27 *
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.
33 *
34 * This operator is useful for debugging your Observables for the correct values
35 * or performing other side effects.
36 *
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.
41 *
42 * ## Example
43 * Map every click to the clientX position of that click, while also logging the click event
44 * ```ts
45 * import { fromEvent } from 'rxjs';
46 * import { tap, map } from 'rxjs/operators';
47 *
48 * const clicks = fromEvent(document, 'click');
49 * const positions = clicks.pipe(
50 * tap(ev => console.log(ev)),
51 * map(ev => ev.clientX),
52 * );
53 * positions.subscribe(x => console.log(x));
54 * ```
55 *
56 * @see {@link map}
57 * @see {@link Observable#subscribe}
58 *
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.
65 * @name tap
66 */
67export 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));
72 };
73}
74
75class 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) {
79 }
80 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
81 return source.subscribe(new TapSubscriber(subscriber, this.nextOrObserver, this.error, this.complete));
82 }
83}
84
85/**
86 * We need this JSDoc comment for affecting ESDoc.
87 * @ignore
88 * @extends {Ignored}
89 */
90
91class TapSubscriber<T> extends Subscriber<T> {
92 private _context: any;
93
94 private _tapNext: ((value: T) => void) = noop;
95
96 private _tapError: ((err: any) => void) = noop;
97
98 private _tapComplete: (() => void) = noop;
99
100 constructor(destination: Subscriber<T>,
101 observerOrNext?: PartialObserver<T> | ((value: T) => void),
102 error?: (e?: any) => void,
103 complete?: () => void) {
104 super(destination);
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;
115 }
116 }
117
118 _next(value: T) {
119 try {
120 this._tapNext.call(this._context, value);
121 } catch (err) {
122 this.destination.error(err);
123 return;
124 }
125 this.destination.next(value);
126 }
127
128 _error(err: any) {
129 try {
130 this._tapError.call(this._context, err);
131 } catch (err) {
132 this.destination.error(err);
133 return;
134 }
135 this.destination.error(err);
136 }
137
138 _complete() {
139 try {
140 this._tapComplete.call(this._context, );
141 } catch (err) {
142 this.destination.error(err);
143 return;
144 }
145 return this.destination.complete();
146 }
147}
Note: See TracBrowser for help on using the repository browser.