Track read complete to read buffer until the end.

This commit is contained in:
Marc-Andre Ferland 2022-10-11 18:38:23 -04:00
parent 548149de8e
commit c39563b123

View File

@ -486,16 +486,23 @@ async function doMakeImage(task) {
let textDecoder = new TextDecoder() let textDecoder = new TextDecoder()
let finalJSON = '' let finalJSON = ''
let prevTime = -1 let prevTime = -1
let readComplete = false
while (true) { while (true) {
let t = new Date().getTime() let t = new Date().getTime()
let jsonStr = ''
if (!readComplete) {
const {value, done} = await reader.read() const {value, done} = await reader.read()
if (done && !finalJSON && !value) { if (done) {
readComplete = true
}
if (done && finalJSON.length <= 0 && !value) {
break break
} }
if (value) {
let timeTaken = (prevTime === -1 ? -1 : t - prevTime) jsonStr = textDecoder.decode(value)
let jsonStr = (value ? textDecoder.decode(value) : '') }
}
try { try {
// hack for a middleman buffering all the streaming updates, and unleashing them on the poor browser in one shot. // hack for a middleman buffering all the streaming updates, and unleashing them on the poor browser in one shot.
// this results in having to parse JSON like {"step": 1}{"step": 2}...{"status": "succeeded"..} // this results in having to parse JSON like {"step": 1}{"step": 2}...{"status": "succeeded"..}
@ -518,7 +525,7 @@ async function doMakeImage(task) {
throw e throw e
} }
} }
if (done) { if (readComplete && finalJSON.length <= 0) {
break break
} }
if (typeof stepUpdate === 'object' && 'step' in stepUpdate) { if (typeof stepUpdate === 'object' && 'step' in stepUpdate) {
@ -528,6 +535,7 @@ async function doMakeImage(task) {
let percent = 100 * (overallStepCount / totalSteps) let percent = 100 * (overallStepCount / totalSteps)
percent = (percent > 100 ? 100 : percent) percent = (percent > 100 ? 100 : percent)
percent = percent.toFixed(0) percent = percent.toFixed(0)
let timeTaken = (prevTime === -1 ? -1 : t - prevTime)
let stepsRemaining = totalSteps - overallStepCount let stepsRemaining = totalSteps - overallStepCount
stepsRemaining = (stepsRemaining < 0 ? 0 : stepsRemaining) stepsRemaining = (stepsRemaining < 0 ? 0 : stepsRemaining)