-
Notifications
You must be signed in to change notification settings - Fork 105
fix(useThrottleCallback): Cleared timeout on unmount #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(useThrottleCallback): Cleared timeout on unmount #1173
Conversation
Calling clearTimeout is insufficient when checking for undefined. Make sure to set timeout value to undefined explicitly.
|
Not 100% sure of what the source of the problem is. It feels like it should be fine more multiple Given that this same logic is already being done in |
|
could you please, at least attempt, to cover the issue with tests - if not - make an issue with thorough WTR |
|
I'll second what xobotyi is saying. Could you please give us a CodeSandbox that demonstrates this bug? Or a failing test? Given those, we can make sure that this bug is fixed. |
|
Yeah that's fair. I'll see what I can do to reproduce. A sandbox with nextjs isn't reproducing so maybe it really is something i'm doing. |
|
Turns out it was strict mode in next js that was causing it to fail. At least, in my case, when i disabled |
|
I think I might have just been asleep at the wheel and accidentally using |
|
Hmm can't seem to reproduce the problem in unit tests: const { rerender } = renderHook(
(dep) => {
useThrottledEffect(spy, [dep], 200, true);
},
{
initialProps: 1,
wrapper: StrictMode as ComponentType<number>,
}
);This may be due to a subtle difference between actual browser engine and nodejs. I can push the test but it's not actually detecting the bug seen the browser, so probably not valuable. |
|
Your change indeed fixes the problem. Thank you. I'll be looking more into this later. For now, let's run the workflows. I'm not certain why this change is necessary though. I'm pretty sure it has something to do with the same timeoutID getting stuck into the ref if it's not cleared on unmount, but I can't quite grasp the precise mechanics of this yet.
|
Codecov Report
@@ Coverage Diff @@
## master #1173 +/- ##
=======================================
Coverage 98.49% 98.49%
=======================================
Files 62 62
Lines 1064 1065 +1
Branches 179 179
=======================================
+ Hits 1048 1049 +1
Misses 2 2
Partials 14 14
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
ArttuOll
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be the case that if we don't clear the timeout.current on unmount, the same timeoutID gets stuck in it, which causes the hook to hit the if-statement in the wrapped function every time, leading to the execute function only being run once. I could not see StrictMode having any effect on this behavior.
I would be interested to hear if @xobotyi has any insight into this.
I also had trouble writing a test for this behavior. I tried removing the clearing of the timeoutID from useDebouncedCallback as well and seeing if any of its tests would fail, but none did. However, this change breaks none of the existing tests and demonstrateably fixes the bug in the linked CodeSandbox, so I'm approving this.
|
@ArttuOll thanks for looking into it, it does seem a little weird that we can't reproduce it within tests 🤷 . Let me know if there's anything else I can do to move this on. I'm currently using a vendored version of this function just so it doesn't cause confusion with my team due to this behaviour. Would love to update. |
At this point, it's only a matter of @xobotyi approving and merging this. |
|
Welp this one is interesting =) Take a look at this sandbox.
As far as i can imagine the fix works due to internals of strict mode which (it is only a guess) is calling final unmount for the shadowed state and therefore clearing the throttle timeout for second call effect. That assumtion needs deep further investigation. |
## [23.0.1](v23.0.0...v23.0.1) (2023-05-24) ### Bug Fixes * **useThrottleCallback:** Cleared timeout on unmount ([#1173](#1173)) ([8681ad8](8681ad8))
|
🎉 This PR is included in version 23.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What is the current behavior, and the steps to reproduce the issue?
useThrottledCallbackanywhereWhat is the expected behavior?
Method should be called and limited to throttling frequency.
How does this PR fix the problem?
Clears the timeout properly.
Checklist