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

const loadRaw = () => {
  return browser.storage.local.get('settings').then(({ settings }) => {
    if (!settings) {
      return DefaultSettings;
    }
    return Object.assign({}, DefaultSettings, settings);
  });
};

const loadValue = () => {
  return loadRaw().then((settings) => {
    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);
    }
    return Object.assign({},
      settingsValues.valueFromJson(DefaultSettings.json),
      value);
  });
};

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

export { loadRaw, loadValue, save };