From 6ac6c7cfdad72a79f7419d96919c3f65c9f94465 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 28 Apr 2024 19:47:53 +0300 Subject: [PATCH] #2159 Move debounce constants to a separate module --- public/script.js | 2 +- public/scripts/PromptManager.js | 3 ++- public/scripts/RossAscends-mods.js | 3 ++- public/scripts/authors-note.js | 3 ++- public/scripts/constants.js | 14 ++++++++++++++ public/scripts/extensions/expressions/index.js | 3 ++- public/scripts/extensions/memory/index.js | 3 ++- public/scripts/extensions/token-counter/index.js | 3 ++- public/scripts/extensions/vectors/index.js | 3 ++- public/scripts/group-chats.js | 2 +- public/scripts/personas.js | 3 ++- public/scripts/utils.js | 16 +--------------- public/scripts/world-info.js | 3 ++- 13 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 public/scripts/constants.js diff --git a/public/script.js b/public/script.js index 8b9b1bdd5..92d3685a2 100644 --- a/public/script.js +++ b/public/script.js @@ -153,8 +153,8 @@ import { isValidUrl, ensureImageFormatSupported, flashHighlight, - debounce_timeout, } from './scripts/utils.js'; +import { debounce_timeout } from './scripts/constants.js'; import { ModuleWorkerWrapper, doDailyExtensionUpdatesCheck, extension_settings, getContext, loadExtensionSettings, renderExtensionTemplate, renderExtensionTemplateAsync, runGenerationInterceptors, saveMetadataDebounced, writeExtensionField } from './scripts/extensions.js'; import { COMMENT_NAME_DEFAULT, executeSlashCommands, getSlashCommandsHelp, processChatSlashCommands, registerSlashCommand } from './scripts/slash-commands.js'; diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 152a45177..d45da25bf 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -4,7 +4,8 @@ import { callPopup, event_types, eventSource, is_send_press, main_api, substitut import { is_group_generating } from './group-chats.js'; import { Message, TokenHandler } from './openai.js'; import { power_user } from './power-user.js'; -import { debounce, waitUntilCondition, escapeHtml, debounce_timeout } from './utils.js'; +import { debounce, waitUntilCondition, escapeHtml } from './utils.js'; +import { debounce_timeout } from './constants.js'; function debouncePromise(func, delay) { let timeoutId; diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js index 90b7b4fc9..d16de0c80 100644 --- a/public/scripts/RossAscends-mods.js +++ b/public/scripts/RossAscends-mods.js @@ -32,10 +32,11 @@ import { SECRET_KEYS, secret_state, } from './secrets.js'; -import { debounce, debounce_timeout, getStringHash, isValidUrl } from './utils.js'; +import { debounce, getStringHash, isValidUrl } from './utils.js'; import { chat_completion_sources, oai_settings } from './openai.js'; import { getTokenCountAsync } from './tokenizers.js'; import { textgen_types, textgenerationwebui_settings as textgen_settings, getTextGenServer } from './textgen-settings.js'; +import { debounce_timeout } from './constants.js'; import Bowser from '../lib/bowser.min.js'; diff --git a/public/scripts/authors-note.js b/public/scripts/authors-note.js index 9e23317b4..167cf5446 100644 --- a/public/scripts/authors-note.js +++ b/public/scripts/authors-note.js @@ -10,8 +10,9 @@ import { import { selected_group } from './group-chats.js'; import { extension_settings, getContext, saveMetadataDebounced } from './extensions.js'; import { registerSlashCommand } from './slash-commands.js'; -import { getCharaFilename, debounce, delay, debounce_timeout } from './utils.js'; +import { getCharaFilename, debounce, delay } from './utils.js'; import { getTokenCountAsync } from './tokenizers.js'; +import { debounce_timeout } from './constants.js'; export { MODULE_NAME as NOTE_MODULE_NAME }; const MODULE_NAME = '2_floating_prompt'; // <= Deliberate, for sorting lower than memory diff --git a/public/scripts/constants.js b/public/scripts/constants.js new file mode 100644 index 000000000..c70b3af63 --- /dev/null +++ b/public/scripts/constants.js @@ -0,0 +1,14 @@ +/** + * Common debounce timeout values to use with `debounce` calls. + * @enum {number} + */ +export const debounce_timeout = { + /** [100 ms] For ultra-fast responses, typically for keypresses or executions that might happen multiple times in a loop or recursion. */ + quick: 100, + /** [300 ms] Default time for general use, good balance between responsiveness and performance. */ + standard: 300, + /** [1.000 ms] For situations where the function triggers more intensive tasks. */ + relaxed: 1000, + /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */ + extended: 5000, +}; diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index ef179088f..321cfc4a9 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -3,9 +3,10 @@ import { dragElement, isMobile } from '../../RossAscends-mods.js'; import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; import { loadMovingUIState, power_user } from '../../power-user.js'; import { registerSlashCommand } from '../../slash-commands.js'; -import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, debounce_timeout } from '../../utils.js'; +import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence } from '../../utils.js'; import { hideMutedSprites } from '../../group-chats.js'; import { isJsonSchemaSupported } from '../../textgen-settings.js'; +import { debounce_timeout } from '../../constants.js'; export { MODULE_NAME }; const MODULE_NAME = 'expressions'; diff --git a/public/scripts/extensions/memory/index.js b/public/scripts/extensions/memory/index.js index 87af1f904..3cd1d97e5 100644 --- a/public/scripts/extensions/memory/index.js +++ b/public/scripts/extensions/memory/index.js @@ -1,4 +1,4 @@ -import { getStringHash, debounce, waitUntilCondition, extractAllWords, debounce_timeout } from '../../utils.js'; +import { getStringHash, debounce, waitUntilCondition, extractAllWords } from '../../utils.js'; import { getContext, getApiUrl, extension_settings, doExtrasFetch, modules, renderExtensionTemplateAsync } from '../../extensions.js'; import { activateSendButtons, @@ -20,6 +20,7 @@ import { registerSlashCommand } from '../../slash-commands.js'; import { loadMovingUIState } from '../../power-user.js'; import { dragElement } from '../../RossAscends-mods.js'; import { getTextTokens, getTokenCountAsync, tokenizers } from '../../tokenizers.js'; +import { debounce_timeout } from '../../constants.js'; export { MODULE_NAME }; const MODULE_NAME = '1_memory'; diff --git a/public/scripts/extensions/token-counter/index.js b/public/scripts/extensions/token-counter/index.js index 1b8f32c61..0594ff4a9 100644 --- a/public/scripts/extensions/token-counter/index.js +++ b/public/scripts/extensions/token-counter/index.js @@ -2,7 +2,8 @@ import { callPopup, main_api } from '../../../script.js'; import { getContext } from '../../extensions.js'; import { registerSlashCommand } from '../../slash-commands.js'; import { getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, tokenizers } from '../../tokenizers.js'; -import { resetScrollHeight, debounce, debounce_timeout } from '../../utils.js'; +import { resetScrollHeight, debounce } from '../../utils.js'; +import { debounce_timeout } from '../../constants.js'; function rgb2hex(rgb) { rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i); diff --git a/public/scripts/extensions/vectors/index.js b/public/scripts/extensions/vectors/index.js index befc5f1e5..c92ed1aa1 100644 --- a/public/scripts/extensions/vectors/index.js +++ b/public/scripts/extensions/vectors/index.js @@ -22,7 +22,8 @@ import { import { collapseNewlines } from '../../power-user.js'; import { SECRET_KEYS, secret_state, writeSecret } from '../../secrets.js'; import { getDataBankAttachments, getFileAttachment } from '../../chats.js'; -import { debounce, getStringHash as calculateHash, waitUntilCondition, onlyUnique, splitRecursive, debounce_timeout } from '../../utils.js'; +import { debounce, getStringHash as calculateHash, waitUntilCondition, onlyUnique, splitRecursive } from '../../utils.js'; +import { debounce_timeout } from '../../constants.js'; import { getSortedEntries } from '../../world-info.js'; const MODULE_NAME = 'vectors'; diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index 3556e04fe..7e5433e4e 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -11,10 +11,10 @@ import { getBase64Async, resetScrollHeight, initScrollHeight, - debounce_timeout, } from './utils.js'; import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js'; import { power_user, loadMovingUIState, sortEntitiesList } from './power-user.js'; +import { debounce_timeout } from './constants.js'; import { chat, diff --git a/public/scripts/personas.js b/public/scripts/personas.js index b1673c846..e25190b29 100644 --- a/public/scripts/personas.js +++ b/public/scripts/personas.js @@ -18,7 +18,8 @@ import { } from '../script.js'; import { persona_description_positions, power_user } from './power-user.js'; import { getTokenCountAsync } from './tokenizers.js'; -import { debounce, debounce_timeout, delay, download, parseJsonFile } from './utils.js'; +import { debounce, delay, download, parseJsonFile } from './utils.js'; +import { debounce_timeout } from './constants.js'; const GRID_STORAGE_KEY = 'Personas_GridView'; diff --git a/public/scripts/utils.js b/public/scripts/utils.js index c855a36e4..bed9ab1f2 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -2,6 +2,7 @@ import { getContext } from './extensions.js'; import { getRequestHeaders } from '../script.js'; import { isMobile } from './RossAscends-mods.js'; import { collapseNewlines } from './power-user.js'; +import { debounce_timeout } from './constants.js'; /** * Pagination status string template. @@ -18,21 +19,6 @@ export const navigation_option = { previous: -1000, }; -/** - * Common debounce timeout values to use with `debounce` calls. - * @enum {number} - */ -export const debounce_timeout = { - /** [100 ms] For ultra-fast responses, typically for keypresses or executions that might happen multiple times in a loop or recursion. */ - quick: 100, - /** [300 ms] Default time for general use, good balance between responsiveness and performance. */ - standard: 300, - /** [1.000 ms] For situations where the function triggers more intensive tasks. */ - relaxed: 1000, - /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */ - extended: 5000, -}; - export function escapeHtml(str) { return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 6efe7b627..466db3c51 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -1,5 +1,5 @@ import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types, getExtensionPromptByName, saveMetadata, getCurrentChatId, extension_prompt_roles } from '../script.js'; -import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight, debounce_timeout } from './utils.js'; +import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight } from './utils.js'; import { extension_settings, getContext } from './extensions.js'; import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from './authors-note.js'; import { registerSlashCommand } from './slash-commands.js'; @@ -9,6 +9,7 @@ import { getTokenCountAsync } from './tokenizers.js'; import { power_user } from './power-user.js'; import { getTagKeyForEntity } from './tags.js'; import { resolveVariable } from './variables.js'; +import { debounce_timeout } from './constants.js'; export { world_info,