2016-11-10 05:47:20 +00:00
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
class LocalStorage {
|
|
|
|
constructor (basePath) {
|
|
|
|
this._basePath = basePath;
|
|
|
|
|
|
|
|
// Debounce writes on a per key basis
|
|
|
|
this._timeouts = {};
|
2016-11-10 07:34:26 +00:00
|
|
|
this._buffer = {};
|
2016-11-10 05:47:20 +00:00
|
|
|
|
|
|
|
mkdirp.sync(basePath);
|
|
|
|
console.log(`[localstorage] Initialized at ${basePath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
setItem (key, obj) {
|
|
|
|
clearTimeout(this._timeouts[key]);
|
2016-11-10 07:34:26 +00:00
|
|
|
this._buffer[key] = JSON.stringify(obj);
|
|
|
|
this._timeouts[key] = setTimeout(this._flush.bind(this), 100);
|
2016-11-10 05:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getItem (key, defaultObj) {
|
2016-11-10 07:34:26 +00:00
|
|
|
// Make sure things are flushed before we read
|
|
|
|
this._flush();
|
|
|
|
|
2016-11-10 05:47:20 +00:00
|
|
|
let contents = JSON.stringify(defaultObj);
|
|
|
|
|
|
|
|
const path = this._getKeyPath(key);
|
|
|
|
try {
|
|
|
|
contents = fs.readFileSync(path);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ENOENT') {
|
|
|
|
this.setItem(key, defaultObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(contents)
|
|
|
|
} catch (e) {
|
2016-11-10 09:00:29 +00:00
|
|
|
console.error(`[localstorage] Failed to parse item from LocalStorage: ${e}`)
|
|
|
|
return defaultObj;
|
2016-11-10 05:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 07:34:26 +00:00
|
|
|
_flush () {
|
|
|
|
const keys = Object.keys(this._buffer);
|
|
|
|
|
|
|
|
if (!keys.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
const contents = this._buffer[key];
|
|
|
|
const path = this._getKeyPath(key);
|
|
|
|
|
|
|
|
delete this._buffer[key];
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(path, contents);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`[localstorage] Failed to save to LocalStorage: ${e}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 05:47:20 +00:00
|
|
|
_getKeyPath (key) {
|
|
|
|
return path.join(this._basePath, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LocalStorage;
|