Declare setting types
This commit is contained in:
parent
d01db82c0d
commit
a0882bbceb
48 changed files with 1618 additions and 903 deletions
|
@ -1,12 +1,12 @@
|
|||
import Setting from '../domains/Setting';
|
||||
import SettingData from '../../shared/SettingData';
|
||||
|
||||
export default class SettingRepository {
|
||||
async load(): Promise<any> {
|
||||
async load(): Promise<SettingData | null> {
|
||||
let { settings } = await browser.storage.local.get('settings');
|
||||
if (!settings) {
|
||||
return null;
|
||||
}
|
||||
return Setting.deserialize(settings);
|
||||
return SettingData.valueOf(settings);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import MemoryStorage from '../infrastructures/MemoryStorage';
|
||||
import Settings from '../../shared/Settings';
|
||||
import * as PropertyDefs from '../../shared/property-defs';
|
||||
|
||||
const CACHED_SETTING_KEY = 'setting';
|
||||
|
||||
|
@ -9,17 +11,41 @@ export default class SettingRepository {
|
|||
this.cache = new MemoryStorage();
|
||||
}
|
||||
|
||||
get(): Promise<any> {
|
||||
get(): Promise<Settings> {
|
||||
return Promise.resolve(this.cache.get(CACHED_SETTING_KEY));
|
||||
}
|
||||
|
||||
update(value: any): any {
|
||||
update(value: Settings): void {
|
||||
return this.cache.set(CACHED_SETTING_KEY, value);
|
||||
}
|
||||
|
||||
async setProperty(name: string, value: string): Promise<any> {
|
||||
async setProperty(
|
||||
name: string, value: string | number | boolean,
|
||||
): Promise<void> {
|
||||
let def = PropertyDefs.defs.find(d => name === d.name);
|
||||
if (!def) {
|
||||
throw new Error('unknown property: ' + name);
|
||||
}
|
||||
if (typeof value !== def.type) {
|
||||
throw new TypeError(`property type of ${name} mismatch: ${typeof value}`);
|
||||
}
|
||||
let newValue = value;
|
||||
if (typeof value === 'string' && value === '') {
|
||||
newValue = def.defaultValue;
|
||||
}
|
||||
|
||||
let current = await this.get();
|
||||
current.properties[name] = value;
|
||||
switch (name) {
|
||||
case 'hintchars':
|
||||
current.properties.hintchars = newValue as string;
|
||||
break;
|
||||
case 'smoothscroll':
|
||||
current.properties.smoothscroll = newValue as boolean;
|
||||
break;
|
||||
case 'complete':
|
||||
current.properties.complete = newValue as string;
|
||||
break;
|
||||
}
|
||||
return this.update(current);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue