aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/storage.js
blob: 9b8045d6509b14c5cd92ec0508091667e879cc6c (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
34
35
36
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);
    }
    if (!value.properties) {
      value.properties = {};
    }
    return Object.assign({},
      settingsValues.valueFromJson(DefaultSettings.json),
      value);
  });
};

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

export { loadRaw, loadValue, save };