| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { OperatorFunction, ObservableInput } from '../types'; |
| 3 |
import { Subject } from '../Subject'; |
| 4 |
import { operate } from '../util/lift'; |
| 5 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 6 |
import { noop } from '../util/noop'; |
| 7 |
import { innerFrom } from '../observable/innerFrom'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Branch out the source Observable values as a nested Observable whenever |
| 11 |
* `windowBoundaries` emits. |
| 12 |
* |
| 13 |
* <span class="informal">It's like {@link buffer}, but emits a nested Observable |
| 14 |
* instead of an array.</span> |
| 15 |
* |
| 16 |
*  |
| 17 |
* |
| 18 |
* Returns an Observable that emits windows of items it collects from the source |
| 19 |
* Observable. The output Observable emits connected, non-overlapping |
| 20 |
* windows. It emits the current window and opens a new one whenever the |
| 21 |
* `windowBoundaries` emits an item. `windowBoundaries` can be any type that |
| 22 |
* `ObservableInput` accepts. It internally gets converted to an Observable. |
| 23 |
* Because each window is an Observable, the output is a higher-order Observable. |
| 24 |
* |
| 25 |
* ## Example |
| 26 |
* |
| 27 |
* In every window of 1 second each, emit at most 2 click events |
| 28 |
* |
| 29 |
* ```ts |
| 30 |
* import { fromEvent, interval, window, map, take, mergeAll } from 'rxjs'; |
| 31 |
* |
| 32 |
* const clicks = fromEvent(document, 'click'); |
| 33 |
* const sec = interval(1000); |
| 34 |
* const result = clicks.pipe( |
| 35 |
* window(sec), |
| 36 |
* map(win => win.pipe(take(2))), // take at most 2 emissions from each window |
| 37 |
* mergeAll() // flatten the Observable-of-Observables |
| 38 |
* ); |
| 39 |
* result.subscribe(x => console.log(x)); |
| 40 |
* ``` |
| 41 |
* |
| 42 |
* @see {@link windowCount} |
| 43 |
* @see {@link windowTime} |
| 44 |
* @see {@link windowToggle} |
| 45 |
* @see {@link windowWhen} |
| 46 |
* @see {@link buffer} |
| 47 |
* |
| 48 |
* @param windowBoundaries An `ObservableInput` that completes the |
| 49 |
* previous window and starts a new window. |
| 50 |
* @return A function that returns an Observable of windows, which are |
| 51 |
* Observables emitting values of the source Observable. |
| 52 |
*/ |
| 53 |
export function window<T>(windowBoundaries: ObservableInput<any>): OperatorFunction<T, Observable<T>> { |
| 54 |
return operate((source, subscriber) => { |
| 55 |
let windowSubject: Subject<T> = new Subject<T>(); |
| 56 |
|
| 57 |
subscriber.next(windowSubject.asObservable()); |
| 58 |
|
| 59 |
const errorHandler = (err: any) => { |
| 60 |
windowSubject.error(err); |
| 61 |
subscriber.error(err); |
| 62 |
}; |
| 63 |
|
| 64 |
// Subscribe to our source |
| 65 |
source.subscribe( |
| 66 |
createOperatorSubscriber( |
| 67 |
subscriber, |
| 68 |
(value) => windowSubject?.next(value), |
| 69 |
() => { |
| 70 |
windowSubject.complete(); |
| 71 |
subscriber.complete(); |
| 72 |
}, |
| 73 |
errorHandler |
| 74 |
) |
| 75 |
); |
| 76 |
|
| 77 |
// Subscribe to the window boundaries. |
| 78 |
innerFrom(windowBoundaries).subscribe( |
| 79 |
createOperatorSubscriber( |
| 80 |
subscriber, |
| 81 |
() => { |
| 82 |
windowSubject.complete(); |
| 83 |
subscriber.next((windowSubject = new Subject())); |
| 84 |
}, |
| 85 |
noop, |
| 86 |
errorHandler |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
return () => { |
| 91 |
// Unsubscribing the subject ensures that anyone who has captured |
| 92 |
// a reference to this window that tries to use it after it can |
| 93 |
// no longer get values from the source will get an ObjectUnsubscribedError. |
| 94 |
windowSubject?.unsubscribe(); |
| 95 |
windowSubject = null!; |
| 96 |
}; |
| 97 |
}); |
| 98 |
} |