Skip to content

Commit

Permalink
fix: indicate RuntimeError
Browse files Browse the repository at this point in the history
  • Loading branch information
istudyatuni committed Feb 9, 2022
1 parent 328a2e3 commit 5e1c517
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/components/Canvas.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
import { onMount, tick } from 'svelte'
import { onMount } from 'svelte'
import { settings } from 'src/stores/draw'
import { wasm } from 'src/stores/load'
Expand Down Expand Up @@ -30,12 +30,22 @@
export async function draw() {
// not works (status not shown)
wasm.set('calc')
await tick()
// wasm.set('calc')
// await tick()
field = gl.getImageData(0, 0, width, height)
field = await drawMandelbrot(field, $settings.lx, $settings.rx)
try {
field = await drawMandelbrot(field, $settings.lx, $settings.rx)
} catch (e) {
if (e.name === 'RuntimeError') {
wasm.set('fail')
console.error(e)
return
}
throw e
}
gl.putImageData(field, 0, 0)
wasm.set('none')
Expand Down
23 changes: 19 additions & 4 deletions src/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
import InputNumber from 'src/components/atoms/InputNumber.svelte'
import { settings } from 'src/stores/draw'
import { load } from 'src/stores/load'
import { wasm } from 'src/stores/load'
function getLoadText(state) {
/* eslint-disable indent */
switch (state) {
case 'load':
case 'calc':
return 'Loading'
case 'fail':
return 'Fail'
default:
return 'Refresh'
}
}
</script>

<script>
export let draw
$: loadText = $load ? 'Loading' : 'Refresh'
// show
let show = true
Expand All @@ -24,7 +35,11 @@
{#if show}
<div class="flex justify-between mb-3">
<Button on:click={toggleShow} class="mr-2">Hide</Button>
<Button on:click={() => window.location.reload()}>{loadText}</Button>
<Button
on:click={() => window.location.reload()}
danger={$wasm === 'fail'}>
{getLoadText($wasm)}
</Button>
</div>

<div class="flex">
Expand Down
9 changes: 8 additions & 1 deletion src/components/atoms/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<button class="rounded px-3 py-2 bg-gray-300 {$$props.class}" on:click>
<script>
export let danger = false
</script>

<button
class="rounded px-3 py-2 bg-gray-300 {$$props.class}"
on:click
class:text-red-500={danger}>
<slot />
</button>
2 changes: 1 addition & 1 deletion src/stores/load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { derived, writable } from 'svelte/store'

// none, load, calc
// none, load, calc, fail
export const wasm = writable('load')

export const load = derived(wasm, ($wasm, set) => {
Expand Down

0 comments on commit 5e1c517

Please sign in to comment.