cody icon indicating copy to clipboard operation
cody copied to clipboard

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

Open abeatrix opened this issue 9 months ago • 0 comments

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

abeatrix avatar Apr 30 '25 23:04 abeatrix