Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there throttle in RxJava?

I'm trying to find an operator in RxJava that will throttle in a specific way:

  • Emits an element
  • For a set period after the element has been emitted, all other emitted elements are filtered out

I can't seem to find one that matches this behavior. I looked at some similar ones but none of them seem to be correct.

  • debounce/throttleWithTimeout - emits the last element in a sequence of elements that were emitted with a short period between them

  • sample/throttleLast which looks at set time intervals and emits the last element in each time interval.

  • throttleFirst which looks at set time intervals and emits the first element in each time interval. This seems to be the closest to what I want but isn't exactly the same.

Is there any RxJava operator that I can use that will match this? It seems to be a useful use case.

like image 317
Lee Kang Avatar asked May 23 '18 03:05

Lee Kang


People also ask

What is debounce in RxJava?

When developing Android applications with the support of RxJava, you may have used the debounce operator for your Search EditText. It is very helpful in avoiding the overhead of API calls for each user keystroke. However, in some cases, we don't want to debounce all of the emitted items.

How does RxJava chain work?

It changes the thread as many times as you write it. flatMap starts the chain only during root chain data emission. No actions are performed during the root stream subscription process. Operators interval/delay/timer subscribe to computation under the hood, by default.

How does RxJava work internally?

Event-Based: The program executes the codes based on the events generated while the program is running. For Example, a Button click triggers an event and then the program's event handler receives this event and does some work. Observable sequences: It will be better understood by the mechanics of it.

Is RxJava blocking?

Usually, asynchronous code is non-blocking: You call a method that returns immediately, allowing your code to continue its execution. Once the result of your call is available, it is returned via a callback. RxJava is asynchronous, too. And it is blocking as well — at least by default.


1 Answers

throttleFirst was what I was looking for. I didn't realize initially because most of the documentation on the internet is slightly off (http://reactivex.io/documentation/operators/sample.html , https://github.com/ReactiveX/RxJava/wiki/Filtering-Observables) . The Javadocs is correct and where I found what I was looking for.

like image 99
Lee Kang Avatar answered Jan 10 '23 05:01

Lee Kang