How to drop some object (temporary disconnect) when there is pending operation.
See original GitHub issueHello I have a question. Sometimes I want to send an http request when a button is pressed but I don’t want to send too much http request I just want to drop any button press emission when there is a pending http request.
I attempt with a couple solution with some flaw.
Attempt 1 Solution: use onBackpressureDrop and use reactive pull Subscriber as parameter to subscribe(). Issue: This solution require some code implementation in subscriber instance the problem is onBackpressureDrop and subscriber code is too far away from each other (different file). Make it hard to reasoning why the Subscriber instance implement the way it is. This approach will be perfect if I can move reactive pull up the chain. e.g.
Observable buttonPress;
Func1 sendHttpRequest;
buttonPress.onBackpressureDrop().flatMap(sendHttpRequest).[ *** request(1) Operation *** ]
Basically the same as reactive pull but instead of doing it in subscriber do it with some operator inside transformation chain. I simply don’t know such method (or operator) if you know how can I archive the same effect please tell me.
Attempt 2 Solution: use ConnectableObservable to temporary disconnect flatMap operation from source. Issue: So imparative and the actual code looks bad break the purpose of using reactive paradigm. I might did it the wrong way if you have an example to bring this idea into practice I will be very glad to see your example.
If you think both of my attempt are not even close to simple implementation available out of the box. I will be happy to see your suggestion on the original issue.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (5 by maintainers)
The real world use for debounce is interactive search box where you don’t want every key press to cause a http call to a search service.
onTextBoxChange().debounce(100 ms).switchMap(text -> search(text)).subscribe(updateResults)
I’m thinking the reason it doesn’t work for you is that I’m assuming you don’t want to cancel an in flight request if the user give you more input because the operation is not idempotent.
Check out
debounce()
there are three flavors.