1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-10-10 19:14:33 +02:00

Add a database proxy

This commit is contained in:
Kushagra Gour
2017-11-06 10:23:43 +05:30
parent a29139485a
commit a230d6929f
4 changed files with 50 additions and 23 deletions

27
src/db.js Normal file
View File

@@ -0,0 +1,27 @@
(() => {
var local = {
get: (obj, cb) => {
if (typeof obj === 'string') {
setTimeout(() => cb(window.localStorage.getItem(obj)), 100);
} else {
const retVal = {};
Object.keys(obj).forEach(key => {
let val = window.localStorage.getItem(key);
retVal[key] =
val === undefined || val === null ? obj[key] : JSON.parse(val);
});
setTimeout(() => cb(retVal), 100);
}
},
set: (obj, cb) => {
Object.keys(obj).forEach(key => {
window.localStorage.setItem(key, JSON.stringify(obj[key]));
});
setTimeout(() => cb(), 100);
}
};
window.db = {
local: chrome && chrome.storage ? chrome.storage.local : local,
sync: chrome && chrome.storage ? chrome.storage.sync : local
};
})();