fix: handleSelectionUpdate callback is triggered before the renderer is initialized. #5982
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.
Changes Overview
This PR addresses a crash in the ReactNodeView implementation that occurs when the handleSelectionUpdate callback is triggered before the renderer is initialized. The issue arises due to the asynchronous nature of JavaScript, where the subscription to the selectionUpdate event (editor.on) is executed before the renderer initialization.
The proposed fix ensures that the renderer is initialized before subscribing to the selectionUpdate event, maintaining the intended execution order.
Implementation Approach
The subscription to the selectionUpdate event was moved to occur after the initialization of the renderer. This ensures the callback does not execute prematurely and prevents the crash. The implementation adds no new functionality or logic but rearranges the code to avoid the race condition.
Testing Done
The fix was tested by replicating the scenario that caused the crash:
1. Adding a new line before a tag.
2. Observing the behavior when the old node is destroyed, and a new node is created.
The changes successfully prevent the crash in these scenarios. Additionally, basic functionality was verified to ensure no regressions.
This normally occurs with editors built with tiptap, tippy and prosemirror.
Verification Steps
Additional Notes
This fix does not alter the functionality of the library; it only ensures proper execution order, and does not affect any other functionality, but avoids this condition that causes the crash
Checklist
Related Issues
#5870