diff options
Diffstat (limited to 'src/background/repositories/SettingRepository.ts')
-rw-r--r-- | src/background/repositories/SettingRepository.ts | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/background/repositories/SettingRepository.ts b/src/background/repositories/SettingRepository.ts index 15355ba..eb83a2c 100644 --- a/src/background/repositories/SettingRepository.ts +++ b/src/background/repositories/SettingRepository.ts @@ -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); } } |