Learn RxJS
Search…
windowWhen

signature: windowWhen(closingSelector: function(): Observable): Observable

Close window at provided time frame emitting observable of collected values from source.

Examples

Example 1: Open and close window at interval
1
// RxJS v6+
2
import { interval, timer } from 'rxjs';
3
import { windowWhen, tap, mergeAll } from 'rxjs/operators';
4
5
//emit immediately then every 1s
6
const source = timer(0, 1000);
7
const example = source.pipe(
8
//close window every 5s and emit observable of collected values from source
9
windowWhen(() => interval(5000)),
10
tap(_ => console.log('NEW WINDOW!'))
11
);
12
13
const subscribeTwo = example
14
.pipe(
15
//window emits nested observable
16
mergeAll()
17
/*
18
output:
19
"NEW WINDOW!"
20
0
21
1
22
2
23
3
24
4
25
"NEW WINDOW!"
26
5
27
6
28
7
29
8
30
9
31
*/
32
)
33
.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 2yr ago