Skip to content

fix(prompt-editor): fix flushSync warning in PromptEditor #7832

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

abeatrix
Copy link
Contributor

This PR fixes a React warning that occurs during test runs related to improper usage of flushSync within a React lifecycle method. The warning appears in the HumanMessageEditor test:

The issue was in the PromptEditor component, where editor.setEditorState() was being called directly during React's render cycle. The Lexical editor framework internally uses flushSync to ensure synchronous updates, which conflicts with React's rendering process.

To resolve this, requestAnimationFrame is used to ensure that the editor state is set outside of React's render cycle. This change is applied in two places:

  • When setting the editor state from serialized data.
  • When initializing the editor state with the initial editor state.

Test plan

Run pnpm test vscode/webviews/chat/cells/messageCell/human/editor/HumanMessageEditor.test.tsx to confirm the warning is gone. All existing tests should still pass

Before

image

After

image

This commit addresses the `flushSync` warning that occurs when setting the editor state in the PromptEditor component. The issue arises because the editor state is being updated within React's render cycle.

To resolve this, `requestAnimationFrame` is used to ensure that the editor state is set outside of React's render cycle. This change is applied in two places:

- When setting the editor state from serialized data.
- When initializing the editor state with the initial editor state.
@abeatrix abeatrix requested a review from a team April 30, 2025 23:09
@abeatrix abeatrix marked this pull request as draft May 1, 2025 04:26
@abeatrix abeatrix removed the request for review from a team May 1, 2025 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant