Switched to a listener map.

This commit is contained in:
Ajay Ramachandran
2020-01-07 22:59:50 -05:00
parent eb4bf89194
commit fa19e435cc
4 changed files with 193 additions and 134 deletions

59
SB.js
View File

@@ -1,21 +1,69 @@
SB = {};
class ListenerMap extends Map {
constructor(name) {
super();
this.name = name;
}
set(key, value) {
super.set(key, value);
this.updateListener(this.name, this);
}
delete(key) {
this.updateListener(this.name, this);
return super.set(key);
}
clear() {
return super.clear();
}
forEach(callbackfn) {
return super.forEach(callbackfn);
}
get(key) {
return super.get(key);
}
has(key) {
return super.has(key);
}
}
function mapHandler(name, object) {
SB.config[name] = SB.config[name];
// chrome.storage.sync.set({
// [name]: object
// });
// console.log(name)
// console.log(object)
}
function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) {
SB.localconfig[key] = changes[key].newValue;
}
});
var handler = {
set: function(obj, prop, value) {
chrome.storage.sync.set({
[prop]: value
})
});
},
get: function(obj, prop) {
return SB.localconfig[prop]
return SB.localconfig[prop];
}
};
return new Proxy({}, handler);
}
@@ -38,12 +86,17 @@ function migrate() { // Convert sponsorTimes format
async function config() {
await fetchConfig();
addDefaults();
// Setup sponsorTime listener
SB.localconfig.sponsorTimes.updateListener = mapHandler;
SB.config = configProxy();
migrate();
}
SB.defaults = {
"sponsorTimes": new Map(),
"sponsorTimes": new ListenerMap("sponsorTimes"),
"startSponsorKeybind": ";",
"submitKeybind": "'",
"minutesSaved": 0,

View File

@@ -4,7 +4,9 @@ chrome.tabs.onUpdated.addListener(function(tabId) {
}, () => void chrome.runtime.lastError ); // Suppress error on Firefox
});
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
chrome.runtime.onMessage.addListener(async function (request, sender, callback) {
await wait(() => SB.config !== undefined);
switch(request.message) {
case "submitTimes":
submitTimes(request.videoID, callback);

View File

@@ -21,6 +21,8 @@ async function runThePopup() {
inPopup = false;
}
await wait(() => SB.config !== undefined);
["sponsorStart",
// Top toggles
"whitelistChannel",
@@ -229,6 +231,8 @@ async function runThePopup() {
}
//load video times for this video
console.log( SB.config.sponsorTimes.set)
setTimeout(()=> console.log( SB.config.sponsorTimes.set), 200 )
let sponsorTimesStorage = SB.config.sponsorTimes.get(currentVideoID);
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
if (sponsorTimesStorage[sponsorTimesStorage.length - 1] != undefined && sponsorTimesStorage[sponsorTimesStorage.length - 1].length < 2) {