Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConfigStorage.js « js « src - github.com/betaflight/betaflight-configurator.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7bf4936876e3c8a1d990875ebfe7c5a75550dc01 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
'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();
    },
};