Repository navigation
[lexical][lexical-react][lexical-website] Bug Fix: Keep commit listeners in order when editor.read() is called during listener dispatch - #9335
Merged
etrepum merged 1 commit intoOct 11, 2026
Conversation
…ers in order when editor.read() is called during listener dispatch
A mutation listener runs with editor._updating === false, so an
editor.update() it starts begins immediately. When another listener of the
same commit then calls editor.read() (default 'force-commit'), that update is
committed synchronously mid-dispatch: update listeners are notified of the
newer commit before the one still dispatching, and the latter is delivered
with an editor state that is no longer current. CollabV2 syncs whole
snapshots, so it writes the newer state and then the stale one, deleting the
listener's appended paragraph from Yjs for every other collaborator.
@lexical/history records the commits out of order too.
The editor now tracks when it is notifying the mutation, decorator, text
content and update listeners of a commit. A force-commit read in that window
does not commit pending updates; it observes the state being committed, and
the pending update commits via its already scheduled microtask after those
listeners have run. In __DEV__ a console.warn reports the case (only when a
pending update was actually left uncommitted) and points to
editor.getEditorState().read(fn, {editor}) / editor.read('latest', fn).
Discrete updates and setEditorState() are unchanged. The EditorReadMode
JSDoc and the editor-state concept doc describe the exception.
Closes meta#7709
Co-authored-by: Ved Thakar <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Vedthakar
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
October 8, 2026 02:18
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
pushed a commit
to etrepum/lexical
that referenced
this pull request
Oct 8, 2026
… listeners so commit listeners stay in order ## Description Mutation listeners were the only commit listeners notified with `editor._updating === false`; decorator, text content and update listeners go through `triggerListeners(..., true, ...)`. An `editor.update()` in a mutation listener therefore began immediately and left a pending editor state while the rest of the commit was still dispatching. Anything later in that dispatch that commits pending updates (a default `'force-commit'` `editor.read()`, a discrete update) committed it early: update listeners were notified of the newer commit before the one still dispatching, and the latter was then delivered with an editor state that was no longer current. CollabV2 syncs whole snapshots, so it wrote the newer state and then the stale one, deleting the appended content from Yjs for every other collaborator; `@lexical/history` recorded the commits out of order too. `triggerMutationListeners` now sets `editor._updating` for the duration of the dispatch, like `triggerListeners`. An update started by a mutation listener is queued and begun by `$triggerEnqueuedUpdates` after every listener of the commit has run, which is what already happens for updates started by update listeners. `editor.read()` keeps its documented `'force-commit'` semantics. An alternative to meta#9335, which defers the commit inside `editor.read()` instead; that leaves the discrete-update path reordering. The tests are adapted from that PR. Closes meta#7709 ## Test plan ### Before ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 FAIL |unit| packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx > LexicalEditor tests > read() > does not commit an update started by a mutation listener until every listener of that commit has run, with a read in another listener (meta#7709) FAIL |unit| packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx > LexicalEditor tests > read() > does not commit an update started by a mutation listener until every listener of that commit has run, with a discrete update in another listener (meta#7709) AssertionError: expected 'first\n\nsecond\n\nthird' to be 'first\n\nsecond' // Object.is equality FAIL |unit| packages/lexical-react/src/__tests__/unit/Collaboration.test.ts > Collaboration > useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) AssertionError: expected '<p dir="auto"><span data-lexical-text…' to deeply equal '<p dir="auto"><span data-lexical-text…' Tests 3 failed | 1 passed | 113 skipped (117) ``` ### After ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 ✓ ... with a read in another listener (meta#7709) ✓ ... with a discrete update in another listener (meta#7709) ✓ ... useCollabV2: false > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) ✓ ... useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) Tests 4 passed | 113 skipped (117) $ vitest run --project unit packages/lexical/ packages/lexical-react/ packages/lexical-table/ packages/lexical-history/ packages/lexical-yjs/ packages/lexical-list/ packages/lexical-extension/ packages/lexical-code/ Test Files 192 passed (192) Tests 3072 passed | 1 skipped (3073) ``` E2E tests were not run. Co-authored-by: Ved Thakar <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
approved these changes
Oct 11, 2026
etrepum
pushed a commit
to etrepum/lexical
that referenced
this pull request
Oct 11, 2026
…arted by mutation listeners instead of deferring commits in editor.read() ## Description meta#9335 fixed meta#7709 by making a default (`'force-commit'`) `editor.read()` skip committing pending updates while the editor is notifying commit listeners, with a DEV warning. The cause is narrower: mutation listeners are the only commit listeners notified with `editor._updating === false` (decorator, text content and update listeners go through `triggerListeners(..., true, ...)`). An `editor.update()` in a mutation listener therefore begins immediately and leaves a pending editor state while the rest of the commit is still dispatching, and anything later in that dispatch that commits pending updates commits it early. `editor.read()` was one such path; a discrete update in another listener still is on main, and notifies update listeners out of order exactly as meta#7709 describes. This replaces the meta#9335 mechanism with a fix at the source. `triggerMutationListeners` now sets `editor._updating` for the duration of the dispatch, like `triggerListeners`, so an update started by a mutation listener is queued and begun by `$triggerEnqueuedUpdates` after every listener of the commit has run, as updates started by update listeners already are. With no pending state during the dispatch, nothing can commit it early, so the `isDispatchingCommitListeners` tracking, the read-time exception and its DEV warning, and the `EditorReadMode` JSDoc and concept-doc notes are removed: `'force-commit'` again always means what its documentation says. The meta#9335 tests are kept (without the warning assertions) and the LexicalEditor test also covers a discrete update in the second listener. Behaviour changes for mutation listeners: an `editor.update()` started in one begins at the end of the commit rather than immediately, and a discrete update started in one commits when the outer commit finishes rather than inside the listener, both matching update listeners today. Refs meta#7709 ## Test plan ### Before ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 ✓ ... with a read in another listener (meta#7709) × ... with a discrete update in another listener (meta#7709) ✓ ... useCollabV2: false > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) ✓ ... useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) AssertionError: expected 'first\n\nsecond\n\nthird' to be 'first\n\nsecond' // Object.is equality Tests 1 failed | 3 passed | 113 skipped (117) ``` ### After ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 ✓ ... with a read in another listener (meta#7709) ✓ ... with a discrete update in another listener (meta#7709) ✓ ... useCollabV2: false > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) ✓ ... useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) Tests 4 passed | 113 skipped (117) $ vitest run --project unit packages/lexical/ packages/lexical-react/ packages/lexical-table/ packages/lexical-history/ packages/lexical-yjs/ packages/lexical-list/ packages/lexical-extension/ packages/lexical-code/ Test Files 192 passed (192) Tests 3072 passed | 1 skipped (3073) ``` E2E tests were not run. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
zurfyx
added a commit
that referenced
this pull request
Oct 15, 2026
## Description The changes merged since v0.52.0 predate changesets, so without entries the v0.53.0 section would leave them out, including the breaking removal of the deprecated import extensions (#9316). This adds a changeset for every user-facing pull request in v0.52.0..main, written from each pull request, with a `PR: #N` line (they land in this pull request rather than the original ones) and the original author's handle: - breaking: #9316, with the full replacement table as `Migration:` - feature: #9312 - fix: #9335, #9332, #9331, #9328, #9325, #9317, #9321, #9320, #9319, #9283, #9303, #9295, #9291, #9260, #9300, #9301, #9272, #9267, #9302, #9261, #9262, #9263, #9258 - perf: #9264 Skipped as not user-facing: dependency bumps (all devDependencies), website and docs updates, test and CI changes, the private devtools extension and eslint-plugin-internal, and #9353 (a behavior-preserving Flow `Object` to `any` change). It also adds a `[chore]` changeset for this pull request itself, which adds CHANGELOG.md to the `lexical` package's `files`. ## Test plan ### Before ``` $ node scripts/changesets/check.mjs --base origin/main --head HEAD --coverage warn # at the previous commit warning: This pull request changes published code without a changeset for: lexical: packages/lexical/package.json (files) ... Changeset check passed: 0 pending changeset(s) valid; coverage: 1 warning(s), reported only. ``` ### After ``` $ node scripts/changesets/check.mjs --base origin/main Changeset check passed: 27 pending changeset(s) valid; coverage: lexical changed, each named by a changeset. ``` The `latest` release step in a throwaway clone of this branch, with the real tags: ``` $ LATEST_RELEASE=v0.52.0 npm_package_version=0.53.0 node scripts/changesets/release.mjs --date 2026-10-30 Added v0.53.0 to CHANGELOG.md from 27 changeset(s). $ ls .changeset changelog.cjs config.json README.md $ sed -n 1,9p CHANGELOG.md ## v0.53.0 (2026-10-30) ### Breaking changes - **@lexical/code-core, @lexical/code-shiki, @lexical/html, @lexical/link, @lexical/list, @lexical/mdast, @lexical/rich-text, @lexical/table**: Remove the extensions deprecated in v0.52.0 and earlier ([#9316](#9316)) The deprecated import and alias extensions are removed. The `*ImportRules` arrays, including `HorizontalRuleImportRules`, stay. $ grep -n '^### ' CHANGELOG.md | head -7 3:### Breaking changes 28:### Features 36:### Fixes 98:### Performance 102:### Other 106:### Contributors 110:### All changes $ sed -n '/^### Contributors/,+2p' CHANGELOG.md ### Contributors @Tabisharaza, @etrepum, @mbret, @maximilliangrand, @Vedthakar, @rinarakaki, @potatowagon, @followdarko, Gerard Rovira $ sed -n '/^### All changes/,/^## v0.52.0/p' CHANGELOG.md | sed -n '5,6p;$p' | head -2 - [lexical-yjs][Chore] Change last `Object` to `any` in lexical ([#9353](#9353)) Sam Zhou - [lexical-eslint-plugin-internal] Chore: lint window messaging channel scoping ([#9339](#9339)) Sherry ``` (`### All changes` has 50 lines: the 48 commits after v0.52.0, without the `v0.52.0 (#9252)` bump commit, plus this branch's two commits.) Running again before that release PR is merged: ``` $ git tag v0.53.0 # tagged, but CHANGELOG.md on main has no v0.53.0 section $ npm_package_version=0.54.0 node scripts/changesets/release.mjs CHANGELOG.md has no v0.53.0 section, so the v0.53.0 release pull request is not merged yet. Merge it (it consumes the changesets for v0.53.0) before cutting v0.54.0; running again now would put the same changesets in both releases. (exit 1) $ npm_package_version=0.53.1 node scripts/changesets/release.mjs Refusing to release 0.53.1 as a patch: 1 pending [breaking] changeset(s) need a minor release: - .changeset/remove-deprecated-import-extensions.md: Remove the extensions deprecated in v0.52.0 and earlier Run the release with the "minor" increment instead. (exit 1) ``` A prerelease publish (lexical at 0.52.1-nightly.20261016.0, nothing built, so the export check that follows fails as expected): ``` $ node scripts/npm/prepare-release.mjs [lexical] CHANGELOG.md lists pending changes as Unreleased [lexical] package.json references files that are not present in packages/lexical: ./dist/Lexical.dev.js $ sed -n 1,5p packages/lexical/CHANGELOG.md ## Unreleased Merged after the latest stable release and included in 0.52.1-nightly.20261016.0. These entries move into a version section when the next stable release is cut. ### Breaking changes $ git status --short CHANGELOG.md (clean: only the gitignored package copy changed) ```
zurfyx
added a commit
that referenced
this pull request
Oct 15, 2026
## Description The changes merged since v0.52.0 predate changesets, so without entries the v0.53.0 section would leave them out, including the breaking removal of the deprecated import extensions (#9316). This adds a changeset for every user-facing pull request in v0.52.0..main, written from each pull request, with a `PR: #N` line (they land in this pull request rather than the original ones) and the original author's handle: - breaking: #9316, with the full replacement table as `Migration:` - feature: #9312 - fix: #9335, #9332, #9331, #9328, #9325, #9317, #9321, #9320, #9319, #9283, #9303, #9295, #9291, #9260, #9300, #9301, #9272, #9267, #9302, #9261, #9262, #9263, #9258 - perf: #9264 Skipped as not user-facing: dependency bumps (all devDependencies), website and docs updates, test and CI changes, the private devtools extension and eslint-plugin-internal, and #9353 (a behavior-preserving Flow `Object` to `any` change). Four fixes change observable output or behavior, so their changesets have a `Verify:` paragraph and are listed under Behavior changes: #9303 (new tabs get no `window.opener`), #9272 (imported code content is normalized), #9261 (`$patchStyleText` styles the selection it is given), #9319 (editor keys and namespaces are a counter without a DOM). It also adds a `[chore]` changeset for this pull request itself, which adds CHANGELOG.md to the `lexical` package's `files`. ## Test plan ### Before ``` $ node scripts/changesets/check.mjs --base origin/main --head HEAD --coverage warn # at the previous commit warning: This pull request changes published code without a changeset for: lexical: packages/lexical/package.json (files) ... Changeset check passed: 0 pending changeset(s) valid; coverage: 1 warning(s), reported only. ``` ### After ``` $ node scripts/changesets/check.mjs --base origin/main Changeset check passed: 27 pending changeset(s) valid; coverage: lexical changed, each named by a changeset. ``` The `latest` release step in a throwaway clone of this branch, with the real tags: ``` $ LATEST_RELEASE=v0.52.0 npm_package_version=0.53.0 node scripts/changesets/release.mjs --date 2026-10-30 Added v0.53.0 to CHANGELOG.md from 27 changeset(s). $ ls .changeset changelog.cjs config.json README.md $ sed -n 1,9p CHANGELOG.md ## v0.53.0 (2026-10-30) ### Breaking changes - **@lexical/code-core, @lexical/code-shiki, @lexical/html, @lexical/link, @lexical/list, @lexical/mdast, @lexical/rich-text, @lexical/table**: Remove the extensions deprecated in v0.52.0 and earlier ([#9316](#9316)) The deprecated import and alias extensions are removed. The `*ImportRules` arrays, including `HorizontalRuleImportRules`, stay. $ grep -n '^### ' CHANGELOG.md | head -8 3:### Breaking changes 28:### Behavior changes 54:### Features 62:### Fixes 108:### Performance 112:### Other 116:### Contributors 120:### All changes $ sed -n '/^### Behavior changes/,+7p' CHANGELOG.md ### Behavior changes - **@lexical/link**: Open new-tab links from `ClickableLinkPlugin` / `registerClickableLink` without `window.opener` ([#9303](#9303)) This prevents the opened page from navigating the editor's tab (reverse tabnabbing). Same-tab opens are unchanged. Verify: pages opened from editor links that relied on `window.opener`. $ node scripts/changesets/range.mjs --from 0.52.0 --breaking --json # (kind, [(pr, behavior)]) [('breaking', [(9316, False)]), ('behavior', [(9303, True), (9272, True), (9261, True), (9319, True)])] $ sed -n '/^### Contributors/,+2p' CHANGELOG.md ### Contributors @Tabisharaza, @etrepum, @mbret, @maximilliangrand, @Vedthakar, @rinarakaki, @potatowagon, @followdarko, Gerard Rovira $ sed -n '/^### All changes/,/^## v0.52.0/p' CHANGELOG.md | sed -n '5,6p;$p' | head -2 - [lexical-yjs][Chore] Change last `Object` to `any` in lexical ([#9353](#9353)) Sam Zhou - [lexical-eslint-plugin-internal] Chore: lint window messaging channel scoping ([#9339](#9339)) Sherry ``` (`### All changes` has 50 lines: the 48 commits after v0.52.0, without the `v0.52.0 (#9252)` bump commit, plus this branch's two commits.) Running again before that release PR is merged: ``` $ git tag v0.53.0 # tagged, but CHANGELOG.md on main has no v0.53.0 section $ npm_package_version=0.54.0 node scripts/changesets/release.mjs CHANGELOG.md has no v0.53.0 section, so the v0.53.0 release pull request is not merged yet. Merge it (it consumes the changesets for v0.53.0) before cutting v0.54.0; running again now would put the same changesets in both releases. (exit 1) $ npm_package_version=0.53.1 node scripts/changesets/release.mjs Refusing to release 0.53.1 as a patch: 1 pending [breaking] changeset(s) need a minor release: - .changeset/remove-deprecated-import-extensions.md: Remove the extensions deprecated in v0.52.0 and earlier Run the release with the "minor" increment instead. (exit 1) ``` A prerelease publish (lexical at 0.52.1-nightly.20261016.0, nothing built, so the export check that follows fails as expected): ``` $ node scripts/npm/prepare-release.mjs [lexical] CHANGELOG.md lists pending changes as Unreleased [lexical] package.json references files that are not present in packages/lexical: ./dist/Lexical.dev.js $ sed -n 1,5p packages/lexical/CHANGELOG.md ## Unreleased Merged after the latest stable release and included in 0.52.1-nightly.20261016.0. These entries move into a version section when the next stable release is cut. ### Breaking changes $ git status --short CHANGELOG.md (clean: only the gitignored package copy changed) ```
zurfyx
added a commit
that referenced
this pull request
Oct 15, 2026
## Description The changes merged since v0.52.0 predate changesets, so without entries the v0.53.0 section would leave them out, including the breaking removal of the deprecated import extensions (#9316). This adds a changeset for every user-facing pull request in v0.52.0..main, written from each pull request, with a `PR: #N` line (they land in this pull request rather than the original ones) and the original author's handle: - breaking: #9316, with the full replacement table as `Migration:` - feature: #9312 - fix: #9335, #9332, #9331, #9328, #9325, #9317, #9321, #9320, #9319, #9283, #9303, #9295, #9291, #9260, #9300, #9301, #9272, #9267, #9302, #9261, #9262, #9263, #9258 - perf: #9264 Skipped as not user-facing: dependency bumps (all devDependencies), website and docs updates, test and CI changes, the private devtools extension and eslint-plugin-internal, and #9353 (a behavior-preserving Flow `Object` to `any` change). Four fixes change observable output or behavior, so their changesets have a `Verify:` paragraph and are listed under Behavior changes: #9303 (new tabs get no `window.opener`), #9272 (imported code content is normalized), #9261 (`$patchStyleText` styles the selection it is given), #9319 (editor keys and namespaces are a counter without a DOM). It also adds a `[chore]` changeset for this pull request itself, which adds CHANGELOG.md to the `lexical` package's `files`. ## Test plan ### Before ``` $ node scripts/changesets/check.mjs --base origin/main --head HEAD --coverage warn # at the previous commit warning: This pull request changes published code without a changeset for: lexical: packages/lexical/package.json (files) ... Changeset check passed: 0 pending changeset(s) valid; coverage: 1 warning(s), reported only. ``` ### After ``` $ node scripts/changesets/check.mjs --base origin/main Changeset check passed: 27 pending changeset(s) valid; coverage: lexical changed, each named by a changeset. ``` The `latest` release step in a throwaway clone of this branch, with the real tags: ``` $ LATEST_RELEASE=v0.52.0 npm_package_version=0.53.0 node scripts/changesets/release.mjs --date 2026-10-30 Added v0.53.0 to CHANGELOG.md from 27 changeset(s). $ ls .changeset changelog.cjs config.json README.md $ sed -n 1,9p CHANGELOG.md ## v0.53.0 (2026-10-30) ### Breaking changes - **@lexical/code-core, @lexical/code-shiki, @lexical/html, @lexical/link, @lexical/list, @lexical/mdast, @lexical/rich-text, @lexical/table**: Remove the extensions deprecated in v0.52.0 and earlier ([#9316](#9316)) The deprecated import and alias extensions are removed. The `*ImportRules` arrays, including `HorizontalRuleImportRules`, stay. $ grep -n '^### ' CHANGELOG.md | head -8 3:### Breaking changes 28:### Behavior changes 54:### Features 62:### Fixes 108:### Performance 112:### Other 116:### Contributors 120:### All changes $ sed -n '/^### Behavior changes/,+7p' CHANGELOG.md ### Behavior changes - **@lexical/link**: Open new-tab links from `ClickableLinkPlugin` / `registerClickableLink` without `window.opener` ([#9303](#9303)) This prevents the opened page from navigating the editor's tab (reverse tabnabbing). Same-tab opens are unchanged. Verify: pages opened from editor links that relied on `window.opener`. $ node scripts/changesets/range.mjs --from 0.52.0 --breaking --json # (kind, [(pr, behavior)]) [('breaking', [(9316, False)]), ('behavior', [(9303, True), (9272, True), (9261, True), (9319, True)])] $ sed -n '/^### Contributors/,+2p' CHANGELOG.md ### Contributors @Tabisharaza, @etrepum, @mbret, @maximilliangrand, @Vedthakar, @rinarakaki, @potatowagon, @followdarko, Gerard Rovira $ sed -n '/^### All changes/,/^## v0.52.0/p' CHANGELOG.md | sed -n '5,6p;$p' | head -2 - [lexical-yjs][Chore] Change last `Object` to `any` in lexical ([#9353](#9353)) Sam Zhou - [lexical-eslint-plugin-internal] Chore: lint window messaging channel scoping ([#9339](#9339)) Sherry ``` (`### All changes` has 50 lines: the 48 commits after v0.52.0, without the `v0.52.0 (#9252)` bump commit, plus this branch's two commits.) Running again before that release PR is merged: ``` $ git tag v0.53.0 # tagged, but CHANGELOG.md on main has no v0.53.0 section $ npm_package_version=0.54.0 node scripts/changesets/release.mjs CHANGELOG.md has no v0.53.0 section, so the v0.53.0 release pull request is not merged yet. Merge it (it consumes the changesets for v0.53.0) before cutting v0.54.0; running again now would put the same changesets in both releases. (exit 1) $ npm_package_version=0.53.1 node scripts/changesets/release.mjs Refusing to release 0.53.1 as a patch: 1 pending [breaking] changeset(s) need a minor release: - .changeset/remove-deprecated-import-extensions.md: Remove the extensions deprecated in v0.52.0 and earlier Run the release with the "minor" increment instead. (exit 1) ``` A prerelease publish (lexical at 0.52.1-nightly.20261016.0, nothing built, so the export check that follows fails as expected): ``` $ node scripts/npm/prepare-release.mjs [lexical] CHANGELOG.md lists pending changes as Unreleased [lexical] package.json references files that are not present in packages/lexical: ./dist/Lexical.dev.js $ sed -n 1,5p packages/lexical/CHANGELOG.md ## Unreleased Merged after the latest stable release and included in 0.52.1-nightly.20261016.0. These entries move into a version section when the next stable release is cut. ### Breaking changes $ git status --short CHANGELOG.md (clean: only the gitignored package copy changed) ```
etrepum
pushed a commit
to etrepum/lexical
that referenced
this pull request
Oct 20, 2026
…ion-listener updates ## Description The previous commit removed meta#9335's `isDispatchingCommitListeners` guard on the grounds that queueing updates started by mutation listeners leaves no pending state during listener dispatch. That does not hold for commands: `dispatchCommand` runs its handlers through `updateEditorSync`, which begins an update at once even while `editor._updating` is set, so a command dispatched from a commit listener still leaves a pending state, and an `editor.read()` in a later listener committed it early and notified update listeners out of order (meta#7709). This restores main's guard, its DEV warning and the docs describing it, and keeps the PR's change that queues `editor.update()` calls made from mutation listeners. The meta#9335 test now starts the nested update with a dispatched command, the path that still needs the guard, and keeps its exact-count and identity assertions. ## Test plan ### Before With the guard removed (as in the previous commit): ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx -t 7709 AssertionError: expected [ [ EditorState{ …(7) }, …(1) ], …(1) ] to have a length of 1 but got 2 Tests 1 failed | 94 skipped (95) ``` ### After ``` $ vitest run --project unit packages/lexical-y packages/lexical/src/__tests__/unit/ packages/lexical-react/src/__tests__/unit/Collaboration.test.ts Tests 1931 passed (1931) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This solves issue #7709: update listeners can be notified out of order, which corrupts the Yjs document.
Current behavior. A mutation listener runs with
editor._updating === false, so aneditor.update()started inside it begins right away and schedules a microtask commit. If another listener of the same commit then callseditor.read()(which defaults to'force-commit'), that read commits the new update synchronously, in the middle of the outer commit's listener dispatch. Update listeners therefore see the newer commit (S1 → P) before the commit that is still dispatching (S0 → S1), and when they finally see the older one, itseditorStateis no longereditor.getEditorState().Under CollabV2 this loses data:
syncLexicalUpdateToYjsV2__EXPERIMENTALdiffs the whole tree it is given, so it writesP(hello, foo, bar) and then the staleS1(hello, foo), deleting the appended paragraph from Yjs. Client 1 still shows "bar" but other collaborators never get it. CollabV1 converges in the test below only because it applies key-based deltas, but it receives the same out-of-order payloads, which is how theExpected shared type to include type attributecorruption in the issue arises.@lexical/historyis affected the same way.What this changes.
LexicalUpdates.tstracks, per editor, when a commit is notifying its mutation, decorator, text content and update listeners (isDispatchingCommitListeners(editor), internal, not re-exported fromlexical). It uses save/restore so a nested commit can't clear the outer flag, andtry/finallyso a throwing listener can't leave it set.editor.read()in'force-commit'mode no longer commits pending updates during that window. The read observes the state being committed (what the DOM currently shows), and the pending update commits through the microtask itseditor.update()already scheduled, after every listener of the current commit has run.__DEV__, the read logs aconsole.warnwhen this actually deferred something, pointing toeditor.getEditorState().read(fn, {editor})oreditor.read('latest', fn). This is the warning suggested in the issue thread, on top of the fix rather than instead of it.EditorReadModeJSDoc and the "Reading state" section ofconcepts/editor-state.mddocument the exception.Why only
read(). Gating every nested$commitPendingUpdatesduring listener dispatch would also defer{discrete: true}updates andsetEditorState()called from listeners.setEditorState()commits and then overwrites_pendingEditorState, so deferring it would drop updates, and deferringdiscretewould break its documented synchronous semantics. Fixing only CollabV2 (skipping stale payloads) would leave history and application listeners with out-of-order payloads, and would lose local changes when the newer commit isn't synced (e.g.SKIP_COLLAB_TAG). Gating the read fixes ordering for every listener, and only changes behavior where the old behavior already handed stale states to listeners.update(),discreteandsetEditorState()are unchanged.Relation to #9106 (
@lexical/no-nested-editor-updates). No overlap: the lint rule flagseditor.update()lexically nested in callbacks of the same editor. This bug comes from two separate listeners, often in different plugins, so no single function contains both calls and static analysis can't see it. The DEV warning catches it at runtime, and the fix makes it harmless in production.Closes #7709
Test plan
New tests:
packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx›read()› "does not flush an update started by a commit listener until every listener of that commit has run (Bug: triggering an update from an mutation/update listener can lead to Yjs document corruption #7709)". After the outer discrete commit there is exactly one update-listener call whoseeditorStateis current; the read observed the committed text; one DEV warning was logged; and after a microtask the second call'sprevEditorStateis the first call'seditorState.packages/lexical-react/src/__tests__/unit/Collaboration.test.ts›useCollabV2: false|true› "Should sync an update started by a mutation listener when another listener calls editor.read() (Bug: triggering an update from an mutation/update listener can lead to Yjs document corruption #7709)". Ports the failing test from 635cb96 to current markup and asserts that client 2's HTML and the Y doc JSON match client 1.Before
On
mainwith the tests applied and the source change reverted:(The passing test is the
useCollabV2: falsevariant. For V2, client 2 is missing the "bar" paragraph.)After
Author: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code