Formatting + duplicate code removal.

This commit is contained in:
Ajay Ramachandran 2020-01-09 12:30:09 -05:00
parent f42c23cd9a
commit a2e9688418

37
SB.js
View file

@ -4,31 +4,32 @@ Map.prototype.toJSON = function() {
return Array.from(this.entries());
};
class mapIO extends Map {
class MapIO extends Map {
constructor(id) {
super();
super();
this.id = id;
this.map = SB.localconfig[this.id];
}
set(key, value) {
SB.localconfig[this.id].set(key, value);
chrome.storage.sync.set({
[this.id]: storeEncode(this.map)
});
return this.map
this.map.set(key, value);
SB.config.handler.set(undefined, this.id, storeEncode(this.map));
return this.map;
}
get(key) {
return this.map.get(key)
return this.map.get(key);
}
has(key) {
return this.map.has(key)
return this.map.has(key);
}
toJSON() {
return Array.from(this.map.entries())
return Array.from(this.map.entries());
}
deleteProperty(key) {
@ -41,14 +42,13 @@ class mapIO extends Map {
}
size() {
return this.map.size
return this.map.size;
}
delete(key) {
this.map.delete(key);
chrome.storage.sync.set({
[this.id]: storeEncode(this.map)
});
SB.config.handler.set(undefined, this.id, storeEncode(this.map));
}
}
@ -58,7 +58,8 @@ function storeEncode(data) {
}
function mapDecode(data, key) {
if(typeof data !== "string") return data;
if(typeof data !== "string") return data;
try {
let str = JSON.parse(data);
if(!Array.isArray(str)) return data;
@ -70,7 +71,7 @@ function mapDecode(data, key) {
function mapProxy(data, key) {
if(!(data instanceof Map)) return data;
return new mapIO(key);
return new MapIO(key);
}
function configProxy() {
@ -87,12 +88,12 @@ function configProxy() {
});
},
get: function(obj, prop) {
return mapProxy(Reflect.get(SB.localconfig, prop), prop);
return obj[prop] || mapProxy(Reflect.get(SB.localconfig, prop), prop);
}
};
return new Proxy({}, handler);
return new Proxy({handler}, handler);
}
fetchConfig = () => new Promise((resolve, reject) => {