Skip to content

Commit

Permalink
Fixing UI updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Aug 6, 2018
1 parent d418371 commit 20cfe17
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 136 deletions.
20 changes: 11 additions & 9 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var runningworkers = 0;
var workers = [];
var workqueue = [];
var pendingcompare = false;
var activeTab = null
var activeTabId = null
var unsorted = {};
var selection = "";

Expand All @@ -26,7 +26,7 @@ chrome.browserAction.setBadgeText({
chrome.browserAction.onClicked.addListener(function(tab) {
// Send a message to the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
activeTab = tabs[0];
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"command": "clicked_browser_action"});
});
});
Expand All @@ -50,6 +50,7 @@ chrome.runtime.onMessage.addListener(
break;
case "compareselection":
selection = request.selection
activeTabId = sender.tab.id
if (updating){
pendingcompare = true
console.log("Update pending; queing compare")
Expand All @@ -73,12 +74,12 @@ function workeronmessage(event) {
processqueue(); //Message received so see if queue can be cleared.
switch (event.data.command) {
case "progressbarmax":
chrome.tabs.sendMessage(activeTab.id, event);
chrome.tabs.sendMessage(activeTabId, event.data);
// updateProgressBar(event.data.value, null)
// updateBubbleText(event.data.stage);
break;
case "next":
chrome.tabs.sendMessage(activeTab.id, event);
chrome.tabs.sendMessage(activeTabId, event.data);
break;
case "store":
//This path is intended to store a hash of a comparison. Complete
Expand Down Expand Up @@ -174,7 +175,7 @@ function workeronmessage(event) {
workerdone(threadid)
var result = event.data.result;
var spdxid = event.data.spdxid;
chrome.tabs.sendMessage(activeTab.id, {"command": "comparenext", "spdxid":spdxid, "result":result, "id":threadid});
chrome.tabs.sendMessage(activeTabId, {"command": "next", "spdxid":spdxid,"id":threadid});
unsorted[spdxid] = result;
if (Object.keys(unsorted).length >= Object.keys(list["license"]).length){
console.log("Requesting final sort", Object.keys(unsorted).length)
Expand All @@ -186,15 +187,15 @@ function workeronmessage(event) {
var threadid = event.data.id;
workerdone(threadid)
var spdx = event.data.result;
chrome.tabs.sendMessage(activeTab.id, {"command": "sortdone","result": spdx,"id":threadid});
chrome.tabs.sendMessage(activeTabId, {"command": "sortdone","result": spdx,"id":threadid});
break;
case "diffnext":
var threadid = event.data.id;
workerdone(threadid)
var result = event.data.result;
var spdxid = event.data.spdxid;
var record = event.data.record;
chrome.tabs.sendMessage(activeTab.id, {"command": "diffnext", "spdxid":spdxid, "result":result, "record":record, "id":threadid});
chrome.tabs.sendMessage(activeTabId, {"command": "diffnext", "spdxid":spdxid, "result":result, "record":record, "id":threadid});
break;
default:

Expand Down Expand Up @@ -261,10 +262,11 @@ function updateList(){
//This is the first phase to determine edit distance and return a sorted list
// for display in spdx
function compareSelection(selection){
chrome.tabs.sendMessage(activeTab.id, {"message": "progressbarmax","value": Object.keys(list["license"]).length, "stage":"Comparing licenses"});
var total = Object.keys(list["license"]).length;
chrome.tabs.sendMessage(activeTabId, {"message": "progressbarmax","value": total, "stage":"Comparing licenses"});
//updateProgressBar(Object.keys(list["license"]).length, null)
for (var license in list["license"]){
dowork({'command':"compare", 'selection': selection, 'maxLengthDifference':options.maxLengthDifference, 'spdxid':license,'license':list["license"][license]});
dowork({'command':"compare", 'selection': selection, 'maxLengthDifference':options.maxLengthDifference, 'spdxid':license,'license':list["license"][license],'total': total});
}
}

Expand Down
124 changes: 0 additions & 124 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,6 @@ restore_options();
createBubble();

// Event driven functions
//This function processes webworker messages which it launches
//through the workerqueue
function workeronmessage(event) {
processqueue(); //Message received so see if queue can be cleared.
switch (event.data.command) {

case "updatedone":
workerdone(event.data.id)
var arr = event.data.result;
if (typeof list["license"] === "undefined")
list["license"] = {};
for (var i=0; i < arr.length; i++){
list["license"][arr[i]["licenseId"]] = arr[i]
}
updating = false;
if (pendingcompare)
compareSelection(selection)
pendingcompare = false;
break;
case "comparenext":
workerdone(event.data.id)
updateProgressBar(-1, -1)
var result = event.data.result;
var spdxid = event.data.spdxid;
unsorted[spdxid] = result;
if (Object.keys(unsorted).length >= Object.keys(list["license"]).length){
console.log("Requesting final sort", Object.keys(unsorted).length)
dowork({ 'command':"sortlicenses", 'licenses':unsorted});
unsorted = {};
}
break;

default:

}
}

//This function responds to the UI and background.js
chrome.runtime.onMessage.addListener(
Expand Down Expand Up @@ -334,50 +298,6 @@ function storeList(externallicenselist){
console.log('Storing cached copy of ', obj);
});
}
// function loadList(){
// chrome.storage.local.get(['list'], function(result) {
// if (result.list && result.list["licenseListVersion"]){
// list = result.list;
// lastupdate = list["lastupdate"]
// console.log('Loading License list version %s from storage with %s licenses last updated %s',
// list["licenseListVersion"], list.licenses.length, Date(lastupdate));
// if ((Date.now() - lastupdate) >= (options.updateFrequency * 86400000)){
// console.log('Last update was over %s days ago; update required', options.updateFrequency);
// updateList()
// }else{
// for (var j = 0; j < list.licenses.length; j++) {
// var line = list.licenses[j];
// var license = line["licenseId"];
// list["license"] = {}
// console.log('Attempting to load %s from storage', license);
// chrome.storage.local.get([license], function(result) {
// if (result && ! _.isEmpty(result)){
// license = Object.keys(result)[0]
// console.log('%s succesfully loaded from storage', license);
// list.license[license] = result[license];
// }else {
// console.log('%s not found in storage; requesting update', license);
// updateList()
// }
// });
// }
// }
// }else {
// console.log('No license list found in storage; requesting update');
// updateList()
// }
// });
// }
// function updateList(){
// if (updating){
// console.log("Ignoring redundant update request")
// return
// }else {
// updating = true;
// chrome.runtime.sendMessage({ 'command':"updatelicenselist", 'url':chrome.extension.getURL(""), 'remote':true});
// //dowork({ 'command':"updatelicenselist", 'url':chrome.extension.getURL(""), 'remote':true});
// }
// }

function restore_options() {
chrome.storage.sync.get(['options'], function(result) {
Expand All @@ -393,47 +313,3 @@ function restore_options() {
}
});
}

// // Workerqueue functions
// // These functions are for allowing multiple workers.
// function spawnworkers(){
// if (workers.length == options.maxworkers)
// return
// console.log("Spawning %s workers", options.maxworkers)
// for (var i = 0; i < options.maxworkers; i++){
// var worker = new Worker(chrome.runtime.getURL('scripts/worker.js'));
// worker.onmessage = workeronmessage;
// workers[i]= [worker , false];
// }
// }
// //queue and start work
// function processqueue(){
// while (workqueue.length && options.maxworkers > runningworkers){
// var work = workqueue.shift();
// dowork(work)
// }
// }
// function dowork(message, ){
// spawnworkers()
// var offset = options.maxworkers - runningworkers
// if (options.maxworkers > runningworkers ){
// for (var i = runningworkers % options.maxworkers; i < options.maxworkers + offset - 1; i = (i + 1) % options.maxworkers){
// if (!workers[i][1]) {// worker is available
// message["id"] = i;
// var worker = workers[i][0]
// workers[i][1] = true
// worker.postMessage(message)
// runningworkers++
// break
// }else {
// continue
// }
// }
// }else{ // queue up work
// workqueue.push(message)
// }
// }
// function workerdone(id){
// workers[id][1] = false
// runningworkers--
// }
7 changes: 4 additions & 3 deletions app/scripts/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ self.onmessage = function(event) {
var spdxid = event.data.spdxid
var license = event.data.license
var maxLengthDifference = event.data.maxLengthDifference
comparelicense(event.data["selection"], spdxid, license, maxLengthDifference);
var total = event.data.total;
comparelicense(event.data["selection"], spdxid, license, maxLengthDifference, total);
break;
case "sortlicenses":
sortlicenses(event.data.licenses);
Expand Down Expand Up @@ -121,8 +122,8 @@ function processSPDXlist(files, remote=true) {
);
};

function comparelicense(selection, spdxid, license, maxLengthDifference=1000) {
postMessage({"command": "progressbarmax","value": 0, "stage":"Comparing licenses","id":id});
function comparelicense(selection, spdxid, license, maxLengthDifference=1000, total=0) {
postMessage({"command": "progressbarmax","value": total, "stage":"Comparing licenses","id":id});
var result = {}
var count2 = selection.length;
//console.log(id, "Processing selection of " + count2 + " chars.");
Expand Down

0 comments on commit 20cfe17

Please sign in to comment.