Skip to content

Commit c088b0e

Browse files
authored
fix(buffer): Ensure notifier is subscribed after source (#5654)
- Resolves an issue where a multicast observable could not adequately be used to notify a buffer on itself - Corrects a regression that was introduced by a bad merge back in 6.0. This was originally corrected in PR #2195. fixes #1754
1 parent c289688 commit c088b0e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

spec/operators/buffer-spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { buffer, mergeMap, take } from 'rxjs/operators';
2-
import { EMPTY, NEVER, throwError, of } from 'rxjs';
2+
import { EMPTY, NEVER, throwError, of, Subject } from 'rxjs';
33
import { TestScheduler } from 'rxjs/testing';
44
import { observableMatcher } from '../helpers/observableMatcher';
5+
import { expect } from 'chai';
56

67
/** @test {buffer} */
78
describe('Observable.prototype.buffer', () => {
@@ -266,4 +267,23 @@ describe('Observable.prototype.buffer', () => {
266267
expectSubscriptions(b.subscriptions).toBe(bsubs);
267268
});
268269
});
270+
271+
it('should emit properly with an observable using itself as a notifier', () => {
272+
const results: any[] = [];
273+
const subject = new Subject<number>();
274+
275+
const source = subject.pipe(
276+
buffer(subject)
277+
).subscribe({
278+
next: value => results.push(value),
279+
complete: () => results.push('complete')
280+
});
281+
282+
subject.next(1);
283+
expect(results).to.deep.equal([[1]]);
284+
subject.next(2);
285+
expect(results).to.deep.equal([[1], [2]]);
286+
subject.complete();
287+
expect(results).to.deep.equal([[1], [2], 'complete']);
288+
});
269289
});

src/internal/operators/buffer.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class BufferOperator<T> implements Operator<T, T[]> {
5656
}
5757

5858
call(subscriber: Subscriber<T[]>, source: any): any {
59-
return source.subscribe(new BufferSubscriber(subscriber, this.closingNotifier));
59+
const bufferSubscriber = new BufferSubscriber(subscriber);
60+
subscriber.add(source.subscribe(bufferSubscriber));
61+
subscriber.add(innerSubscribe(this.closingNotifier, new SimpleInnerSubscriber(bufferSubscriber)));
62+
return subscriber;
6063
}
6164
}
6265

@@ -68,9 +71,8 @@ class BufferOperator<T> implements Operator<T, T[]> {
6871
class BufferSubscriber<T> extends SimpleOuterSubscriber<T, any> {
6972
private buffer: T[] = [];
7073

71-
constructor(destination: Subscriber<T[]>, closingNotifier: Observable<any>) {
74+
constructor(destination: Subscriber<T[]>) {
7275
super(destination);
73-
this.add(innerSubscribe(closingNotifier, new SimpleInnerSubscriber(this)));
7476
}
7577

7678
protected _next(value: T) {

0 commit comments

Comments
 (0)