From 02e8b55884fd530dee03613d79adad8aa9180b61 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 3 Feb 2020 12:11:17 +0900 Subject: Rename setting repositories and refactor --- .../repositories/CachedSettingRepository.ts | 54 ++++++++++++++++++++++ .../repositories/LocalSettingRepository.ts | 25 ++++++++++ .../repositories/PersistentSettingRepository.ts | 14 ------ src/background/repositories/SettingRepository.ts | 54 ---------------------- 4 files changed, 79 insertions(+), 68 deletions(-) create mode 100644 src/background/repositories/CachedSettingRepository.ts create mode 100644 src/background/repositories/LocalSettingRepository.ts delete mode 100644 src/background/repositories/PersistentSettingRepository.ts delete mode 100644 src/background/repositories/SettingRepository.ts (limited to 'src/background/repositories') diff --git a/src/background/repositories/CachedSettingRepository.ts b/src/background/repositories/CachedSettingRepository.ts new file mode 100644 index 0000000..b73d94a --- /dev/null +++ b/src/background/repositories/CachedSettingRepository.ts @@ -0,0 +1,54 @@ +import { injectable } from 'tsyringe'; +import MemoryStorage from '../infrastructures/MemoryStorage'; +import Settings from '../../shared/settings/Settings'; +import Properties from '../../shared/settings/Properties'; + +const CACHED_SETTING_KEY = 'setting'; + +@injectable() +export default class CachedSettingRepository { + private cache: MemoryStorage; + + constructor() { + this.cache = new MemoryStorage(); + } + + get(): Promise { + const data = this.cache.get(CACHED_SETTING_KEY); + return Promise.resolve(Settings.fromJSON(data)); + } + + update(value: Settings): void { + return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); + } + + async setProperty( + name: string, value: string | number | boolean, + ): Promise { + const def = Properties.def(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; + } + + const current = await this.get(); + 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); + } +} diff --git a/src/background/repositories/LocalSettingRepository.ts b/src/background/repositories/LocalSettingRepository.ts new file mode 100644 index 0000000..285b207 --- /dev/null +++ b/src/background/repositories/LocalSettingRepository.ts @@ -0,0 +1,25 @@ +import { injectable } from 'tsyringe'; +import SettingData from '../../shared/SettingData'; + +@injectable() +export default class LocalSettingRepository { + async load(): Promise { + const { settings } = await browser.storage.local.get('settings'); + if (!settings) { + return null; + } + return SettingData.fromJSON(settings as any); + } + + onChange(callback: () => void) { + browser.storage.onChanged.addListener((changes, area) => { + if (area !== 'local') { + return; + } + if (changes.settings) { + callback(); + } + }); + } +} + diff --git a/src/background/repositories/PersistentSettingRepository.ts b/src/background/repositories/PersistentSettingRepository.ts deleted file mode 100644 index c10f2cf..0000000 --- a/src/background/repositories/PersistentSettingRepository.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { injectable } from 'tsyringe'; -import SettingData from '../../shared/SettingData'; - -@injectable() -export default class SettingRepository { - async load(): Promise { - const { settings } = await browser.storage.local.get('settings'); - if (!settings) { - return null; - } - return SettingData.fromJSON(settings as any); - } -} - diff --git a/src/background/repositories/SettingRepository.ts b/src/background/repositories/SettingRepository.ts deleted file mode 100644 index ba24e36..0000000 --- a/src/background/repositories/SettingRepository.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { injectable } from 'tsyringe'; -import MemoryStorage from '../infrastructures/MemoryStorage'; -import Settings from '../../shared/settings/Settings'; -import Properties from '../../shared/settings/Properties'; - -const CACHED_SETTING_KEY = 'setting'; - -@injectable() -export default class SettingRepository { - private cache: MemoryStorage; - - constructor() { - this.cache = new MemoryStorage(); - } - - get(): Promise { - const data = this.cache.get(CACHED_SETTING_KEY); - return Promise.resolve(Settings.fromJSON(data)); - } - - update(value: Settings): void { - return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); - } - - async setProperty( - name: string, value: string | number | boolean, - ): Promise { - const def = Properties.def(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; - } - - const current = await this.get(); - 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); - } -} -- cgit v1.2.3