Skip to content

Commit 65fc1c9

Browse files
committed
refactor(result-preview): add manual timeout clearing for syntax error checks
1 parent 5c816c8 commit 65fc1c9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

app/components/ResultPreview/IframeContent.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ const IframeContent: React.FC<IframeContentProps> = ({
277277
// Set up syntax error monitoring
278278
const model = editor.getModel();
279279
if (model) {
280+
// Holds the timeout id for pending syntax-error checks so we can cancel
281+
// any previously scheduled run before queuing a new one. This acts as a
282+
// lightweight, manual debounce without bringing in lodash or a similar
283+
// utility.
284+
let syntaxErrorCheckTimeoutId: ReturnType<typeof setTimeout> | null = null;
285+
286+
const scheduleSyntaxCheck = (delay: number) => {
287+
if (syntaxErrorCheckTimeoutId !== null) {
288+
clearTimeout(syntaxErrorCheckTimeoutId);
289+
}
290+
syntaxErrorCheckTimeoutId = setTimeout(checkSyntaxErrors, delay);
291+
};
292+
280293
const checkSyntaxErrors = () => {
281294
// Get ALL markers for our model from all sources
282295
const allMarkers = monaco.editor.getModelMarkers({
@@ -296,21 +309,22 @@ const IframeContent: React.FC<IframeContentProps> = ({
296309
};
297310

298311
// Initial check after a short delay to allow language service to initialize
299-
setTimeout(checkSyntaxErrors, 100);
312+
scheduleSyntaxCheck(100);
300313

301314
// Listen for marker changes - check every time markers change
302315
const disposable = monaco.editor.onDidChangeMarkers((uris) => {
303316
// Check if our model's URI is in the changed URIs
304317
if (uris.some((uri) => uri.toString() === model.uri.toString())) {
305318
// Add a small delay to ensure markers are updated
306-
setTimeout(checkSyntaxErrors, 50);
319+
scheduleSyntaxCheck(50);
307320
}
308321
});
309322

310323
// Also listen for model content changes as a backup
311324
const contentDisposable = editor.onDidChangeModelContent(() => {
312-
// Debounce content changes to avoid excessive checks
313-
setTimeout(checkSyntaxErrors, 500);
325+
// Queue a syntax check, cancelling any pending one, to avoid stacking
326+
// up checks during rapid typing.
327+
scheduleSyntaxCheck(500);
314328
});
315329

316330
disposablesRef.current.push(disposable);

0 commit comments

Comments
 (0)