aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/storage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/settings/storage.ts')
-rw-r--r--src/shared/settings/storage.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/shared/settings/storage.ts b/src/shared/settings/storage.ts
new file mode 100644
index 0000000..5dce3b0
--- /dev/null
+++ b/src/shared/settings/storage.ts
@@ -0,0 +1,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 };