Last active
May 12, 2017 14:27
-
-
Save ste2425/33af9dbd6499964d932533b852c19af2 to your computer and use it in GitHub Desktop.
Electron persistent store
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
'use stirct'; | |
const electron = require('electron'), | |
userDataPath = (electron.app || electron.remote.app).getPath('userData'), | |
path = require('path'), | |
fs = require('fs'); | |
class Store { | |
constructor(configName = '') { | |
// Probably want to implement error handling | |
this.path = path.join(userDataPath, `${configName}.json`); | |
const data = JSON.parse(fs.readFileSync(this.path)); | |
this.data = new Proxy(data, { | |
set: (target, property, value) => { | |
Reflect.set(target, property, value); | |
fs.writeFileSync(this.path, JSON.stringify(this.data)); | |
return true; | |
} | |
}); | |
} | |
get(key) { | |
return this.data[key]; | |
} | |
set(key, val) { | |
this.data[key] = val; | |
} | |
} | |
module.exports = Store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment