Player: extract handlers

This commit is contained in:
He4eT 2021-07-19 23:40:24 +05:00
commit c74c425041
2 changed files with 48 additions and 47 deletions

View file

@ -13,6 +13,8 @@ import GridBuffer from './GridBuffer'
import InputBox from './InputBox'
import Status from './Status'
import {Handlers} from './playerHandlers'
import './player.css'
const INITIAL_STATUS = {
@ -30,53 +32,6 @@ const runMachine = ({ engine: Engine, file, handlers }) => {
return { sendFn, instance: vm }
}
const Handlers = ({
setStatus,
setWindows,
setCurrentWindowId,
setInputType,
setInbox
}) => ({
onInit: _ => {
setStatus({ stage: 'ready' })
},
/* */
onUpdateWindows: windows => {
setWindows(windows)
},
onUpdateInputs: data => {
if (data.length === 0) return void null
const {type, id} = data[0]
setCurrentWindowId(id)
setInputType(type)
},
onUpdateContent: inbox => {
setInbox(inbox)
},
onDisable: _ => {
setInputType(null)
},
/* */
onFileNameRequest: (tosave, usage, _, setFileName) => {
setFileName({
usage,
filename: prompt('Enter the filename')
})
},
onFileRead: ({ filename }) => {
const content = localStorage.getItem(`fake-fs/${filename}`)
return decode(content)
},
onFileWrite: ({ filename }, content) => {
localStorage.setItem(`fake-fs/${filename}`, encode(content))
},
/* */
onExit: _ => {
setInputType(null)
}
})
export default function ({
vmParts: { file, engine }, singleWindow
}) {

View file

@ -0,0 +1,46 @@
export const Handlers = ({
setStatus,
setWindows,
setCurrentWindowId,
setInputType,
setInbox
}) => ({
onInit: _ => {
setStatus({ stage: 'ready' })
},
/* */
onUpdateWindows: windows => {
setWindows(windows)
},
onUpdateInputs: data => {
if (data.length === 0) return void null
const {type, id} = data[0]
setCurrentWindowId(id)
setInputType(type)
},
onUpdateContent: inbox => {
setInbox(inbox)
},
onDisable: _ => {
setInputType(null)
},
/* */
onFileNameRequest: (tosave, usage, _, setFileName) => {
setFileName({
usage,
filename: prompt('Enter the filename')
})
},
onFileRead: ({ filename }) => {
const content = localStorage.getItem(`fake-fs/${filename}`)
return decode(content)
},
onFileWrite: ({ filename }, content) => {
localStorage.setItem(`fake-fs/${filename}`, encode(content))
},
/* */
onExit: _ => {
setInputType(null)
}
})