Skip to content

Commit 908ae56

Browse files
Andre Medeirosbenlesh
authored andcommitted
fix(windowCount): fix windowCount window opening times
Fix bugs with windowCount. This commit reverts PR #273, in order to have windowCount pass comprehensive marble tests. windowCount must open the first window immediately, not when the first next() event arrives, to comply with legacy windowWithCount and with RxJS Next window and windowTime.
1 parent ababa3d commit 908ae56

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/operators/windowCount.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,65 @@ import tryCatch from '../util/tryCatch';
1010
import {errorObject} from '../util/errorObject';
1111
import bindCallback from '../util/bindCallback';
1212

13-
export default function windowCount<T>(windowSize: number, startWindowEvery: number = 0): Observable<Observable<T>> {
13+
export default function windowCount<T>(windowSize: number,
14+
startWindowEvery: number = 0): Observable<Observable<T>> {
1415
return this.lift(new WindowCountOperator(windowSize, startWindowEvery));
1516
}
1617

1718
class WindowCountOperator<T, R> implements Operator<T, R> {
1819

19-
constructor(private windowSize: number, private startWindowEvery: number) {
20+
constructor(private windowSize: number,
21+
private startWindowEvery: number) {
2022
}
2123

2224
call(subscriber: Subscriber<T>): Subscriber<T> {
2325
return new WindowCountSubscriber(subscriber, this.windowSize, this.startWindowEvery);
2426
}
2527
}
2628

27-
interface WindowObject<T> {
28-
count: number;
29-
notified: boolean;
30-
window: Subject<T>;
31-
}
32-
3329
class WindowCountSubscriber<T> extends Subscriber<T> {
34-
private windows: WindowObject<T>[] = [
35-
{ count: 0, notified : false, window : new Subject<T>() }
36-
];
30+
private windows: Subject<T>[] = [ new Subject<T>() ];
3731
private count: number = 0;
3832

39-
constructor(destination: Subscriber<T>, private windowSize: number, private startWindowEvery: number) {
33+
constructor(destination: Subscriber<T>,
34+
private windowSize: number,
35+
private startWindowEvery: number) {
4036
super(destination);
37+
destination.next(this.windows[0]);
4138
}
4239

4340
_next(value: T) {
44-
const count = (this.count += 1);
4541
const startWindowEvery = (this.startWindowEvery > 0) ? this.startWindowEvery : this.windowSize;
4642
const windowSize = this.windowSize;
4743
const windows = this.windows;
4844
const len = windows.length;
4945

50-
if (count % startWindowEvery === 0) {
51-
let window = new Subject<T>();
52-
windows.push({ count: 0, notified : false, window : window });
53-
}
54-
5546
for (let i = 0; i < len; i++) {
56-
let w = windows[i];
57-
const window = w.window;
58-
59-
if (!w.notified) {
60-
w.notified = true;
61-
this.destination.next(window);
62-
}
63-
64-
window.next(value);
65-
if (windowSize === (w.count += 1)) {
66-
window.complete();
67-
}
47+
windows[i].next(value);
48+
}
49+
const c = this.count - windowSize + 1;
50+
if (c >= 0 && c % startWindowEvery === 0) {
51+
windows.shift().complete();
52+
}
53+
if (++this.count % startWindowEvery === 0) {
54+
let window = new Subject<T>();
55+
windows.push(window);
56+
this.destination.next(window);
6857
}
6958
}
7059

7160
_error(err: any) {
7261
const windows = this.windows;
7362
while (windows.length > 0) {
74-
windows.shift().window.error(err);
63+
windows.shift().error(err);
7564
}
7665
this.destination.error(err);
7766
}
7867

7968
_complete() {
8069
const windows = this.windows;
8170
while (windows.length > 0) {
82-
windows.shift().window.complete();
71+
windows.shift().complete();
8372
}
8473
this.destination.complete();
8574
}

0 commit comments

Comments
 (0)