Use document event handling for task events

This commit is contained in:
cmdr2 2023-08-22 12:02:34 +05:30
parent 4807744aa7
commit a8c0abfd5d
2 changed files with 47 additions and 27 deletions

View File

@ -2703,29 +2703,33 @@ let recentResolutionsValues = []
}) })
})() })()
TASK_CALLBACKS["before_task_start"].push(function(task) { document.addEventListener("before_task_start", (e) => {
let task = e.detail.task
// Update the seed *before* starting the processing so it's retained if user stops the task // Update the seed *before* starting the processing so it's retained if user stops the task
if (randomSeedField.checked) { if (randomSeedField.checked) {
seedField.value = task.seed seedField.value = task.seed
} }
}) })
TASK_CALLBACKS["after_task_start"].push(function(task) { document.addEventListener("after_task_start", (e) => {
// setStatus("request", "fetching..") // no-op implementation
renderButtons.style.display = "flex" renderButtons.style.display = "flex"
renameMakeImageButton() renameMakeImageButton()
updateInitialText() updateInitialText()
}) })
TASK_CALLBACKS["on_task_step"].push(function(task, reqBody, stepUpdate, outputContainer) { document.addEventListener("on_task_step", (e) => {
showImages(reqBody, stepUpdate, outputContainer, true) showImages(e.detail.reqBody, e.detail.stepUpdate, e.detail.outputContainer, true)
}) })
TASK_CALLBACKS["on_render_task_success"].push(function(task, reqBody, stepUpdate, outputContainer) { document.addEventListener("on_render_task_success", (e) => {
showImages(reqBody, stepUpdate, outputContainer, false) showImages(e.detail.reqBody, e.detail.stepUpdate, e.detail.outputContainer, false)
}) })
TASK_CALLBACKS["on_render_task_fail"].push(function(task, reqBody, stepUpdate, outputContainer) { document.addEventListener("on_render_task_fail", (e) => {
let task = e.detail.task
let stepUpdate = e.detail.stepUpdate
const outputMsg = task["outputMsg"] const outputMsg = task["outputMsg"]
let msg = "" let msg = ""
if ("detail" in stepUpdate && typeof stepUpdate.detail === "string" && stepUpdate.detail.length > 0) { if ("detail" in stepUpdate && typeof stepUpdate.detail === "string" && stepUpdate.detail.length > 0) {
@ -2774,7 +2778,7 @@ TASK_CALLBACKS["on_render_task_fail"].push(function(task, reqBody, stepUpdate, o
logError(msg, stepUpdate, outputMsg) logError(msg, stepUpdate, outputMsg)
}) })
TASK_CALLBACKS["on_all_tasks_complete"].push(function() { document.addEventListener("on_all_tasks_complete", (e) => {
renderButtons.style.display = "none" renderButtons.style.display = "none"
renameMakeImageButton() renameMakeImageButton()

View File

@ -4,15 +4,6 @@ const pauseBtn = document.querySelector("#pause")
const resumeBtn = document.querySelector("#resume") const resumeBtn = document.querySelector("#resume")
const processOrder = document.querySelector("#process_order_toggle") const processOrder = document.querySelector("#process_order_toggle")
let TASK_CALLBACKS = {
before_task_start: [],
after_task_start: [],
on_task_step: [],
on_render_task_success: [],
on_render_task_fail: [],
on_all_tasks_complete: [],
}
let pauseClient = false let pauseClient = false
async function onIdle() { async function onIdle() {
@ -141,7 +132,7 @@ async function onTaskStart(task) {
task["instances"].push(instance) task["instances"].push(instance)
task.batchesDone++ task.batchesDone++
TASK_CALLBACKS["before_task_start"].forEach((callback) => callback(task)) document.dispatchEvent(new CustomEvent("before_task_start", { detail: { task: task } }))
instance.enqueue(getTaskUpdater(task, newTaskReqBody, outputContainer)).then( instance.enqueue(getTaskUpdater(task, newTaskReqBody, outputContainer)).then(
(renderResult) => { (renderResult) => {
@ -152,7 +143,7 @@ async function onTaskStart(task) {
} }
) )
TASK_CALLBACKS["after_task_start"].forEach((callback) => callback(task)) document.dispatchEvent(new CustomEvent("after_task_start", { detail: { task: task } }))
} }
function getTaskUpdater(task, reqBody, outputContainer) { function getTaskUpdater(task, reqBody, outputContainer) {
@ -242,8 +233,15 @@ function getTaskUpdater(task, reqBody, outputContainer) {
progressBarInner.style.width = `${percent}%` progressBarInner.style.width = `${percent}%`
if (stepUpdate.output) { if (stepUpdate.output) {
TASK_CALLBACKS["on_task_step"].forEach((callback) => document.dispatchEvent(
callback(task, reqBody, stepUpdate, outputContainer) new CustomEvent("on_task_step", {
detail: {
task: task,
reqBody: reqBody,
stepUpdate: stepUpdate,
outputContainer: outputContainer,
},
})
) )
} }
} }
@ -253,13 +251,27 @@ function getTaskUpdater(task, reqBody, outputContainer) {
function onRenderTaskCompleted(task, reqBody, instance, outputContainer, stepUpdate) { function onRenderTaskCompleted(task, reqBody, instance, outputContainer, stepUpdate) {
if (typeof stepUpdate === "object") { if (typeof stepUpdate === "object") {
if (stepUpdate.status === "succeeded") { if (stepUpdate.status === "succeeded") {
TASK_CALLBACKS["on_render_task_success"].forEach((callback) => document.dispatchEvent(
callback(task, reqBody, stepUpdate, outputContainer) new CustomEvent("on_render_task_success", {
detail: {
task: task,
reqBody: reqBody,
stepUpdate: stepUpdate,
outputContainer: outputContainer,
},
})
) )
} else { } else {
task.isProcessing = false task.isProcessing = false
TASK_CALLBACKS["on_render_task_fail"].forEach((callback) => document.dispatchEvent(
callback(task, reqBody, stepUpdate, outputContainer) new CustomEvent("on_render_task_fail", {
detail: {
task: task,
reqBody: reqBody,
stepUpdate: stepUpdate,
outputContainer: outputContainer,
},
})
) )
} }
} }
@ -307,7 +319,11 @@ function onRenderTaskCompleted(task, reqBody, instance, outputContainer, stepUpd
resumeBtn.click() resumeBtn.click()
} }
TASK_CALLBACKS["on_all_tasks_complete"].forEach((callback) => callback()) document.dispatchEvent(
new CustomEvent("on_all_tasks_complete", {
detail: {},
})
)
} }
function resumeClient() { function resumeClient() {