insomnia/packages/insomnia-app/app/main/local-storage.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

73 lines
1.5 KiB
JavaScript

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 = {};
this._buffer = {};
mkdirp.sync(basePath);
console.log(`[localstorage] Initialized at ${basePath}`);
}
setItem (key, obj) {
clearTimeout(this._timeouts[key]);
this._buffer[key] = JSON.stringify(obj);
this._timeouts[key] = setTimeout(this._flush.bind(this), 100);
}
getItem (key, defaultObj) {
// Make sure things are flushed before we read
this._flush();
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) {
console.error(`[localstorage] Failed to parse item from LocalStorage: ${e}`);
return defaultObj;
}
}
_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}`);
}
}
}
_getKeyPath (key) {
return path.join(this._basePath, key);
}
}
export default LocalStorage;