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/settings/storage.ts | |
parent | 8b9388f6e60fe67d20638f55aecb0ed1b281871a (diff) | |
parent | b2fc46ebf79ebb1ffa068fb513d1eeb9b50d7b3f (diff) |
Merge pull request #708 from ueokande/sync-storage
Synchronize settings with Firefox Sync
Diffstat (limited to 'src/settings/storage.ts')
-rw-r--r-- | src/settings/storage.ts | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/settings/storage.ts b/src/settings/storage.ts index f375e58..55cca96 100644 --- a/src/settings/storage.ts +++ b/src/settings/storage.ts @@ -1,20 +1,31 @@ import SettingData, { DefaultSettingData } from '../shared/SettingData'; -export const load = async(): Promise<SettingData> => { - const { settings } = await browser.storage.local.get('settings'); - if (!settings) { - return DefaultSettingData; +const loadSettingData = async(): Promise<SettingData> => { + const { settings: syncSettings } = await browser.storage.sync.get('settings'); + if (syncSettings) { + return SettingData.fromJSON(syncSettings as any); } + const { settings: localSettings } = await browser.storage.local.get('settings'); + if (localSettings) { + return SettingData.fromJSON(localSettings as any); + } + return DefaultSettingData +}; + +export const load = async(): Promise<SettingData> => { try { - return SettingData.fromJSON(settings as any); + return loadSettingData(); } catch (e) { console.error('unable to load settings', e); return DefaultSettingData; } }; -export const save = (data: SettingData) => { - return browser.storage.local.set({ +export const save = async (data: SettingData) => { + await browser.storage.local.set({ + settings: data.toJSON(), + }); + return browser.storage.sync.set({ settings: data.toJSON(), }); }; |