windowTime

function stable

Branch out the source Observable values as a nested Observable periodically in time.

windowTime<T>(windowTimeSpan: number, ...otherArgs: any[]): OperatorFunction<T, Observable<T>>

Parameters

windowTimeSpan

The amount of time, in milliseconds, to fill each window.

otherArgs

Type: any[].

Returns

OperatorFunction<T, Observable<T>>: A function that returns an Observable of windows, which in turn are Observables.

Description

It's like bufferTime, but emits a nested Observable instead of an array.

windowTime marble diagram

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. If maxWindowCount is provided, each window will emit at most fixed number of values. Window will complete immediately after emitting last value and next one still will open as specified by windowTimeSpan and windowCreationInterval arguments.

Examples

In every window of 1 second each, emit at most 2 click events

import { fromEvent } from 'rxjs';
import { windowTime, map, mergeAll, take } from 'rxjs/operators';

const clicks = fromEvent(document, 'click');
const result = clicks.pipe(
  windowTime(1000),
  map(win => win.pipe(take(2))), // each window has at most 2 emissions
  mergeAll(),                    // flatten the Observable-of-Observables
);
result.subscribe(x => console.log(x));

Every 5 seconds start a window 1 second long, and emit at most 2 click events per window

import { fromEvent } from 'rxjs';
import { windowTime, map, mergeAll, take } from 'rxjs/operators';

const clicks = fromEvent(document, 'click');
const result = clicks.pipe(
  windowTime(1000, 5000),
  map(win => win.pipe(take(2))), // each window has at most 2 emissions
  mergeAll(),                    // flatten the Observable-of-Observables
);
result.subscribe(x => console.log(x));

Same as example above but with maxWindowCount instead of take

import { fromEvent } from 'rxjs';
import { windowTime, mergeAll } from 'rxjs/operators';

const clicks = fromEvent(document, 'click');
const result = clicks.pipe(
  windowTime(1000, 5000, 2), // each window has still at most 2 emissions
  mergeAll(),                // flatten the Observable-of-Observables
);
result.subscribe(x => console.log(x));

Overloads

windowTime(windowTimeSpan: number, scheduler?: SchedulerLike): OperatorFunction<T, Observable<T>>

Parameters

windowTimeSpan

Type: number.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, Observable<T>>

windowTime(windowTimeSpan: number, windowCreationInterval: number, scheduler?: SchedulerLike): OperatorFunction<T, Observable<T>>

Parameters

windowTimeSpan

Type: number.

windowCreationInterval

Type: number.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, Observable<T>>

windowTime(windowTimeSpan: number, windowCreationInterval: number | void, maxWindowSize: number, scheduler?: SchedulerLike): OperatorFunction<T, Observable<T>>

Parameters

windowTimeSpan

Type: number.

windowCreationInterval

Type: number | void.

maxWindowSize

Type: number.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, Observable<T>>

See Also

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/windowTime