SigGP
SigGP

Reputation: 786

How to take only last emitted value for each second

The use case is that I've got an observable that emits a value with a very high frequency. I use the value to update the view, so I'd like to subscribe to the observable and take a last emitted value each second (to update the view only once per second) - so to debounce the stream for a second, take latest value, debounce for the next second, take next latest value, and so on.

Upvotes: 1

Views: 640

Answers (1)

Fabian Strathaus
Fabian Strathaus

Reputation: 3580

You can use throttleTime operator (see https://www.learnrxjs.io/learn-rxjs/operators/filtering/throttletime):

// emits 10 times per second
const source = interval(100);

// emits once every second
const example = source.pipe(throttleTime(1000));

edit after feedback in comment

const example = merge(
  source.pipe(
    throttleTime(1000)
  ), 
  source.pipe(
    last()
  )
);

Upvotes: 2

Related Questions