Skip to content

Commit 1b30aa9

Browse files
kwonojbenlesh
authored andcommitted
fix(buffer): cleanup notifier subscription when unsubscribed
1 parent 69aa51d commit 1b30aa9

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

spec/operators/buffer-spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/* globals describe, it, expect, expectObservable, hot */
1+
/* globals describe, it, expect, expectObservable, expectSubscriptions, hot */
22
var Rx = require('../../dist/cjs/Rx');
33
var Observable = Rx.Observable;
44

5-
describe('Observable.prototype.buffer', function () {
5+
describe('Observable.prototype.buffer()', function () {
66
it('should work with empty and empty selector', function () {
77
var a = Observable.empty();
88
var b = Observable.empty();
@@ -167,4 +167,21 @@ describe('Observable.prototype.buffer', function () {
167167
expectObservable(a.buffer(b)).toBe(expected, expectedValues, new Error('too bad'));
168168
expectSubscriptions(a.subscriptions).toBe(subs);
169169
});
170+
171+
it('should unsubscribe notifier when source unsubscribed', function () {
172+
var a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
173+
var unsub = ' ! ';
174+
var subs = '^ ! ';
175+
var b = hot('--------^--a-------b---cd| ');
176+
var bsubs = '^ ! ';
177+
var expected = '---a-------b--- ';
178+
var expectedValues = {
179+
a: ['3'],
180+
b: ['4', '5']
181+
};
182+
183+
expectObservable(a.buffer(b), unsub).toBe(expected, expectedValues);
184+
expectSubscriptions(a.subscriptions).toBe(subs);
185+
expectSubscriptions(b.subscriptions).toBe(bsubs);
186+
});
170187
});

src/operators/buffer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ class BufferOperator<T, R> implements Operator<T, R> {
3030
}
3131

3232
class BufferSubscriber<T> extends Subscriber<T> {
33-
buffer: T[] = [];
33+
private buffer: T[] = [];
34+
private notifierSubscriber: BufferClosingNotifierSubscriber<any> = null;
3435

3536
constructor(destination: Subscriber<T>, closingNotifier: Observable<any>) {
3637
super(destination);
37-
this.add(closingNotifier._subscribe(new BufferClosingNotifierSubscriber(this)));
38+
this.notifierSubscriber = new BufferClosingNotifierSubscriber(this);
39+
this.add(closingNotifier._subscribe(this.notifierSubscriber));
3840
}
3941

4042
_next(value: T) {
@@ -53,6 +55,10 @@ class BufferSubscriber<T> extends Subscriber<T> {
5355
const buffer = this.buffer;
5456
this.buffer = [];
5557
this.destination.next(buffer);
58+
59+
if (this.isUnsubscribed) {
60+
this.notifierSubscriber.unsubscribe();
61+
}
5662
}
5763
}
5864

0 commit comments

Comments
 (0)