Skip to content
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

Fix discrete nested updates #6419

Merged
merged 2 commits into from
Jul 18, 2024
Merged

Fix discrete nested updates #6419

merged 2 commits into from
Jul 18, 2024

Conversation

zurfyx
Copy link
Member

@zurfyx zurfyx commented Jul 18, 2024

If the discrete flag was part of a nested update it was being ignored. Instead, the correct behavior is that this update and the previously computed updates should be flushed.

Copy link

vercel bot commented Jul 18, 2024

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 Jul 18, 2024 7:32pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 18, 2024 7:32pm

@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 Jul 18, 2024
Copy link

github-actions bot commented Jul 18, 2024

size-limit report 📦

Path Size
lexical - cjs 28.75 KB (0%)
lexical - esm 28.61 KB (0%)
@lexical/rich-text - cjs 37.2 KB (0%)
@lexical/rich-text - esm 28.13 KB (0%)
@lexical/plain-text - cjs 35.81 KB (0%)
@lexical/plain-text - esm 25.48 KB (0%)
@lexical/react - cjs 39.05 KB (0%)
@lexical/react - esm 29.48 KB (0%)

expect(onUpdate).toHaveBeenCalledTimes(1);
});

it('can use discrete after a non-discrete setEditorState to flush the entire queue', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is flushing the entire queue correct or should we flush the pending updates, then flush the new update separately? I'm worried about leaking flags and such.

Copy link
Member Author

Choose a reason for hiding this comment

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

@acywatson fair point, this is correct as far as the current behavior goes. If you had a discrete update with nested non-discrete updates, the nested updates would also be bundled into the discrete. I think this is worth an offline discussion, clearly nested updates and discrete are very unpredictable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Last time I looked at this I couldn't think of a way to do it correctly with the current API.

✅ probably ok, because the outer update doesn't do anything on its own (common in a command listener)

editor.update(() => {
  editor.update(() => $changeSomeStuff(), { discrete: true });
  // this is fine assuming reconciliation already happened, in which case the rest of the update is more like a read
});

✅ probably ok, because the discrete update is a "tail call" so it would be equivalent to floating the discrete flag upwards

editor.update(() => {
  $changeSomeStuff();
  editor.update(() => $changeSomeStuff(), { discrete: true });
  // this is fine assuming reconciliation already happened, in which case the rest of the update is more like a read
});

❌ this breaks assumptions about update atomicity

editor.update(() => {
  $changeSomeStuff();
  editor.update(() => $changeSomeStuff(), { discrete: true });
  // oh no! this will be a separate logical update or the discrete update actually hasn't been reconciled yet
  $changeSomeStuff();
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the "most correct" workaround for this would be to have some way to mark the outer update as read-only after flush. I think any solution (or non-solution) here is going to be a bit convoluted.

@zurfyx zurfyx added this pull request to the merge queue Jul 18, 2024
Merged via the queue into main with commit 3fcf02d Jul 18, 2024
60 checks passed
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