queue all auto-executes until APP_READY

This commit is contained in:
LenAnderson 2024-01-17 20:32:25 +00:00
parent 617cabd7b5
commit 6fe17a1bed
1 changed files with 29 additions and 4 deletions

View File

@ -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));