Skip to content

Commit

Permalink
fix(windowCount): set default value for skip argument, do not emit em…
Browse files Browse the repository at this point in the history
…pty buffer at the end
  • Loading branch information
kwonoj authored and benlesh committed Sep 8, 2015
1 parent 621b1ea commit a513dbb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 14 additions & 0 deletions spec/operators/windowCount-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
19 changes: 12 additions & 7 deletions src/operators/windowCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,34 @@ class WindowCountOperator<T, R> implements Operator<T, R> {
}

class WindowCountSubscriber<T> extends Subscriber<T> {
private windows: { count: number, window: Subject<T> } [] = [];
private windows: { count: number, notified : boolean, window: Subject<T> } [] = [{ count: 0, notified : false, window : new Subject<T>() }];
private count: number = 0;

constructor(destination: Observer<T>, 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<T>();
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();
Expand Down

0 comments on commit a513dbb

Please sign in to comment.