Adds debounce mechanism for API key refresh
Implements debounce timer to prevent excessive API calls when updating API key Prevents rapid repeated model refreshes by: - Adding a 500ms delay timer - Clearing previous timers to avoid multiple concurrent requests - Cleaning up timers when settings tab is hidden Improves performance and reduces unnecessary API load during key entry
This commit is contained in:
1 file changed
+26
-2
+26
-2
@@ -56,6 +56,10 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
private modelDropdown: DropdownComponent | null = null;
|
private modelDropdown: DropdownComponent | null = null;
|
||||||
/** Cached list of available models for the dropdown */
|
/** Cached list of available models for the dropdown */
|
||||||
private availableModels: ModelOption[] = [];
|
private availableModels: ModelOption[] = [];
|
||||||
|
/** Debounce timer for API key changes to avoid excessive API calls */
|
||||||
|
private apiKeyDebounceTimer: number | null = null;
|
||||||
|
/** Debounce delay in milliseconds */
|
||||||
|
private readonly API_KEY_DEBOUNCE_MS = 500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new settings tab instance.
|
* Creates a new settings tab instance.
|
||||||
@@ -68,6 +72,17 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup method to clear any pending timers.
|
||||||
|
* Called when settings tab is closed or destroyed.
|
||||||
|
*/
|
||||||
|
hide(): void {
|
||||||
|
if (this.apiKeyDebounceTimer !== null) {
|
||||||
|
window.clearTimeout(this.apiKeyDebounceTimer);
|
||||||
|
this.apiKeyDebounceTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the settings UI.
|
* Renders the settings UI.
|
||||||
*
|
*
|
||||||
@@ -96,9 +111,18 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
this.plugin.promptGenerator.updateApiKey(value);
|
this.plugin.promptGenerator.updateApiKey(value);
|
||||||
|
|
||||||
// Trigger model refresh when API key is entered
|
// Clear any existing debounce timer
|
||||||
|
if (this.apiKeyDebounceTimer !== null) {
|
||||||
|
window.clearTimeout(this.apiKeyDebounceTimer);
|
||||||
|
this.apiKeyDebounceTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debounce model refresh to avoid excessive API calls on every keystroke
|
||||||
if (value.trim().length > 0) {
|
if (value.trim().length > 0) {
|
||||||
await this.refreshModels();
|
this.apiKeyDebounceTimer = window.setTimeout(async () => {
|
||||||
|
await this.refreshModels();
|
||||||
|
this.apiKeyDebounceTimer = null;
|
||||||
|
}, this.API_KEY_DEBOUNCE_MS);
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.then(setting => {
|
.then(setting => {
|
||||||
|
|||||||
Reference in new issue
Block a user