Skip to content

Fix issue with syncing yjs changes to Lexical - #4621

Merged
ivailop7 merged 1 commit into
meta:mainfrom
ebads67:fix-undefined-behavior-yjs-delta
Jun 9, 2023
Merged

ivailop7 merged 1 commit into
meta:mainfrom
ebads67:fix-undefined-behavior-yjs-delta

Conversation

@ebads67

@ebads67 ebads67 commented Jun 8, 2023

Copy link
Copy Markdown
Contributor

Issue explanation:

Lexical uses the delta in the events coming from yjs document to sync the state of the editor with the latest changes. According to yjs delta is a computed property, meaning that it is computed when it is accessed for the first time. yjs also says :

Note that this can only be safely computed during the event call. Computing this property after other changes happened might result in unexpected behavior.

Link to yjs repo where it's explained.

Lexical accesses delta in a callback function passed to editor.update and this function can be scheduled to run in a separate event call. That means delta can be computed unsafely in a different event call. This affects the changes coming from undo manager particularly.

…mpute the delta syncronously to avoid unexpected behavior
@vercel

vercel Bot commented Jun 8, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 8, 2023 3:05pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 8, 2023 3:05pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 8, 2023
@fantactuka

Copy link
Copy Markdown
Contributor

That makes sense, curious how did you catch it?

@ebads67

ebads67 commented Jun 8, 2023 •

Copy link
Copy Markdown
Contributor Author

That makes sense, curious how did you catch it?

This sometimes affects the changes coming from the yjs undo manager for some reason. I synced two clients on a yjs document. Then I started making some changes from client 1. And then did some undo/redo operations. At some point client 1's editor state goes out of sync. I noted that the yjs documents in both clients are synced, and only the editor's state does not show the correct state. Then I examined the delta coming from ydoc on undo/redo events and it was empty for some events.
Happy to attach a screen recording of this to the pr.

@fantactuka

Copy link
Copy Markdown
Contributor

Happy to attach a screen recording of this to the pr.

That would be useful!

@ebads67

ebads67 commented Jun 8, 2023 •

Copy link
Copy Markdown
Contributor Author

test
@fantactuka

@ivailop7 ivailop7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ivailop7
ivailop7 merged commit 81e9bf4 into meta:main Jun 9, 2023
@zurfyx zurfyx mentioned this pull request Jul 11, 2023

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — e750cf00 Deployed Jun 8, 2023 by vercel[bot]
Preview – lexical — e750cf00 Deployed Jun 8, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants