Skip to content

Commit 40a76f5

Browse files
committed
Remove callback from set and remove
1 parent 128b024 commit 40a76f5

17 files changed

+592
-655
lines changed

src/js/ConfigStorage.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,37 @@
44
// localStorage deals with strings, not objects, so the objects have been serialized.
55
const ConfigStorage = {
66
// key can be one string, or array of strings
7-
get: function(key, callback) {
7+
get: function(key) {
8+
let result = {};
89
if (Array.isArray(key)) {
9-
let obj = {};
1010
key.forEach(function (element) {
1111
try {
12-
obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
12+
result = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
1313
} catch (e) {
1414
// is okay
1515
}
1616
});
17-
callback(obj);
1817
} else {
1918
const keyValue = window.localStorage.getItem(key);
2019
if (keyValue) {
21-
let obj = {};
2220
try {
23-
obj = JSON.parse(keyValue);
21+
result = JSON.parse(keyValue);
2422
} catch (e) {
2523
// It's fine if we fail that parse
2624
}
27-
callback(obj);
28-
} else {
29-
callback({});
3025
}
3126
}
27+
return result;
3228
},
3329
// set takes an object like {'userLanguageSelect':'DEFAULT'}
34-
set: function(input, callback) {
30+
set: function(input) {
3531
Object.keys(input).forEach(function (element) {
3632
const tmpObj = {};
3733
tmpObj[element] = input[element];
3834
window.localStorage.setItem(element, JSON.stringify(tmpObj));
3935
});
40-
if (callback) {
41-
callback();
42-
}
4336
},
44-
remove: function(item, callback) {
37+
remove: function(item) {
4538
window.localStorage.removeItem(item);
46-
if (callback) {
47-
callback();
48-
}
4939
}
5040
};

src/js/FirmwareCache.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ let FirmwareCache = (function () {
5151
* @param {Function} callback
5252
*/
5353
function load(callback) {
54-
ConfigStorage.get(CACHEKEY, obj => {
55-
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
56-
? obj[CACHEKEY]
57-
: [];
58-
callback(entries);
59-
});
54+
const obj = ConfigStorage.get(CACHEKEY);
55+
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
56+
? obj[CACHEKEY]
57+
: [];
58+
callback(entries);
6059
}
6160

6261
return {
@@ -76,18 +75,14 @@ let FirmwareCache = (function () {
7675
}
7776
let key = oldest[0];
7877
let cacheKey = withCachePrefix(key);
79-
ConfigStorage.get(cacheKey, obj => {
80-
/** @type {CacheItem} */
81-
let cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey)
82-
? obj[cacheKey]
83-
: null;
84-
if (cached === null) {
85-
return;
86-
}
87-
ConfigStorage.remove(cacheKey, () => {
88-
onRemoveFromCache(cached.release);
89-
});
90-
});
78+
const obj = ConfigStorage.get(cacheKey);
79+
/** @type {CacheItem} */
80+
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
81+
if (cached === null) {
82+
return;
83+
}
84+
ConfigStorage.remove(cacheKey);
85+
onRemoveFromCache(cached.release);
9186
return oldest;
9287
};
9388

@@ -143,9 +138,8 @@ let FirmwareCache = (function () {
143138
release: release,
144139
hexdata: hexdata,
145140
};
146-
ConfigStorage.set(obj, () => {
147-
onPutToCache(release);
148-
});
141+
ConfigStorage.set(obj);
142+
onPutToCache(release);
149143
}
150144

151145
/**
@@ -163,13 +157,9 @@ let FirmwareCache = (function () {
163157
return;
164158
}
165159
let cacheKey = withCachePrefix(key);
166-
ConfigStorage.get(cacheKey, obj => {
167-
/** @type {CacheItem} */
168-
let cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey)
169-
? obj[cacheKey]
170-
: null;
171-
callback(cached);
172-
});
160+
const obj = ConfigStorage.get(cacheKey);
161+
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
162+
callback(cached);
173163
}
174164

175165
/**
@@ -184,19 +174,19 @@ let FirmwareCache = (function () {
184174
for (let key of journal.keys()) {
185175
cacheKeys.push(withCachePrefix(key));
186176
}
187-
ConfigStorage.get(cacheKeys, obj => {
188-
if (typeof obj !== "object") {
189-
return;
190-
}
191-
for (let cacheKey of cacheKeys) {
192-
if (obj.hasOwnProperty(cacheKey)) {
193-
/** @type {CacheItem} */
194-
let item = obj[cacheKey];
195-
onRemoveFromCache(item.release);
196-
}
177+
const obj = ConfigStorage.get(cacheKeys);
178+
if (typeof obj !== "object") {
179+
return;
180+
}
181+
console.log(obj.entries());
182+
for (let cacheKey of cacheKeys) {
183+
if (obj.hasOwnProperty(cacheKey)) {
184+
/** @type {CacheItem} */
185+
let item = obj[cacheKey];
186+
onRemoveFromCache(item.release);
197187
}
198-
ConfigStorage.remove(cacheKeys);
199-
});
188+
}
189+
ConfigStorage.remove(cacheKeys);
200190
journal.clear();
201191
JournalStorage.persist(journal.toJSON());
202192
}

