aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/SettingUseCase.ts
blob: ee131c764aacda3f96e96629ffabefc134851eb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { injectable } from 'tsyringe';
import PersistentSettingRepository
  from '../repositories/PersistentSettingRepository';
import SettingRepository from '../repositories/SettingRepository';
import { DefaultSettingData } from '../../shared/SettingData';
import Settings from '../../shared/Settings';
import NotifyPresenter from '../presenters/NotifyPresenter';

@injectable()
export default class SettingUseCase {

  constructor(
    private persistentSettingRepository: PersistentSettingRepository,
    private settingRepository: SettingRepository,
    private notifyPresenter: NotifyPresenter,
  ) {
  }

  get(): Promise<Settings> {
    return this.settingRepository.get();
  }

  async reload(): Promise<Settings> {
    let data = await this.persistentSettingRepository.load();
    if (!data) {
      data = DefaultSettingData;
    }

    let value: Settings;
    try {
      value = data.toSettings();
    } catch (e) {
      this.notifyPresenter.notifyInvalidSettings(() => {
        browser.runtime.openOptionsPage();
      });
      value = DefaultSettingData.toSettings();
    }
    this.settingRepository.update(value!!);
    return value;
  }
}