Imagine we have a queue of booleans (we don't need a plex datastructure since we wanna store only the fact of the order). Items can get into the queue at anytime in any pace. The Observable would pop items from this queue and emit them in a delayed manner but the first emit is not needed to be delayed. After the queue gets empty it stay on hold until a new item gets in the queue which it will emmit right away. Please help me implement this behaviour.
Given a user can press a button which increments a counter. At start the counter is on 0 and the user clicks two times in 500 ms. The first click increments the counter to one but the second click will take action delayed by 500 ms. The user then waits more than 500 ms and clicks. The third click has to increment the counter right away since there is no other increment action in progress so to say. If the third click happens in the 500 ms window then it has to wait till the other finishes.
Yeah, I think I get your problem @estus. Somehow it would be neccessary to register where is the last action in its progress. Saving the time when it was emitted or something.
Given the first click has happened 300ms before and a new click happens. In this case the 2nd click has to wait only 200ms.
Update1: Here is the code based on Dorus's answer
Imagine we have a queue of booleans (we don't need a plex datastructure since we wanna store only the fact of the order). Items can get into the queue at anytime in any pace. The Observable would pop items from this queue and emit them in a delayed manner but the first emit is not needed to be delayed. After the queue gets empty it stay on hold until a new item gets in the queue which it will emmit right away. Please help me implement this behaviour.
Given a user can press a button which increments a counter. At start the counter is on 0 and the user clicks two times in 500 ms. The first click increments the counter to one but the second click will take action delayed by 500 ms. The user then waits more than 500 ms and clicks. The third click has to increment the counter right away since there is no other increment action in progress so to say. If the third click happens in the 500 ms window then it has to wait till the other finishes.
Yeah, I think I get your problem @estus. Somehow it would be neccessary to register where is the last action in its progress. Saving the time when it was emitted or something.
Given the first click has happened 300ms before and a new click happens. In this case the 2nd click has to wait only 200ms.
Update1: Here is the code based on Dorus's answer
Share Improve this question edited Oct 17, 2016 at 20:11 apreg asked Oct 17, 2016 at 13:09 apregapreg 6379 silver badges19 bronze badges 8- These are two different cases. Posting the existing code would help to get an answer that suits your case (if there is any). – Estus Flask Commented Oct 17, 2016 at 14:00
- There is no code yet. I'm about to implement something in jsbin using Dorus's answer. The observable is like a guardian. It does not let others go through until the passenger has left the bufferzone aka. got far away enough from the gate. If there is noone in the buffer zone then the next passenger can go through right away. That's what I need to implement in an observable manner. – apreg Commented Oct 17, 2016 at 14:08
- For the first case, please, specify which input you expect and which output (with ms timeline), because the description isn't specific enough. Again, the second case is not just an example but a different case, and it is't clear what should happen with 3rd click in 500ms window. If your question requires recipes for both cases then please, state this explicitly. – Estus Flask Commented Oct 17, 2016 at 14:30
- Passengers are analogous to user clicks so the queue is not a must have I just thought it is easier to explain/implement that way. – apreg Commented Oct 17, 2016 at 14:31
-
The thing you're describing is pretty close to generic throttle/debounce. I cannot be sure but possibly
throttleTime
operator does exactly what you want. – Estus Flask Commented Oct 17, 2016 at 15:15
1 Answer
Reset to default 7You can use concatMap
:
delay = 1000; // delay in ms
queue.concatMap(e =>
Rx.Observable.of(e) // emit first item right away
.concat(Rx.Observable.empty().delay(delay)) // delay next item
)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745317179a4622273.html
评论列表(0条)