Fix: consecutive updates may trigger a blocks reset #18219
Merged
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.
Description
Fixes: #18169
We had a bug in our editor provider. We subscribed to the store and saved the blocks when they changed in isSyncingOutcomingValue. When the value of the component changed, and it was not equal to the value we saved in isSyncingOutcomingValue we assumed it was an external update happening, and we triggered a block reset.
This mechanism had a problem when the updates were fast. The subscribe may execute multiple times, and setting isSyncingOutcomingValue to something more recent before componentDidUpdate executed.
When componentDidUpdate executed, the isSyncingOutcomingValue was different (more recent) than the value prop componentDidUpdate received, so we assumed there was an external update happening and triggered a block reset.
It is possible to verify that by logging calls to the resetBlock action, the action is called on Cover Block, Media & Text, and Navigation Block. These blocks use InnerBlocks with templates, and during the block setup, there are multiple updates.
The reset to a previous value was responsible for causing bug #18169.
In this PR, we make isSyncingOutcomingValue an array, and on componentDidUpdate, we compare the value against the values the array contains.
This PR should improve the performance of blocks that use templates because we avoid unnecessary resetBlocks action.
How has this been tested?
I verified I could add multiple navigation blocks, and all of them contain the top-level pages as menu items.
I added logs in resetBlocks action, and I verified when I add Media & Text, cover block (after choosing a background), and navigation block the reset blocks action was not called.