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