src/js/cordova_startup.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,27 @@ const cordovaUI = {
2222
if (screenWidth > 575 && screenHeight > 575) {
2323
self.canChangeUI = false;
2424
}
25-
ConfigStorage.get('cordovaForceComputerUI', function (result) {
26-
if (result.cordovaForceComputerUI === undefined) {
27-
if ((orientation === 'landscape' && screenHeight <= 575)
28-
|| (orientation === 'portrait' && screenWidth <= 575)) {
29-
ConfigStorage.set({'cordovaForceComputerUI': false});
30-
} else {
31-
ConfigStorage.set({'cordovaForceComputerUI': true});
32-
}
25+
const result = ConfigStorage.get('cordovaForceComputerUI');
26+
if (result.cordovaForceComputerUI === undefined) {
27+
if ((orientation === 'landscape' && screenHeight <= 575)
28+
|| (orientation === 'portrait' && screenWidth <= 575)) {
29+
ConfigStorage.set({'cordovaForceComputerUI': false});
30+
} else {
31+
ConfigStorage.set({'cordovaForceComputerUI': true});
3332
}
34-
});
33+
}
3534
self.set();
3635
},
3736
set: function() {
3837
const self = this;
39-
ConfigStorage.get('cordovaForceComputerUI', function (result) {
40-
if (result.cordovaForceComputerUI) {
41-
window.screen.orientation.lock('landscape');
42-
$('body').css('zoom', self.uiZoom);
43-
} else {
44-
window.screen.orientation.lock('portrait');
45-
$('body').css('zoom', 1);
46-
}
47-
});
38+
const result = ConfigStorage.get('cordovaForceComputerUI');
39+
if (result.cordovaForceComputerUI) {
40+
window.screen.orientation.lock('landscape');
41+
$('body').css('zoom', self.uiZoom);
42+
} else {
43+
window.screen.orientation.lock('portrait');
44+
$('body').css('zoom', 1);
45+
}
4846
},
4947
};
5048

src/js/gui.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,12 @@ GuiControl.prototype.content_ready = function (callback) {
387387
};
388388

389389
GuiControl.prototype.selectDefaultTabWhenConnected = function() {
390-
ConfigStorage.get(['rememberLastTab', 'lastTab'], function (result) {
391-
if (result.rememberLastTab && result.lastTab) {
392-
$(`#tabs ul.mode-connected .${result.lastTab} a`).click();
393-
} else {
394-
$('#tabs ul.mode-connected .tab_setup a').click();
395-
}
396-
});
390+
const result = ConfigStorage.get(['rememberLastTab', 'lastTab']);
391+
if (result.rememberLastTab && result.lastTab) {
392+
$(`#tabs ul.mode-connected .${result.lastTab} a`).click();
393+
} else {
394+
$('#tabs ul.mode-connected .tab_setup a').click();
395+
}
397396
};
398397

