From d1e7df0264c80353fb4e9ff503f156881f2bb41e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 22:15:28 +0100 Subject: [PATCH] #48 Typescript implementation for settings and configuration --- src/javascript/browserApi/storageProvider.ts | 16 +++++++++------- src/javascript/configuration/configuration.ts | 11 +++++++---- src/javascript/settings.ts | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/javascript/browserApi/storageProvider.ts b/src/javascript/browserApi/storageProvider.ts index ff53fa8..31a76bf 100644 --- a/src/javascript/browserApi/storageProvider.ts +++ b/src/javascript/browserApi/storageProvider.ts @@ -1,20 +1,22 @@ const { assigned } = require("../utils/helpers") +export type ConfigurationStorageBooleanCallback = (value: boolean) => void; + export interface ICustomBrowserStorageProvider { - setData(key: string, value: string): void; - getData(key: string, defaultValue: string, callback: (x: string) => void): void; + setDataAsBoolean(key: string, value: boolean): void; + getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void; } class StorageProviderChromium implements ICustomBrowserStorageProvider { - setData(key: string, value: string): void { + setDataAsBoolean(key: string, value: boolean): void { let obj = {}; obj[key] = value; this.getStorage().set(obj); } - getData(key: string, defaultValue: string, callback: (x: string) => void): void { + getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void { this.getStorage().get(key, items => { if (assigned(items) && assigned(items[key])) { callback(items[key]); @@ -37,19 +39,19 @@ class StorageProviderChromium implements ICustomBrowserStorageProvider { class StorageProviderFirefox implements ICustomBrowserStorageProvider { - setData(key: string, value: string): void { + setDataAsBoolean(key: string, value: boolean): void { let obj = {}; obj[key] = value; this.getStorage().set(obj); } - getData(key: string, defaultValue: string, callback: (x: string) => void): void { + getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void { let promise = this.getStorage().get(key); promise.then(items => { if (assigned(items) && assigned(items[key])) { - callback(items[key] as string); + callback(items[key] as boolean); } else { callback(defaultValue); diff --git a/src/javascript/configuration/configuration.ts b/src/javascript/configuration/configuration.ts index a0939f6..9f7136d 100644 --- a/src/javascript/configuration/configuration.ts +++ b/src/javascript/configuration/configuration.ts @@ -1,4 +1,4 @@ -import { getGlobalStorageProvider } from "../browserApi/storageProvider"; +import { ConfigurationStorageBooleanCallback, getGlobalStorageProvider } from "../browserApi/storageProvider"; import { assigned } from "../utils/helpers"; // website @@ -16,17 +16,20 @@ export const SETTINGS_playerAutoplayAfterScreenshot = 'playerAutoplayAfterScreen // w2g export const SETTINGS_w2gDisplayCharacterCounter = 'w2gDisplayCharacterCounter'; class Configuration { + settingsCache: Map; + constructor() { this.settingsCache = new Map(); } - getProperty(key, callback) { + getProperty(key: string, callback: ConfigurationStorageBooleanCallback): void { if (this.settingsCache.has(key)) { callback(this.settingsCache.get(key)); } else { + // TODO see commenz // OOOPS // currently all settings are default true. This isnĀ“t a problem but there should be much better soloutions after migration to typescript.... - getGlobalStorageProvider().getData(key, true, value => { + getGlobalStorageProvider().getDataAsBoolean(key, true, value => { this.settingsCache.set(key, value); callback(value); }); @@ -34,7 +37,7 @@ class Configuration { } } -let __globalConfig; +let __globalConfig: Configuration; export function getGlobalConfiguration() { if (!assigned(__globalConfig)) { diff --git a/src/javascript/settings.ts b/src/javascript/settings.ts index 86e4208..db82c4e 100644 --- a/src/javascript/settings.ts +++ b/src/javascript/settings.ts @@ -5,7 +5,7 @@ const OPTION_SELECTOR = 'input[type="checkbox"'; function storeOptions() { document.querySelectorAll(OPTION_SELECTOR).forEach(optionElement => { - getGlobalStorageProvider().setData(optionElement.id, optionElement.checked); + getGlobalStorageProvider().setDataAsBoolean(optionElement.id, optionElement.checked); }); } @@ -13,7 +13,7 @@ function restoreOptions() { document.querySelectorAll(OPTION_SELECTOR).forEach(optionElement => { let defaultValue = optionElement.dataset.defaultValue === 'true' ? true : false; - getGlobalStorageProvider().getData(optionElement.id, defaultValue, value => { + getGlobalStorageProvider().getDataAsBoolean(optionElement.id, defaultValue, value => { optionElement.checked = value; }); });