|
| 1 | +import Operator from '../Operator'; |
| 2 | +import Observer from '../Observer'; |
| 3 | +import Subscriber from '../Subscriber'; |
| 4 | + |
| 5 | +import noop from '../util/noop'; |
| 6 | +import tryCatch from '../util/tryCatch'; |
| 7 | +import {errorObject} from '../util/errorObject'; |
| 8 | +import bindCallback from '../util/bindCallback'; |
| 9 | + |
| 10 | +export default function _do<T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void) { |
| 11 | + return this.lift(new DoOperator(next || noop, error || noop, complete || noop)); |
| 12 | +} |
| 13 | + |
| 14 | +export class DoOperator<T, R> extends Operator<T, R> { |
| 15 | + |
| 16 | + next: (x: T) => void; |
| 17 | + error: (e: any) => void; |
| 18 | + complete: () => void; |
| 19 | + |
| 20 | + constructor(next: (x: T) => void, error: (e: any) => void, complete: () => void) { |
| 21 | + super(); |
| 22 | + this.next = next; |
| 23 | + this.error = error; |
| 24 | + this.complete = complete; |
| 25 | + } |
| 26 | + |
| 27 | + call(observer: Observer<T>): Observer<T> { |
| 28 | + return new DoSubscriber(observer, this.next, this.error, this.complete); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export class DoSubscriber<T> extends Subscriber<T> { |
| 33 | + |
| 34 | + __next: (x: T) => void; |
| 35 | + __error: (e: any) => void; |
| 36 | + __complete: () => void; |
| 37 | + |
| 38 | + |
| 39 | + constructor(destination: Observer<T>, next: (x: T) => void, error: (e: any) => void, complete: () => void) { |
| 40 | + super(destination); |
| 41 | + this.__next = next; |
| 42 | + this.__error = error; |
| 43 | + this.__complete = complete; |
| 44 | + } |
| 45 | + |
| 46 | + _next(x) { |
| 47 | + const result = tryCatch(this.__next)(x); |
| 48 | + if (result === errorObject) { |
| 49 | + this.destination.error(errorObject.e); |
| 50 | + } else { |
| 51 | + this.destination.next(x); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + _error(e) { |
| 56 | + const result = tryCatch(this.__error)(e); |
| 57 | + if (result === errorObject) { |
| 58 | + this.destination.error(errorObject.e); |
| 59 | + } else { |
| 60 | + this.destination.error(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + _complete() { |
| 65 | + const result = tryCatch(this.__complete)(); |
| 66 | + if (result === errorObject) { |
| 67 | + this.destination.error(errorObject.e); |
| 68 | + } else { |
| 69 | + this.destination.complete(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments