#48 Typescript implementation for settings and configuration
This commit is contained in:
parent
43e6cfc8f3
commit
d1e7df0264
|
@ -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);
|
||||
|
|
|
@ -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<string, boolean>;
|
||||
|
||||
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)) {
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue