Repository navigation
fix(muya): drop pending op batch on setContent to prevent cross-document corruption (#2938) - #4658
Merged
Merged
Conversation
…ocument corruption (#2938) `JSONState.setContent` replaced `_state` but left any deferred operation batch in `_operationCache` (with its scheduled requestAnimationFrame). Switching files within the same frame as a pending edit then flushed the outgoing document's op against the new document's state — corrupting it, or throwing and leaving `_isGoing` stuck so all further edits froze. Either way the switched-to file could no longer be saved correctly. Track the scheduled flush's rAF handle; on `setContent` cancel it, clear `_operationCache`, and reset `_isGoing` before swapping the state. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`_isGoing` (boolean "a flush is scheduled") and `_rafId` (the rAF handle) were always set and cleared together, so they tracked the same state redundantly. Drop `_isGoing` and use `_rafId !== null` as the batching guard directly. No behavior change — the existing setContent/op-batching tests stay green. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The op cache flushes on a requestAnimationFrame, so a same-frame document swap (a tab switch calls setContent right after the last keystroke) drops the pending edit. Add a public flush() that runs the queued batch now and emits json-change, so a caller can make the outgoing edit durable before swapping. Extracted the rAF body into _flushOperationCache, shared by the frame callback and flush(). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
On tab switch the store sets currentFile then synchronously reloads the engine via setContent, so the previous tab's last same-frame keystroke (still queued in the engine) was either lost or applied to the incoming document. Flush the engine while currentFile still points at the outgoing tab (via a new flush-active-editor bus event), so the edit is applied and persisted under the right tab before the swap. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 task done
This was referenced Aug 13, 2026
Closed
Jocs
pushed a commit
that referenced
this pull request
Sep 11, 2026
…5103) An edit made in the same frame as the WYSIWYG -> source switch (e.g. a task-checkbox click) was missing from the CodeMirror snapshot, and the swap back out of source mode cancels the engine's scheduled op flush (#4658), silently reverting the edit. Flush the rAF batch in the sync sourceCode watcher so the tab holds fresh markdown before sourceCode.vue mounts — the same guard saving (#3803/#4859) and tab switching (#2938) already use. Fixes #5102 Co-authored-by: Claude Fable 5 <[email protected]>
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.
Fixes #2938
Problem
Switching files could leave the switched-to file unsaveable / corrupted. If you typed in tab A and clicked tab B within the same animation frame, tab A's not-yet-flushed edit was applied to tab B's document.
Root cause
packages/muya/src/state/index.ts— operations are deferred:editOperation/insertOperation/… push to_operationCacheand_emitStateChange()schedules arequestAnimationFrameflush.setContent(the tab-switch path) replaced_statebut never cleared_operationCacheor cancelled the scheduled flush. When the rAF fired, it composed and applied the previous document's op to the new state — corrupting it, or throwing and leaving_isGoing = true(which permanently froze all later deferred ops).Fix
Track the scheduled flush's rAF handle. On
setContent, cancel a pending flush, clear_operationCache, and reset_isGoingbefore swapping the state.Verification
setContentClearsOpCache.spec.ts: a pending insert against doc A does not appear in doc B aftersetContent(RED→GREEN: was['B1','STALE','B2']); a fresh edit after the switch still flushes (not frozen).lint:types+ ESLint clean.🤖 Generated with Claude Code