Skip to content

Commit

Permalink
Add a progress bar for the route snapper download. #23
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Nov 17, 2022
1 parent e1126a0 commit 92b545b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
7 changes: 3 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import { dropdown } from "./forms.js";
import { RouteSnapper } from "./route-snapper/lib.js";
import { RouteSnapper, fetchWithProgress } from "./route-snapper/lib.js";

export class App {
constructor() {
Expand Down Expand Up @@ -404,7 +404,6 @@ async function setupRouteSnapper(app) {
// TODO Slight hack. These files are stored in an S3 bucket, which only has an HTTP interface. When deployed to Github pages over HTTPS, we can't mix HTTP and HTTPS content, so use the Cloudfront HTTPS interface. That'll need CDN invalidations when we update these files. But when serving locally for development, HTTPS is also fine to use.
const url = `https://play.abstreet.org/route-snappers/${app.authority}.bin`;
console.log(`Grabbing ${url}`);
const resp = await fetch(url);
const mapBytes = await resp.arrayBuffer();
window.routeSnapper = new RouteSnapper(app, new Uint8Array(mapBytes));
const mapBytes = await fetchWithProgress(url, "snap-progress");
window.routeSnapper = new RouteSnapper(app, mapBytes);
}
32 changes: 32 additions & 0 deletions route-snapper/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,35 @@ export class RouteSnapper {
.setData(JSON.parse(this.inner.renderGeojson()));
}
}

export async function fetchWithProgress(url, progressId) {
const response = await fetch(url);
const reader = response.body.getReader();

const contentLength = response.headers.get("Content-Length");
const progressBar = document.getElementById(progressId);

let receivedLength = 0;
let chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}

chunks.push(value);
receivedLength += value.length;

const percent = (100.0 * receivedLength) / contentLength;
progressBar.style = `background: red; width: ${percent}%`;
}

let allChunks = new Uint8Array(receivedLength);
let position = 0;
for (let chunk of chunks) {
allChunks.set(chunk, position);
position += chunk.length;
}

return allChunks;
}
3 changes: 2 additions & 1 deletion scheme.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ <h1 id="authority">Loading...</h1>
</div>
<div id="panel" class="content-container"></div>
<div id="snap-tool" class="overlay-topright">
Route snapping tool loading...
<!-- TODO the text should be fixed, and the progress bar float -->
<div id="snap-progress">Route snapping tool loading...</div>
</div>
</main>

Expand Down

0 comments on commit 92b545b

Please sign in to comment.