Skip to content

Commit

Permalink
Merge pull request #376 from margelo/osp/fix-concurrency-with-set-state
Browse files Browse the repository at this point in the history
Fix race condition when tempState is deleted before loading flag is set to false
  • Loading branch information
mountiny authored Sep 29, 2023
2 parents 41ffc1a + 0367b4c commit 2ae80f9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/withOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
const prevValue = this.state[statePropertyName];

// If the component is not loading (read "mounting"), then we can just update the state
if (!this.state.loading) {
// There is a small race condition.
// When calling setWithOnyxState we delete the tempState object that is used to hold temporary state updates while the HOC is gathering data.
// However the loading flag is only set on the setState callback down below. setState however is an async operation that is also batched,
// therefore there is a small window of time where the loading flag is not false but the tempState is already gone
// (while the update is queued and waiting to be applied).
// This simply bypasses the loading check if the tempState is gone and the update can be safely queued with a normal setStateProxy.
if (!this.state.loading || !this.tempState) {
// Performance optimization, do not trigger update with same values
if (prevValue === val || utils.areObjectsEmpty(prevValue, val)) {
return;
Expand Down

0 comments on commit 2ae80f9

Please sign in to comment.