UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

249 lines (239 loc) 14.9 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">es6/operator/windowTime.js | RxJS API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <script data-ice="userScript" src="user/script/0-Rx.js"></script> <script data-ice="userScript" src="user/script/1-devtools-welcome.js"></script> <script data-ice="userScript" src="user/script/2-custom-manual-styles.js"></script> <script data-ice="userScript" src="user/script/3-decision-tree-widget.min.js"></script> <script data-ice="userScript" src="user/script/4-theme-toggler.js"></script> <link data-ice="userStyle" rel="stylesheet" href="user/css/0-main.css"> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a href="test.html" data-ice="testLink">Test</a> <a data-ice="repoURL" href="https://github.com/ReactiveX/RxJS" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AsyncSubject">AsyncSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BehaviorSubject">BehaviorSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Notification">Notification</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Observable">Observable</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ReplaySubject">ReplaySubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Scheduler">Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AnonymousSubject">AnonymousSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subject">Subject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-SubjectSubscriber">SubjectSubscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscriber">Subscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscription">Subscription</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Scheduler">Rx.Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Symbol">Rx.Symbol</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ConnectableObservable">ConnectableObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable/dom</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxError">AjaxError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxResponse">AjaxResponse</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxTimeoutError">AjaxTimeoutError</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">operator</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-GroupedObservable">GroupedObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">scheduler</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Action">Action</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">util</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ArgumentOutOfRangeError">ArgumentOutOfRangeError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-EmptyError">EmptyError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ObjectUnsubscribedError">ObjectUnsubscribedError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TimeoutError">TimeoutError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-UnsubscriptionError">UnsubscriptionError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-root">root</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">es6/operator/windowTime.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import { Subject } from &apos;../Subject&apos;; import { async } from &apos;../scheduler/async&apos;; import { Subscriber } from &apos;../Subscriber&apos;; /** * Branch out the source Observable values as a nested Observable periodically * in time. * * &lt;span class=&quot;informal&quot;&gt;It&apos;s like {@link bufferTime}, but emits a nested * Observable instead of an array.&lt;/span&gt; * * &lt;img src=&quot;./img/windowTime.png&quot; width=&quot;100%&quot;&gt; * * 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 &lt;caption&gt;In every window of 1 second each, emit at most 2 click events&lt;/caption&gt; * var clicks = Rx.Observable.fromEvent(document, &apos;click&apos;); * var result = clicks.windowTime(1000) * .map(win =&gt; win.take(2)) // each window has at most 2 emissions * .mergeAll(); // flatten the Observable-of-Observables * result.subscribe(x =&gt; console.log(x)); * * @example &lt;caption&gt;Every 5 seconds start a window 1 second long, and emit at most 2 click events per window&lt;/caption&gt; * var clicks = Rx.Observable.fromEvent(document, &apos;click&apos;); * var result = clicks.windowTime(1000, 5000) * .map(win =&gt; win.take(2)) // each window has at most 2 emissions * .mergeAll(); // flatten the Observable-of-Observables * result.subscribe(x =&gt; 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&lt;Observable&lt;T&gt;&gt;} An observable of windows, which in turn * are Observables. * @method windowTime * @owner Observable */ export function windowTime(windowTimeSpan, windowCreationInterval, scheduler) { if (windowCreationInterval === void 0) { windowCreationInterval = null; } if (scheduler === void 0) { scheduler = async; } return this.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler)); } var WindowTimeOperator = (function () { function WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler) { this.windowTimeSpan = windowTimeSpan; this.windowCreationInterval = windowCreationInterval; this.scheduler = scheduler; } WindowTimeOperator.prototype.call = function (subscriber, source) { return source._subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.scheduler)); }; return WindowTimeOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var WindowTimeSubscriber = (function (_super) { __extends(WindowTimeSubscriber, _super); function WindowTimeSubscriber(destination, windowTimeSpan, windowCreationInterval, scheduler) { _super.call(this, destination); this.destination = destination; this.windowTimeSpan = windowTimeSpan; this.windowCreationInterval = windowCreationInterval; this.scheduler = scheduler; this.windows = []; if (windowCreationInterval !== null &amp;&amp; windowCreationInterval &gt;= 0) { var window_1 = this.openWindow(); var closeState = { subscriber: this, window: window_1, context: null }; var creationState = { windowTimeSpan: windowTimeSpan, windowCreationInterval: windowCreationInterval, subscriber: this, scheduler: scheduler }; this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState)); this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState)); } else { var window_2 = this.openWindow(); var timeSpanOnlyState = { subscriber: this, window: window_2, windowTimeSpan: windowTimeSpan }; this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState)); } } WindowTimeSubscriber.prototype._next = function (value) { var windows = this.windows; var len = windows.length; for (var i = 0; i &lt; len; i++) { var window_3 = windows[i]; if (!window_3.closed) { window_3.next(value); } } }; WindowTimeSubscriber.prototype._error = function (err) { var windows = this.windows; while (windows.length &gt; 0) { windows.shift().error(err); } this.destination.error(err); }; WindowTimeSubscriber.prototype._complete = function () { var windows = this.windows; while (windows.length &gt; 0) { var window_4 = windows.shift(); if (!window_4.closed) { window_4.complete(); } } this.destination.complete(); }; WindowTimeSubscriber.prototype.openWindow = function () { var window = new Subject(); this.windows.push(window); var destination = this.destination; destination.next(window); return window; }; WindowTimeSubscriber.prototype.closeWindow = function (window) { window.complete(); var windows = this.windows; windows.splice(windows.indexOf(window), 1); }; return WindowTimeSubscriber; }(Subscriber)); function dispatchWindowTimeSpanOnly(state) { var subscriber = state.subscriber, windowTimeSpan = state.windowTimeSpan, window = state.window; if (window) { window.complete(); } state.window = subscriber.openWindow(); this.schedule(state, windowTimeSpan); } function dispatchWindowCreation(state) { var windowTimeSpan = state.windowTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler, windowCreationInterval = state.windowCreationInterval; var window = subscriber.openWindow(); var action = this; var context = { action: action, subscription: null }; var timeSpanState = { subscriber: subscriber, window: window, context: context }; context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState); action.add(context.subscription); action.schedule(state, windowCreationInterval); } function dispatchWindowClose(arg) { var subscriber = arg.subscriber, window = arg.window, context = arg.context; if (context &amp;&amp; context.action &amp;&amp; context.subscription) { context.action.remove(context.subscription); } subscriber.closeWindow(window); } //# sourceMappingURL=windowTime.js.map</code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.8)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>