refactor: type 'config' Proxy

This commit is contained in:
Max Baumann
2020-12-15 18:29:47 +01:00
parent 094ef84f15
commit 09c527417d

View File

@@ -275,7 +275,7 @@ function decodeStoredItem<T>(id: string, data: T): T | SBMap<string, SponsorTime
return data; return data;
} }
function configProxy(): any { function configProxy(): SBConfig {
chrome.storage.onChanged.addListener((changes: {[key: string]: chrome.storage.StorageChange}) => { chrome.storage.onChanged.addListener((changes: {[key: string]: chrome.storage.StorageChange}) => {
for (const key in changes) { for (const key in changes) {
Config.localConfig[key] = decodeStoredItem(key, changes[key].newValue); Config.localConfig[key] = decodeStoredItem(key, changes[key].newValue);
@@ -286,8 +286,8 @@ function configProxy(): any {
} }
}); });
const handler: ProxyHandler<any> = { const handler: ProxyHandler<SBConfig> = {
set(obj, prop, value) { set<K extends keyof SBConfig>(obj: SBConfig, prop: K, value: SBConfig[K]) {
Config.localConfig[prop] = value; Config.localConfig[prop] = value;
chrome.storage.sync.set({ chrome.storage.sync.set({
@@ -297,13 +297,13 @@ function configProxy(): any {
return true; return true;
}, },
get(obj, prop): any { get<K extends keyof SBConfig>(obj: SBConfig, prop: K): SBConfig[K] {
const data = Config.localConfig[prop]; const data = Config.localConfig[prop];
return obj[prop] || data; return obj[prop] || data;
}, },
deleteProperty(obj, prop) { deleteProperty(obj: SBConfig, prop: keyof SBConfig) {
chrome.storage.sync.remove(<string> prop); chrome.storage.sync.remove(<string> prop);
return true; return true;
@@ -311,7 +311,7 @@ function configProxy(): any {
}; };
return new Proxy({handler}, handler); return new Proxy<SBConfig>({handler} as unknown as SBConfig, handler);
} }
function fetchConfig(): Promise<void> { function fetchConfig(): Promise<void> {