RxJS window() Transformation Operator
RxJS window() operator is a transformation operator that periodically subdivides the items from an Observable into Observable windows and emit these windows rather than emitting the items one at a time.
In other words, we can say that the RxJS window() operator branches out the source Observable values as a nested Observable whenever the windowBoundaries emits the values.
The RxJS window() operator is similar to RxJS buffer() operator, but rather than emitting packets of items from the source Observable (array), it emits nested Observables. Each emitted nested Observables emits a subset of items from the source Observable and then terminated when getting the onCompleted notification.
Syntax:
Following is the syntax of the window() operator:
or
Parameter Explanation
windowBoundaries: It specifies an Observable that completes the previous window and starts a new window.
Return value
The window() operator’s return value is observable of windows it collects from the source Observable. The output Observable emits connected non-overlapping windows. It emits the current window and opens a new one whenever the Observable windowBoundaries emits an item. The output will always be a higher-order Observable.
Let us see an example of window() operator to understand it clearly.
Example
Output:
After executing the above example, you will see the following result as array:
In the above example, you can see that we have a set a time interval of 1 second to emit values and after every 5 second, the window is changed automatically.