You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
942 B
JavaScript
36 lines
942 B
JavaScript
2 years ago
|
// SARS-CoV-2-Viz
|
||
|
// Animated COVID case count visualization
|
||
|
// Copyright 2022 Edward L. Platt <ed@elplatt.com>
|
||
|
|
||
|
async function fetchWithProgress(url, onProgress) {
|
||
|
|
||
|
// Make request and get headers
|
||
|
let response = await fetch(url);
|
||
|
const reader = response.body.getReader();
|
||
|
const contentLength = response.headers.get('Content-Length');
|
||
|
|
||
|
// Receive chunks
|
||
|
let chunks = [];
|
||
|
let contentReceived = 0;
|
||
|
while (true) {
|
||
|
const {done, value} = await reader.read()
|
||
|
if (done) { break; }
|
||
|
chunks.push(value);
|
||
|
let bytes = value.length;
|
||
|
contentReceived += bytes;
|
||
|
|
||
|
// Update progress
|
||
|
onProgress(contentReceived);
|
||
|
}
|
||
|
|
||
|
// Combine chunks and decode
|
||
|
let contentBytes = new Uint8Array(contentReceived);
|
||
|
let position = 0;
|
||
|
chunks.forEach((chunk) => {
|
||
|
contentBytes.set(chunk, position);
|
||
|
position += chunk.length;
|
||
|
});
|
||
|
let content = new TextDecoder("utf-8").decode(contentBytes);
|
||
|
return content;
|
||
|
}
|