Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windowCount): set default value for skip argument, do not emit em… #273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #271 (comment), some function initialize default value to 0 and some sets it to null for number types. As same as previous changes only updates value but not default value since not sure which one's recommended.

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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag is introduced to let window to be notified only if window will have any values to be contained, such as Observable.range(0,6).windowCount(2) emits empty window at the end. Basic intention is to align operator behavior as same as bufferCount does.

note : it seems current RxJS (Reactive-Extensions/RxJS#148) allows empty window being emitted at the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now working on #378 and I changed the implementation of windowCount to align with RxJS legacy, and it seems like I brought the "empty window at the end" back.

@Blesh @kwonoj

w.notified = true;
this.destination.next(window);
}

window.next(value);
if (windowSize === (w.count += 1)) {
window.complete();
Expand Down