Skip to content

Commit

Permalink
Remove callback from set and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Nov 21, 2021
1 parent 128b024 commit 6dcf2ac
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 655 deletions.
24 changes: 7 additions & 17 deletions src/js/ConfigStorage.js
Expand Up @@ -4,47 +4,37 @@
// 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, callback) {
get: function(key) {
let result = {};
if (Array.isArray(key)) {
let obj = {};
key.forEach(function (element) {
try {
obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
result = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
} catch (e) {
// is okay
}
});
callback(obj);
} else {
const keyValue = window.localStorage.getItem(key);
if (keyValue) {
let obj = {};
try {
obj = JSON.parse(keyValue);
result = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
}
callback(obj);
} else {
callback({});
}
}
return result;
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input, callback) {
set: function(input) {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj));
});
if (callback) {
callback();
}
},
remove: function(item, callback) {
remove: function(item) {
window.localStorage.removeItem(item);
if (callback) {
callback();
}
}
};
70 changes: 30 additions & 40 deletions src/js/FirmwareCache.js
Expand Up @@ -51,12 +51,11 @@ let FirmwareCache = (function () {
* @param {Function} callback
*/
function load(callback) {
ConfigStorage.get(CACHEKEY, obj => {
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
? obj[CACHEKEY]
: [];
callback(entries);
});
const obj = ConfigStorage.get(CACHEKEY);
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
? obj[CACHEKEY]
: [];
callback(entries);
}

return {
Expand All @@ -76,18 +75,14 @@ let FirmwareCache = (function () {
}
let key = oldest[0];
let cacheKey = withCachePrefix(key);
ConfigStorage.get(cacheKey, obj => {
/** @type {CacheItem} */
let cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey)
? obj[cacheKey]
: null;
if (cached === null) {
return;
}
ConfigStorage.remove(cacheKey, () => {
onRemoveFromCache(cached.release);
});
});
const obj = ConfigStorage.get(cacheKey);
/** @type {CacheItem} */
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
if (cached === null) {
return undefined;
}
ConfigStorage.remove(cacheKey);
onRemoveFromCache(cached.release);
return oldest;
};

Expand Down Expand Up @@ -143,9 +138,8 @@ let FirmwareCache = (function () {
release: release,
hexdata: hexdata,
};
ConfigStorage.set(obj, () => {
onPutToCache(release);
});
ConfigStorage.set(obj);
onPutToCache(release);
}

/**
Expand All @@ -163,13 +157,9 @@ let FirmwareCache = (function () {
return;
}
let cacheKey = withCachePrefix(key);
ConfigStorage.get(cacheKey, obj => {
/** @type {CacheItem} */
let cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey)
? obj[cacheKey]
: null;
callback(cached);
});
const obj = ConfigStorage.get(cacheKey);
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
callback(cached);
}

/**
Expand All @@ -184,19 +174,19 @@ let FirmwareCache = (function () {
for (let key of journal.keys()) {
cacheKeys.push(withCachePrefix(key));
}
ConfigStorage.get(cacheKeys, obj => {
if (typeof obj !== "object") {
return;
}
for (let cacheKey of cacheKeys) {
if (obj.hasOwnProperty(cacheKey)) {
/** @type {CacheItem} */
let item = obj[cacheKey];
onRemoveFromCache(item.release);
}
const obj = ConfigStorage.get(cacheKeys);
if (typeof obj !== "object") {
return;
}
console.log(obj.entries());
for (let cacheKey of cacheKeys) {
if (obj.hasOwnProperty(cacheKey)) {
/** @type {CacheItem} */
let item = obj[cacheKey];
onRemoveFromCache(item.release);
}
ConfigStorage.remove(cacheKeys);
});
}
ConfigStorage.remove(cacheKeys);
journal.clear();
JournalStorage.persist(journal.toJSON());
}
Expand Down
34 changes: 16 additions & 18 deletions src/js/cordova_startup.js
Expand Up @@ -22,29 +22,27 @@ const cordovaUI = {
if (screenWidth > 575 && screenHeight > 575) {
self.canChangeUI = false;
}
ConfigStorage.get('cordovaForceComputerUI', function (result) {
if (result.cordovaForceComputerUI === undefined) {
if ((orientation === 'landscape' && screenHeight <= 575)
|| (orientation === 'portrait' && screenWidth <= 575)) {
ConfigStorage.set({'cordovaForceComputerUI': false});
} else {
ConfigStorage.set({'cordovaForceComputerUI': true});
}
const result = ConfigStorage.get('cordovaForceComputerUI');
if (result.cordovaForceComputerUI === undefined) {
if ((orientation === 'landscape' && screenHeight <= 575)
|| (orientation === 'portrait' && screenWidth <= 575)) {
ConfigStorage.set({'cordovaForceComputerUI': false});
} else {
ConfigStorage.set({'cordovaForceComputerUI': true});
}
});
}
self.set();
},
set: function() {
const self = this;
ConfigStorage.get('cordovaForceComputerUI', function (result) {
if (result.cordovaForceComputerUI) {
window.screen.orientation.lock('landscape');
$('body').css('zoom', self.uiZoom);
} else {
window.screen.orientation.lock('portrait');
$('body').css('zoom', 1);
}
});
const result = ConfigStorage.get('cordovaForceComputerUI');
if (result.cordovaForceComputerUI) {
window.screen.orientation.lock('landscape');
$('body').css('zoom', self.uiZoom);
} else {
window.screen.orientation.lock('portrait');
$('body').css('zoom', 1);
}
},
};

Expand Down
13 changes: 6 additions & 7 deletions src/js/gui.js
Expand Up @@ -387,13 +387,12 @@ GuiControl.prototype.content_ready = function (callback) {
};

