Hi everyone. Having the following flow of events (...
# rx
u
Hi everyone. Having the following flow of events (there’s a button click that toggles some state):
Copy code
state1 -> state2 -> state1 -> state2 -> state1 -> state2 -> ...
When there’s no event within 3 seconds, restore default state (state1). How would I do that? Any help would be appreciated.
l
u
Debounce will ignore emit, while I need to emit an event after certain timespan within which nothing happens. Suppose I have a light bulb, which has two states: on and off. Like in real life, I want to turn it off automatically after certain timespan (when no-one presses the button), and it could be still turned on again afterwards.
l
What do you mean it ignores emit? Debounce does exactly what you described:
only emit an item from an Observable if a particular timespan has passed without it emitting another item
Perhaps if you share some code, we can understand your problem properly
a
@leolima "emit item only if timespan has passed without observable emitting another item" - ignore item if another was emitted within timespan - "ignore emission"
l
If he wants to do everything in a single chain, intermediate emissions could still be captured by a doOnNext before the debounce. He could also have a Subject with two observers, one emitting everything and another debounced. That’s why seeing some code could help.
u
@leolima, @arekolek, here’s my use case: Suppose I have a light bulb, which has two states: on and off. Like in real life, I want to turn it off automatically after certain timespan (when no-one presses the button), and it could be still turned on again afterwards. So when the button is pressed, the state is toggled between
on
and
off
. If user stops pressing the button within the timespan of 3 seconds, and current state is
on
observable will emit default value (
off
). It’s a bit verbose, sorry. So, basically, I have three events: 1)
on
2)
off
3)
nothing
(if the last state was
off
and there’s at least 3 seconds that user does not press the button, emit a default value =
off
)
s
@ubu There are a few ways to implement a debouncing algorithm and RX implements them under a few different names. What it sounds like you want is the “Throttle” operator: www.introtorx.com/Content/v1.0.10621.0/13_TimeShiftedSequences.html#Throttle
Here’s how I think I would implement your flow: * Emit
on
and
off
events whenever the state is toggled * Whenever
on
is toggled, also send out a throttled event with a three second timeout * That 3s timer will be interrupted and replaced by any additional
on
events, by the definition of the throttle operator * When the throttle expires, emit an
off
event
u
@Seri, thanks. what do you mean by send out?
s
Forgive me if my language isn’t precise, since I’m still new to RX. I think you could just add
.debounce().subscribe()
to the end of your chain to get the behavior I described
u
@Seri, @leolima, @arekolek, thanks again. I’ve tried both options and it’s working: https://gist.github.com/uburoiubu/a12169904ffcea1f994904b24690c381 Though, it does not seem correct to just ignore
state
in second subscription in each of the examples.