Extracting file processing handlers

This commit is contained in:
He4eT 2021-02-23 14:43:32 +05:00
commit aa6f7cabf1
3 changed files with 16 additions and 8 deletions

View file

@ -7,30 +7,28 @@ class FakeDialog {
constructor(handlers) { constructor(handlers) {
this.streaming = false this.streaming = false
this.handlers = handlers this.handlers = handlers
this.path = 'fake/path'
} }
file_ref_exists = ref => false file_ref_exists = ref => false
file_construct_ref(filename, usage, gameid) { file_construct_ref(filename, usage, gameid) {
return { return {
filename: [this.path, filename].join('/'), filename,
usage: usage || '' usage: usage || ''
} }
} }
file_read(dirent, israw) { file_read(dirent, israw) {
console.log('fake_file_read', dirent, israw) return this.handlers.onFileRead(dirent, israw)
return 'content'
} }
file_write(dirent, content, israw) { file_write(dirent, content, israw) {
if (content.length === 0) return (void null) if (content.length === 0) return (void null)
console.log('fake_file_write', dirent, israw, content.length) this.handlers.onFileWrite(dirent, content, israw)
} }
open(tosave, usage, gameid, callback) { open(tosave, usage, gameid, callback) {
this.handlers.onFileNameRequest(tosave, usage, callback) this.handlers.onFileNameRequest(tosave, usage, gameid, callback)
} }
log(message) { log(message) {

View file

@ -9,6 +9,8 @@ const noopHandlers = [
'onDisable', 'onDisable',
'onUpdateInputs', 'onUpdateInputs',
'onFileNameRequest', 'onFileNameRequest',
'onFileRead',
'onFileWrite',
'onExit', 'onExit',
'setSend' 'setSend'
].reduce((acc, x) => ((acc[x] = noop), acc), {}) ].reduce((acc, x) => ((acc[x] = noop), acc), {})

View file

@ -71,15 +71,21 @@ const onDisable = disable =>
? detach_handlers() ? detach_handlers()
: attach_handlers() : attach_handlers()
const onFileNameRequest = (tosave, usage, callback) => { const onFileNameRequest = (tosave, usage, gameid, callback) => {
stdout.write('\n') stdout.write('\n')
rl.question( rl.question(
'Please enter a file name (without an extension): ', 'Please enter a file name: ',
filename => callback(filename filename => callback(filename
? { filename, usage } ? { filename, usage }
: null)) : null))
} }
const onFileRead = (dirent, israw) =>
void console.log('onFileRead:', dirent)
const onFileWrite = (dirent, content, israw) =>
void console.log('onFileWrite:', dirent, content.length)
const handle_char_input = (str, key) => { const handle_char_input = (str, key) => {
const key_replacements = { const key_replacements = {
'\x7F': 'delete', '\x7F': 'delete',
@ -132,6 +138,8 @@ module.exports.handlers = {
onDisable, onDisable,
onUpdateInputs, onUpdateInputs,
onFileNameRequest, onFileNameRequest,
onFileRead,
onFileWrite,
onExit, onExit,
setSend setSend
} }