UNPKG

1.76 kBPlain TextView Raw
1
2import { Observable } from 'rxjs';
3import { window as higherOrder } from 'rxjs/operators';
4
5/**
6 * Branch out the source Observable values as a nested Observable whenever
7 * `windowBoundaries` emits.
8 *
9 * <span class="informal">It's like {@link buffer}, but emits a nested Observable
10 * instead of an array.</span>
11 *
12 * <img src="./img/window.png" width="100%">
13 *
14 * Returns an Observable that emits windows of items it collects from the source
15 * Observable. The output Observable emits connected, non-overlapping
16 * windows. It emits the current window and opens a new one whenever the
17 * Observable `windowBoundaries` emits an item. Because each window is an
18 * Observable, the output is a higher-order Observable.
19 *
20 * @example <caption>In every window of 1 second each, emit at most 2 click events</caption>
21 * var clicks = Rx.Observable.fromEvent(document, 'click');
22 * var interval = Rx.Observable.interval(1000);
23 * var result = clicks.window(interval)
24 * .map(win => win.take(2)) // each window has at most 2 emissions
25 * .mergeAll(); // flatten the Observable-of-Observables
26 * result.subscribe(x => console.log(x));
27 *
28 * @see {@link windowCount}
29 * @see {@link windowTime}
30 * @see {@link windowToggle}
31 * @see {@link windowWhen}
32 * @see {@link buffer}
33 *
34 * @param {Observable<any>} windowBoundaries An Observable that completes the
35 * previous window and starts a new window.
36 * @return {Observable<Observable<T>>} An Observable of windows, which are
37 * Observables emitting values of the source Observable.
38 * @method window
39 * @owner Observable
40 */
41export function window<T>(this: Observable<T>, windowBoundaries: Observable<any>): Observable<Observable<T>> {
42 return higherOrder(windowBoundaries)(this) as Observable<Observable<T>>;
43}