diff options
Diffstat (limited to 'src/background/repositories')
4 files changed, 64 insertions, 55 deletions
diff --git a/src/background/repositories/CachedSettingRepository.ts b/src/background/repositories/CachedSettingRepository.ts index b73d94a..1af15d4 100644 --- a/src/background/repositories/CachedSettingRepository.ts +++ b/src/background/repositories/CachedSettingRepository.ts @@ -1,12 +1,20 @@ -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 { +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() { @@ -18,8 +26,9 @@ export default class CachedSettingRepository { return Promise.resolve(Settings.fromJSON(data)); } - update(value: Settings): void { - return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); + update(value: Settings): Promise<void> { + this.cache.set(CACHED_SETTING_KEY, value.toJSON()); + return Promise.resolve() } async setProperty( @@ -49,6 +58,6 @@ export default class CachedSettingRepository { current.properties.complete = newValue as string; break; } - return this.update(current); + await this.update(current); } } diff --git a/src/background/repositories/LocalSettingRepository.ts b/src/background/repositories/LocalSettingRepository.ts deleted file mode 100644 index 0c9ef3b..0000000 --- a/src/background/repositories/LocalSettingRepository.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { injectable } from 'tsyringe'; -import SettingData from '../../shared/SettingData'; - -@injectable() -export default class LocalSettingRepository { - async load(): Promise<SettingData | null> { - 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/SettingRepository.ts b/src/background/repositories/SettingRepository.ts new file mode 100644 index 0000000..b522045 --- /dev/null +++ b/src/background/repositories/SettingRepository.ts @@ -0,0 +1,49 @@ +import SettingData from '../../shared/SettingData'; + +export default interface SettingRepository { + load(): Promise<SettingData | null>; + + onChange(callback: () => void): void; +} + +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); + } + + onChange(callback: () => void) { + browser.storage.onChanged.addListener((changes, area) => { + if (area !== 'local') { + return; + } + if (changes.settings) { + callback(); + } + }); + } +} + +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); + } + + onChange(callback: () => void) { + browser.storage.onChanged.addListener((changes, area) => { + if (area !== 'sync') { + return; + } + if (changes.settings) { + callback(); + } + }); + } +}
\ No newline at end of file diff --git a/src/background/repositories/SyncSettingRepository.ts b/src/background/repositories/SyncSettingRepository.ts deleted file mode 100644 index 9f59e61..0000000 --- a/src/background/repositories/SyncSettingRepository.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { injectable } from 'tsyringe'; -import SettingData from '../../shared/SettingData'; - -@injectable() -export default class SyncSettingRepository { - async load(): Promise<SettingData | null> { - const { settings } = await browser.storage.sync.get('settings'); - if (!settings) { - return null; - } - return SettingData.fromJSON(settings as any); - } - - onChange(callback: () => void) { - browser.storage.onChanged.addListener((changes, area) => { - if (area !== 'sync') { - return; - } - if (changes.settings) { - callback(); - } - }); - } -} - |