refactored client side upload loop

This commit is contained in:
Danny Coates 2019-08-07 13:47:59 -07:00
parent e4231bbc0f
commit 27be72e0cd
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 14 additions and 11 deletions

View File

@ -212,32 +212,35 @@ async function upload(
let state = await reader.read();
let size = 0;
while (!state.done) {
const buf = state.value;
if (canceller.cancelled) {
throw canceller.error;
ws.close();
}
if (ws.readyState !== WebSocket.OPEN) {
break;
}
const buf = state.value;
ws.send(buf);
onprogress(size);
size += buf.length;
state = await reader.read();
while (
ws.bufferedAmount > ECE_RECORD_SIZE * 2 &&
ws.readyState === WebSocket.OPEN
ws.readyState === WebSocket.OPEN &&
!canceller.cancelled
) {
await delay();
}
}
const footer = new Uint8Array([0]);
ws.send(footer);
if (ws.readyState === WebSocket.OPEN) {
ws.send(new Uint8Array([0])); //EOF
}
await completedResponse;
ws.close();
return uploadInfo;
} catch (e) {
ws.close(4000);
throw e;
} finally {
if (![WebSocket.CLOSED, WebSocket.CLOSING].includes(ws.readyState)) {
ws.close();
}
}
}