Skip to content

Commit 8654e85

Browse files
committed
fix(diff): prevent stale fetch responses from showing wrong diff content
Add a generation counter to DiffViewerDialog so that when the user navigates between files rapidly, only the most recent IPC response updates the signals. Also guard MonacoDiffEditor setValue calls to skip redundant updates when the model already holds the correct value.
1 parent cbd753f commit 8654e85

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/components/DiffViewerDialog.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ export function DiffViewerDialog(props: DiffViewerDialogProps) {
8080
const [hasChanges, setHasChanges] = createSignal(true);
8181
const [metadataOnly, setMetadataOnly] = createSignal(false);
8282

83+
let fetchGeneration = 0;
84+
8385
createEffect(() => {
8486
const file = props.file;
8587
if (!file) return;
8688

8789
const worktreePath = props.worktreePath;
8890
const projectRoot = props.projectRoot;
8991
const branchName = props.branchName;
92+
const thisGen = ++fetchGeneration;
9093

9194
setLoading(true);
9295
setError('');
@@ -112,6 +115,7 @@ export function DiffViewerDialog(props: DiffViewerDialogProps) {
112115
return { diff: '', oldContent: '', newContent: '' };
113116
})
114117
.then((result) => {
118+
if (thisGen !== fetchGeneration) return;
115119
if (isBinaryDiff(result.diff)) {
116120
setBinary(true);
117121
} else {
@@ -122,8 +126,13 @@ export function DiffViewerDialog(props: DiffViewerDialogProps) {
122126
setMetadataOnly(result.diff !== '' && !contentDiffers);
123127
}
124128
})
125-
.catch((err) => setError(String(err)))
126-
.finally(() => setLoading(false));
129+
.catch((err) => {
130+
if (thisGen !== fetchGeneration) return;
131+
setError(String(err));
132+
})
133+
.finally(() => {
134+
if (thisGen === fetchGeneration) setLoading(false);
135+
});
127136
});
128137

129138
return (

src/components/MonacoDiffEditor.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ export function MonacoDiffEditor(props: MonacoDiffEditorProps) {
6060
});
6161

6262
createEffect(() => {
63-
if (originalModel) originalModel.setValue(props.oldContent);
63+
const value = props.oldContent;
64+
if (originalModel && originalModel.getValue() !== value) {
65+
originalModel.setValue(value);
66+
}
6467
});
6568

6669
createEffect(() => {
67-
if (modifiedModel) modifiedModel.setValue(props.newContent);
70+
const value = props.newContent;
71+
if (modifiedModel && modifiedModel.getValue() !== value) {
72+
modifiedModel.setValue(value);
73+
}
6874
});
6975

7076
createEffect(() => {

0 commit comments

Comments
 (0)