prevent parallel execution of initVoiceMap

26d506874f5e6ce8c74809b2478c9ee2b69cdce7

Risenafis <reverse.hierarchy@gmail.com>

Signed
1 files changed, +25 -0Ignore whitespace
public/scripts/extensions/tts/index.js+25 -0
@@ -35,6 +35,7 @@ let lastMessage = null;
35let lastMessageHash = null;35let lastMessageHash = null;
36let periodicMessageGenerationTimer = null;36let periodicMessageGenerationTimer = null;
37let lastPositionOfParagraphEnd = -1;37let lastPositionOfParagraphEnd = -1;
38let currentInitVoiceMapPromise = null;
3839
39const DEFAULT_VOICE_MARKER = '[Default Voice]';40const DEFAULT_VOICE_MARKER = '[Default Voice]';
40const DISABLED_VOICE_MARKER = 'disabled';41const DISABLED_VOICE_MARKER = 'disabled';
@@ -1010,9 +1011,33 @@ class VoiceMapEntry {
10101011
1011/**1012/**
1012 * Init voiceMapEntries for character select list.1013 * Init voiceMapEntries for character select list.
1014 * If an initialization is already in progress, it returns the existing Promise instead of starting a new one.
1013 * @param {boolean} unrestricted - If true, will include all characters in voiceMapEntries, even if they are not in the current chat.1015 * @param {boolean} unrestricted - If true, will include all characters in voiceMapEntries, even if they are not in the current chat.
1016 * @returns {Promise} A promise that resolves when the initialization is complete.
1014 */1017 */
1015export async function initVoiceMap(unrestricted = false) {1018export async function initVoiceMap(unrestricted = false) {
1019 // Preventing parallel execution
1020 if (currentInitVoiceMapPromise) {
1021 return currentInitVoiceMapPromise;
1022 }
1023
1024 currentInitVoiceMapPromise = (async () => {
1025 try {
1026 await initVoiceMapInternal(unrestricted);
1027 } finally {
1028 currentInitVoiceMapPromise = null;
1029 }
1030 })();
1031
1032 return currentInitVoiceMapPromise;
1033}
1034
1035/**
1036 * Init voiceMapEntries for character select list.
1037 * @param {boolean} unrestricted - If true, will include all characters in voiceMapEntries, even if they are not in the current chat.
1038 */
1039async function initVoiceMapInternal(unrestricted) {
1040
1016 // Gate initialization if not enabled or TTS Provider not ready. Prevents error popups.1041 // Gate initialization if not enabled or TTS Provider not ready. Prevents error popups.
1017 const enabled = $('#tts_enabled').is(':checked');1042 const enabled = $('#tts_enabled').is(':checked');
1018 if (!enabled) {1043 if (!enabled) {