youssefreda9 commited on
Commit
5a4cea7
·
1 Parent(s): bd81b49

fix: prevent analyze loop - skip redundant API calls when text unchanged

Browse files
Files changed (1) hide show
  1. src/js/editor.js +13 -1
src/js/editor.js CHANGED
@@ -10,6 +10,9 @@ const MAX_ANALYZE_LENGTH = 5000;
10
  // Pipeline Hardening v3.3: Guard to prevent input events during suggestion apply
11
  let _isApplyingSuggestion = false;
12
 
 
 
 
13
  // ── Custom Undo/Redo Stack ──
14
  const _undoStack = [];
15
  const _redoStack = [];
@@ -243,7 +246,7 @@ function findSuggestionElement(id) {
243
  return document.querySelector(`[data-suggestion-id="${id}"]`);
244
  }
245
 
246
- async function analyzeText() {
247
  const text = getEditorText();
248
  updateEditorStats();
249
  updatePlaceholder();
@@ -255,9 +258,14 @@ async function analyzeText() {
255
  updateSuggestionsList([]);
256
  window.currentSuggestions = [];
257
  updateAnalysisLimitBanner(false);
 
258
  return;
259
  }
260
 
 
 
 
 
261
  const isTruncated = text.length > MAX_ANALYZE_LENGTH;
262
  const textForApi = isTruncated ? text.substring(0, MAX_ANALYZE_LENGTH) : text;
263
  updateAnalysisLimitBanner(isTruncated);
@@ -522,6 +530,8 @@ function applySuggestionAtOffsets(suggestion) {
522
  }
523
  // P2/User Request: Auto re-analyze after applying suggestion
524
  // Calls analyzeText() DIRECTLY (not delayed) for instant re-analysis.
 
 
525
  setTimeout(() => { analyzeText(); }, 300);
526
  }
527
 
@@ -607,6 +617,7 @@ function applyAlternativeCorrection(suggestion, correctionText) {
607
  _isApplyingSuggestion = false;
608
  }
609
  // P2/User Request: Auto re-analyze after applying alternative correction
 
610
  setTimeout(() => { analyzeText(); }, 300);
611
  }
612
 
@@ -688,6 +699,7 @@ function applyAllSuggestions() {
688
  _isApplyingSuggestion = false;
689
  }
690
  // P2/User Request: Auto re-analyze after applying all suggestions
 
691
  setTimeout(() => { analyzeText(); }, 300);
692
  }
693
 
 
10
  // Pipeline Hardening v3.3: Guard to prevent input events during suggestion apply
11
  let _isApplyingSuggestion = false;
12
 
13
+ // Track last analyzed text to prevent redundant API calls
14
+ let _lastAnalyzedText = '';
15
+
16
  // ── Custom Undo/Redo Stack ──
17
  const _undoStack = [];
18
  const _redoStack = [];
 
246
  return document.querySelector(`[data-suggestion-id="${id}"]`);
247
  }
248
 
249
+ async function analyzeText(forceReanalyze = false) {
250
  const text = getEditorText();
251
  updateEditorStats();
252
  updatePlaceholder();
 
258
  updateSuggestionsList([]);
259
  window.currentSuggestions = [];
260
  updateAnalysisLimitBanner(false);
261
+ _lastAnalyzedText = '';
262
  return;
263
  }
264
 
265
+ // Skip redundant analysis if text hasn't changed (prevents overlay→input→analyze loop)
266
+ if (!forceReanalyze && text === _lastAnalyzedText) return;
267
+ _lastAnalyzedText = text;
268
+
269
  const isTruncated = text.length > MAX_ANALYZE_LENGTH;
270
  const textForApi = isTruncated ? text.substring(0, MAX_ANALYZE_LENGTH) : text;
271
  updateAnalysisLimitBanner(isTruncated);
 
530
  }
531
  // P2/User Request: Auto re-analyze after applying suggestion
532
  // Calls analyzeText() DIRECTLY (not delayed) for instant re-analysis.
533
+ // forceReanalyze=true because the text changed (suggestion was applied)
534
+ _lastAnalyzedText = ''; // Reset so analysis isn't skipped
535
  setTimeout(() => { analyzeText(); }, 300);
536
  }
537
 
 
617
  _isApplyingSuggestion = false;
618
  }
619
  // P2/User Request: Auto re-analyze after applying alternative correction
620
+ _lastAnalyzedText = ''; // Reset so analysis isn't skipped
621
  setTimeout(() => { analyzeText(); }, 300);
622
  }
623
 
 
699
  _isApplyingSuggestion = false;
700
  }
701
  // P2/User Request: Auto re-analyze after applying all suggestions
702
+ _lastAnalyzedText = ''; // Reset so analysis isn't skipped
703
  setTimeout(() => { analyzeText(); }, 300);
704
  }
705