diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-02-03 19:07:55 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-02-03 19:08:51 +0900 |
commit | 818ba9ea1a6c46036a7ed448861217e63d3aa5be (patch) | |
tree | 947db38186678a7698ab3ec5470971b3f450e857 /src/settings | |
parent | 02e8b55884fd530dee03613d79adad8aa9180b61 (diff) |
Synchronize settings via Firefox Sync
Diffstat (limited to 'src/settings')
-rw-r--r-- | src/settings/actions/setting.ts | 1 | ||||
-rw-r--r-- | src/settings/storage.ts | 25 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/settings/actions/setting.ts b/src/settings/actions/setting.ts index 589ec36..93ca5f8 100644 --- a/src/settings/actions/setting.ts +++ b/src/settings/actions/setting.ts @@ -26,6 +26,7 @@ const save = async(data: SettingData): Promise<actions.SettingAction> => { return set(data); }; + const switchToForm = (json: JSONTextSettings): actions.SettingAction => { try { // toSettings exercise validation 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(), }); }; |