Skip to content

Commit

Permalink
fix(windowTime): Passing no creation interval will now properly open …
Browse files Browse the repository at this point in the history
…new window when old one closes

This was a long-broken bit of functionality, but windowTime was so little used, I chose to ignore it. With this change, we could make `bufferTime` based off of `windowTime` and `toArray`.
  • Loading branch information
benlesh committed Sep 13, 2020
1 parent 2dbc7ec commit cbd0ac0
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 205 deletions.
25 changes: 16 additions & 9 deletions spec/operators/windowTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { of, Observable } from 'rxjs';
import { observableMatcher } from '../helpers/observableMatcher';

/** @test {windowTime} */
describe('windowTime operator', () => {
describe('windowTime', () => {
let rxTestScheduler: TestScheduler;

beforeEach(() => {
Expand Down Expand Up @@ -32,19 +32,26 @@ describe('windowTime operator', () => {
});
});

// NOTE: This test and behavior were broken in 5.x and 6.x, to where
// Not passing a creationInterval would not cause new windows to open
// when old ones closed.
it('should close windows after max count is reached', () => {
rxTestScheduler.run(({ hot, time, cold, expectObservable, expectSubscriptions }) => {
const source = hot('--1--2--^--a--b--c--d--e--f--g-----|');
const subs = '^--------------------------!';
const timeSpan = time( '----------|');
// 10 frames 0---------1---------2------|
const expected = 'x---------y---------z------|';
const x = cold( '---a--(b|) ');
const y = cold( '--d--(e|) ');
const z = cold( '-g-----|');
const values = { x, y, z };

const result = source.pipe(windowTime(timeSpan, null as any, 2, rxTestScheduler));
// ----------|
// ----------|
// ----------|
// ---------
const expected = 'w-----x-----y-----z--------|';
const w = cold( '---a--(b|) ');
const x = cold( '---c--(d|) ');
const y = cold( '---e--(f|) ');
const z = cold( '---g-----|')
const values = { w, x, y, z };

const result = source.pipe(windowTime(timeSpan, null, 2, rxTestScheduler));

expectObservable(result).toBe(expected, values);
expectSubscriptions(source.subscriptions).toBe(subs);
Expand Down
5 changes: 2 additions & 3 deletions src/internal/operators/bufferTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export function bufferTime<T>(bufferTimeSpan: number, ...otherArgs: any[]): Oper
const bufferCreationInterval = (otherArgs[0] as number) ?? null;
const maxBufferSize = (otherArgs[1] as number) || Infinity;

return function bufferTimeOperatorFunction(source: Observable<T>) {
return lift(source, function (this: Subscriber<T[]>, source: Observable<T>) {
return (source: Observable<T>) =>
lift(source, function (this: Subscriber<T[]>, source: Observable<T>) {
const subscriber = this;
// The active buffers, their related subscriptions, and removal functions.
let bufferRecords: { buffer: T[]; subs: Subscription; remove: () => void }[] | null = [];
Expand Down Expand Up @@ -200,5 +200,4 @@ export function bufferTime<T>(bufferTimeSpan: number, ...otherArgs: any[]): Oper

source.subscribe(bufferTimeSubscriber);
});
};
}
Loading

0 comments on commit cbd0ac0

Please sign in to comment.