149 lines
6.3 KiB
JavaScript
149 lines
6.3 KiB
JavaScript
|
/** PURE_IMPORTS_START tslib,_scheduler_async,_Subscriber,_util_isScheduler PURE_IMPORTS_END */
|
||
|
import * as tslib_1 from "tslib";
|
||
|
import { async } from '../scheduler/async';
|
||
|
import { Subscriber } from '../Subscriber';
|
||
|
import { isScheduler } from '../util/isScheduler';
|
||
|
export function bufferTime(bufferTimeSpan) {
|
||
|
var length = arguments.length;
|
||
|
var scheduler = async;
|
||
|
if (isScheduler(arguments[arguments.length - 1])) {
|
||
|
scheduler = arguments[arguments.length - 1];
|
||
|
length--;
|
||
|
}
|
||
|
var bufferCreationInterval = null;
|
||
|
if (length >= 2) {
|
||
|
bufferCreationInterval = arguments[1];
|
||
|
}
|
||
|
var maxBufferSize = Number.POSITIVE_INFINITY;
|
||
|
if (length >= 3) {
|
||
|
maxBufferSize = arguments[2];
|
||
|
}
|
||
|
return function bufferTimeOperatorFunction(source) {
|
||
|
return source.lift(new BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler));
|
||
|
};
|
||
|
}
|
||
|
var BufferTimeOperator = /*@__PURE__*/ (function () {
|
||
|
function BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
|
||
|
this.bufferTimeSpan = bufferTimeSpan;
|
||
|
this.bufferCreationInterval = bufferCreationInterval;
|
||
|
this.maxBufferSize = maxBufferSize;
|
||
|
this.scheduler = scheduler;
|
||
|
}
|
||
|
BufferTimeOperator.prototype.call = function (subscriber, source) {
|
||
|
return source.subscribe(new BufferTimeSubscriber(subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.maxBufferSize, this.scheduler));
|
||
|
};
|
||
|
return BufferTimeOperator;
|
||
|
}());
|
||
|
var Context = /*@__PURE__*/ (function () {
|
||
|
function Context() {
|
||
|
this.buffer = [];
|
||
|
}
|
||
|
return Context;
|
||
|
}());
|
||
|
var BufferTimeSubscriber = /*@__PURE__*/ (function (_super) {
|
||
|
tslib_1.__extends(BufferTimeSubscriber, _super);
|
||
|
function BufferTimeSubscriber(destination, bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
|
||
|
var _this = _super.call(this, destination) || this;
|
||
|
_this.bufferTimeSpan = bufferTimeSpan;
|
||
|
_this.bufferCreationInterval = bufferCreationInterval;
|
||
|
_this.maxBufferSize = maxBufferSize;
|
||
|
_this.scheduler = scheduler;
|
||
|
_this.contexts = [];
|
||
|
var context = _this.openContext();
|
||
|
_this.timespanOnly = bufferCreationInterval == null || bufferCreationInterval < 0;
|
||
|
if (_this.timespanOnly) {
|
||
|
var timeSpanOnlyState = { subscriber: _this, context: context, bufferTimeSpan: bufferTimeSpan };
|
||
|
_this.add(context.closeAction = scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
|
||
|
}
|
||
|
else {
|
||
|
var closeState = { subscriber: _this, context: context };
|
||
|
var creationState = { bufferTimeSpan: bufferTimeSpan, bufferCreationInterval: bufferCreationInterval, subscriber: _this, scheduler: scheduler };
|
||
|
_this.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
|
||
|
_this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
|
||
|
}
|
||
|
return _this;
|
||
|
}
|
||
|
BufferTimeSubscriber.prototype._next = function (value) {
|
||
|
var contexts = this.contexts;
|
||
|
var len = contexts.length;
|
||
|
var filledBufferContext;
|
||
|
for (var i = 0; i < len; i++) {
|
||
|
var context_1 = contexts[i];
|
||
|
var buffer = context_1.buffer;
|
||
|
buffer.push(value);
|
||
|
if (buffer.length == this.maxBufferSize) {
|
||
|
filledBufferContext = context_1;
|
||
|
}
|
||
|
}
|
||
|
if (filledBufferContext) {
|
||
|
this.onBufferFull(filledBufferContext);
|
||
|
}
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype._error = function (err) {
|
||
|
this.contexts.length = 0;
|
||
|
_super.prototype._error.call(this, err);
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype._complete = function () {
|
||
|
var _a = this, contexts = _a.contexts, destination = _a.destination;
|
||
|
while (contexts.length > 0) {
|
||
|
var context_2 = contexts.shift();
|
||
|
destination.next(context_2.buffer);
|
||
|
}
|
||
|
_super.prototype._complete.call(this);
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype._unsubscribe = function () {
|
||
|
this.contexts = null;
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype.onBufferFull = function (context) {
|
||
|
this.closeContext(context);
|
||
|
var closeAction = context.closeAction;
|
||
|
closeAction.unsubscribe();
|
||
|
this.remove(closeAction);
|
||
|
if (!this.closed && this.timespanOnly) {
|
||
|
context = this.openContext();
|
||
|
var bufferTimeSpan = this.bufferTimeSpan;
|
||
|
var timeSpanOnlyState = { subscriber: this, context: context, bufferTimeSpan: bufferTimeSpan };
|
||
|
this.add(context.closeAction = this.scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
|
||
|
}
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype.openContext = function () {
|
||
|
var context = new Context();
|
||
|
this.contexts.push(context);
|
||
|
return context;
|
||
|
};
|
||
|
BufferTimeSubscriber.prototype.closeContext = function (context) {
|
||
|
this.destination.next(context.buffer);
|
||
|
var contexts = this.contexts;
|
||
|
var spliceIndex = contexts ? contexts.indexOf(context) : -1;
|
||
|
if (spliceIndex >= 0) {
|
||
|
contexts.splice(contexts.indexOf(context), 1);
|
||
|
}
|
||
|
};
|
||
|
return BufferTimeSubscriber;
|
||
|
}(Subscriber));
|
||
|
function dispatchBufferTimeSpanOnly(state) {
|
||
|
var subscriber = state.subscriber;
|
||
|
var prevContext = state.context;
|
||
|
if (prevContext) {
|
||
|
subscriber.closeContext(prevContext);
|
||
|
}
|
||
|
if (!subscriber.closed) {
|
||
|
state.context = subscriber.openContext();
|
||
|
state.context.closeAction = this.schedule(state, state.bufferTimeSpan);
|
||
|
}
|
||
|
}
|
||
|
function dispatchBufferCreation(state) {
|
||
|
var bufferCreationInterval = state.bufferCreationInterval, bufferTimeSpan = state.bufferTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler;
|
||
|
var context = subscriber.openContext();
|
||
|
var action = this;
|
||
|
if (!subscriber.closed) {
|
||
|
subscriber.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber: subscriber, context: context }));
|
||
|
action.schedule(state, bufferCreationInterval);
|
||
|
}
|
||
|
}
|
||
|
function dispatchBufferClose(arg) {
|
||
|
var subscriber = arg.subscriber, context = arg.context;
|
||
|
subscriber.closeContext(context);
|
||
|
}
|
||
|
//# sourceMappingURL=bufferTime.js.map
|