399398
GuiControl.prototype.isNWJS = function () {

src/js/jenkins_loader.js

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

24-
ConfigStorage.get([cacheLastUpdateTag, jobsDataTag], function (result) {
25-
const jobsDataTimestamp = $.now();
26-
const cachedJobsData = result[jobsDataTag];
27-
const cachedJobsLastUpdate = result[cacheLastUpdateTag];
28-
29-
const cachedCallback = () => {
30-
if (cachedJobsData) {
31-
GUI.log(i18n.getMessage('buildServerUsingCached', ['jobs']));
32-
}
24+
const result = ConfigStorage.get([cacheLastUpdateTag, jobsDataTag]);
25+
const jobsDataTimestamp = $.now();
26+
const cachedJobsData = result[jobsDataTag];
27+
const cachedJobsLastUpdate = result[cacheLastUpdateTag];
28+
29+
const cachedCallback = () => {
30+
if (cachedJobsData) {
31+
GUI.log(i18n.getMessage('buildServerUsingCached', ['jobs']));
32+
}
3333

34-
wrappedCallback(cachedJobsData ? cachedJobsData : []);
35-
};
34+
wrappedCallback(cachedJobsData ? cachedJobsData : []);
35+
};
3636

37-
if (!cachedJobsData || !cachedJobsLastUpdate || jobsDataTimestamp - cachedJobsLastUpdate > self._cacheExpirationPeriod) {
38-
const url = `${viewUrl}${self._jobsRequest}`;
37+
if (!cachedJobsData || !cachedJobsLastUpdate || jobsDataTimestamp - cachedJobsLastUpdate > self._cacheExpirationPeriod) {
38+
const url = `${viewUrl}${self._jobsRequest}`;
3939

40-
$.get(url, jobsInfo => {
41-
GUI.log(i18n.getMessage('buildServerLoaded', ['jobs']));
40+
$.get(url, jobsInfo => {
41+
GUI.log(i18n.getMessage('buildServerLoaded', ['jobs']));
4242

43-
// remove Betaflight prefix, rename Betaflight job to Development
44-
const jobs = jobsInfo.jobs.map(job => {
45-
return { title: job.name.replace('Betaflight ', '').replace('Betaflight', 'Development'), name: job.name };
46-
});
43+
// remove Betaflight prefix, rename Betaflight job to Development
44+
const jobs = jobsInfo.jobs.map(job => {
45+
return { title: job.name.replace('Betaflight ', '').replace('Betaflight', 'Development'), name: job.name };
46+
});
4747

48-
// cache loaded info
49-
const object = {};
50-
object[jobsDataTag] = jobs;
51-
object[cacheLastUpdateTag] = $.now();
52-
ConfigStorage.set(object);
48+
// cache loaded info
49+
const object = {};
50+
object[jobsDataTag] = jobs;
51+
object[cacheLastUpdateTag] = $.now();
52+
ConfigStorage.set(object);
5353

54-
wrappedCallback(jobs);
55-
}).fail(xhr => {
56-
GUI.log(i18n.getMessage('buildServerLoadFailed', ['jobs', `HTTP ${xhr.status}`]));
57-
cachedCallback();
58-
});
59-
} else {
54+
wrappedCallback(jobs);
55+
}).fail(xhr => {
56+
GUI.log(i18n.getMessage('buildServerLoadFailed', ['jobs', `HTTP ${xhr.status}`]));
6057
cachedCallback();
61-
}
62-
});
58+
});
59+
} else {
60+
cachedCallback();
61+
}
6362
};
6463

