diff options
Diffstat (limited to 'src/background/repositories/SettingRepository.ts')
-rw-r--r-- | src/background/repositories/SettingRepository.ts | 49 |
1 files changed, 49 insertions, 0 deletions
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 |