-
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 all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<script lang="ts"> | ||
import type { LineString } from "geojson"; | ||
import init from "route-snapper"; | ||
import { fetchWithProgress } from "route-snapper/lib.js"; | ||
import { onMount } from "svelte"; | ||
import { onMount, tick } from "svelte"; | ||
import type { FeatureWithProps } from "../../../maplibre_helpers"; | ||
import { currentMode, map } from "../../../stores"; | ||
import type { Mode } from "../../../types"; | ||
|
@@ -16,10 +15,14 @@ | |
export let changeMode: (m: Mode) => void; | ||
export let url: string; | ||
|
||
let progress: HTMLDivElement; | ||
export let routeTool: RouteTool; | ||
export let eventHandler: EventHandler; | ||
|
||
let progress: number = 0; | ||
let routeToolReady = false; | ||
$: downloadComplete = progress >= 100; | ||
let failedToLoadRouteTool = false; | ||
|
||
// While the new feature is being drawn, remember its last valid version | ||
let unsavedFeature: { value: FeatureWithProps<LineString> | null } = { | ||
value: null, | ||
|
@@ -45,11 +48,15 @@ | |
|
||
console.log(`Grabbing ${url}`); | ||
try { | ||
const graphBytes = await fetchWithProgress(url, progress); | ||
routeTool = new RouteTool($map, graphBytes); | ||
const graphBytes = await fetchWithProgress( | ||
url, | ||
(percentLoaded) => (progress = percentLoaded) | ||
); | ||
routeTool = new RouteTool($map, graphBytes, routeToolInitialised); | ||
} catch (err) { | ||
console.log(`Route tool broke: ${err}`); | ||
progress.innerHTML = "Failed to load"; | ||
failedToLoadRouteTool = true; | ||
|
||
return; | ||
} | ||
|
||
|
@@ -61,11 +68,58 @@ | |
changeMode | ||
); | ||
}); | ||
|
||
async function fetchWithProgress(url, setProgress: (number) => void) { | ||
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 routeToolInitialised() { | ||
progress = 100; | ||
routeToolReady = true; | ||
} | ||
</script> | ||
|
||
{#if !routeTool} | ||
<!-- TODO the text should be fixed, and the progress bar float --> | ||
<div bind:this={progress}>Route tool loading...</div> | ||
{#if !routeToolReady && !failedToLoadRouteTool && !downloadComplete} | ||
<label for="route-loading">Route tool loading</label> | ||
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. Elsewhere we've been doing |
||
<progress id="route-loading" value={progress} /> | ||
{:else if downloadComplete && !routeToolReady && !failedToLoadRouteTool} | ||
<label for="route-unpacking">Route data unpacking</label> | ||
<progress id="route-unpacking" /> | ||
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 really like the default animation of bouncing back and forth! So the strategy is:
|
||
{:else if failedToLoadRouteTool} | ||
<p>Failed to load</p> | ||
{:else if $currentMode == thisMode} | ||
<RouteControls {routeTool} extendRoute /> | ||
{/if} | ||
|
||
<style> | ||
progress { | ||
width: 100%; | ||
} | ||
</style> |
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.