es6/operator/catch.js
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
/**
* 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) {
var operator = new CatchOperator(selector);
var caught = this.lift(operator);
return (operator.caught = caught);
}
var CatchOperator = (function () {
function CatchOperator(selector) {
this.selector = selector;
}
CatchOperator.prototype.call = function (subscriber, source) {
return source._subscribe(new CatchSubscriber(subscriber, this.selector, this.caught));
};
return CatchOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var CatchSubscriber = (function (_super) {
__extends(CatchSubscriber, _super);
function CatchSubscriber(destination, selector, caught) {
_super.call(this, 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`.
CatchSubscriber.prototype.error = function (err) {
if (!this.isStopped) {
var result = void 0;
try {
result = this.selector(err, this.caught);
}
catch (err) {
this.destination.error(err);
return;
}
this.unsubscribe();
this.destination.remove(this);
subscribeToResult(this, result);
}
};
return CatchSubscriber;
}(OuterSubscriber));
//# sourceMappingURL=catch.js.map