Skip to content

Commit bed2b8a

Browse files
authored
Merge pull request #2937 from haslinghuis/fix-auto-detect
Fix MSP_BOARD_INFO accumulation and localStorage Quota Exceeded
2 parents 10645cb + b51a7b2 commit bed2b8a

File tree

9 files changed

+126
-46
lines changed

9 files changed

+126
-46
lines changed

src/js/ConfigStorage.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ const ConfigStorage = {
99
if (Array.isArray(key)) {
1010
key.forEach(function (element) {
1111
try {
12-
result = {...result, ...JSON.parse(window.localStorage.getItem(element))};
12+
result = {...result, ...JSON.parse(localStorage.getItem(element))};
1313
} catch (e) {
14-
// is okay
14+
console.error(e);
1515
}
1616
});
1717
} else {
18-
const keyValue = window.localStorage.getItem(key);
18+
const keyValue = localStorage.getItem(key);
1919
if (keyValue) {
2020
try {
2121
result = JSON.parse(keyValue);
2222
} catch (e) {
23-
// It's fine if we fail that parse
23+
console.error(e);
2424
}
2525
}
2626
}
@@ -32,10 +32,17 @@ const ConfigStorage = {
3232
Object.keys(input).forEach(function (element) {
3333
const tmpObj = {};
3434
tmpObj[element] = input[element];
35-
window.localStorage.setItem(element, JSON.stringify(tmpObj));
35+
try {
36+
localStorage.setItem(element, JSON.stringify(tmpObj));
37+
} catch (e) {
38+
console.error(e);
39+
}
3640
});
3741
},
3842
remove: function(item) {
39-
window.localStorage.removeItem(item);
43+
localStorage.removeItem(item);
44+
},
45+
clear: function() {
46+
localStorage.clear();
4047
},
4148
};

src/js/FirmwareCache.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ let FirmwareCache = (function () {
4444
function persist(data) {
4545
let obj = {};
4646
obj[CACHEKEY] = data;
47-
ConfigStorage.set(obj);
47+
SessionStorage.set(obj);
4848
}
4949

5050
/**
5151
* @param {Function} callback
5252
*/
5353
function load(callback) {
54-
const obj = ConfigStorage.get(CACHEKEY);
54+
const obj = SessionStorage.get(CACHEKEY);
5555
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
5656
? obj[CACHEKEY]
5757
: [];
@@ -75,13 +75,13 @@ let FirmwareCache = (function () {
7575
}
7676
let key = oldest[0];
7777
let cacheKey = withCachePrefix(key);
78-
const obj = ConfigStorage.get(cacheKey);
78+
const obj = SessionStorage.get(cacheKey);
7979
/** @type {CacheItem} */
8080
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
8181
if (cached === null) {
8282
return undefined;
8383
}
84-
ConfigStorage.remove(cacheKey);
84+
SessionStorage.remove(cacheKey);
8585
onRemoveFromCache(cached.release);
8686
return oldest;
8787
};
@@ -138,7 +138,7 @@ let FirmwareCache = (function () {
138138
release: release,
139139
hexdata: hexdata,
140140
};
141-
ConfigStorage.set(obj);
141+
SessionStorage.set(obj);
142142
onPutToCache(release);
143143
}
144144

@@ -157,7 +157,7 @@ let FirmwareCache = (function () {
157157
return;
158158
}
159159
let cacheKey = withCachePrefix(key);
160-
const obj = ConfigStorage.get(cacheKey);
160+
const obj = SessionStorage.get(cacheKey);
161161
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
162162
callback(cached);
163163
}
@@ -174,7 +174,7 @@ let FirmwareCache = (function () {
174174
for (let key of journal.keys()) {
175175
cacheKeys.push(withCachePrefix(key));
176176
}
177-
const obj = ConfigStorage.get(cacheKeys);
177+
const obj = SessionStorage.get(cacheKeys);
178178
if (typeof obj !== "object") {
179179
return;
180180
}
@@ -186,7 +186,7 @@ let FirmwareCache = (function () {
186186
onRemoveFromCache(item.release);
187187
}
188188
}
189-
ConfigStorage.remove(cacheKeys);
189+
SessionStorage.remove(cacheKeys);
190190
journal.clear();
191191
JournalStorage.persist(journal.toJSON());
192192
}

src/js/SessionStorage.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const SessionStorage = {
4+
// key can be one string, or array of strings
5+
get: function(key) {
6+
let result = {};
7+
if (Array.isArray(key)) {
8+
key.forEach(function (element) {
9+
try {
10+
result = {...result, ...JSON.parse(sessionStorage.getItem(element))};
11+
} catch (e) {
12+
console.error(e);
13+
}
14+
});
15+
} else {
16+
const keyValue = sessionStorage.getItem(key);
17+
if (keyValue) {
18+
try {
19+
result = JSON.parse(keyValue);
20+
} catch (e) {
21+
console.error(e);
22+
}
23+
}
24+
}
25+
26+
return result;
27+
},
28+
set: function(input) {
29+
Object.keys(input).forEach(function (element) {
30+
const tmpObj = {};
31+
tmpObj[element] = input[element];
32+
try {
33+
sessionStorage.setItem(element, JSON.stringify(tmpObj));
34+
} catch (e) {
35+
console.error(e);
36+
}
37+
});
38+
},
39+
remove: function(item) {
40+
sessionStorage.removeItem(item);
41+
},
42+
clear: function() {
43+
sessionStorage.clear();
44+
},
45+
};

src/js/jenkins_loader.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
2121
callback(jobs);
2222
};
2323

24-
const result = ConfigStorage.get([cacheLastUpdateTag, jobsDataTag]);
24+
const result = SessionStorage.get([cacheLastUpdateTag, jobsDataTag]);
2525
const jobsDataTimestamp = $.now();
2626
const cachedJobsData = result[jobsDataTag];
2727
const cachedJobsLastUpdate = result[cacheLastUpdateTag];
@@ -49,7 +49,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
4949
const object = {};
5050
object[jobsDataTag] = jobs;
5151
object[cacheLastUpdateTag] = $.now();
52-
ConfigStorage.set(object);
52+
SessionStorage.set(object);
5353

5454
wrappedCallback(jobs);
5555
}).fail(xhr => {
@@ -68,7 +68,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
6868
const buildsDataTag = `${jobUrl}BuildsData`;
6969
const cacheLastUpdateTag = `${jobUrl}BuildsLastUpdate`;
7070

71-
const result = ConfigStorage.get([cacheLastUpdateTag, buildsDataTag]);
71+
const result = SessionStorage.get([cacheLastUpdateTag, buildsDataTag]);
7272
const buildsDataTimestamp = $.now();
7373
const cachedBuildsData = result[buildsDataTag];
7474
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];
@@ -100,8 +100,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
100100
const object = {};
101101
object[buildsDataTag] = builds;
102102
object[cacheLastUpdateTag] = $.now();
103-
ConfigStorage.set(object);
104-
103+
SessionStorage.set(object);
105104
self._parseBuilds(jobUrl, jobName, builds, callback);
106105
}).fail(xhr => {
107106
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));

