Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/js/ConfigStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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)) {
key.forEach(function (element) {
Expand All @@ -14,7 +14,6 @@ const ConfigStorage = {
// is okay
}
});
callback?.(result);
} else {
const keyValue = window.localStorage.getItem(key);
if (keyValue) {
Expand All @@ -23,9 +22,6 @@ const ConfigStorage = {
} catch (e) {
// It's fine if we fail that parse
}
callback?.(result);
} else {
callback?.(result);
}
}

Expand All @@ -39,4 +35,7 @@ const ConfigStorage = {
window.localStorage.setItem(element, JSON.stringify(tmpObj));
});
},
remove: function(item) {
window.localStorage.removeItem(item);
},
};
72 changes: 31 additions & 41 deletions src/js/FirmwareCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ let FirmwareCache = (function () {
function persist(data) {
let obj = {};
obj[CACHEKEY] = data;
chrome.storage.local.set(obj);
ConfigStorage.set(obj);
}

/**
* @param {Function} callback
*/
function load(callback) {
chrome.storage.local.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);
chrome.storage.local.get(cacheKey, obj => {
/** @type {CacheItem} */
let cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey)
? obj[cacheKey]
: null;
if (cached === null) {
return;
}
chrome.storage.local.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,
};
chrome.storage.local.set(obj, () => {
onPutToCache(release);
});
ConfigStorage.set(obj);
onPutToCache(release);
}

/**
Expand All @@ -163,13 +157,9 @@ let FirmwareCache = (function () {
return;
}
let cacheKey = withCachePrefix(key);
chrome.storage.local.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));
}
chrome.storage.local.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);
}
chrome.storage.local.remove(cacheKeys);
});
}
ConfigStorage.remove(cacheKeys);
journal.clear();
JournalStorage.persist(journal.toJSON());
}
Expand Down
34 changes: 16 additions & 18 deletions src/js/cordova_startup.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,44 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
callback(jobs);
};

chrome.storage.local.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();
chrome.storage.local.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`;

chrome.storage.local.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();
chrome.storage.local.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
Loading