diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-05 08:03:29 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-06 22:17:18 +0900 |
commit | a0882bbceb7ed71d56bf8557620449fbc3f19749 (patch) | |
tree | f2087d44f21dd68fc3584f62cfb9b62ae58bab2b /src/background/repositories | |
parent | d01db82c0dca352de2d7644c383d388fc3ec0366 (diff) |
Declare setting types
Diffstat (limited to 'src/background/repositories')
-rw-r--r-- | src/background/repositories/PersistentSettingRepository.ts | 6 | ||||
-rw-r--r-- | src/background/repositories/SettingRepository.ts | 34 |
2 files changed, 33 insertions, 7 deletions
diff --git a/src/background/repositories/PersistentSettingRepository.ts b/src/background/repositories/PersistentSettingRepository.ts index 3f2f4a1..18476fd 100644 --- a/src/background/repositories/PersistentSettingRepository.ts +++ b/src/background/repositories/PersistentSettingRepository.ts @@ -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); } } 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); } } |