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