Skip to content

Commit ce07964

Browse files
feat: Implement debounced search update for autocomplete
1 parent d39a915 commit ce07964

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

web/js/autocomplete.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,25 @@ export class AutocompleteEventHandler {
941941
constructor() {
942942
this.autocompleteUI = new AutocompleteUI();
943943
this.keyDownWithModifier = new Map(); // Keep track of keydown events with modifiers
944+
this._debounceTimer = null; // Timer ID for debounced search
945+
}
946+
947+
/**
948+
* Calls autocompleteUI.updateDisplay() with debounce for sequential search, or immediately for fast search.
949+
* @param {HTMLTextAreaElement} target
950+
*/
951+
_triggerUpdateDisplay(target) {
952+
if (settingValues.useFastSearch || settingValues.searchDebounceTime <= 0) {
953+
// FastSearch or debounce disabled: immediate update
954+
this.autocompleteUI.updateDisplay(target);
955+
} else {
956+
// Sequential search: debounced update
957+
clearTimeout(this._debounceTimer);
958+
this._debounceTimer = setTimeout(() => {
959+
this.autocompleteUI.updateDisplay(target);
960+
this._debounceTimer = null;
961+
}, settingValues.searchDebounceTime);
962+
}
944963
}
945964

946965
/**
@@ -954,6 +973,7 @@ export class AutocompleteEventHandler {
954973

955974
const partialTag = getCurrentPartialTag(event.target);
956975
if (partialTag.length <= 0) {
976+
clearTimeout(this._debounceTimer); // Cancel pending debounced search
957977
this.autocompleteUI.hide();
958978
}
959979
}
@@ -1064,7 +1084,7 @@ export class AutocompleteEventHandler {
10641084
// and default action is not prevented, update the display.
10651085
// This will typically be for character inputs, Delete, Backspace or IME composition.
10661086
if (!event.defaultPrevented) {
1067-
this.autocompleteUI.updateDisplay(event.target);
1087+
this._triggerUpdateDisplay(event.target);
10681088
}
10691089
}
10701090

web/js/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const settingValues = {
1212
replaceUnderscoreWithSpace: true, // Replace underscores with spaces in tag insertion
1313
prefixArtist: '', // Prefix to be attached before artist tags
1414
autoInsertComma: true,
15+
searchDebounceTime: 100, // Sequential search debounce time in milliseconds
1516

1617
// Related tags feature settings
1718
enableRelatedTags: true,

0 commit comments

Comments
 (0)