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 --- src/background/Application.ts | 11 ++--- src/background/controllers/SettingController.ts | 2 +- .../repositories/CachedSettingRepository.ts | 54 ++++++++++++++++++++++ .../repositories/LocalSettingRepository.ts | 25 ++++++++++ .../repositories/PersistentSettingRepository.ts | 14 ------ src/background/repositories/SettingRepository.ts | 54 ---------------------- src/background/usecases/CommandUseCase.ts | 4 +- src/background/usecases/CompletionsUseCase.ts | 4 +- src/background/usecases/SettingUseCase.ts | 17 ++++--- 9 files changed, 96 insertions(+), 89 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') diff --git a/src/background/Application.ts b/src/background/Application.ts index 08013cd..d5e7a67 100644 --- a/src/background/Application.ts +++ b/src/background/Application.ts @@ -2,6 +2,7 @@ import { injectable } from 'tsyringe'; import ContentMessageListener from './infrastructures/ContentMessageListener'; import SettingController from './controllers/SettingController'; import VersionController from './controllers/VersionController'; +import LocalSettingRepository from "./repositories/LocalSettingRepository"; @injectable() export default class Application { @@ -9,6 +10,7 @@ export default class Application { private contentMessageListener: ContentMessageListener, private settingController: SettingController, private versionController: VersionController, + private localSettingRepository: LocalSettingRepository ) { } @@ -23,13 +25,8 @@ export default class Application { }); this.contentMessageListener.run(); - browser.storage.onChanged.addListener((changes, area) => { - if (area !== 'local') { - return; - } - if (changes.settings) { - this.settingController.reload(); - } + this.localSettingRepository.onChange(() => { + this.settingController.reload(); }); } } diff --git a/src/background/controllers/SettingController.ts b/src/background/controllers/SettingController.ts index 8d05852..26edc07 100644 --- a/src/background/controllers/SettingController.ts +++ b/src/background/controllers/SettingController.ts @@ -12,7 +12,7 @@ export default class SettingController { } getSetting(): Promise { - return this.settingUseCase.get(); + return this.settingUseCase.getCached(); } async reload(): Promise { 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); - } -} diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index fcb898d..20ded7e 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -5,7 +5,7 @@ import * as urls from '../../shared/urls'; import TabPresenter from '../presenters/TabPresenter'; import WindowPresenter from '../presenters/WindowPresenter'; import HelpPresenter from '../presenters/HelpPresenter'; -import SettingRepository from '../repositories/SettingRepository'; +import CachedSettingRepository from '../repositories/CachedSettingRepository'; import BookmarkRepository from '../repositories/BookmarkRepository'; import ConsoleClient from '../infrastructures/ConsoleClient'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; @@ -17,7 +17,7 @@ export default class CommandIndicator { private tabPresenter: TabPresenter, private windowPresenter: WindowPresenter, private helpPresenter: HelpPresenter, - private settingRepository: SettingRepository, + private settingRepository: CachedSettingRepository, private bookmarkRepository: BookmarkRepository, private consoleClient: ConsoleClient, private contentMessageClient: ContentMessageClient, diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts index 779c61d..72ba929 100644 --- a/src/background/usecases/CompletionsUseCase.ts +++ b/src/background/usecases/CompletionsUseCase.ts @@ -3,7 +3,7 @@ import CompletionGroup from '../domains/CompletionGroup'; import CommandDocs from '../domains/CommandDocs'; import CompletionsRepository from '../repositories/CompletionsRepository'; import * as filters from './filters'; -import SettingRepository from '../repositories/SettingRepository'; +import CachedSettingRepository from '../repositories/CachedSettingRepository'; import TabPresenter from '../presenters/TabPresenter'; import Properties from '../../shared/settings/Properties'; @@ -17,7 +17,7 @@ export default class CompletionsUseCase { constructor( private tabPresenter: TabPresenter, private completionsRepository: CompletionsRepository, - private settingRepository: SettingRepository, + private settingRepository: CachedSettingRepository, ) { } diff --git a/src/background/usecases/SettingUseCase.ts b/src/background/usecases/SettingUseCase.ts index d78d440..bd61157 100644 --- a/src/background/usecases/SettingUseCase.ts +++ b/src/background/usecases/SettingUseCase.ts @@ -1,7 +1,6 @@ import { injectable } from 'tsyringe'; -import PersistentSettingRepository - from '../repositories/PersistentSettingRepository'; -import SettingRepository from '../repositories/SettingRepository'; +import LocalSettingRepository from '../repositories/LocalSettingRepository'; +import CachedSettingRepository from '../repositories/CachedSettingRepository'; import { DefaultSettingData } from '../../shared/SettingData'; import Settings from '../../shared/settings/Settings'; import NotifyPresenter from '../presenters/NotifyPresenter'; @@ -10,20 +9,20 @@ import NotifyPresenter from '../presenters/NotifyPresenter'; export default class SettingUseCase { constructor( - private persistentSettingRepository: PersistentSettingRepository, - private settingRepository: SettingRepository, + private localSettingRepository: LocalSettingRepository, + private cachedSettingRepository: CachedSettingRepository, private notifyPresenter: NotifyPresenter, ) { } - get(): Promise { - return this.settingRepository.get(); + getCached(): Promise { + return this.cachedSettingRepository.get(); } async reload(): Promise { let data; try { - data = await this.persistentSettingRepository.load(); + data = await this.localSettingRepository.load(); } catch (e) { this.showUnableToLoad(e); } @@ -38,7 +37,7 @@ export default class SettingUseCase { this.showUnableToLoad(e); value = DefaultSettingData.toSettings(); } - this.settingRepository.update(value!!); + this.cachedSettingRepository.update(value!!); return value; } -- cgit v1.2.3