joesan
joesan

Reputation: 15345

Usage of Monix Debounce Observable

I'm trying out some of the operations that I could do on the Observable from Monix. I came across this debounce operator and could not understand its behavior:

Observable.interval(5.seconds).debounce(2.seconds)

This one above just emits a Long every 5 seconds.

Observable.interval(2.seconds).debounce(5.seconds)

This one however does not emit anything at all. So what is the real purpose of the debounce operator and in which cases could I use it?

Upvotes: 1

Views: 161

Answers (1)

Lachlan
Lachlan

Reputation: 3784

The term debounce comes from mechanical relays. You can think of it as a frequency filter: o.debounce(5.seconds) filters out any events that are emitted more frequently than once every 5 seconds.

An example of where I've used it is where I expect to get a batch of similar events in rapid succession, and my response to each event is the same. By debouncing I can reduce the amount of work I need to do by making the batch look like just one event.

It isn't useful in situations like your examples where the input frequency is constant, as the only possibilities are that it does nothing or it filters out everything.

Upvotes: 2

Related Questions