Skip to content

Commit 456ef33

Browse files
committedAug 19, 2018
fix(catchError): stop listening to a synchronous inner-obervable when unsubscribed
1 parent ee1a339 commit 456ef33

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
 

‎spec/operators/catch-spec.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
2-
import { concat, Observable, of, throwError, EMPTY, from } from 'rxjs';
3-
import { catchError, map, mergeMap } from 'rxjs/operators';
2+
import { concat, defer, Observable, of, throwError, EMPTY, from } from 'rxjs';
3+
import { catchError, map, mergeMap, takeWhile } from 'rxjs/operators';
44
import { TestScheduler } from 'rxjs/testing';
55
import * as sinon from 'sinon';
66
import { createObservableInputs } from '../helpers/test-helper';
@@ -121,6 +121,31 @@ describe('catchError operator', () => {
121121
expectSubscriptions(e2.subscriptions).toBe(e2subs);
122122
});
123123

124+
it('should stop listening to a synchronous observable when unsubscribed', () => {
125+
const sideEffects: number[] = [];
126+
const synchronousObservable = concat(
127+
defer(() => {
128+
sideEffects.push(1);
129+
return of(1);
130+
}),
131+
defer(() => {
132+
sideEffects.push(2);
133+
return of(2);
134+
}),
135+
defer(() => {
136+
sideEffects.push(3);
137+
return of(3);
138+
})
139+
);
140+
141+
throwError(new Error('Some error')).pipe(
142+
catchError(() => synchronousObservable),
143+
takeWhile((x) => x != 2) // unsubscribe at the second side-effect
144+
).subscribe(() => { /* noop */ });
145+
146+
expect(sideEffects).to.deep.equal([1, 2]);
147+
});
148+
124149
it('should catch error and replace it with a hot Observable', () => {
125150
const e1 = hot('--a--b--# ');
126151
const e1subs = '^ ! ';

‎src/internal/operators/catchError.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Subscriber} from '../Subscriber';
33
import {Observable} from '../Observable';
44

55
import {OuterSubscriber} from '../OuterSubscriber';
6+
import { InnerSubscriber } from '../InnerSubscriber';
67
import {subscribeToResult} from '../util/subscribeToResult';
78
import {ObservableInput, OperatorFunction, MonoTypeOperatorFunction} from '../types';
89

@@ -121,7 +122,9 @@ class CatchSubscriber<T, R> extends OuterSubscriber<T, T | R> {
121122
return;
122123
}
123124
this._unsubscribeAndRecycle();
124-
this.add(subscribeToResult(this, result));
125+
const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
126+
this.add(innerSubscriber);
127+
subscribeToResult(this, result, undefined, undefined, innerSubscriber);
125128
}
126129
}
127130
}

0 commit comments

Comments
 (0)
Please sign in to comment.