-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the route loading more obvious. #260
Changes from 3 commits
7ef20fe
682c69e
d208791
ce01e61
03d6a7b
061a24a
acdf41b
6762dd0
91ae41e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script lang="ts"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks unused. Is the intention to keep this or the one in |
||
import { onMount } from "svelte"; | ||
|
||
export let url: string; | ||
export let onSuccess: (bytes: Uint8Array) => void; | ||
|
||
// Both are units of bytes | ||
let bytesReceived = 0; | ||
let maxBytes = 100; | ||
let progressBar = { | ||
style: "background: linear-gradient(to right, red 0%, transparent 0);", | ||
}; | ||
|
||
onMount(async () => { | ||
console.log(`Fetching ${url} with a progress bar`); | ||
let response = await fetch(url); | ||
let reader = response.body.getReader(); | ||
|
||
// TODO Handle when missing? | ||
maxBytes = parseInt(response.headers.get("Content-Length")); | ||
|
||
let chunks = []; | ||
while (true) { | ||
let { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
|
||
chunks.push(value); | ||
bytesReceived += value.length; | ||
|
||
const percent = (bytesReceived / maxBytes) * 100; | ||
progressBar.style = `background: linear-gradient(to right, red ${percent}%, transparent 0);`; | ||
} | ||
|
||
let outputBytes = new Uint8Array(maxBytes); | ||
let position = 0; | ||
console.log( | ||
`max bytes ${maxBytes} - bytes received ${bytesReceived} - progress bar style ${progressBar.style}` | ||
); | ||
for (let chunk of chunks) { | ||
console.log( | ||
`position ${position}, outputBytes ${outputBytes.length} - chunk length ${chunk.length}` | ||
); | ||
outputBytes.set(chunk, position); | ||
position += chunk.length; | ||
} | ||
onSuccess(outputBytes); | ||
}); | ||
</script> | ||
|
||
<progress style={progressBar.style} value={bytesReceived} max={maxBytes} /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<script lang="ts"> | ||
import ProgressBar from "@megapenthes/svelte4-progressbar"; | ||
import type { LineString } from "geojson"; | ||
import init from "route-snapper"; | ||
import { fetchWithProgress } from "route-snapper/lib.js"; | ||
import { onMount } from "svelte"; | ||
import type { FeatureWithProps } from "../../../maplibre_helpers"; | ||
import { currentMode, map } from "../../../stores"; | ||
import { currentMode, map, routeInfo } from "../../../stores"; | ||
import type { Mode } from "../../../types"; | ||
import { handleUnsavedFeature, setupEventListeners } from "../common"; | ||
import type { EventHandler } from "../event_handler"; | ||
|
@@ -16,10 +16,12 @@ | |
export let changeMode: (m: Mode) => void; | ||
export let url: string; | ||
|
||
let progress: HTMLDivElement; | ||
export let routeTool: RouteTool; | ||
export let eventHandler: EventHandler; | ||
|
||
let progress: Array<number> = [0 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an array with just one element? (also, |
||
let routeToolReady = false; | ||
|
||
// While the new feature is being drawn, remember its last valid version | ||
let unsavedFeature: { value: FeatureWithProps<LineString> | null } = { | ||
value: null, | ||
|
@@ -45,11 +47,13 @@ | |
|
||
console.log(`Grabbing ${url}`); | ||
try { | ||
const graphBytes = await fetchWithProgress(url, progress); | ||
routeTool = new RouteTool($map, graphBytes); | ||
const graphBytes = await fetchWithProgress( | ||
url, | ||
(percentLoaded) => (progress[0] = Math.min(percentLoaded, 90)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So here's where we say "we got all of Content-Length, cap at 90". This is subtle / unintuitive, so definitely worth a comment |
||
); | ||
routeTool = new RouteTool($map, graphBytes, routeInfoDeserialised); | ||
} catch (err) { | ||
console.log(`Route tool broke: ${err}`); | ||
progress.innerHTML = "Failed to load"; | ||
return; | ||
} | ||
|
||
|
@@ -61,11 +65,51 @@ | |
changeMode | ||
); | ||
}); | ||
|
||
async function fetchWithProgress( | ||
url, | ||
setProgress: (number) => void = (percentLoaded) => {} | ||
) { | ||
const response = await fetch(url); | ||
const reader = response.body.getReader(); | ||
|
||
const contentLength = parseInt(response.headers.get("Content-Length")); | ||
|
||
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; | ||
setProgress(percent); | ||
} | ||
|
||
let allChunks = new Uint8Array(receivedLength); | ||
let position = 0; | ||
for (let chunk of chunks) { | ||
allChunks.set(chunk, position); | ||
position += chunk.length; | ||
} | ||
|
||
return allChunks; | ||
} | ||
|
||
function routeInfoDeserialised() { | ||
progress[0] = 100; | ||
routeToolReady = true; | ||
} | ||
</script> | ||
|
||
{#if !routeTool} | ||
{#if !routeToolReady} | ||
<!-- TODO the text should be fixed, and the progress bar float --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment becomes out of date with this PR |
||
<div bind:this={progress}>Route tool loading...</div> | ||
<p>Route tool loading</p> | ||
<ProgressBar bind:series={progress} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this new dependency helps us. It can draw lots of fancier progress bars, but we just want a simple linear one. In the spirit of keeping dependencies low, shouldn't we avoid it? We could maybe replace the older CSS hacks with https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress |
||
{:else if $currentMode == thisMode} | ||
<RouteControls {routeTool} extendRoute /> | ||
{/if} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,12 @@ export class RouteTool { | |
) => void)[]; | ||
eventListenersFailure: (() => void)[]; | ||
|
||
constructor(map: Map, graphBytes: Uint8Array) { | ||
constructor(map: Map, graphBytes: Uint8Array, deserialisedCallback=()=>{}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has only one caller, who sets the callback, so a default value seems odd. How about we just write the type? |
||
this.map = map; | ||
console.time("Deserialize and setup JsRouteSnapper"); | ||
this.inner = new JsRouteSnapper(graphBytes); | ||
console.timeEnd("Deserialize and setup JsRouteSnapper"); | ||
deserialisedCallback(); | ||
this.active = false; | ||
this.eventListenersSuccess = []; | ||
this.eventListenersUpdated = []; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we revert the changes here please? Guessing this was a rebase problem. Without changes to package.json, I wouldn't expect the lock file to change generally