From 3e3b8eab6f5769016c8237169cc78ed29083b834 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 14:29:03 +0000 Subject: [PATCH 1/7] Save Map content --- SB.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/SB.js b/SB.js index 336e7057..0f19bbb0 100644 --- a/SB.js +++ b/SB.js @@ -21,9 +21,9 @@ class MapIO { set(key, value) { this.map.set(key, value); - - SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); - + chrome.storage.sync.set({ + [this.id]: encodeStoredItem(this.map) + }); return this.map; } @@ -38,6 +38,9 @@ class MapIO { deleteProperty(key) { if (this.map.has(key)) { this.map.delete(key); + chrome.storage.sync.set({ + [this.id]: encodeStoredItem(this.map) + }); return true; } else { return false; @@ -50,8 +53,6 @@ class MapIO { delete(key) { this.map.delete(key); - - SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); } } From 251339f26bb350757b81151cc9a0539faf1ae45b Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 17:57:54 +0000 Subject: [PATCH 2/7] Comments and added delete --- SB.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/SB.js b/SB.js index 0f19bbb0..269ac89c 100644 --- a/SB.js +++ b/SB.js @@ -15,32 +15,36 @@ Map.prototype.toJSON = function() { class MapIO { constructor(id) { - this.id = id; - this.map = SB.localConfig[this.id]; + this.id = id; + this.map = SB.localConfig[this.id]; } set(key, value) { + // Proxy to map this.map.set(key, value); + // Store updated map locally chrome.storage.sync.set({ [this.id]: encodeStoredItem(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); } deleteProperty(key) { if (this.map.has(key)) { + // Proxy to map this.map.delete(key); - chrome.storage.sync.set({ - [this.id]: encodeStoredItem(this.map) - }); + // Store updated map locally + chrome.storage.sync.set({ + [this.id]: encodeStoredItem(this.map) + }); return true; } else { return false; @@ -52,7 +56,12 @@ class MapIO { } delete(key) { + // Proxy to map this.map.delete(key); + // Store updated map locally + chrome.storage.sync.set({ + [this.id]: encodeStoredItem(this.map) + }); } } From e784dc017d3f17f6843d2eb6091dea1cc6e75c45 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 18:02:48 +0000 Subject: [PATCH 3/7] Added clear --- SB.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SB.js b/SB.js index 269ac89c..69f95d31 100644 --- a/SB.js +++ b/SB.js @@ -63,6 +63,10 @@ class MapIO { [this.id]: encodeStoredItem(this.map) }); } + + clear(key) { + return this.map.clear(); + } } /** From e286797ac5980bddc0f854fdb6155d48bf36b1c8 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 18:03:40 +0000 Subject: [PATCH 4/7] Update SB.js --- SB.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SB.js b/SB.js index 69f95d31..4b7f9330 100644 --- a/SB.js +++ b/SB.js @@ -65,7 +65,10 @@ class MapIO { } clear(key) { - return this.map.clear(); + this.map.clear(); + chrome.storage.sync.set({ + [this.id]: encodeStoredItem(this.map) + }); } } From 5df0801c64d867a26f03b240a439a9e884d5bc06 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 18:13:07 +0000 Subject: [PATCH 5/7] deleteProperty() if no key provided delete by id --- SB.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SB.js b/SB.js index 4b7f9330..37fcbea9 100644 --- a/SB.js +++ b/SB.js @@ -38,6 +38,9 @@ class MapIO { } deleteProperty(key) { + if(key === undefined) { + return chrome.storage.sync.remove(this.id); + } if (this.map.has(key)) { // Proxy to map this.map.delete(key); From 4cdb874b4f2dc0844c6ab6a40f3c42112aa10297 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 18:27:29 +0000 Subject: [PATCH 6/7] Removed deleteProperty from MapIO --- SB.js | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/SB.js b/SB.js index 37fcbea9..c11575d7 100644 --- a/SB.js +++ b/SB.js @@ -37,23 +37,6 @@ class MapIO { return this.map.has(key); } - deleteProperty(key) { - if(key === undefined) { - return chrome.storage.sync.remove(this.id); - } - if (this.map.has(key)) { - // Proxy to map - this.map.delete(key); - // Store updated map locally - chrome.storage.sync.set({ - [this.id]: encodeStoredItem(this.map) - }); - return true; - } else { - return false; - } - } - size() { return this.map.size; } @@ -67,7 +50,7 @@ class MapIO { }); } - clear(key) { + clear() { this.map.clear(); chrome.storage.sync.set({ [this.id]: encodeStoredItem(this.map) @@ -82,8 +65,8 @@ class MapIO { * @param {*} data */ function encodeStoredItem(data) { - if(!(data instanceof Map)) return data; - return JSON.stringify(data); + if(!(data instanceof Map)) return data; + return JSON.stringify(data); } /** @@ -135,7 +118,7 @@ function configProxy() { }, deleteProperty(obj, prop) { - chrome.storage.sync.remove(prop); + chrome.storage.sync.remove(prop); } }; From f7a3f98ad3c5e978ef11ba3a5ad469a38ccf873c Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Thu, 16 Jan 2020 18:43:49 +0000 Subject: [PATCH 7/7] Comments --- SB.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/SB.js b/SB.js index c11575d7..86582311 100644 --- a/SB.js +++ b/SB.js @@ -9,16 +9,21 @@ SB = { // Function setup +// Allows a map to be conveted into json form +// Currently used for local storage Map.prototype.toJSON = function() { return Array.from(this.entries()); }; +// Proxy Map changes to Map in SB.localConfig +// Saves the changes to chrome.storage in json form class MapIO { constructor(id) { + // The name of the item in the array this.id = id; + // A local copy of the map (SB.config.mapname.map) this.map = SB.localConfig[this.id]; } - set(key, value) { // Proxy to map this.map.set(key, value); @@ -65,6 +70,7 @@ class MapIO { * @param {*} data */ function encodeStoredItem(data) { + // if data is Map convert to json for storing if(!(data instanceof Map)) return data; return JSON.stringify(data); } @@ -120,7 +126,7 @@ function configProxy() { deleteProperty(obj, prop) { chrome.storage.sync.remove(prop); } - + }; return new Proxy({handler}, handler);