Clear existing timeout in initAutoRefresh #168
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is this PR and why do we need it?
In React Strict mode, effects are run twice. This means that if the
shouldAutoRefresh
flag is on, the following effect will be triggered twice on first render:In the SDK Core, the
initAutoRefresh
method will thus be called twice, setting two time-outs. However, only the last one will get tracked. When theFusionAuthProvider
component gets re-rendered, it will create a newcore
, attempting todispose()
the old one. Thedispose
will successfully cancel the second timeout, but the first one is still running.React
initAutoRefresh
1=> SDKCore
initAutoRefresh
setsthis.refreshTokenTimeout
(let's say to value 1)React
initAutoRefresh
2 (effect is re-run)=> SDKCore
initAutoRefresh
setsthis.refreshTokenTimeout
(let's say to value 2)=>
refreshTimeout 1
is not cleared and keeps runningReact FusionAuthProvider rerenders
=> SDKCore
dispose
method is called, clearingrefreshTokenTimeout 2
Eventually
refreshTimeout 1
will run and could potentially return a wrong value (in my case the clientId was not correct anymore)Pre-Merge Checklist (if applicable)