Add queue processing & callbacks

This commit is contained in:
Dominik Reh
2023-01-28 23:28:15 +01:00
parent b70b0b72cb
commit d1d3cd2bf5
3 changed files with 34 additions and 4 deletions

View File

@@ -39,9 +39,9 @@ var umiPreviousTags = [];
/// Similar to a callback system, but primitive.
// Queues
var afterInsertQueue = [];
var afterSetupQueue = [];
var afterConfigChangeQueue = [];
const afterInsertQueue = [];
const afterSetupQueue = [];
const afterConfigChangeQueue = [];
// List of parsers to try
var parsers = [];
const parsers = [];

View File

@@ -93,4 +93,23 @@ function escapeHTML(unsafeText) {
let div = document.createElement('div');
div.textContent = unsafeText;
return div.innerHTML;
}
// Queue calling function to process global queues
function processQueue(queue, context, ...args) {
for (let i = 0; i < queue.length; i++) {
queue[i].call(context, ...args);
}
}
// The same but with return values
function processQueueReturn(queue, context, ...args)
{
let results = [];
for (let i = 0; i < queue.length; i++) {
results.push(queue[i].call(context, ...args));
}
return results;
}
function processParsers(textArea, prompt) {
return processQueueReturn(parsers, null, textArea, prompt);
}

View File

@@ -224,6 +224,9 @@ async function syncOptions() {
// Apply changes
CFG = newCFG;
// Callback
processQueue(afterConfigChangeQueue, null);
}
// Create the result list div and necessary styling
@@ -359,6 +362,9 @@ function insertTextAtCursor(textArea, result, tagword) {
}
previousTags = tags;
// Callback
processQueue(afterInsertQueue, null, tagType);
// If it was a yaml wildcard, also update the umiPreviousTags
if (tagType === ResultType.yamlWildcard && originalTagword.length > 0) {
let umiSubPrompts = [...newPrompt.matchAll(UMI_PROMPT_REGEX)];
@@ -612,6 +618,8 @@ async function autocomplete(textArea, prompt, fixedTag = null) {
results = [];
tagword = tagword.toLowerCase().replace(/[\n\r]/g, "");
let resultCandidates = processParsers(textArea, prompt);
if (CFG.useWildcards && [...tagword.matchAll(WC_REGEX)].length > 0) {
// Show wildcards from a file with that name
wcMatch = [...tagword.matchAll(WC_REGEX)]
@@ -1286,6 +1294,9 @@ async function setup() {
acStyle.appendChild(document.createTextNode(css));
}
gradioApp().appendChild(acStyle);
// Callback
processQueue(afterSetupQueue, null);
}
onUiUpdate(async () => {