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/usecases/SettingUseCase.ts | |
parent | 8b9388f6e60fe67d20638f55aecb0ed1b281871a (diff) | |
parent | b2fc46ebf79ebb1ffa068fb513d1eeb9b50d7b3f (diff) |
Merge pull request #708 from ueokande/sync-storage
Synchronize settings with Firefox Sync
Diffstat (limited to 'src/background/usecases/SettingUseCase.ts')
-rw-r--r-- | src/background/usecases/SettingUseCase.ts | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/background/usecases/SettingUseCase.ts b/src/background/usecases/SettingUseCase.ts index d78d440..69b4572 100644 --- a/src/background/usecases/SettingUseCase.ts +++ b/src/background/usecases/SettingUseCase.ts @@ -1,35 +1,32 @@ -import { injectable } from 'tsyringe'; -import PersistentSettingRepository - from '../repositories/PersistentSettingRepository'; -import SettingRepository from '../repositories/SettingRepository'; -import { DefaultSettingData } from '../../shared/SettingData'; +import {inject, injectable} from 'tsyringe'; +import CachedSettingRepository from '../repositories/CachedSettingRepository'; +import SettingData, {DefaultSettingData} from '../../shared/SettingData'; import Settings from '../../shared/settings/Settings'; -import NotifyPresenter from '../presenters/NotifyPresenter'; +import Notifier from '../presenters/Notifier'; +import SettingRepository from "../repositories/SettingRepository"; @injectable() export default class SettingUseCase { constructor( - private persistentSettingRepository: PersistentSettingRepository, - private settingRepository: SettingRepository, - private notifyPresenter: NotifyPresenter, + @inject("LocalSettingRepository") private localSettingRepository: SettingRepository, + @inject("SyncSettingRepository") private syncSettingRepository: SettingRepository, + @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository, + @inject("Notifier") private notifier: Notifier, ) { } - get(): Promise<Settings> { - return this.settingRepository.get(); + getCached(): Promise<Settings> { + return this.cachedSettingRepository.get(); } async reload(): Promise<Settings> { - let data; + let data = DefaultSettingData; try { - data = await this.persistentSettingRepository.load(); + data = await this.loadSettings(); } catch (e) { this.showUnableToLoad(e); } - if (!data) { - data = DefaultSettingData; - } let value: Settings; try { @@ -38,13 +35,25 @@ export default class SettingUseCase { this.showUnableToLoad(e); value = DefaultSettingData.toSettings(); } - this.settingRepository.update(value!!); + await this.cachedSettingRepository.update(value!!); return value; } + private async loadSettings(): Promise<SettingData> { + const sync = await this.syncSettingRepository.load(); + if (sync) { + return sync; + } + const local = await this.localSettingRepository.load(); + if (local) { + return local; + } + return DefaultSettingData; + } + private showUnableToLoad(e: Error) { console.error('unable to load settings', e); - this.notifyPresenter.notifyInvalidSettings(() => { + this.notifier.notifyInvalidSettings(() => { browser.runtime.openOptionsPage(); }); } |