Map support :D

This commit is contained in:
Official Noob
2020-01-09 16:39:23 +00:00
committed by GitHub
parent 339d05e157
commit 52f60d70e2

85
SB.js
View File

@@ -9,60 +9,77 @@ function storeEncode(data) {
return JSON.stringify(data); return JSON.stringify(data);
} }
function strParser(data) { function mapDecode(data, key) {
if(typeof data !== "string") return data;
try { try {
return new Map(JSON.parse(data)); let str = JSON.parse(data);
if(!Array.isArray(str)) return data;
return new Map(str);
} catch(e) { } catch(e) {
return data return data
} }
} }
class ListenerMap extends Map { function mapProxy(data, key) {
constructor(name) { if(!(data instanceof Map)) return data;
super(); return new mapIO(key);
}
this.name = name; class mapIO extends Map {
constructor(id) {
super();
this.id = id;
this.map = SB.localconfig[this.id];
} }
set(key, value) { set(key, value) {
super.set(key, value); SB.localconfig[this.id].set(key, value);
chrome.storage.sync.set({
this.updateListener(this.name, this); [this.id]: storeEncode(this.map)
} });
return this.map
delete(key) {
this.updateListener(this.name, this);
return super.set(key);
}
clear() {
return super.clear();
}
forEach(callbackfn) {
return super.forEach(callbackfn);
} }
get(key) { get(key) {
return strParser(super.get(key)); return this.map.get(key)
} }
has(key) { has(key) {
return super.has(key); return this.map.has(key)
}
toJSON() {
return Array.from(this.map.entries())
}
deleteProperty(key) {
if (this.map.has(key)) {
this.map.delete(key);
return true;
} else {
return false;
} }
} }
function mapHandler(name, object) { size() {
SB.config[name] = storeEncode(object.value); return this.map.size
}
delete(key) {
this.map.delete(key);
chrome.storage.sync.set({
[this.id]: storeEncode(this.map)
});
}
} }
function configProxy() { function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => { chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) { for (key in changes) {
Reflect.set(SB.localconfig, key, changes[key].newValue); Reflect.set(SB.localconfig, key, mapDecode(changes[key].newValue, key));
} }
}); });
var handler = { var handler = {
set: function(obj, prop, value) { set: function(obj, prop, value) {
chrome.storage.sync.set({ chrome.storage.sync.set({
@@ -70,7 +87,7 @@ function configProxy() {
}); });
}, },
get: function(obj, prop) { get: function(obj, prop) {
return strParser(Reflect.get(SB.localconfig, prop)); return mapProxy(Reflect.get(SB.localconfig, prop), prop);
} }
}; };
@@ -97,14 +114,13 @@ function migrate() { // Convert sponsorTimes format
async function config() { async function config() {
await fetchConfig(); await fetchConfig();
addDefaults(); addDefaults();
// Setup sponsorTime listener convertJson();
SB.localconfig.sponsorTimes.updateListener = mapHandler;
SB.config = configProxy(); SB.config = configProxy();
migrate(); migrate();
} }
SB.defaults = { SB.defaults = {
"sponsorTimes": new ListenerMap("sponsorTimes"), "sponsorTimes": new Map(),
"startSponsorKeybind": ";", "startSponsorKeybind": ";",
"submitKeybind": "'", "submitKeybind": "'",
"minutesSaved": 0, "minutesSaved": 0,
@@ -126,6 +142,11 @@ function resetConfig() {
SB.config = SB.defaults; SB.config = SB.defaults;
}; };
function convertJson() {
Object.keys(SB.defaults).forEach(key => {
SB.localconfig[key] = mapDecode(SB.localconfig[key], key);
});
}
// Add defaults // Add defaults
function addDefaults() { function addDefaults() {
Object.keys(SB.defaults).forEach(key => { Object.keys(SB.defaults).forEach(key => {