From dfb7483fbc086e896610944c9bf98e6bc6000a42 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2016 10:59:58 -0700 Subject: [PATCH] fix(catch): accept selector returns ObservableInput closes #1857 --- spec/operators/catch-spec.ts | 12 ++++++++++++ src/operator/catch.ts | 32 +++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/spec/operators/catch-spec.ts b/spec/operators/catch-spec.ts index 1c2aeefc65d..f41307ba8ec 100644 --- a/spec/operators/catch-spec.ts +++ b/spec/operators/catch-spec.ts @@ -227,4 +227,16 @@ describe('Observable.prototype.catch', () => { done(); }); }); + + it('should accept selector returns promise', (done: MochaDone) => { + Observable.throw('bad') + .catch(err => Promise.resolve(42)) + .subscribe(x => { + expect(x).to.be.equal(42); + }, (err: any) => { + done(new Error('should not be called')); + }, () => { + done(); + }); + }); }); diff --git a/src/operator/catch.ts b/src/operator/catch.ts index 13d68145452..395d0527524 100644 --- a/src/operator/catch.ts +++ b/src/operator/catch.ts @@ -1,6 +1,9 @@ import {Operator} from '../Operator'; import {Subscriber} from '../Subscriber'; -import {Observable} from '../Observable'; +import {Observable, ObservableInput} from '../Observable'; + +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. @@ -12,21 +15,21 @@ import {Observable} from '../Observable'; * @method catch * @owner Observable */ -export function _catch(selector: (err: any, caught: Observable) => Observable): Observable { +export function _catch(selector: (err: any, caught: Observable) => ObservableInput): Observable { const operator = new CatchOperator(selector); const caught = this.lift(operator); return (operator.caught = caught); } export interface CatchSignature { - (selector: (err: any, caught: Observable) => Observable): Observable; - (selector: (err: any, caught: Observable) => Observable): Observable; + (selector: (err: any, caught: Observable) => ObservableInput): Observable; + (selector: (err: any, caught: Observable) => ObservableInput): Observable; } class CatchOperator implements Operator { - caught: Observable; + caught: Observable; - constructor(private selector: (err: any, caught: Observable) => Observable) { + constructor(private selector: (err: any, caught: Observable) => ObservableInput) { } call(subscriber: Subscriber, source: any): any { @@ -39,11 +42,10 @@ class CatchOperator implements Operator { * @ignore * @extends {Ignored} */ -class CatchSubscriber extends Subscriber { - +class CatchSubscriber extends OuterSubscriber { constructor(destination: Subscriber, - private selector: (err: any, caught: Observable) => Observable, - private caught: Observable) { + private selector: (err: any, caught: Observable) => ObservableInput, + private caught: Observable) { super(destination); } @@ -60,13 +62,9 @@ class CatchSubscriber extends Subscriber { return; } - this._innerSub(result); + this.unsubscribe(); + (this.destination).remove(this); + subscribeToResult(this, result); } } - - private _innerSub(result: Observable) { - this.unsubscribe(); - (this.destination).remove(this); - result.subscribe(this.destination); - } }