Opal-Estate-Pro/node_modules/rxjs/_esm2015/operators/bufferCount.js

129 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-09-13 06:27:52 +02:00
import { Subscriber } from '../Subscriber';
/**
* 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(bufferSize, startBufferEvery = null) {
return function bufferCountOperatorFunction(source) {
return source.lift(new BufferCountOperator(bufferSize, startBufferEvery));
};
}
class BufferCountOperator {
constructor(bufferSize, startBufferEvery) {
this.bufferSize = bufferSize;
this.startBufferEvery = startBufferEvery;
if (!startBufferEvery || bufferSize === startBufferEvery) {
this.subscriberClass = BufferCountSubscriber;
}
else {
this.subscriberClass = BufferSkipCountSubscriber;
}
}
call(subscriber, source) {
return source.subscribe(new this.subscriberClass(subscriber, this.bufferSize, this.startBufferEvery));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class BufferCountSubscriber extends Subscriber {
constructor(destination, bufferSize) {
super(destination);
this.bufferSize = bufferSize;
this.buffer = [];
}
_next(value) {
const buffer = this.buffer;
buffer.push(value);
if (buffer.length == this.bufferSize) {
this.destination.next(buffer);
this.buffer = [];
}
}
_complete() {
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 extends Subscriber {
constructor(destination, bufferSize, startBufferEvery) {
super(destination);
this.bufferSize = bufferSize;
this.startBufferEvery = startBufferEvery;
this.buffers = [];
this.count = 0;
}
_next(value) {
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);
}
}
}
_complete() {
const { buffers, destination } = this;
while (buffers.length > 0) {
let buffer = buffers.shift();
if (buffer.length > 0) {
destination.next(buffer);
}
}
super._complete();
}
}
//# sourceMappingURL=bufferCount.js.map