From a513dbbca4d6bcc6af2f62a3341940e3b116f306 Mon Sep 17 00:00:00 2001 From: kwonoj Date: Sun, 6 Sep 2015 16:21:41 -0700 Subject: [PATCH] fix(windowCount): set default value for skip argument, do not emit empty buffer at the end --- spec/operators/windowCount-spec.js | 14 ++++++++++++++ src/operators/windowCount.ts | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/spec/operators/windowCount-spec.js b/spec/operators/windowCount-spec.js index fb6c1b5f54..c6d86cdaaa 100644 --- a/spec/operators/windowCount-spec.js +++ b/spec/operators/windowCount-spec.js @@ -18,4 +18,18 @@ describe('Observable.prototype.windowCount', function () { expect(w).toEqual(expected.shift()) }, null, done); }, 2000); + + it('should emit buffers at buffersize of intervals if not specified', function (done) { + var expected = [ + [0, 1], + [2, 3], + [4, 5] + ]; + Observable.range(0, 6) + .windowCount(2) + .flatMap(function (x) { return x.toArray(); }) + .subscribe(function (w) { + expect(w).toEqual(expected.shift()) + }, null, done); + }, 2000); }); \ No newline at end of file diff --git a/src/operators/windowCount.ts b/src/operators/windowCount.ts index aa94d80c38..62d69c21be 100644 --- a/src/operators/windowCount.ts +++ b/src/operators/windowCount.ts @@ -25,29 +25,34 @@ class WindowCountOperator implements Operator { } class WindowCountSubscriber extends Subscriber { - private windows: { count: number, window: Subject } [] = []; + private windows: { count: number, notified : boolean, window: Subject } [] = [{ count: 0, notified : false, window : new Subject() }]; private count: number = 0; constructor(destination: Observer, private windowSize: number, private startWindowEvery: number) { - super(destination); + super(destination); } _next(value: T) { const count = (this.count += 1); - const startWindowEvery = this.startWindowEvery; + const startWindowEvery = (this.startWindowEvery > 0) ? this.startWindowEvery : this.windowSize; const windowSize = this.windowSize; const windows = this.windows; + const len = windows.length; - if (startWindowEvery && count % this.startWindowEvery === 0) { + if (count % startWindowEvery === 0) { let window = new Subject(); - windows.push({ count: 0, window }); - this.destination.next(window); + windows.push({ count: 0, notified : false, window : window }); } - const len = windows.length; for (let i = 0; i < len; i++) { let w = windows[i]; const window = w.window; + + if (!w.notified) { + w.notified = true; + this.destination.next(window); + } + window.next(value); if (windowSize === (w.count += 1)) { window.complete();