Skip to content

Commit a513dbb

Browse files
kwonojbenlesh
authored andcommitted
fix(windowCount): set default value for skip argument, do not emit empty buffer at the end
1 parent 621b1ea commit a513dbb

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spec/operators/windowCount-spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@ describe('Observable.prototype.windowCount', function () {
1818
expect(w).toEqual(expected.shift())
1919
}, null, done);
2020
}, 2000);
21+
22+
it('should emit buffers at buffersize of intervals if not specified', function (done) {
23+
var expected = [
24+
[0, 1],
25+
[2, 3],
26+
[4, 5]
27+
];
28+
Observable.range(0, 6)
29+
.windowCount(2)
30+
.flatMap(function (x) { return x.toArray(); })
31+
.subscribe(function (w) {
32+
expect(w).toEqual(expected.shift())
33+
}, null, done);
34+
}, 2000);
2135
});

src/operators/windowCount.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,34 @@ class WindowCountOperator<T, R> implements Operator<T, R> {
2525
}
2626

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

3131
constructor(destination: Observer<T>, private windowSize: number, private startWindowEvery: number) {
32-
super(destination);
32+
super(destination);
3333
}
3434

3535
_next(value: T) {
3636
const count = (this.count += 1);
37-
const startWindowEvery = this.startWindowEvery;
37+
const startWindowEvery = (this.startWindowEvery > 0) ? this.startWindowEvery : this.windowSize;
3838
const windowSize = this.windowSize;
3939
const windows = this.windows;
40+
const len = windows.length;
4041

41-
if (startWindowEvery && count % this.startWindowEvery === 0) {
42+
if (count % startWindowEvery === 0) {
4243
let window = new Subject<T>();
43-
windows.push({ count: 0, window });
44-
this.destination.next(window);
44+
windows.push({ count: 0, notified : false, window : window });
4545
}
4646

47-
const len = windows.length;
4847
for (let i = 0; i < len; i++) {
4948
let w = windows[i];
5049
const window = w.window;
50+
51+
if (!w.notified) {
52+
w.notified = true;
53+
this.destination.next(window);
54+
}
55+
5156
window.next(value);
5257
if (windowSize === (w.count += 1)) {
5358
window.complete();

0 commit comments

Comments
 (0)