UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

57 lines 1.95 kB
import { Subscriber } from '../Subscriber'; /** * Catches errors on the observable to be handled by returning a new observable or throwing an error. * @param {function} selector a function that takes as arguments `err`, which is the error, and `caught`, which * is the source observable, in case you'd like to "retry" that observable by returning it again. Whatever observable * is returned by the `selector` will be used to continue the observable chain. * @return {Observable} an observable that originates from either the source or the observable returned by the * catch `selector` function. * @method catch * @owner Observable */ export function _catch(selector) { const operator = new CatchOperator(selector); const caught = this.lift(operator); return (operator.caught = caught); } class CatchOperator { constructor(selector) { this.selector = selector; } call(subscriber, source) { return source._subscribe(new CatchSubscriber(subscriber, this.selector, this.caught)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class CatchSubscriber extends Subscriber { constructor(destination, selector, caught) { super(destination); this.selector = selector; this.caught = caught; } // NOTE: overriding `error` instead of `_error` because we don't want // to have this flag this subscriber as `isStopped`. error(err) { if (!this.isStopped) { let result; try { result = this.selector(err, this.caught); } catch (err) { this.destination.error(err); return; } this._innerSub(result); } } _innerSub(result) { this.unsubscribe(); this.destination.remove(this); result.subscribe(this.destination); } } //# sourceMappingURL=catch.js.map