1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-26 01:35:28 +03:00
betaflight-configurator/src/js/ConfigStorage.js
2021-11-23 22:28:56 -06:00

42 lines
1.4 KiB
JavaScript

'use strict';
// idea here is to abstract around the use of chrome.storage.local as it functions differently from "localStorage" and IndexedDB
// localStorage deals with strings, not objects, so the objects have been serialized.
const ConfigStorage = {
// key can be one string, or array of strings
get: function(key, callback) {
let result = {};
if (Array.isArray(key)) {
key.forEach(function (element) {
try {
result = {...result, ...JSON.parse(window.localStorage.getItem(element))};
} catch (e) {
// is okay
}
});
callback?.(result);
} else {
const keyValue = window.localStorage.getItem(key);
if (keyValue) {
try {
result = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
}
callback?.(result);
} else {
callback?.(result);
}
}
return result;
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj));
});
}
};