diff --git a/public/scripts/extensions/quick-reply/index.js b/public/scripts/extensions/quick-reply/index.js index c0142978b..48d200ae3 100644 --- a/public/scripts/extensions/quick-reply/index.js +++ b/public/scripts/extensions/quick-reply/index.js @@ -58,6 +58,10 @@ const defaultSettings = { }; +/** @type {Boolean}*/ +let isReady = false; +/** @type {Function[]}*/ +let executeQueue = []; /** @type {QuickReplySettings}*/ let settings; /** @type {SettingsUi} */ @@ -144,6 +148,16 @@ const loadSettings = async () => { } }; +const executeIfReadyElseQueue = async (functionToCall, args) => { + if (isReady) { + log('calling', { functionToCall, args }); + await functionToCall(...args); + } else { + log('queueing', { functionToCall, args }); + executeQueue.push(async()=>await functionToCall(...args)); + } +}; + @@ -183,9 +197,20 @@ const init = async () => { slash.init(); autoExec = new AutoExecuteHandler(settings); + log('executing startup'); await autoExec.handleStartup(); + log('/executing startup'); + + log(`executing queue (${executeQueue.length} items)`); + while (executeQueue.length > 0) { + const func = executeQueue.shift(); + await func(); + } + log('/executing queue'); + isReady = true; + log('READY'); }; -await init(); +init(); const onChatChanged = async (chatIdx) => { log('CHAT_CHANGED', chatIdx); @@ -199,14 +224,14 @@ const onChatChanged = async (chatIdx) => { await autoExec.handleChatChanged(); }; -eventSource.on(event_types.CHAT_CHANGED, onChatChanged); +eventSource.on(event_types.CHAT_CHANGED, (...args)=>executeIfReadyElseQueue(onChatChanged, args)); const onUserMessage = async () => { await autoExec.handleUser(); }; -eventSource.on(event_types.USER_MESSAGE_RENDERED, onUserMessage); +eventSource.on(event_types.USER_MESSAGE_RENDERED, (...args)=>executeIfReadyElseQueue(onUserMessage, args)); const onAiMessage = async () => { await autoExec.handleAi(); }; -eventSource.on(event_types.CHARACTER_MESSAGE_RENDERED, onAiMessage); +eventSource.on(event_types.CHARACTER_MESSAGE_RENDERED, (...args)=>executeIfReadyElseQueue(onAiMessage, args));