Deextract prepareVM

This commit is contained in:
He4eT 2021-02-25 00:59:41 +05:00
commit 58235e18fb
5 changed files with 56 additions and 43 deletions

View file

@ -27,6 +27,7 @@
"wouter-preact": "^2.7.3" "wouter-preact": "^2.7.3"
}, },
"staticFiles": { "staticFiles": {
"staticPath": "node_modules/emglken/build/*.wasm" "staticPath": "node_modules/emglken/build",
"excludeGlob": "*.js"
} }
} }

View file

@ -0,0 +1,9 @@
import { h } from 'preact'
export default function ({ vmParts: { file, engine } }) {
return (
<div>
Player
</div>
)
}

View file

@ -1,30 +1,51 @@
import { h } from 'preact' import { h } from 'preact'
import { useState, useEffect } from 'preact/hooks' import { useState, useEffect } from 'preact/hooks'
import { prepareVM } from './common/if' import { engineByFilename } from './common/engines'
import Player from './Player'
const INITIAL_STATUS = { const INITIAL_STATUS = {
stage: 'loading', stage: 'loading',
details: 'Loading...' details: 'Loading...'
} }
const prepareVM = ({ url, setStatus, setParts }) => {
const st = (stage, details) => args => {
setStatus({ stage, details })
return args
}
return Promise.resolve()
.then(st('loading', 'Downloading file...'))
.then(_ => fetch(url))
.then(st('loading', 'Processing file...'))
.then(response => response.arrayBuffer())
.then(arrayBuffer => new Uint8Array(arrayBuffer))
.then(st('loading', 'Downloading engine...'))
.then(file => setParts({
file,
engine: engineByFilename(url)
}))
.then(st('loading', 'Running...'))
.catch(e => {
console.error(e)
setStatus({ stage: 'fail', details: e.message })
})
}
export default function ({ url }) { export default function ({ url }) {
const [status, setStatus] = useState(INITIAL_STATUS) const [status, setStatus] = useState(INITIAL_STATUS)
const [vmParts, setParts] = useState(null)
const [vm, setVM] = useState(null)
useEffect(prepareVM({
url,
setStatus,
setVM
}), [url])
useEffect(() => { useEffect(() => {
if (vm) console.log('success', vm) setStatus(INITIAL_STATUS)
}, [vm]) setParts(null)
return ( prepareVM({ url, setStatus, setParts })
<main> }, [url])
{status.details}
</main>) return vmParts
? (<Player vmParts={vmParts} />)
: (<div>{status.details}</div>)
} }

View file

@ -1,31 +1,7 @@
// import CheapGlkOte from 'cheap-glkote' // import CheapGlkOte from 'cheap-glkote'
// import engine from 'emglken/src/tads.js' // import engine from 'emglken/src/tads.js'
import { engineByFilename } from './engines' // import { engineByFilename } from './engines'
export const prepareVM = ({ url, setStatus, setVM }) => _ => {
const st = (stage, details) => args => {
setStatus({ stage, details })
return args
}
return Promise.resolve()
.then(st('loading', 'Downloading file...'))
.then(_ => fetch(url))
.then(st('loading', 'Processing file...'))
.then(response => response.arrayBuffer())
.then(arrayBuffer => new Uint8Array(arrayBuffer))
.then(st('loading', 'Downloading engine...'))
.then(file => setVM({
file,
engine: engineByFilename(url)
}))
.then(st('loading', 'Running...'))
.catch(e => {
console.error(e)
setStatus({ stage: 'fail', details: e.message })
})
}
// export const fetchGameFile = url => fetch(url) // export const fetchGameFile = url => fetch(url)
// .then(response => (console.log(response), response)) // .then(response => (console.log(response), response))

View file

@ -3,11 +3,17 @@ import { useState, useEffect } from 'preact/hooks'
import UrlPlayer from '~/src/components/player/UrlPlayer' import UrlPlayer from '~/src/components/player/UrlPlayer'
export default function ({setTheme, theme, encodedUrl}) { const decode = encodedUrl => decodeURIComponent(encodedUrl)
const [url] = useState(decodeURIComponent(encodedUrl))
export default function ({setTheme, theme, encodedUrl}) {
useEffect(() => setTheme(theme), [theme]) useEffect(() => setTheme(theme), [theme])
const [url, setUrl] = useState(decode(encodedUrl))
useEffect(() => {
setUrl(decode(encodedUrl))
}, [encodedUrl])
return ( return (
<main> <main>
<UrlPlayer url={url} /> <UrlPlayer url={url} />