From e5677b620d590460eaba47f1262d185312bb6535 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 17 May 2025 15:38:52 +0300 Subject: [PATCH] Cancel debounced chat save before clearing chat --- public/script.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/public/script.js b/public/script.js index 3f97bcb5f..3290d4f73 100644 --- a/public/script.js +++ b/public/script.js @@ -1979,7 +1979,19 @@ export async function printMessages() { } } +/** + * Cancels the debounced chat save if it is currently pending. + */ +export function cancelDebouncedChatSave() { + if (chatSaveTimeout) { + console.debug('Debounced chat save cancelled'); + clearTimeout(chatSaveTimeout); + chatSaveTimeout = null; + } +} + export async function clearChat() { + cancelDebouncedChatSave(); closeMessageEditor(); extension_prompts = {}; if (is_delete_mode) { @@ -6929,11 +6941,7 @@ export function saveChatDebounced() { const chid = this_chid; const selectedGroup = selected_group; - if (chatSaveTimeout) { - console.debug('Clearing chat save timeout'); - clearTimeout(chatSaveTimeout); - chatSaveTimeout = null; - } + cancelDebouncedChatSave(); chatSaveTimeout = setTimeout(async () => { if (selectedGroup !== selected_group) { @@ -8646,11 +8654,7 @@ export async function saveChatConditional() { } try { - if (chatSaveTimeout) { - console.debug('Debounced chat save canceled'); - clearTimeout(chatSaveTimeout); - chatSaveTimeout = null; - } + cancelDebouncedChatSave(); isChatSaving = true;