1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-21 07:15:15 +03:00

add an alternative to chrome.storage.local

This commit is contained in:
Kyle K 2019-08-02 06:13:39 +00:00
parent 5eedc3b507
commit 426e753b53
11 changed files with 141 additions and 99 deletions

41
src/js/ConfigStorage.js Normal file
View file

@ -0,0 +1,41 @@
'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.
var ConfigStorage = {
// key can be one string, or array of strings
get: function(key, callback) {
//console.log('Abstraction.get',key);
if (Array.isArray(key)) {
var obj = {};
key.forEach(function (element) {
try {
obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
} catch (e) {
// is okay
}
});
callback(obj);
} else {
var keyValue = window.localStorage.getItem(key);
if (keyValue) {
var obj = {};
try {
obj = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
}
callback(obj);
} else {
callback({});
}
}
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) {
//console.log('Abstraction.set',input);
Object.keys(input).forEach(function (element) {
window.localStorage.setItem(element, JSON.stringify(input));
});
}
}