1 import { async } from '../scheduler/async';
2 import { Subscriber } from '../Subscriber';
3 import { isScheduler } from '../util/isScheduler';
4 export function bufferTime(bufferTimeSpan) {
5 let length = arguments.length;
7 if (isScheduler(arguments[arguments.length - 1])) {
8 scheduler = arguments[arguments.length - 1];
11 let bufferCreationInterval = null;
13 bufferCreationInterval = arguments[1];
15 let maxBufferSize = Number.POSITIVE_INFINITY;
17 maxBufferSize = arguments[2];
19 return function bufferTimeOperatorFunction(source) {
20 return source.lift(new BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler));
23 class BufferTimeOperator {
24 constructor(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
25 this.bufferTimeSpan = bufferTimeSpan;
26 this.bufferCreationInterval = bufferCreationInterval;
27 this.maxBufferSize = maxBufferSize;
28 this.scheduler = scheduler;
30 call(subscriber, source) {
31 return source.subscribe(new BufferTimeSubscriber(subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.maxBufferSize, this.scheduler));
39 class BufferTimeSubscriber extends Subscriber {
40 constructor(destination, bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
42 this.bufferTimeSpan = bufferTimeSpan;
43 this.bufferCreationInterval = bufferCreationInterval;
44 this.maxBufferSize = maxBufferSize;
45 this.scheduler = scheduler;
47 const context = this.openContext();
48 this.timespanOnly = bufferCreationInterval == null || bufferCreationInterval < 0;
49 if (this.timespanOnly) {
50 const timeSpanOnlyState = { subscriber: this, context, bufferTimeSpan };
51 this.add(context.closeAction = scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
54 const closeState = { subscriber: this, context };
55 const creationState = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler };
56 this.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
57 this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
61 const contexts = this.contexts;
62 const len = contexts.length;
63 let filledBufferContext;
64 for (let i = 0; i < len; i++) {
65 const context = contexts[i];
66 const buffer = context.buffer;
68 if (buffer.length == this.maxBufferSize) {
69 filledBufferContext = context;
72 if (filledBufferContext) {
73 this.onBufferFull(filledBufferContext);
77 this.contexts.length = 0;
81 const { contexts, destination } = this;
82 while (contexts.length > 0) {
83 const context = contexts.shift();
84 destination.next(context.buffer);
91 onBufferFull(context) {
92 this.closeContext(context);
93 const closeAction = context.closeAction;
94 closeAction.unsubscribe();
95 this.remove(closeAction);
96 if (!this.closed && this.timespanOnly) {
97 context = this.openContext();
98 const bufferTimeSpan = this.bufferTimeSpan;
99 const timeSpanOnlyState = { subscriber: this, context, bufferTimeSpan };
100 this.add(context.closeAction = this.scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
104 const context = new Context();
105 this.contexts.push(context);
108 closeContext(context) {
109 this.destination.next(context.buffer);
110 const contexts = this.contexts;
111 const spliceIndex = contexts ? contexts.indexOf(context) : -1;
112 if (spliceIndex >= 0) {
113 contexts.splice(contexts.indexOf(context), 1);
117 function dispatchBufferTimeSpanOnly(state) {
118 const subscriber = state.subscriber;
119 const prevContext = state.context;
121 subscriber.closeContext(prevContext);
123 if (!subscriber.closed) {
124 state.context = subscriber.openContext();
125 state.context.closeAction = this.schedule(state, state.bufferTimeSpan);
128 function dispatchBufferCreation(state) {
129 const { bufferCreationInterval, bufferTimeSpan, subscriber, scheduler } = state;
130 const context = subscriber.openContext();
132 if (!subscriber.closed) {
133 subscriber.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber, context }));
134 action.schedule(state, bufferCreationInterval);
137 function dispatchBufferClose(arg) {
138 const { subscriber, context } = arg;
139 subscriber.closeContext(context);
141 //# sourceMappingURL=bufferTime.js.map