1 import { Operator } from '../Operator';
2 import { async } from '../scheduler/async';
3 import { Observable } from '../Observable';
4 import { Subscriber } from '../Subscriber';
5 import { Subscription } from '../Subscription';
6 import { isScheduler } from '../util/isScheduler';
7 import { OperatorFunction, SchedulerAction, SchedulerLike } from '../types';
9 /* tslint:disable:max-line-length */
10 export function bufferTime<T>(bufferTimeSpan: number, scheduler?: SchedulerLike): OperatorFunction<T, T[]>;
11 export function bufferTime<T>(bufferTimeSpan: number, bufferCreationInterval: number | null | undefined, scheduler?: SchedulerLike): OperatorFunction<T, T[]>;
12 export function bufferTime<T>(bufferTimeSpan: number, bufferCreationInterval: number | null | undefined, maxBufferSize: number, scheduler?: SchedulerLike): OperatorFunction<T, T[]>;
13 /* tslint:enable:max-line-length */
16 * Buffers the source Observable values for a specific time period.
18 * <span class="informal">Collects values from the past as an array, and emits
19 * those arrays periodically in time.</span>
23 * Buffers values from the source for a specific time duration `bufferTimeSpan`.
24 * Unless the optional argument `bufferCreationInterval` is given, it emits and
25 * resets the buffer every `bufferTimeSpan` milliseconds. If
26 * `bufferCreationInterval` is given, this operator opens the buffer every
27 * `bufferCreationInterval` milliseconds and closes (emits and resets) the
28 * buffer every `bufferTimeSpan` milliseconds. When the optional argument
29 * `maxBufferSize` is specified, the buffer will be closed either after
30 * `bufferTimeSpan` milliseconds or when it contains `maxBufferSize` elements.
34 * Every second, emit an array of the recent click events
37 * import { fromEvent } from 'rxjs';
38 * import { bufferTime } from 'rxjs/operators';
40 * const clicks = fromEvent(document, 'click');
41 * const buffered = clicks.pipe(bufferTime(1000));
42 * buffered.subscribe(x => console.log(x));
45 * Every 5 seconds, emit the click events from the next 2 seconds
48 * import { fromEvent } from 'rxjs';
49 * import { bufferTime } from 'rxjs/operators';
51 * const clicks = fromEvent(document, 'click');
52 * const buffered = clicks.pipe(bufferTime(2000, 5000));
53 * buffered.subscribe(x => console.log(x));
57 * @see {@link bufferCount}
58 * @see {@link bufferToggle}
59 * @see {@link bufferWhen}
60 * @see {@link windowTime}
62 * @param {number} bufferTimeSpan The amount of time to fill each buffer array.
63 * @param {number} [bufferCreationInterval] The interval at which to start new
65 * @param {number} [maxBufferSize] The maximum buffer size.
66 * @param {SchedulerLike} [scheduler=async] The scheduler on which to schedule the
67 * intervals that determine buffer boundaries.
68 * @return {Observable<T[]>} An observable of arrays of buffered values.
72 export function bufferTime<T>(bufferTimeSpan: number): OperatorFunction<T, T[]> {
73 let length: number = arguments.length;
75 let scheduler: SchedulerLike = async;
76 if (isScheduler(arguments[arguments.length - 1])) {
77 scheduler = arguments[arguments.length - 1];
81 let bufferCreationInterval: number = null;
83 bufferCreationInterval = arguments[1];
86 let maxBufferSize: number = Number.POSITIVE_INFINITY;
88 maxBufferSize = arguments[2];
91 return function bufferTimeOperatorFunction(source: Observable<T>) {
92 return source.lift(new BufferTimeOperator<T>(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler));
96 class BufferTimeOperator<T> implements Operator<T, T[]> {
97 constructor(private bufferTimeSpan: number,
98 private bufferCreationInterval: number,
99 private maxBufferSize: number,
100 private scheduler: SchedulerLike) {
103 call(subscriber: Subscriber<T[]>, source: any): any {
104 return source.subscribe(new BufferTimeSubscriber(
105 subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.maxBufferSize, this.scheduler
112 closeAction: Subscription;
115 interface DispatchCreateArg<T> {
116 bufferTimeSpan: number;
117 bufferCreationInterval: number;
118 subscriber: BufferTimeSubscriber<T>;
119 scheduler: SchedulerLike;
122 interface DispatchCloseArg<T> {
123 subscriber: BufferTimeSubscriber<T>;
128 * We need this JSDoc comment for affecting ESDoc.
132 class BufferTimeSubscriber<T> extends Subscriber<T> {
133 private contexts: Array<Context<T>> = [];
134 private timespanOnly: boolean;
136 constructor(destination: Subscriber<T[]>,
137 private bufferTimeSpan: number,
138 private bufferCreationInterval: number,
139 private maxBufferSize: number,
140 private scheduler: SchedulerLike) {
142 const context = this.openContext();
143 this.timespanOnly = bufferCreationInterval == null || bufferCreationInterval < 0;
144 if (this.timespanOnly) {
145 const timeSpanOnlyState = { subscriber: this, context, bufferTimeSpan };
146 this.add(context.closeAction = scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
148 const closeState = { subscriber: this, context };
149 const creationState: DispatchCreateArg<T> = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler };
150 this.add(context.closeAction = scheduler.schedule<DispatchCloseArg<T>>(dispatchBufferClose, bufferTimeSpan, closeState));
151 this.add(scheduler.schedule<DispatchCreateArg<T>>(dispatchBufferCreation, bufferCreationInterval, creationState));
155 protected _next(value: T) {
156 const contexts = this.contexts;
157 const len = contexts.length;
158 let filledBufferContext: Context<T>;
159 for (let i = 0; i < len; i++) {
160 const context = contexts[i];
161 const buffer = context.buffer;
163 if (buffer.length == this.maxBufferSize) {
164 filledBufferContext = context;
168 if (filledBufferContext) {
169 this.onBufferFull(filledBufferContext);
173 protected _error(err: any) {
174 this.contexts.length = 0;
178 protected _complete() {
179 const { contexts, destination } = this;
180 while (contexts.length > 0) {
181 const context = contexts.shift();
182 destination.next(context.buffer);
187 /** @deprecated This is an internal implementation detail, do not use. */
189 this.contexts = null;
192 protected onBufferFull(context: Context<T>) {
193 this.closeContext(context);
194 const closeAction = context.closeAction;
195 closeAction.unsubscribe();
196 this.remove(closeAction);
198 if (!this.closed && this.timespanOnly) {
199 context = this.openContext();
200 const bufferTimeSpan = this.bufferTimeSpan;
201 const timeSpanOnlyState = { subscriber: this, context, bufferTimeSpan };
202 this.add(context.closeAction = this.scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
206 openContext(): Context<T> {
207 const context: Context<T> = new Context<T>();
208 this.contexts.push(context);
212 closeContext(context: Context<T>) {
213 this.destination.next(context.buffer);
214 const contexts = this.contexts;
216 const spliceIndex = contexts ? contexts.indexOf(context) : -1;
217 if (spliceIndex >= 0) {
218 contexts.splice(contexts.indexOf(context), 1);
223 function dispatchBufferTimeSpanOnly(this: SchedulerAction<any>, state: any) {
224 const subscriber: BufferTimeSubscriber<any> = state.subscriber;
226 const prevContext = state.context;
228 subscriber.closeContext(prevContext);
231 if (!subscriber.closed) {
232 state.context = subscriber.openContext();
233 state.context.closeAction = this.schedule(state, state.bufferTimeSpan);
237 function dispatchBufferCreation<T>(this: SchedulerAction<DispatchCreateArg<T>>, state: DispatchCreateArg<T>) {
238 const { bufferCreationInterval, bufferTimeSpan, subscriber, scheduler } = state;
239 const context = subscriber.openContext();
240 const action = <SchedulerAction<DispatchCreateArg<T>>>this;
241 if (!subscriber.closed) {
242 subscriber.add(context.closeAction = scheduler.schedule<DispatchCloseArg<T>>(dispatchBufferClose, bufferTimeSpan, { subscriber, context }));
243 action.schedule(state, bufferCreationInterval);
247 function dispatchBufferClose<T>(arg: DispatchCloseArg<T>) {
248 const { subscriber, context } = arg;
249 subscriber.closeContext(context);