egghead-redux | Egghead Redux Series by Dan Abramov | State Container library
kandi X-RAY | egghead-redux Summary
kandi X-RAY | egghead-redux Summary
Egghead Redux Series by Dan Abramov
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of egghead-redux
egghead-redux Key Features
egghead-redux Examples and Code Snippets
Community Discussions
Trending Discussions on egghead-redux
QUESTION
I have a redux-observable epic that polls an endpoint, getting progress updates until the progress is 100%. The polling interval is acheived using debounceTime like so:
...ANSWER
Answered 2018-Jun-19 at 02:44Interestingly enough, I played around with your code and am fairly confident you just found a bug in the debounceTime
operator, which causes the apparent swallowing the scheduled debounce. The bad news is that even if that bug is fixed, you're code still wouldn't do what you're looking for order wise.
Bear with me as shit is about to get real:
- Epic receives action PROCESSING and schedules debounce, yielding execution to your test
- Your test calls
scheduler.flush()
and the VirtualScheduler executes the scheduled debounce work, which will pass along the original PROCESSING action to themergeMap
- Fake ajax is made, which synchronously emits a response
- Response is mapped to the second PROCESSING action
- Your epic emits that second action synchronously
- The second action is recursively received by your epic and given to the debounce
- The debounceTime operator now schedules that second action on the VirtualScheduler but the debounceTime operator is in the middle of executing the previously scheduled work still from the first action.
- The call stack unwinds a bunch up until it reaches inside the previously scheduled debounce work from the first action that had just next()'d the first action. The rxjs code for
debounceTime
then setsthis.lastValue = null
andthis.hasValue = false
This is the rxjs bug, it needs to be done before nexting into the destination - The stack unwinds some more to the running flush() method of the VirtualScheduler, which now dequeues the second scheduled debounced action because it was added the scheduled work array synchronously, before this the flushing finished. Remember, we've only called
scheduler.flush()
ONCE so far, which is the function we're in back in at this point. - The second scheduled debounce work is run, but
this.hasValue === false
because the first scheduled one set it, so the debounceTime operator does not emit anything. - Stack unwinds to our first
scheduler.flush()
- We
console.log('CHECK CORRECT STATE FOR PROGRESS 25')
- All the other
scheduler.flush()
calls do nothing as there's nothing scheduled.
This is technically a bug, but it's not surprising that no one has run into it since running debounce synchronously without any delay defeats the point of it, except when you're testing, of course. This ticket is basically the same thing and OJ says RxJS doesn't make reentrancy guarantees, but I that might be up for debate in this case. I've filed a PR with the fix to discuss
Remember, this bug wouldn't have solved your underlying question about the ordering, but would have prevented the actions from being swallowed.
Off the top of my head I'm not sure how you would do what you'd like to do specifically if you want to maintain 100% synchronous behavior (VirtualScheduler). You'd need some way of yielding to your test in between debounces. For me when and if I write integration tests I mock out very little, if anything. e.g. let the debounces actually debounce either naturally or by mocking out setTimeout to advance them quicker but still keeping them async which will yield back to your test allowing you to check the state, but making your test also async.
For anyone wanting to reproduce, here's the StackBlitz code I used
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install egghead-redux
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page