From 6405cf42428cfccc0279c57e3ae984cbd756437b Mon Sep 17 00:00:00 2001 From: Ben Farr Date: Wed, 7 Dec 2016 12:24:35 +0000 Subject: [PATCH] fix(buffer): emit last buffer if source completes Fix buffer operator not emitting the last buffer when the source completes. This is closer to rxjs v4* behaviour and matches the behaviour of the other buffer operators. The _complete method is very similar to thoses in bufferCount, bufferTime and bufferWhen. *rxjs v4 will always emit the buffer if the source completes even if the buffer is empty. This fix only emits if the buffer is non empty. BREAKING CHANGE: The `buffer()` operator now emits what's partially buffered when the source completes. This is closer to rxjs v4* behaviour and matches the v5 behaviour of the other buffer operators. --- spec/operators/buffer-spec.ts | 12 ++++++++++++ src/operator/buffer.ts | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/spec/operators/buffer-spec.ts b/spec/operators/buffer-spec.ts index 6912aaf7b2..63b99d059b 100644 --- a/spec/operators/buffer-spec.ts +++ b/spec/operators/buffer-spec.ts @@ -219,4 +219,16 @@ describe('Observable.prototype.buffer', () => { expectObservable(a.buffer(b).take(1)).toBe(expected, expectedValues); expectSubscriptions(b.subscriptions).toBe(bsubs); }); + + it('should emit last buffer if source completes', () => { + const a = hot('-a-b-c-d-e-f-g-h-i-|'); + const b = hot('-----B-----B--------'); + const expected = '-----x-----y-------(z|)'; + const expectedValues = { + x: ['a', 'b', 'c'], + y: ['d', 'e', 'f'], + z: ['g', 'h', 'i'] + }; + expectObservable(a.buffer(b)).toBe(expected, expectedValues); + }); }); diff --git a/src/operator/buffer.ts b/src/operator/buffer.ts index e4b5878183..a47644e8ef 100644 --- a/src/operator/buffer.ts +++ b/src/operator/buffer.ts @@ -69,6 +69,14 @@ class BufferSubscriber extends OuterSubscriber { this.buffer.push(value); } + protected _complete() { + const buffer = this.buffer; + if (buffer.length > 0) { + this.destination.next(buffer); + } + super._complete(); + } + notifyNext(outerValue: T, innerValue: any, outerIndex: number, innerIndex: number, innerSub: InnerSubscriber): void {