Skip to content

Commit c4002f3

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

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
 

Diff for: ‎spec/operators/mergeScan-spec.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
22
import { TestScheduler } from 'rxjs/testing';
3-
import { of, EMPTY, NEVER, concat, throwError } from 'rxjs';
4-
import { mergeScan, delay, mergeMap } from 'rxjs/operators';
3+
import { of, defer, EMPTY, NEVER, concat, throwError } from 'rxjs';
4+
import { mergeScan, delay, mergeMap, takeWhile } from 'rxjs/operators';
5+
import { expect } from 'chai';
56

67
declare const rxTestScheduler: TestScheduler;
78
/** @test {mergeScan} */
@@ -136,6 +137,31 @@ describe('mergeScan', () => {
136137
expectSubscriptions(e1.subscriptions).toBe(e1subs);
137138
});
138139

140+
it('should stop listening to a synchronous observable when unsubscribed', () => {
141+
const sideEffects: number[] = [];
142+
const synchronousObservable = concat(
143+
defer(() => {
144+
sideEffects.push(1);
145+
return of(1);
146+
}),
147+
defer(() => {
148+
sideEffects.push(2);
149+
return of(2);
150+
}),
151+
defer(() => {
152+
sideEffects.push(3);
153+
return of(3);
154+
})
155+
);
156+
157+
of(null).pipe(
158+
mergeScan(() => synchronousObservable, 0),
159+
takeWhile((x) => x != 2) // unsubscribe at the second side-effect
160+
).subscribe(() => { /* noop */ });
161+
162+
expect(sideEffects).to.deep.equal([1, 2]);
163+
});
164+
139165
it('should handle errors in the projection function', () => {
140166
const e1 = hot('--a--^--b--c--d--e--f--g--|');
141167
const e1subs = '^ !';

Diff for: ‎src/internal/operators/mergeScan.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ export class MergeScanSubscriber<T, R> extends OuterSubscriber<T, R> {
100100
}
101101

102102
private _innerSub(ish: any, value: T, index: number): void {
103-
this.add(subscribeToResult<T, R>(this, ish, value, index));
103+
const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
104+
this.add(innerSubscriber);
105+
subscribeToResult<T, R>(this, ish, value, index, innerSubscriber);
104106
}
105107

106108
protected _complete(): void {

0 commit comments

Comments
 (0)
Please sign in to comment.