aboutsummaryrefslogtreecommitdiff
path: root/src/settings/storage.ts
blob: 55cca961adc94a601cea4439e3d6d84efca7a711 (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
import SettingData, { DefaultSettingData } from '../shared/SettingData';

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 loadSettingData();
  } catch (e) {
    console.error('unable to load settings', e);
    return DefaultSettingData;
  }
};

export const save = async (data: SettingData) => {
  await browser.storage.local.set({
    settings: data.toJSON(),
  });
  return browser.storage.sync.set({
    settings: data.toJSON(),
  });
};