From 100bf6807016b54e303ebb16195aafde29b649e1 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Tue, 4 Jun 2024 16:21:16 +0300 Subject: [PATCH] Await results of router.push ref #721 --- src/App.vue | 6 +++--- src/views/LoadScaleView.vue | 39 ++++++++++++++++++++----------------- src/views/MosView.vue | 8 ++++---- src/views/NotFoundView.vue | 4 ++-- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/App.vue b/src/App.vue index 34791b50..ffd785aa 100644 --- a/src/App.vue +++ b/src/App.vue @@ -348,7 +348,7 @@ onMounted(async () => { // Special handling for the empty app state so that // the browser's back button can undo to the clean state. if (![...query.keys()].length) { - router.push({ path: getPath(url), query: { version } }) + await router.push({ path: getPath(url), query: { version } }) } else if (!query.has('version')) { // Scale Workshop 1 compatibility try { @@ -378,7 +378,7 @@ onMounted(async () => { audio.releaseTime = scaleWorkshopOneData.releaseTime // Replace query with version 3. - router.push({ path: getPath(url), query: { version } }) + await router.push({ path: getPath(url), query: { version } }) } catch (error) { console.error('Error parsing version 1 URL', error) } @@ -433,7 +433,7 @@ onMounted(async () => { scale.computeScale() // Replace query with version 3. - router.push({ path: getPath(url), query: { version } }) + await router.push({ path: getPath(url), query: { version } }) } catch (error) { console.error(`Error parsing version ${query.get('version')} URL`, error) } diff --git a/src/views/LoadScaleView.vue b/src/views/LoadScaleView.vue index 7b7c64a0..18e17134 100644 --- a/src/views/LoadScaleView.vue +++ b/src/views/LoadScaleView.vue @@ -13,14 +13,14 @@ const router = useRouter() const text = ref('Loading scale...') -onMounted(() => { +onMounted(async () => { const route = useRoute() - // Tildes are now wiki or filesystem friendly. + // Tildes are not wiki friendly. // Versions < 3.0.0-beta.38 used them. This replacing can be removed at the end of the beta cycle. const id = (route.params.id as string).replaceAll('~', '_') if (id === '000000000') { - router.push('/') + await router.push('/') return } @@ -32,27 +32,30 @@ onMounted(() => { if (!API_URL) { alert('API URL not configured') } else { - fetch(new URL(`scale/${id}.json.gz`, API_URL)) - .then((res) => { - if (res.ok) { - text.value = 'Scale loaded. Redirecting...' - return res.text() - } else if (res.status === 404) { - text.value = 'Scale not found.' - } else { - text.value = 'Internal server error.' - } - }) - .then((body) => { + try { + // XXX: Dashes are not filesystem friendly, but that's a problem for sw-server to solve. + // XXX: The api should probably be extensionless now that compression negotation makes sw-server bypassing much harder. + const res = await fetch(new URL(`scale/${id}.json.gz`, API_URL)) + if (res.ok) { + text.value = 'Scale loaded. Redirecting...' + const body = await res.text() if (body) { const payload = unpackPayload(body, id) audio.initialize() audio.fromJSON(payload.audio) scale.fromJSON(payload.scale) - router.push('/') + await router.push('/') + } else { + text.value = 'Received empty response from the server.' } - }) - .catch(() => (text.value = 'Failed to connect to server.')) + } else if (res.status === 404) { + text.value = 'Scale not found.' + } else { + text.value = 'Internal server error.' + } + } catch { + text.value = 'Failed to connect to server.' + } } }) diff --git a/src/views/MosView.vue b/src/views/MosView.vue index 3f481821..195932cc 100644 --- a/src/views/MosView.vue +++ b/src/views/MosView.vue @@ -66,9 +66,9 @@ function mos(mosName: string, mosPattern: string, udpStr: string) { updateScale() } -function done() { +async function done() { computeScale() - router.push('/') + await router.push('/') } const EASTER_EGG_SOURCE = `(* Create a fairly even lattice using the generators *) @@ -77,11 +77,11 @@ parallelotope([3, 5, 7], [1, 1, 1], [0, 0, 1], 1\\6) PrimeMapping(1200.000, 1901.955, 2786.316, 3368.819, 4151.323) ` -function easterEgg() { +async function easterEgg() { scale.name = 'Spoob' scale.sourceText = EASTER_EGG_SOURCE scale.computeScale() - router.push('/') + await router.push('/') } diff --git a/src/views/NotFoundView.vue b/src/views/NotFoundView.vue index 0f3fb7ae..42165aec 100644 --- a/src/views/NotFoundView.vue +++ b/src/views/NotFoundView.vue @@ -13,11 +13,11 @@ const ritualInProgress = ref(false) const router = useRouter() const scale = useScaleStore() -function openTheGates(source: string) { +async function openTheGates(source: string) { scale.sourceText = source scale.computeScale() ritualInProgress.value = false - router.push({ path: '/' }) + await router.push({ path: '/' }) }