import { Scheduler } from '../Scheduler';
import { Action } from '../scheduler/Action';
import { Subject } from '../Subject';
import { Operator } from '../Operator';
import { async } from '../scheduler/async';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { Subscription } from '../Subscription';
/**
* Branch out the source Observable values as a nested Observable periodically
* in time.
*
* It's like {@link bufferTime}, but emits a nested
* Observable instead of an array.
*
*
*
* Returns an Observable that emits windows of items it collects from the source
* Observable. The output Observable starts a new window periodically, as
* determined by the `windowCreationInterval` argument. It emits each window
* after a fixed timespan, specified by the `windowTimeSpan` argument. When the
* source Observable completes or encounters an error, the output Observable
* emits the current window and propagates the notification from the source
* Observable. If `windowCreationInterval` is not provided, the output
* Observable starts a new window when the previous window of duration
* `windowTimeSpan` completes.
*
* @example
In every window of 1 second each, emit at most 2 click events
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.windowTime(1000)
* .map(win => win.take(2)) // each window has at most 2 emissions
* .mergeAll(); // flatten the Observable-of-Observables
* result.subscribe(x => console.log(x));
*
* @example Every 5 seconds start a window 1 second long, and emit at most 2 click events per window
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.windowTime(1000, 5000)
* .map(win => win.take(2)) // each window has at most 2 emissions
* .mergeAll(); // flatten the Observable-of-Observables
* result.subscribe(x => console.log(x));
*
* @see {@link window}
* @see {@link windowCount}
* @see {@link windowToggle}
* @see {@link windowWhen}
* @see {@link bufferTime}
*
* @param {number} windowTimeSpan The amount of time to fill each window.
* @param {number} [windowCreationInterval] The interval at which to start new
* windows.
* @param {Scheduler} [scheduler=async] The scheduler on which to schedule the
* intervals that determine window boundaries.
* @return {Observable>} An observable of windows, which in turn
* are Observables.
* @method windowTime
* @owner Observable
*/
export function windowTime(this: Observable, windowTimeSpan: number,
windowCreationInterval: number = null,
scheduler: Scheduler = async): Observable> {
return this.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler));
}
class WindowTimeOperator implements Operator> {
constructor(private windowTimeSpan: number,
private windowCreationInterval: number,
private scheduler: Scheduler) {
}
call(subscriber: Subscriber>, source: any): any {
return source.subscribe(new WindowTimeSubscriber(
subscriber, this.windowTimeSpan, this.windowCreationInterval, this.scheduler
));
}
}
interface CreationState {
windowTimeSpan: number;
windowCreationInterval: number;
subscriber: WindowTimeSubscriber;
scheduler: Scheduler;
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class WindowTimeSubscriber extends Subscriber {
private windows: Subject[] = [];
constructor(protected destination: Subscriber>,
private windowTimeSpan: number,
private windowCreationInterval: number,
private scheduler: Scheduler) {
super(destination);
if (windowCreationInterval !== null && windowCreationInterval >= 0) {
let window = this.openWindow();
const closeState = { subscriber: this, window, context: null };
const creationState: CreationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };
this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
} else {
let window = this.openWindow();
const timeSpanOnlyState = { subscriber: this, window, windowTimeSpan };
this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
}
}
protected _next(value: T) {
const windows = this.windows;
const len = windows.length;
for (let i = 0; i < len; i++) {
const window = windows[i];
if (!window.closed) {
window.next(value);
}
}
}
protected _error(err: any) {
const windows = this.windows;
while (windows.length > 0) {
windows.shift().error(err);
}
this.destination.error(err);
}
protected _complete() {
const windows = this.windows;
while (windows.length > 0) {
const window = windows.shift();
if (!window.closed) {
window.complete();
}
}
this.destination.complete();
}
openWindow(): Subject {
const window = new Subject();
this.windows.push(window);
const destination = this.destination;
destination.next(window);
return window;
}
closeWindow(window: Subject) {
window.complete();
const windows = this.windows;
windows.splice(windows.indexOf(window), 1);
}
}
interface TimeSpanOnlyState {
window: Subject;
windowTimeSpan: number;
subscriber: WindowTimeSubscriber;
}
function dispatchWindowTimeSpanOnly(this: Action>, state: TimeSpanOnlyState) {
const { subscriber, windowTimeSpan, window } = state;
if (window) {
window.complete();
}
state.window = subscriber.openWindow();
this.schedule(state, windowTimeSpan);
}
interface Context {
action: Action>;
subscription: Subscription;
}
interface DispatchArg {
subscriber: WindowTimeSubscriber;
window: Subject;
context: Context;
}
function dispatchWindowCreation(this: Action>, state: CreationState) {
let { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state;
let window = subscriber.openWindow();
let action = this;
let context: Context = { action, subscription: null };
const timeSpanState: DispatchArg = { subscriber, window, context };
context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
action.add(context.subscription);
action.schedule(state, windowCreationInterval);
}
function dispatchWindowClose(arg: DispatchArg) {
const { subscriber, window, context } = arg;
if (context && context.action && context.subscription) {
context.action.remove(context.subscription);
}
subscriber.closeWindow(window);
}