fix(editor): 保存回调加过期守卫,防跨文件串状态与基线

This commit is contained in:
unsc9 committed 2026-11-06 14:22:10 +08:00
1 parent 2b22c05f7e
commit 1ff3cde0a3
2 files changed
+102 -1

No files matched your search

+11 -1
View File
@@ -814,6 +814,12 @@ export function TextEditor(props: FileViewerProps) {
// external editor). `null` = the file did not exist when loaded.
api.fsWrite(scope, path, text, mtimeRef.current ?? null).then((result) => {
savingRef.current = false
// A file switch during the write destroys `view` and re-seats viewRef on
// the NEW document. This result then belongs to the OLD file: write
// nothing else, or A's text / save state / mtime leak into B. The save
// lock is already released above so B can still save.
const liveView = viewRef.current
if (liveView === null || liveView !== view) return
// Adopt the fresh baseline the host reports (absent on a stat failure —
// keep the old one, the next save just re-checks).
if (typeof result.mtimeMs === 'number') mtimeRef.current = result.mtimeMs
@@ -821,7 +827,8 @@ export function TextEditor(props: FileViewerProps) {
setConflict(false)
// Keystrokes during the write are NOT the bytes on disk: keep the tab
// dirty and its durable backup, or the next close/quit drops them.
const live = view.state.doc.toString()
// Read the LIVE view, never the closure's possibly-destroyed one.
const live = liveView.state.doc.toString()
if (live !== text) {
setDraft(live)
setSaveState('idle')
@@ -839,6 +846,9 @@ export function TextEditor(props: FileViewerProps) {
setGitRevision(value => value + 1)
}).catch((error: unknown) => {
savingRef.current = false
// Same expiry guard as the success path: an error from the OLD file must
// not raise B's conflict banner or mark B's save failed.
if (viewRef.current !== view) return
if (error instanceof SidebarApiError && error.code === 'fs-conflict') {
setConflict(true)
setSaveState('idle')
+91
View File
@@ -17,6 +17,13 @@
* was sent are NOT the bytes on disk, so the tab must stay dirty and keep its
* durable backup instead of declaring itself saved (which would let the next
* close/quit drop those edits silently).
*
* The fourth case is the cross-file save race (#3): a save sent for file A is
* still in flight when the viewer switches to file B — the [content, path]
* effect destroys A's view and points viewRef at B's. An expired result may
* only release the save lock (and adopt the server's mtime): it must not write
* A's text / save state into B's draft, while typing during a save of the
* CURRENT file still keeps that tab dirty (the third case above).
*/
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -49,6 +56,10 @@ vi.mock('../src/client/editor-draft-backup.ts', async (importOriginal) => {
const CONTENT = 'const a = 1\n'
const NEXT_CONTENT = 'const a = 2\n'
// Distinct markdown headings: the preview renders whichever document `draft`
// currently holds, so a leaked draft from file A is visible in the DOM.
const A_CONTENT = '# A-source-heading\n'
const B_CONTENT = '# B-clean-heading\n'
function viewerProps(overrides: Partial<FileViewerProps> = {}): FileViewerProps {
return {
@@ -204,4 +215,84 @@ describe('TextEditor save baseline (#1059)', () => {
editor.unmount()
}
})
it('lets an in-flight save from the previous file expire without touching the new file', async () => {
let finish: ((value: { ok: true; mtimeMs: number }) => void) | undefined
fsWrite.mockImplementation(() => new Promise<{ ok: true; mtimeMs: number }>((resolve) => { finish = resolve }))
const editor = mountEditor()
try {
clearDraftBackup.mockClear()
const view = editor.view()
type(view, '// first\n')
await act(async () => { editor.controls().save() })
expect(editor.state().saveState).toBe('saving')
// A genuinely different document: the file-switch effect destroys the
// view the save captured and re-seats a clean one for NEXT_CONTENT.
editor.rerender({ content: NEXT_CONTENT, mtimeMs: 400 })
expect(editor.view().state.doc.toString()).toBe(NEXT_CONTENT)
expect(editor.state()).toMatchObject({ mode: 'preview', dirty: false, saveState: 'idle' })
clearDraftBackup.mockClear()
await act(async () => {
finish?.({ ok: true, mtimeMs: 999 })
await Promise.resolve()
await Promise.resolve()
})
// The result belongs to A: it must not declare B saved, drop B's
// durable backup, or move B's dirty/draft state.
expect(editor.state()).toMatchObject({ dirty: false, saveState: 'idle' })
expect(clearDraftBackup).not.toHaveBeenCalled()
// The save lock was released, so B can still save normally.
fsWrite.mockResolvedValue({ ok: true, mtimeMs: 1000 })
await act(async () => { editor.controls().save() })
await act(async () => { await Promise.resolve() })
expect(fsWrite).toHaveBeenCalledTimes(2)
expect(fsWrite.mock.calls[1]![2]).toBe(NEXT_CONTENT)
} finally {
editor.unmount()
}
})
it('does not publish the old file text as the new file draft when a save expires', async () => {
let finish: ((value: { ok: true; mtimeMs: number }) => void) | undefined
fsWrite.mockImplementation(() => new Promise<{ ok: true; mtimeMs: number }>((resolve) => { finish = resolve }))
const editor = mountEditor({
viewerId: 'markdown', path: '/p/a.md', title: 'a.md',
content: A_CONTENT, mtimeMs: 100,
})
try {
clearDraftBackup.mockClear()
act(() => { editor.controls().setMode('edit') })
const view = editor.view()
type(view, 'LEAK-FROM-A\n')
await act(async () => { editor.controls().save() })
expect(editor.state().saveState).toBe('saving')
// More keystrokes while the write is in flight: the old view's live
// document no longer equals the bytes that were sent, which is the
// branch that used to call setDraft(live).
type(view, 'MORE-FROM-A\n')
editor.rerender({ content: B_CONTENT, mtimeMs: 400 })
expect(editor.view().state.doc.toString()).toBe(B_CONTENT)
expect(editor.container.textContent).toContain('B-clean-heading')
expect(editor.state()).toMatchObject({ mode: 'preview', dirty: false, saveState: 'idle' })
clearDraftBackup.mockClear()
await act(async () => {
finish?.({ ok: true, mtimeMs: 999 })
await Promise.resolve()
await Promise.resolve()
})
// The preview still renders B's own content, not A's leaked text.
expect(editor.container.textContent).toContain('B-clean-heading')
expect(editor.container.textContent).not.toContain('MORE-FROM-A')
expect(editor.state()).toMatchObject({ dirty: false, saveState: 'idle' })
expect(clearDraftBackup).not.toHaveBeenCalled()
// The save lock was released, so B can still save normally.
fsWrite.mockResolvedValue({ ok: true, mtimeMs: 1000 })
await act(async () => { editor.controls().save() })
await act(async () => { await Promise.resolve() })
expect(fsWrite).toHaveBeenCalledTimes(2)
expect(fsWrite.mock.calls[1]![2]).toBe(B_CONTENT)
} finally {
editor.unmount()
}
})
})