youssefreda9 commited on
Commit
8d7698b
·
1 Parent(s): 1eb5022

fix: Increase analyze debounce to 1.5s to prevent text overwrite while typing - Debounce 500ms -> 1500ms - Abort in-flight requests when user starts typing again - Double-check no new input before running analysis

Browse files
Files changed (1) hide show
  1. src/js/editor.js +14 -2
src/js/editor.js CHANGED
@@ -3,7 +3,8 @@
3
 
4
  let analyzeTimeout;
5
  let analyzeAbortController = null;
6
- const ANALYZE_DEBOUNCE_MS = 500;
 
7
  const MAX_ANALYZE_LENGTH = 5000;
8
 
9
  /**
@@ -27,6 +28,9 @@ function initEditor() {
27
  } catch (e) {}
28
 
29
  editor.addEventListener('input', () => {
 
 
 
30
  analyzeTextDelayed();
31
  try {
32
  localStorage.setItem('bayan_editor_draft', editor.innerHTML);
@@ -83,8 +87,16 @@ function updatePlaceholder() {
83
 
84
  function analyzeTextDelayed() {
85
  clearTimeout(analyzeTimeout);
 
 
 
 
86
  analyzeTimeout = setTimeout(() => {
87
- analyzeText();
 
 
 
 
88
  }, ANALYZE_DEBOUNCE_MS);
89
  }
90
 
 
3
 
4
  let analyzeTimeout;
5
  let analyzeAbortController = null;
6
+ let _lastInputTime = 0;
7
+ const ANALYZE_DEBOUNCE_MS = 1500;
8
  const MAX_ANALYZE_LENGTH = 5000;
9
 
10
  /**
 
28
  } catch (e) {}
29
 
30
  editor.addEventListener('input', () => {
31
+ _lastInputTime = Date.now();
32
+ updateEditorStats();
33
+ updatePlaceholder();
34
  analyzeTextDelayed();
35
  try {
36
  localStorage.setItem('bayan_editor_draft', editor.innerHTML);
 
87
 
88
  function analyzeTextDelayed() {
89
  clearTimeout(analyzeTimeout);
90
+ // Abort any in-flight request so it doesn't overwrite while user types
91
+ if (analyzeAbortController) {
92
+ analyzeAbortController.abort();
93
+ }
94
  analyzeTimeout = setTimeout(() => {
95
+ // Double-check user hasn't typed in the last DEBOUNCE period
96
+ const timeSinceLastInput = Date.now() - _lastInputTime;
97
+ if (timeSinceLastInput >= ANALYZE_DEBOUNCE_MS - 100) {
98
+ analyzeText();
99
+ }
100
  }, ANALYZE_DEBOUNCE_MS);
101
  }
102