6564
JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
@@ -69,49 +68,48 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
6968
const buildsDataTag = `${jobUrl}BuildsData`;
7069
const cacheLastUpdateTag = `${jobUrl}BuildsLastUpdate`;
7170

72-
ConfigStorage.get([cacheLastUpdateTag, buildsDataTag], function (result) {
73-
const buildsDataTimestamp = $.now();
74-
const cachedBuildsData = result[buildsDataTag];
75-
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];
71+
const result = ConfigStorage.get([cacheLastUpdateTag, buildsDataTag]);
72+
const buildsDataTimestamp = $.now();
73+
const cachedBuildsData = result[buildsDataTag];
74+
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];
7675

77-
const cachedCallback = () => {
78-
if (cachedBuildsData) {
79-
GUI.log(i18n.getMessage('buildServerUsingCached', [jobName]));
80-
}
76+
const cachedCallback = () => {
77+
if (cachedBuildsData) {
78+
GUI.log(i18n.getMessage('buildServerUsingCached', [jobName]));
79+
}
8180

82-
self._parseBuilds(jobUrl, jobName, cachedBuildsData ? cachedBuildsData : [], callback);
83-
};
84-
85-
if (!cachedBuildsData || !cachedBuildsLastUpdate || buildsDataTimestamp - cachedBuildsLastUpdate > self._cacheExpirationPeriod) {
86-
const url = `${jobUrl}${self._buildsRequest}`;
87-
88-
$.get(url, function (buildsInfo) {
89-
GUI.log(i18n.getMessage('buildServerLoaded', [jobName]));
90-
91-
// filter successful builds
92-
const builds = buildsInfo.builds.filter(build => build.result == 'SUCCESS')
93-
.map(build => ({
94-
number: build.number,
95-
artifacts: build.artifacts.map(artifact => artifact.relativePath),
96-
changes: build.changeSet.items.map(item => `* ${item.msg}`).join('<br>\n'),
97-
timestamp: build.timestamp
98-
}));
99-
100-
// cache loaded info
101-
const object = {};
102-
object[buildsDataTag] = builds;
103-
object[cacheLastUpdateTag] = $.now();
104-
ConfigStorage.set(object);
105-
106-
self._parseBuilds(jobUrl, jobName, builds, callback);
107-
}).fail(xhr => {
108-
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));
109-
cachedCallback();
110-
});
111-
} else {
81+
self._parseBuilds(jobUrl, jobName, cachedBuildsData ? cachedBuildsData : [], callback);
82+
};
83+
84+
if (!cachedBuildsData || !cachedBuildsLastUpdate || buildsDataTimestamp - cachedBuildsLastUpdate > self._cacheExpirationPeriod) {
85+
const url = `${jobUrl}${self._buildsRequest}`;
86+
87+
$.get(url, function (buildsInfo) {
88+
GUI.log(i18n.getMessage('buildServerLoaded', [jobName]));
89+
90+
// filter successful builds
91+
const builds = buildsInfo.builds.filter(build => build.result == 'SUCCESS')
92+
.map(build => ({
93+
number: build.number,
94+
artifacts: build.artifacts.map(artifact => artifact.relativePath),
95+
changes: build.changeSet.items.map(item => `* ${item.msg}`).join('<br>\n'),
96+
timestamp: build.timestamp
97+
}));
98+
99+
// cache loaded info
100+
const object = {};
101+
object[buildsDataTag] = builds;
102+
object[cacheLastUpdateTag] = $.now();
103+
ConfigStorage.set(object);
104+
105+
self._parseBuilds(jobUrl, jobName, builds, callback);
106+
}).fail(xhr => {
107+
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));
112108
cachedCallback();
113-
}
114-
});
109+
});
110+
} else {
111+
cachedCallback();
112+
}
115113
};
116114

117115
JenkinsLoader.prototype._parseBuilds = function (jobUrl, jobName, builds, callback) {

0 commit comments

Comments
 (0)