diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-02-09 13:43:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-09 13:43:37 +0900 |
commit | e9f81d2e2494396bc9ebd827ab02aa8128f1a3ac (patch) | |
tree | c6382665c547443358163cb7cbaeeaba52d688b3 /src/background/repositories | |
parent | 8b9388f6e60fe67d20638f55aecb0ed1b281871a (diff) | |
parent | b2fc46ebf79ebb1ffa068fb513d1eeb9b50d7b3f (diff) |
Merge pull request #708 from ueokande/sync-storage
Synchronize settings with Firefox Sync
Diffstat (limited to 'src/background/repositories')
-rw-r--r-- | src/background/repositories/CachedSettingRepository.ts | 63 | ||||
-rw-r--r-- | src/background/repositories/PersistentSettingRepository.ts | 14 | ||||
-rw-r--r-- | src/background/repositories/SettingRepository.ts | 83 |
3 files changed, 102 insertions, 58 deletions
diff --git a/src/background/repositories/CachedSettingRepository.ts b/src/background/repositories/CachedSettingRepository.ts new file mode 100644 index 0000000..1af15d4 --- /dev/null +++ b/src/background/repositories/CachedSettingRepository.ts @@ -0,0 +1,63 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; +import Settings from '../../shared/settings/Settings'; +import Properties from '../../shared/settings/Properties'; + +const CACHED_SETTING_KEY = 'setting'; + +export default interface CachedSettingRepository { + get(): Promise<Settings>; + + update(value: Settings): Promise<void>; + + setProperty( + name: string, value: string | number | boolean, + ): Promise<void>; +} + +export class CachedSettingRepositoryImpl implements CachedSettingRepository { + private cache: MemoryStorage; + + constructor() { + this.cache = new MemoryStorage(); + } + + get(): Promise<Settings> { + const data = this.cache.get(CACHED_SETTING_KEY); + return Promise.resolve(Settings.fromJSON(data)); + } + + update(value: Settings): Promise<void> { + this.cache.set(CACHED_SETTING_KEY, value.toJSON()); + return Promise.resolve() + } + + async setProperty( + name: string, value: string | number | boolean, + ): Promise<void> { + 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; + } + await this.update(current); + } +} 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<SettingData | null> { - 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 index ba24e36..b522045 100644 --- a/src/background/repositories/SettingRepository.ts +++ b/src/background/repositories/SettingRepository.ts @@ -1,54 +1,49 @@ -import { injectable } from 'tsyringe'; -import MemoryStorage from '../infrastructures/MemoryStorage'; -import Settings from '../../shared/settings/Settings'; -import Properties from '../../shared/settings/Properties'; +import SettingData from '../../shared/SettingData'; -const CACHED_SETTING_KEY = 'setting'; +export default interface SettingRepository { + load(): Promise<SettingData | null>; -@injectable() -export default class SettingRepository { - private cache: MemoryStorage; - - constructor() { - this.cache = new MemoryStorage(); - } + onChange(callback: () => void): void; +} - get(): Promise<Settings> { - const data = this.cache.get(CACHED_SETTING_KEY); - return Promise.resolve(Settings.fromJSON(data)); +export class LocalSettingRepository implements SettingRepository { + async load(): Promise<SettingData | null> { + const {settings} = await browser.storage.local.get('settings'); + if (!settings) { + return null; + } + return SettingData.fromJSON(settings as any); } - update(value: Settings): void { - return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); + onChange(callback: () => void) { + browser.storage.onChanged.addListener((changes, area) => { + if (area !== 'local') { + return; + } + if (changes.settings) { + callback(); + } + }); } +} - async setProperty( - name: string, value: string | number | boolean, - ): Promise<void> { - 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; +export class SyncSettingRepository implements SettingRepository { + async load(): Promise<SettingData | null> { + const {settings} = await browser.storage.sync.get('settings'); + if (!settings) { + return null; } + return SettingData.fromJSON(settings as any); + } - 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); + onChange(callback: () => void) { + browser.storage.onChanged.addListener((changes, area) => { + if (area !== 'sync') { + return; + } + if (changes.settings) { + callback(); + } + }); } -} +}
\ No newline at end of file |