Skip to content

Commit

Permalink
fix(switchMap): stop listening to a synchronous inner-obervable when …
Browse files Browse the repository at this point in the history
…unsubscribed
  • Loading branch information
peaBerberian committed Aug 19, 2018
1 parent 030eee7 commit 260d52a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 27 additions & 2 deletions spec/operators/switchMap-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { switchMap, mergeMap, map } from 'rxjs/operators';
import { of, Observable } from 'rxjs';
import { switchMap, mergeMap, map, takeWhile } from 'rxjs/operators';
import { concat, defer, of, Observable } from 'rxjs';

declare function asDiagram(arg: string): Function;

Expand Down Expand Up @@ -169,6 +169,31 @@ describe('switchMap', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should stop listening to a synchronous observable when unsubscribed', () => {
const sideEffects: number[] = [];
const synchronousObservable = concat(
defer(() => {
sideEffects.push(1);
return of(1);
}),
defer(() => {
sideEffects.push(2);
return of(2);
}),
defer(() => {
sideEffects.push(3);
return of(3);
})
);

of(null).pipe(
switchMap(() => synchronousObservable),
takeWhile((x) => x != 2) // unsubscribe at the second side-effect
).subscribe(() => { /* noop */ });

expect(sideEffects).to.deep.equal([1, 2]);
});

it('should switch inner cold observables, inner never completes', () => {
const x = cold( '--a--b--c--d--e--| ');
const xsubs = ' ^ ! ';
Expand Down
4 changes: 3 additions & 1 deletion src/internal/operators/switchMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ class SwitchMapSubscriber<T, R> extends OuterSubscriber<T, R> {
if (innerSubscription) {
innerSubscription.unsubscribe();
}
this.add(this.innerSubscription = subscribeToResult(this, result, value, index));
const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
this.add(innerSubscriber);
this.innerSubscription = subscribeToResult(this, result, value, index, innerSubscriber);
}

protected _complete(): void {
Expand Down

0 comments on commit 260d52a

Please sign in to comment.