aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/storage.js
blob: 5dce3b06f277e48756a345ba2de343a5e63ef226 (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
32
import DefaultSettings from './default';
import * as settingsValues from './values';

const loadRaw = async() => {
  let { settings } = await browser.storage.local.get('settings');
  if (!settings) {
    return DefaultSettings;
  }
  return { ...DefaultSettings, ...settings };
};

const loadValue = async() => {
  let settings = await loadRaw();
  let value = JSON.parse(DefaultSettings.json);
  if (settings.source === 'json') {
    value = settingsValues.valueFromJson(settings.json);
  } else if (settings.source === 'form') {
    value = settingsValues.valueFromForm(settings.form);
  }
  if (!value.properties) {
    value.properties = {};
  }
  return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value };
};

const save = (settings) => {
  return browser.storage.local.set({
    settings,
  });
};

export { loadRaw, loadValue, save };