GuiControl.prototype.selectDefaultTabWhenConnected = function() {
ConfigStorage.get(['rememberLastTab', 'lastTab'], function (result) {
if (result.rememberLastTab && result.lastTab) {
$(`#tabs ul.mode-connected .${result.lastTab} a`).click();
} else {
$('#tabs ul.mode-connected .tab_setup a').click();
}
});
const result = ConfigStorage.get(['rememberLastTab', 'lastTab']);
if (result.rememberLastTab && result.lastTab) {
$(`#tabs ul.mode-connected .${result.lastTab} a`).click();
} else {
$('#tabs ul.mode-connected .tab_setup a').click();
}
};

GuiControl.prototype.isNWJS = function () {
Expand Down
142 changes: 70 additions & 72 deletions src/js/jenkins_loader.js
Expand Up @@ -21,45 +21,44 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
callback(jobs);
};

ConfigStorage.get([cacheLastUpdateTag, jobsDataTag], function (result) {
const jobsDataTimestamp = $.now();
const cachedJobsData = result[jobsDataTag];
const cachedJobsLastUpdate = result[cacheLastUpdateTag];

const cachedCallback = () => {
if (cachedJobsData) {
GUI.log(i18n.getMessage('buildServerUsingCached', ['jobs']));
}
const result = ConfigStorage.get([cacheLastUpdateTag, jobsDataTag]);
const jobsDataTimestamp = $.now();
const cachedJobsData = result[jobsDataTag];
const cachedJobsLastUpdate = result[cacheLastUpdateTag];

const cachedCallback = () => {
if (cachedJobsData) {
GUI.log(i18n.getMessage('buildServerUsingCached', ['jobs']));
}

wrappedCallback(cachedJobsData ? cachedJobsData : []);
};
wrappedCallback(cachedJobsData ? cachedJobsData : []);
};

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

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

// remove Betaflight prefix, rename Betaflight job to Development
const jobs = jobsInfo.jobs.map(job => {
return { title: job.name.replace('Betaflight ', '').replace('Betaflight', 'Development'), name: job.name };
});
// remove Betaflight prefix, rename Betaflight job to Development
const jobs = jobsInfo.jobs.map(job => {
return { title: job.name.replace('Betaflight ', '').replace('Betaflight', 'Development'), name: job.name };
});

// cache loaded info
const object = {};
object[jobsDataTag] = jobs;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);
// cache loaded info
const object = {};
object[jobsDataTag] = jobs;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);

wrappedCallback(jobs);
}).fail(xhr => {
GUI.log(i18n.getMessage('buildServerLoadFailed', ['jobs', `HTTP ${xhr.status}`]));
cachedCallback();
});
} else {
wrappedCallback(jobs);
}).fail(xhr => {
GUI.log(i18n.getMessage('buildServerLoadFailed', ['jobs', `HTTP ${xhr.status}`]));
cachedCallback();
}
});
});
} else {
cachedCallback();
}
};

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

ConfigStorage.get([cacheLastUpdateTag, buildsDataTag], function (result) {
const buildsDataTimestamp = $.now();
const cachedBuildsData = result[buildsDataTag];
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];
const result = ConfigStorage.get([cacheLastUpdateTag, buildsDataTag]);
const buildsDataTimestamp = $.now();
const cachedBuildsData = result[buildsDataTag];
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];

const cachedCallback = () => {
if (cachedBuildsData) {
GUI.log(i18n.getMessage('buildServerUsingCached', [jobName]));
}
const cachedCallback = () => {
if (cachedBuildsData) {
GUI.log(i18n.getMessage('buildServerUsingCached', [jobName]));
}

self._parseBuilds(jobUrl, jobName, cachedBuildsData ? cachedBuildsData : [], callback);
};

if (!cachedBuildsData || !cachedBuildsLastUpdate || buildsDataTimestamp - cachedBuildsLastUpdate > self._cacheExpirationPeriod) {
const url = `${jobUrl}${self._buildsRequest}`;

$.get(url, function (buildsInfo) {
GUI.log(i18n.getMessage('buildServerLoaded', [jobName]));

// filter successful builds
const builds = buildsInfo.builds.filter(build => build.result == 'SUCCESS')
.map(build => ({
number: build.number,
artifacts: build.artifacts.map(artifact => artifact.relativePath),
changes: build.changeSet.items.map(item => `* ${item.msg}`).join('<br>\n'),
timestamp: build.timestamp
}));

// cache loaded info
const object = {};
object[buildsDataTag] = builds;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);

self._parseBuilds(jobUrl, jobName, builds, callback);
}).fail(xhr => {
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));
cachedCallback();
});
} else {
self._parseBuilds(jobUrl, jobName, cachedBuildsData ? cachedBuildsData : [], callback);
};

if (!cachedBuildsData || !cachedBuildsLastUpdate || buildsDataTimestamp - cachedBuildsLastUpdate > self._cacheExpirationPeriod) {
const url = `${jobUrl}${self._buildsRequest}`;

$.get(url, function (buildsInfo) {
GUI.log(i18n.getMessage('buildServerLoaded', [jobName]));

// filter successful builds
const builds = buildsInfo.builds.filter(build => build.result == 'SUCCESS')
.map(build => ({
number: build.number,
artifacts: build.artifacts.map(artifact => artifact.relativePath),
changes: build.changeSet.items.map(item => `* ${item.msg}`).join('<br>\n'),
timestamp: build.timestamp
}));

// cache loaded info
const object = {};
object[buildsDataTag] = builds;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);

self._parseBuilds(jobUrl, jobName, builds, callback);
}).fail(xhr => {
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));
cachedCallback();
}
});
});
} else {
cachedCallback();
}
};

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

0 comments on commit 6dcf2ac

Please sign in to comment.