Skip to content

Instantly share code, notes, and snippets.

@ste2425
Last active May 12, 2017 14:27
Show Gist options
  • Save ste2425/33af9dbd6499964d932533b852c19af2 to your computer and use it in GitHub Desktop.
Save ste2425/33af9dbd6499964d932533b852c19af2 to your computer and use it in GitHub Desktop.
Electron persistent store
'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