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

Change lexical scope configInsert and configStorage

This commit is contained in:
Mark Haslinghuis 2020-12-08 19:49:52 +01:00
parent 317f937fd5
commit e0bc8174d8
2 changed files with 22 additions and 24 deletions

View file

@ -1,16 +1,16 @@
'use strict'; 'use strict';
var ConfigInserter = function () { const ConfigInserter = function () {
} }
const CUSTOM_DEFAULTS_POINTER_ADDRESS = 0x08002800; const CUSTOM_DEFAULTS_POINTER_ADDRESS = 0x08002800;
const BLOCK_SIZE = 16384; const BLOCK_SIZE = 16384;
function seek(firmware, address) { function seek(firmware, address) {
var index = 0; let index = 0;
for (; index < firmware.data.length && address >= firmware.data[index].address + firmware.data[index].bytes; index++); for (; index < firmware.data.length && address >= firmware.data[index].address + firmware.data[index].bytes; index++);
var result = { const result = {
lineIndex: index lineIndex: index
}; };
@ -22,8 +22,8 @@ function seek(firmware, address) {
} }
function readUint32(firmware, index) { function readUint32(firmware, index) {
var result = 0; let result = 0;
for (var position = 0; position < 4; position++) { for (let position = 0; position < 4; position++) {
result += firmware.data[index.lineIndex].data[index.byteIndex++] << (8 * position); result += firmware.data[index.lineIndex].data[index.byteIndex++] << (8 * position);
if (index.byteIndex >= firmware.data[index.lineIndex].bytes) { if (index.byteIndex >= firmware.data[index.lineIndex].bytes) {
index.lineIndex++; index.lineIndex++;
@ -35,9 +35,9 @@ function readUint32(firmware, index) {
} }
function getCustomDefaultsArea(firmware) { function getCustomDefaultsArea(firmware) {
var result = {}; const result = {};
var index = seek(firmware, CUSTOM_DEFAULTS_POINTER_ADDRESS); const index = seek(firmware, CUSTOM_DEFAULTS_POINTER_ADDRESS);
if (index.byteIndex === undefined) { if (index.byteIndex === undefined) {
return; return;
@ -50,9 +50,9 @@ function getCustomDefaultsArea(firmware) {
} }
function generateData(firmware, input, startAddress) { function generateData(firmware, input, startAddress) {
var address = startAddress; let address = startAddress;
var index = seek(firmware, address); const index = seek(firmware, address);
if (index.byteIndex !== undefined) { if (index.byteIndex !== undefined) {
throw new Error('Configuration area in firmware not free.'); throw new Error('Configuration area in firmware not free.');
@ -61,10 +61,10 @@ function generateData(firmware, input, startAddress) {
// Add 0 terminator // Add 0 terminator
input = input + '\0'; input = input + '\0';
var inputIndex = 0; let inputIndex = 0;
while (inputIndex < input.length) { while (inputIndex < input.length) {
var remaining = input.length - inputIndex; const remaining = input.length - inputIndex;
var line = { const line = {
address: address, address: address,
bytes: BLOCK_SIZE > remaining ? remaining : BLOCK_SIZE, bytes: BLOCK_SIZE > remaining ? remaining : BLOCK_SIZE,
data: [] data: []
@ -74,7 +74,7 @@ function generateData(firmware, input, startAddress) {
throw new Error("Aborting data generation, free area too small."); throw new Error("Aborting data generation, free area too small.");
} }
for (var i = 0; i < line.bytes; i++) { for (let i = 0; i < line.bytes; i++) {
line.data.push(input.charCodeAt(inputIndex++)); line.data.push(input.charCodeAt(inputIndex++));
} }
@ -87,15 +87,13 @@ function generateData(firmware, input, startAddress) {
} }
function microtime() { function microtime() {
var now = new Date().getTime() / 1000; return new Date().getTime() / 1000;
return now;
} }
ConfigInserter.prototype.insertConfig = function (firmware, input) { ConfigInserter.prototype.insertConfig = function (firmware, input) {
var time_parsing_start = microtime(); // track time const timeParsingStart = microtime(); // track time
var customDefaultsArea = getCustomDefaultsArea(firmware); const customDefaultsArea = getCustomDefaultsArea(firmware);
if (!customDefaultsArea || customDefaultsArea.endAddress - customDefaultsArea.startAddress === 0) { if (!customDefaultsArea || customDefaultsArea.endAddress - customDefaultsArea.startAddress === 0) {
return false; return false;
@ -105,7 +103,7 @@ ConfigInserter.prototype.insertConfig = function (firmware, input) {
generateData(firmware, input, customDefaultsArea.startAddress); generateData(firmware, input, customDefaultsArea.startAddress);
console.log('Custom defaults inserted in: ' + (microtime() - time_parsing_start).toFixed(4) + ' seconds.'); console.log(`Custom defaults inserted in: ${microtime() - timeParsingStart.toFixed(4)} seconds.`);
return true; return true;
} }

View file

@ -2,11 +2,11 @@
// idea here is to abstract around the use of chrome.storage.local as it functions differently from "localStorage" and IndexedDB // 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. // localStorage deals with strings, not objects, so the objects have been serialized.
var ConfigStorage = { const ConfigStorage = {
// key can be one string, or array of strings // key can be one string, or array of strings
get: function(key, callback) { get: function(key, callback) {
if (Array.isArray(key)) { if (Array.isArray(key)) {
var obj = {}; let obj = {};
key.forEach(function (element) { key.forEach(function (element) {
try { try {
obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))}; obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
@ -16,9 +16,9 @@ var ConfigStorage = {
}); });
callback(obj); callback(obj);
} else { } else {
var keyValue = window.localStorage.getItem(key); const keyValue = window.localStorage.getItem(key);
if (keyValue) { if (keyValue) {
var obj = {}; let obj = {};
try { try {
obj = JSON.parse(keyValue); obj = JSON.parse(keyValue);
} catch (e) { } catch (e) {
@ -33,7 +33,7 @@ var ConfigStorage = {
// set takes an object like {'userLanguageSelect':'DEFAULT'} // set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) { set: function(input) {
Object.keys(input).forEach(function (element) { Object.keys(input).forEach(function (element) {
var tmpObj = {}; const tmpObj = {};
tmpObj[element] = input[element]; tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj)); window.localStorage.setItem(element, JSON.stringify(tmpObj));
}); });