src/js/main.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,31 @@ function readConfiguratorVersionMetadata() {
2828
CONFIGURATOR.gitRevision = manifest.gitRevision;
2929
}
3030

31+
function cleanupLocalStorage() {
32+
33+
const cleanupLocalStorageList = [
34+
'cache',
35+
'firmware',
36+
'https',
37+
'selected_board',
38+
'unifiedConfigLast',
39+
'unifiedSourceCache',
40+
];
41+
42+
for (const key in localStorage) {
43+
for (const item of cleanupLocalStorageList) {
44+
if (key.includes(item)) {
45+
localStorage.removeItem(key);
46+
}
47+
}
48+
}
49+
}
50+
3151
function appReady() {
3252
readConfiguratorVersionMetadata();
3353

54+
cleanupLocalStorage();
55+
3456
i18n.init(function() {
3557
startProcess();
3658

src/js/msp/MSPHelper.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -843,49 +843,50 @@ MspHelper.prototype.process_data = function(dataHandler) {
843843
break;
844844

845845
case MSPCodes.MSP_BOARD_INFO:
846-
let boardIdentifier = '';
846+
FC.CONFIG.boardIdentifier = '';
847+
847848
for (let i = 0; i < 4; i++) {
848-
boardIdentifier += String.fromCharCode(data.readU8());
849+
FC.CONFIG.boardIdentifier += String.fromCharCode(data.readU8());
849850
}
850-
FC.CONFIG.boardIdentifier = boardIdentifier;
851+
851852
FC.CONFIG.boardVersion = data.readU16();
853+
FC.CONFIG.boardType = 0;
852854

853855
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_35)) {
854856
FC.CONFIG.boardType = data.readU8();
855-
} else {
856-
FC.CONFIG.boardType = 0;
857857
}
858858

859+
FC.CONFIG.targetCapabilities = 0;
860+
FC.CONFIG.targetName = '';
861+
859862
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_37)) {
860863
FC.CONFIG.targetCapabilities = data.readU8();
861-
862-
let length = data.readU8();
864+
const length = data.readU8();
863865
for (let i = 0; i < length; i++) {
864866
FC.CONFIG.targetName += String.fromCharCode(data.readU8());
865867
}
866-
} else {
867-
FC.CONFIG.targetCapabilities = 0;
868-
FC.CONFIG.targetName = "";
869868
}
870869

870+
FC.CONFIG.boardName = '';
871+
FC.CONFIG.manufacturerId = '';
872+
FC.CONFIG.signature = [];
873+
871874
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_39)) {
872875
let length = data.readU8();
876+
873877
for (let i = 0; i < length; i++) {
874878
FC.CONFIG.boardName += String.fromCharCode(data.readU8());
875879
}
876880

877881
length = data.readU8();
882+
878883
for (let i = 0; i < length; i++) {
879884
FC.CONFIG.manufacturerId += String.fromCharCode(data.readU8());
880885
}
881886

882887
for (let i = 0; i < self.SIGNATURE_LENGTH; i++) {
883888
FC.CONFIG.signature.push(data.readU8());
884889
}
885-
} else {
886-
FC.CONFIG.boardName = "";
887-
FC.CONFIG.manufacturerId = "";
888-
FC.CONFIG.signature = [];
889890
}
890891

891892
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_41)) {

src/js/release_checker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ReleaseChecker = function (releaseName, releaseUrl) {
1111

1212
ReleaseChecker.prototype.loadReleaseData = function (processFunction) {
1313
const self = this;
14-
const result = ConfigStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]);
14+
const result = SessionStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]);
1515
const releaseDataTimestamp = $.now();
1616
const cacheReleaseData = result[self._releaseDataTag];
1717
const cachedReleaseLastUpdate = result[self._releaseLastUpdateTag];
@@ -23,7 +23,7 @@ ReleaseChecker.prototype.loadReleaseData = function (processFunction) {
2323
const data = {};
2424
data[self._releaseDataTag] = releaseData;
2525
data[self._releaseLastUpdateTag] = releaseDataTimestamp;
26-
ConfigStorage.set(data);
26+
SessionStorage.set(data);
2727

2828
self._processReleaseData(releaseData, processFunction);
2929
}).fail(function (data) {

0 commit comments

Comments
 (0)