Last active
February 19, 2020 01:21
-
-
Save ryunp/349bd3a154265633b06755b2ac705a74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mock File System | |
var files = new Map([ | |
["settings.json", '{"updateInterval": 1800000,"channelName":"myChannel"}'] | |
]) | |
// Mock File IO interface | |
var fsPromises = { | |
readFile: function readFile (path) { | |
const resolver = (resolve) => { | |
setTimeout(() => resolve(files.get(path)), 2000) | |
} | |
return new Promise(resolver) | |
}, | |
writeFile: function writeFile (path, data) { | |
const resolver = (resolve) => { | |
setTimeout(() => { | |
files.set(path, data) | |
resolve() | |
}, 2000) | |
} | |
return new Promise(resolver) | |
} | |
} | |
// App runtime state | |
class State extends EventTarget { | |
constructor() { | |
super() | |
this.saved = {"updateInterval": 3600000, "channelName": ""} | |
} | |
load () { | |
const onSuccess = (string) => { | |
this.dispatchEvent(new Event('loadComplete')) | |
this.saved = JSON.parse(string) | |
console.log(`Read ${string.length} bytes from ${CONFIG.savePath}`) | |
} | |
const onError = () => { | |
this.dispatchEvent(new Event('error')) | |
console.error(`Failed to read ${CONFIG.savePath}`) | |
return Promise.reject(error) | |
} | |
this.dispatchEvent(new Event('loadInitialized')) | |
return fsPromises.readFile(CONFIG.savePath).then(onSuccess, onError) | |
} | |
save () { | |
const string = JSON.stringify(this.saved) | |
const onSuccess = () => { | |
this.dispatchEvent(new Event('saveComplete')) | |
console.log(`Wrote ${string.length} bytes to ${CONFIG.savePath}`) | |
} | |
const onError = () => { | |
this.dispatchEvent(new Event('error')) | |
console.error(`Failed to write ${string.length} bytes to ${CONFIG.savePath}`) | |
return Promise.reject(error) | |
} | |
this.dispatchEvent(new Event('saveInitialized')) | |
return fsPromises.writeFile(CONFIG.savePath, string).then(onSuccess, onError) | |
} | |
get (key) { | |
return this.saved[key] | |
} | |
set (key, value) { | |
this.saved[key] = value | |
} | |
} | |
// App wide config options | |
var CONFIG = { | |
savePath: "settings.json" | |
} | |
// Create a new state object | |
var testState = new State() | |
// Configure event listeners | |
testState.addEventListener("loadInitialized", createCallback("testState", "loadInitialized")) | |
testState.addEventListener("loadComplete", createCallback("testState", "loadComplete")) | |
testState.addEventListener("saveInitialized", createCallback("testState", "saveInitialized")) | |
testState.addEventListener("saveComplete", createCallback("testState", "saveComplete")) | |
function createCallback(objName, eventName) { | |
return function eventCallback(e) { | |
console.log(`${objName} fired "${eventName}"`) | |
} | |
} | |
// Build asynchronous event chain | |
loadState() | |
.then(alterSetting.bind(null, "channelName", "derpyChannel")) | |
.then(saveState) | |
.catch(console.error) | |
function loadState () { | |
return testState.load().then(logFileContents) | |
} | |
function alterSetting (key, value) { | |
testState.set(key, value) | |
console.log(`Set '${key}' to '${value}'`) | |
} | |
function saveState () { | |
return testState.save().then(logFileContents) | |
} | |
function logFileContents () { | |
console.log(`File Contents: ${files.get(CONFIG.savePath)}`) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fsPromises = require('fs').promises | |
var EventEmitter = require('events') | |
class State extends EventEmitter { | |
varrutor(savePath) { | |
this.data = {} | |
this.savePath = savePath | |
} | |
save () { | |
fsPromises.writeFile(this.savePath, JSON.stringify(this.data)) | |
.then(() => { | |
this.emit('save') | |
logger.info(`Wrote ${fileData.length} bytes to ${this.savePath}`) | |
}) | |
.catch(error => { | |
this.emit('error', error) | |
logger.error(`Failed to write ${fileData.length} bytes to ${this.savePath}`) | |
return Promise.reject(error) | |
}) | |
} | |
load () { | |
fsPromises.readFile(this.savePath) | |
.then(data => { | |
this.emit('load') | |
this.data = JSON.parse(data) | |
logger.info(`Read ${data.length} bytes from ${this.savePath}`) | |
}) | |
.catch(error => { | |
this.emit('error', error) | |
logger.error(`Failed to read ${this.savePath}`) | |
return Promise.reject(error) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment