Yuriy Zhilovets
Yuriy Zhilovets

Reputation: 430

How to split a stream by pauses

I have a stream of similar events and want to split it by proximity in time: every event that follows the previous one in, let's say, less than 5 minutes, must go into one chain where I can mark then the beginning and end of this chain. All in real time, no bufferization.

Like this (- denotes pause less than 5 min, = denotes 5 min time span):

a - a - a = a - a - a =-- a - a - a
B           EB         E  B

Upvotes: 0

Views: 41

Answers (1)

paulpdaniels
paulpdaniels

Reputation: 18663

You can make use of windowWhen + timeoutWith operators to accomplish this:

let sharedSource = source.share();

sharedSource.windowWhen(() =>   
  sharedSource.timeoutWith(5 * 60 * 60, Observable.empty()).ignoreElements())
.subscribe(window => {
  window.subscribe(/*Handle items in the stream*/);
});

The above builds an Observable of Observables or windows, each window is made of elements that are at most 5 minutes apart.

Upvotes: 2

Related Questions