SimpleChat:HandleResponseMultiPart using NewLines helper
Make handle_response_multipart logic better and cleaner. Now it allows for working with the situation, where the delta data line got from server in stream mode, could be split up when recving, but still the logic will handle it appropriately. ALERT: Rather except (for now) for last data line wrt a request's response.
This commit is contained in:
parent
7251714bcb
commit
07923745cf
1 changed files with 16 additions and 14 deletions
|
@ -322,30 +322,32 @@ class SimpleChat {
|
||||||
async handle_response_multipart(resp, apiEP, elDiv) {
|
async handle_response_multipart(resp, apiEP, elDiv) {
|
||||||
let elP = ui.el_create_append_p("", elDiv);
|
let elP = ui.el_create_append_p("", elDiv);
|
||||||
if (!resp.body) {
|
if (!resp.body) {
|
||||||
throw Error("ERRR:SimpleChat:SC:ReadJsonEarly:No body...");
|
throw Error("ERRR:SimpleChat:SC:HandleResponseMultiPart:No body...");
|
||||||
}
|
}
|
||||||
let tdUtf8 = new TextDecoder("utf-8");
|
let tdUtf8 = new TextDecoder("utf-8");
|
||||||
let rr = resp.body.getReader();
|
let rr = resp.body.getReader();
|
||||||
let gotBody = "";
|
let gotBody = "";
|
||||||
|
let xLines = new du.NewLines();
|
||||||
while(true) {
|
while(true) {
|
||||||
let { value: cur, done: done } = await rr.read();
|
let { value: cur, done: done } = await rr.read();
|
||||||
let curBody = tdUtf8.decode(cur);
|
let curBody = tdUtf8.decode(cur);
|
||||||
console.debug("DBUG:SC:PART:Str:", curBody);
|
console.debug("DBUG:SC:PART:Str:", curBody);
|
||||||
if (curBody.length > 0) {
|
xLines.add_append(curBody);
|
||||||
let curArrays = curBody.split("\n");
|
while(true) {
|
||||||
for(let curArray of curArrays) {
|
let curLine = xLines.shift();
|
||||||
console.debug("DBUG:SC:PART:StrPart:", curArray);
|
if (curLine == undefined) {
|
||||||
if (curArray.length <= 0) {
|
break;
|
||||||
|
}
|
||||||
|
if (curLine.trim() == "") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (curArray.startsWith("data:")) {
|
if (curLine.startsWith("data:")) {
|
||||||
curArray = curArray.substring(5);
|
curLine = curLine.substring(5);
|
||||||
}
|
}
|
||||||
let curJson = JSON.parse(curArray);
|
let curJson = JSON.parse(curLine);
|
||||||
console.debug("DBUG:SC:PART:Json:", curJson);
|
console.debug("DBUG:SC:PART:Json:", curJson);
|
||||||
gotBody += this.response_extract_stream(curJson, apiEP);
|
gotBody += this.response_extract_stream(curJson, apiEP);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
elP.innerText = gotBody;
|
elP.innerText = gotBody;
|
||||||
elP.scrollIntoView(false);
|
elP.scrollIntoView(false);
|
||||||
if (done) {
|
if (done) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue