Skip to content

Commit

Permalink
feat(bufferCount): add higher-order lettable version of bufferCount
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 13, 2017
1 parent d8ca9de commit 0ae2ed5
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 97 deletions.
100 changes: 3 additions & 97 deletions src/operator/bufferCount.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';

import { Observable } from '../Observable';
import { TeardownLogic } from '../Subscription';
import { bufferCount as higherOrder } from '../operators';

/**
* Buffers the source Observable values until the size hits the maximum
Expand Down Expand Up @@ -45,98 +44,5 @@ import { TeardownLogic } from '../Subscription';
* @owner Observable
*/
export function bufferCount<T>(this: Observable<T>, bufferSize: number, startBufferEvery: number = null): Observable<T[]> {
return this.lift(new BufferCountOperator<T>(bufferSize, startBufferEvery));
}

class BufferCountOperator<T> implements Operator<T, T[]> {
private subscriberClass: any;

constructor(private bufferSize: number, private startBufferEvery: number) {
if (!startBufferEvery || bufferSize === startBufferEvery) {
this.subscriberClass = BufferCountSubscriber;
} else {
this.subscriberClass = BufferSkipCountSubscriber;
}
}

call(subscriber: Subscriber<T[]>, source: any): TeardownLogic {
return source.subscribe(new this.subscriberClass(subscriber, this.bufferSize, this.startBufferEvery));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class BufferCountSubscriber<T> extends Subscriber<T> {
private buffer: T[] = [];

constructor(destination: Subscriber<T[]>, private bufferSize: number) {
super(destination);
}

protected _next(value: T): void {
const buffer = this.buffer;

buffer.push(value);

if (buffer.length == this.bufferSize) {
this.destination.next(buffer);
this.buffer = [];
}
}

protected _complete(): void {
const buffer = this.buffer;
if (buffer.length > 0) {
this.destination.next(buffer);
}
super._complete();
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class BufferSkipCountSubscriber<T> extends Subscriber<T> {
private buffers: Array<T[]> = [];
private count: number = 0;

constructor(destination: Subscriber<T[]>, private bufferSize: number, private startBufferEvery: number) {
super(destination);
}

protected _next(value: T): void {
const { bufferSize, startBufferEvery, buffers, count } = this;

this.count++;
if (count % startBufferEvery === 0) {
buffers.push([]);
}

for (let i = buffers.length; i--; ) {
const buffer = buffers[i];
buffer.push(value);
if (buffer.length === bufferSize) {
buffers.splice(i, 1);
this.destination.next(buffer);
}
}
}

protected _complete(): void {
const { buffers, destination } = this;

while (buffers.length > 0) {
let buffer = buffers.shift();
if (buffer.length > 0) {
destination.next(buffer);
}
}
super._complete();
}

return higherOrder(bufferSize, startBufferEvery)(this);
}
145 changes: 145 additions & 0 deletions src/operators/bufferCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { TeardownLogic } from '../Subscription';
import { OperatorFunction } from '../interfaces';

/**
* Buffers the source Observable values until the size hits the maximum
* `bufferSize` given.
*
* <span class="informal">Collects values from the past as an array, and emits
* that array only when its size reaches `bufferSize`.</span>
*
* <img src="./img/bufferCount.png" width="100%">
*
* Buffers a number of values from the source Observable by `bufferSize` then
* emits the buffer and clears it, and starts a new buffer each
* `startBufferEvery` values. If `startBufferEvery` is not provided or is
* `null`, then new buffers are started immediately at the start of the source
* and when each buffer closes and is emitted.
*
* @example <caption>Emit the last two click events as an array</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var buffered = clicks.bufferCount(2);
* buffered.subscribe(x => console.log(x));
*
* @example <caption>On every click, emit the last two click events as an array</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var buffered = clicks.bufferCount(2, 1);
* buffered.subscribe(x => console.log(x));
*
* @see {@link buffer}
* @see {@link bufferTime}
* @see {@link bufferToggle}
* @see {@link bufferWhen}
* @see {@link pairwise}
* @see {@link windowCount}
*
* @param {number} bufferSize The maximum size of the buffer emitted.
* @param {number} [startBufferEvery] Interval at which to start a new buffer.
* For example if `startBufferEvery` is `2`, then a new buffer will be started
* on every other value from the source. A new buffer is started at the
* beginning of the source by default.
* @return {Observable<T[]>} An Observable of arrays of buffered values.
* @method bufferCount
* @owner Observable
*/
export function bufferCount<T>(bufferSize: number, startBufferEvery: number = null): OperatorFunction<T, T[]> {
return function bufferCountOperatorFunction(source: Observable<T>) {
return source.lift(new BufferCountOperator<T>(bufferSize, startBufferEvery));
};
}

class BufferCountOperator<T> implements Operator<T, T[]> {
private subscriberClass: any;

constructor(private bufferSize: number, private startBufferEvery: number) {
if (!startBufferEvery || bufferSize === startBufferEvery) {
this.subscriberClass = BufferCountSubscriber;
} else {
this.subscriberClass = BufferSkipCountSubscriber;
}
}

call(subscriber: Subscriber<T[]>, source: any): TeardownLogic {
return source.subscribe(new this.subscriberClass(subscriber, this.bufferSize, this.startBufferEvery));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class BufferCountSubscriber<T> extends Subscriber<T> {
private buffer: T[] = [];

constructor(destination: Subscriber<T[]>, private bufferSize: number) {
super(destination);
}

protected _next(value: T): void {
const buffer = this.buffer;

buffer.push(value);

if (buffer.length == this.bufferSize) {
this.destination.next(buffer);
this.buffer = [];
}
}

protected _complete(): void {
const buffer = this.buffer;
if (buffer.length > 0) {
this.destination.next(buffer);
}
super._complete();
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class BufferSkipCountSubscriber<T> extends Subscriber<T> {
private buffers: Array<T[]> = [];
private count: number = 0;

constructor(destination: Subscriber<T[]>, private bufferSize: number, private startBufferEvery: number) {
super(destination);
}

protected _next(value: T): void {
const { bufferSize, startBufferEvery, buffers, count } = this;

this.count++;
if (count % startBufferEvery === 0) {
buffers.push([]);
}

for (let i = buffers.length; i--; ) {
const buffer = buffers[i];
buffer.push(value);
if (buffer.length === bufferSize) {
buffers.splice(i, 1);
this.destination.next(buffer);
}
}
}

protected _complete(): void {
const { buffers, destination } = this;

while (buffers.length > 0) {
let buffer = buffers.shift();
if (buffer.length > 0) {
destination.next(buffer);
}
}
super._complete();
}

}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { audit } from './audit';
export { auditTime } from './auditTime';
export { buffer } from './buffer';
export { bufferCount } from './bufferCount';
export { catchError } from './catchError';
export { concat } from './concat';
export { concatAll } from './concatAll';
Expand Down

0 comments on commit 0ae2ed5

Please sign in to comment.