From a2e968841820e09f9a6aa1fb00293916de130f9c Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 9 Jan 2020 12:30:09 -0500 Subject: [PATCH] Formatting + duplicate code removal. --- SB.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/SB.js b/SB.js index b38c9642..d33dfc88 100644 --- a/SB.js +++ b/SB.js @@ -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) => {