1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-26 09:45:28 +03:00
betaflight-configurator/src/js/ConfigStorage.js
Mark Haslinghuis b51a7b288a Fix MSP_BOARD_INFO && Storage Quota
Cleanup LocalStorage
2022-05-30 23:19:51 +02:00

48 lines
1.5 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) {
let result = {};
if (Array.isArray(key)) {
key.forEach(function (element) {
try {
result = {...result, ...JSON.parse(localStorage.getItem(element))};
} catch (e) {
console.error(e);
}
});
} else {
const keyValue = localStorage.getItem(key);
if (keyValue) {
try {
result = JSON.parse(keyValue);
} catch (e) {
console.error(e);
}
}
}
return result;
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
try {
localStorage.setItem(element, JSON.stringify(tmpObj));
} catch (e) {
console.error(e);
}
});
},
remove: function(item) {
localStorage.removeItem(item);
},
clear: function() {
localStorage.clear();
},
};