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MAAM,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;QACtD,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC9C,CAAC;IACD,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC","sourcesContent":["import { IScheduler } from '../Scheduler';\nimport { Action } from '../scheduler/Action';\nimport { Subject } from '../Subject';\nimport { Operator } from '../Operator';\nimport { async } from '../scheduler/async';\nimport { Subscriber } from '../Subscriber';\nimport { Observable } from '../Observable';\nimport { Subscription } from '../Subscription';\nimport { isNumeric } from '../util/isNumeric';\nimport { isScheduler } from '../util/isScheduler';\nimport { OperatorFunction } from '../interfaces';\n\n/**\n * Branch out the source Observable values as a nested Observable periodically\n * in time.\n *\n * <span class=\"informal\">It's like {@link bufferTime}, but emits a nested\n * Observable instead of an array.</span>\n *\n * <img src=\"./img/windowTime.png\" width=\"100%\">\n *\n * Returns an Observable that emits windows of items it collects from the source\n * Observable. The output Observable starts a new window periodically, as\n * determined by the `windowCreationInterval` argument. It emits each window\n * after a fixed timespan, specified by the `windowTimeSpan` argument. When the\n * source Observable completes or encounters an error, the output Observable\n * emits the current window and propagates the notification from the source\n * Observable. If `windowCreationInterval` is not provided, the output\n * Observable starts a new window when the previous window of duration\n * `windowTimeSpan` completes. If `maxWindowCount` is provided, each window\n * will emit at most fixed number of values. Window will complete immediately\n * after emitting last value and next one still will open as specified by\n * `windowTimeSpan` and `windowCreationInterval` arguments.\n *\n * @example <caption>In every window of 1 second each, emit at most 2 click events</caption>\n * var clicks = Rx.Observable.fromEvent(document, 'click');\n * var result = clicks.windowTime(1000)\n * .map(win => win.take(2)) // each window has at most 2 emissions\n * .mergeAll(); // flatten the Observable-of-Observables\n * result.subscribe(x => console.log(x));\n *\n * @example <caption>Every 5 seconds start a window 1 second long, and emit at most 2 click events per window</caption>\n * var clicks = Rx.Observable.fromEvent(document, 'click');\n * var result = clicks.windowTime(1000, 5000)\n * .map(win => win.take(2)) // each window has at most 2 emissions\n * .mergeAll(); // flatten the Observable-of-Observables\n * result.subscribe(x => console.log(x));\n *\n * @example <caption>Same as example above but with maxWindowCount instead of take</caption>\n * var clicks = Rx.Observable.fromEvent(document, 'click');\n * var result = clicks.windowTime(1000, 5000, 2) // each window has still at most 2 emissions\n * .mergeAll(); // flatten the Observable-of-Observables\n * result.subscribe(x => console.log(x));\n\n * @see {@link window}\n * @see {@link windowCount}\n * @see {@link windowToggle}\n * @see {@link windowWhen}\n * @see {@link bufferTime}\n *\n * @param {number} windowTimeSpan The amount of time to fill each window.\n * @param {number} [windowCreationInterval] The interval at which to start new\n * windows.\n * @param {number} [maxWindowSize=Number.POSITIVE_INFINITY] Max number of\n * values each window can emit before completion.\n * @param {Scheduler} [scheduler=async] The scheduler on which to schedule the\n * intervals that determine window boundaries.\n * @return {Observable<Observable<T>>} An observable of windows, which in turn\n * are Observables.\n * @method windowTime\n * @owner Observable\n */\nexport function windowTime<T>(windowTimeSpan: number,\n scheduler?: IScheduler): OperatorFunction<T, Observable<T>>;\nexport function windowTime<T>(windowTimeSpan: number,\n windowCreationInterval: number,\n scheduler?: IScheduler): OperatorFunction<T, Observable<T>>;\nexport function windowTime<T>(windowTimeSpan: number,\n windowCreationInterval: number,\n maxWindowSize: number,\n scheduler?: IScheduler): OperatorFunction<T, Observable<T>>;\n\nexport function windowTime<T>(windowTimeSpan: number): OperatorFunction<T, Observable<T>> {\n let scheduler: IScheduler = async;\n let windowCreationInterval: number = null;\n let maxWindowSize: number = Number.POSITIVE_INFINITY;\n\n if (isScheduler(arguments[3])) {\n scheduler = arguments[3];\n }\n\n if (isScheduler(arguments[2])) {\n scheduler = arguments[2];\n } else if (isNumeric(arguments[2])) {\n maxWindowSize = arguments[2];\n }\n\n if (isScheduler(arguments[1])) {\n scheduler = arguments[1];\n } else if (isNumeric(arguments[1])) {\n windowCreationInterval = arguments[1];\n }\n\n return function windowTimeOperatorFunction(source: Observable<T>) {\n return source.lift(new WindowTimeOperator<T>(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler));\n };\n}\n\nclass WindowTimeOperator<T> implements Operator<T, Observable<T>> {\n\n constructor(private windowTimeSpan: number,\n private windowCreationInterval: number | null,\n private maxWindowSize: number,\n private scheduler: IScheduler) {\n }\n\n call(subscriber: Subscriber<Observable<T>>, source: any): any {\n return source.subscribe(new WindowTimeSubscriber(\n subscriber, this.windowTimeSpan, this.windowCreationInterval, this.maxWindowSize, this.scheduler\n ));\n }\n}\n\ninterface CreationState<T> {\n windowTimeSpan: number;\n windowCreationInterval: number;\n subscriber: WindowTimeSubscriber<T>;\n scheduler: IScheduler;\n}\n\ninterface TimeSpanOnlyState<T> {\n window: CountedSubject<T>;\n windowTimeSpan: number;\n subscriber: WindowTimeSubscriber<T>;\n }\n\ninterface CloseWindowContext<T> {\n action: Action<CreationState<T>>;\n subscription: Subscription;\n}\n\ninterface CloseState<T> {\n subscriber: WindowTimeSubscriber<T>;\n window: CountedSubject<T>;\n context: CloseWindowContext<T>;\n}\n\nclass CountedSubject<T> extends Subject<T> {\n private _numberOfNextedValues: number = 0;\n\n next(value?: T): void {\n this._numberOfNextedValues++;\n super.next(value);\n }\n\n get numberOfNextedValues(): number {\n return this._numberOfNextedValues;\n }\n}\n\n/**\n * We need this JSDoc comment for affecting ESDoc.\n * @ignore\n * @extends {Ignored}\n */\nclass WindowTimeSubscriber<T> extends Subscriber<T> {\n private windows: CountedSubject<T>[] = [];\n\n constructor(protected destination: Subscriber<Observable<T>>,\n private windowTimeSpan: number,\n private windowCreationInterval: number | null,\n private maxWindowSize: number,\n private scheduler: IScheduler) {\n super(destination);\n\n const window = this.openWindow();\n if (windowCreationInterval !== null && windowCreationInterval >= 0) {\n const closeState: CloseState<T> = { subscriber: this, window, context: <any>null };\n const creationState: CreationState<T> = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };\n this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));\n this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));\n } else {\n const timeSpanOnlyState: TimeSpanOnlyState<T> = { subscriber: this, window, windowTimeSpan };\n this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));\n }\n }\n\n protected _next(value: T): void {\n const windows = this.windows;\n const len = windows.length;\n for (let i = 0; i < len; i++) {\n const window = windows[i];\n if (!window.closed) {\n window.next(value);\n if (window.numberOfNextedValues >= this.maxWindowSize) {\n this.closeWindow(window);\n }\n }\n }\n }\n\n protected _error(err: any): void {\n const windows = this.windows;\n while (windows.length > 0) {\n windows.shift().error(err);\n }\n this.destination.error(err);\n }\n\n protected _complete(): void {\n const windows = this.windows;\n while (windows.length > 0) {\n const window = windows.shift();\n if (!window.closed) {\n window.complete();\n }\n }\n this.destination.complete();\n }\n\n public openWindow(): CountedSubject<T> {\n const window = new CountedSubject<T>();\n this.windows.push(window);\n const destination = this.destination;\n destination.next(window);\n return window;\n }\n\n public closeWindow(window: CountedSubject<T>): void {\n window.complete();\n const windows = this.windows;\n windows.splice(windows.indexOf(window), 1);\n }\n}\n\nfunction dispatchWindowTimeSpanOnly<T>(this: Action<TimeSpanOnlyState<T>>, state: TimeSpanOnlyState<T>): void {\n const { subscriber, windowTimeSpan, window } = state;\n if (window) {\n subscriber.closeWindow(window);\n }\n state.window = subscriber.openWindow();\n this.schedule(state, windowTimeSpan);\n}\n\nfunction dispatchWindowCreation<T>(this: Action<CreationState<T>>, state: CreationState<T>): void {\n const { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state;\n const window = subscriber.openWindow();\n const action = this;\n let context: CloseWindowContext<T> = { action, subscription: <any>null };\n const timeSpanState: CloseState<T> = { subscriber, window, context };\n context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);\n action.add(context.subscription);\n action.schedule(state, windowCreationInterval);\n}\n\nfunction dispatchWindowClose<T>(state: CloseState<T>): void {\n const { subscriber, window, context } = state;\n if (context && context.action && context.subscription) {\n context.action.remove(context.subscription);\n }\n subscriber.closeWindow(window);\n}\n"]}
|