From bcdf1f1e064b4c207e09c89df0fdfcfb32c27d5d Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Fri, 6 Nov 2026 14:51:38 +0800 Subject: [PATCH] =?UTF-8?q?perf(editor):=20=E4=BF=9D=E5=AD=98=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E6=96=87=E6=A1=A3=E5=BC=95=E7=94=A8=EF=BC=8C=E7=9C=81?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=95=B4=E7=AF=87=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/TextEditor.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/client/TextEditor.tsx b/src/client/TextEditor.tsx index 7c38f5a1..16b0895c 100644 --- a/src/client/TextEditor.tsx +++ b/src/client/TextEditor.tsx @@ -805,7 +805,8 @@ export function TextEditor(props: FileViewerProps) { if (view === null || savingRef.current) return // The bytes this request writes: the same string becomes the baseline text // once the host accepts it (the draft may keep changing while in flight). - const text = view.state.doc.toString() + const sentDoc = view.state.doc + const text = sentDoc.toString() savingRef.current = true setSaveState('saving') // Optimistic concurrency: the draft was based on the bytes read at @@ -818,8 +819,7 @@ export function TextEditor(props: FileViewerProps) { // 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 + if (viewRef.current !== 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 @@ -827,8 +827,10 @@ 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. - // Read the LIVE view, never the closure's possibly-destroyed one. - const live = liveView.state.doc.toString() + // CM's `Text` is immutable: an unchanged doc is the same object, so + // `text` can be reused instead of stringifying the document again. + const liveDoc = view.state.doc + const live = liveDoc === sentDoc ? text : liveDoc.toString() if (live !== text) { setDraft(live) setSaveState('idle')