Improve queueing for getDish... messages.

Force wheel event to be not passive.
Change maxworkers to be navigator.hardwareConcurrency - 1
This commit is contained in:
j433866 2019-06-13 14:48:28 +01:00
parent 84204c1d12
commit 34d03973f9
6 changed files with 100 additions and 34 deletions

View File

@ -144,11 +144,10 @@ class Dish {
/** /**
* Detects the MIME type of the current dish * Detects the MIME type of the current dish
*
* @returns {string} * @returns {string}
*/ */
async detectDishType() { async detectDishType() {
const data = new Uint8Array(this.value), const data = new Uint8Array(this.value.slice(0, 2048)),
types = detectFileType(data); types = detectFileType(data);
if (!types.length || !types[0].mime || !types[0].mime === "text/plain") { if (!types.length || !types[0].mime || !types[0].mime === "text/plain") {
@ -168,7 +167,6 @@ class Dish {
async getTitle(maxLength) { async getTitle(maxLength) {
let title = ""; let title = "";
// LIST OF FILES - Say e.g. "3 files"
switch (this.type) { switch (this.type) {
case Dish.FILE: case Dish.FILE:
title = this.value.name; title = this.value.name;
@ -180,6 +178,7 @@ class Dish {
case Dish.BYTE_ARRAY: case Dish.BYTE_ARRAY:
title = await this.detectDishType(); title = await this.detectDishType();
if (title === null) { if (title === null) {
this.value = this.value.slice(0, 2048);
title = await this.get("string"); title = await this.get("string");
} }
break; break;

View File

@ -166,7 +166,6 @@ class Manager {
this.addListeners("#btn-next-input-tab,#btn-previous-input-tab", "mouseout", this.input.tabMouseUp, this.input); this.addListeners("#btn-next-input-tab,#btn-previous-input-tab", "mouseout", this.input.tabMouseUp, this.input);
document.getElementById("btn-go-to-input-tab").addEventListener("click", this.input.goToTab.bind(this.input)); document.getElementById("btn-go-to-input-tab").addEventListener("click", this.input.goToTab.bind(this.input));
document.getElementById("btn-find-input-tab").addEventListener("click", this.input.findTab.bind(this.input)); document.getElementById("btn-find-input-tab").addEventListener("click", this.input.findTab.bind(this.input));
this.addDynamicListener("#input-tabs li .input-tab-content", "wheel", this.input.scrollTab, this.input);
this.addDynamicListener("#input-tabs li .input-tab-content", "click", this.input.changeTabClick, this.input); this.addDynamicListener("#input-tabs li .input-tab-content", "click", this.input.changeTabClick, this.input);
document.getElementById("input-show-pending").addEventListener("change", this.input.filterTabSearch.bind(this.input)); document.getElementById("input-show-pending").addEventListener("change", this.input.filterTabSearch.bind(this.input));
document.getElementById("input-show-loading").addEventListener("change", this.input.filterTabSearch.bind(this.input)); document.getElementById("input-show-loading").addEventListener("change", this.input.filterTabSearch.bind(this.input));
@ -202,7 +201,6 @@ class Manager {
document.getElementById("show-file-overlay").addEventListener("click", this.output.showFileOverlayClick.bind(this.output)); document.getElementById("show-file-overlay").addEventListener("click", this.output.showFileOverlayClick.bind(this.output));
this.addDynamicListener(".extract-file,.extract-file i", "click", this.output.extractFileClick, this.output); this.addDynamicListener(".extract-file,.extract-file i", "click", this.output.extractFileClick, this.output);
this.addDynamicListener("#output-tabs-wrapper #output-tabs li .output-tab-content", "click", this.output.changeTabClick, this.output); this.addDynamicListener("#output-tabs-wrapper #output-tabs li .output-tab-content", "click", this.output.changeTabClick, this.output);
this.addDynamicListener("#output-tabs-wrapper #output-tabs li .output-tab-content", "wheel", this.output.scrollTab, this.output);
document.getElementById("btn-previous-output-tab").addEventListener("mousedown", this.output.previousTabClick.bind(this.output)); document.getElementById("btn-previous-output-tab").addEventListener("mousedown", this.output.previousTabClick.bind(this.output));
document.getElementById("btn-next-output-tab").addEventListener("mousedown", this.output.nextTabClick.bind(this.output)); document.getElementById("btn-next-output-tab").addEventListener("mousedown", this.output.nextTabClick.bind(this.output));
this.addListeners("#btn-next-output-tab,#btn-previous-output-tab", "mouseup", this.output.tabMouseUp, this.output); this.addListeners("#btn-next-output-tab,#btn-previous-output-tab", "mouseup", this.output.tabMouseUp, this.output);

View File

@ -49,9 +49,14 @@ class InputWaiter {
this.loaderWorkers = []; this.loaderWorkers = [];
this.workerId = 0; this.workerId = 0;
this.maxTabs = this.manager.tabs.calcMaxTabs(); this.maxTabs = this.manager.tabs.calcMaxTabs();
this.maxWorkers = navigator.hardwareConcurrency || 4;
this.callbacks = {}; this.callbacks = {};
this.callbackID = 0; this.callbackID = 0;
this.maxWorkers = 1;
if (navigator.hardwareConcurrency !== undefined &&
navigator.hardwareConcurrency > 1) {
this.maxWorkers = navigator.hardwareConcurrency - 1;
}
} }
/** /**

View File

@ -269,7 +269,8 @@ class OutputWaiter {
this.toggleLoader(true); this.toggleLoader(true);
return new Promise(async function(resolve, reject) { return new Promise(async function(resolve, reject) {
const output = this.outputs[inputNum]; const output = this.outputs[inputNum],
activeTab = this.manager.tabs.getActiveOutputTab();
if (output === undefined || output === null) return; if (output === undefined || output === null) return;
if (typeof inputNum !== "number") inputNum = parseInt(inputNum, 10); if (typeof inputNum !== "number") inputNum = parseInt(inputNum, 10);
@ -370,7 +371,7 @@ class OutputWaiter {
outputHtml.innerHTML = ""; outputHtml.innerHTML = "";
length = output.data.result.byteLength; length = output.data.result.byteLength;
this.setFile(await this.getDishBuffer(output.data.dish)); this.setFile(await this.getDishBuffer(output.data.dish), activeTab);
break; break;
case "string": case "string":
default: default:
@ -405,8 +406,10 @@ class OutputWaiter {
* Shows file details * Shows file details
* *
* @param {ArrayBuffer} buf * @param {ArrayBuffer} buf
* @param {number} activeTab
*/ */
setFile(buf) { setFile(buf, activeTab) {
if (activeTab !== this.manager.tabs.getActiveOutputTab()) return;
// Display file overlay in output area with details // Display file overlay in output area with details
const fileOverlay = document.getElementById("output-file"), const fileOverlay = document.getElementById("output-file"),
fileSize = document.getElementById("output-file-size"), fileSize = document.getElementById("output-file-size"),

View File

@ -157,6 +157,8 @@ class TabWaiter {
newTabContent.innerText = `Tab ${inputNum.toString()}`; newTabContent.innerText = `Tab ${inputNum.toString()}`;
newTabContent.addEventListener("wheel", this.manager[io].scrollTab.bind(this.manager[io]), {passive: false});
newTab.appendChild(newTabContent); newTab.appendChild(newTabContent);
if (io === "input") { if (io === "input") {

View File

@ -6,6 +6,7 @@
*/ */
import ChefWorker from "worker-loader?inline&fallback=false!../../core/ChefWorker"; import ChefWorker from "worker-loader?inline&fallback=false!../../core/ChefWorker";
import DishWorker from "worker-loader?inline&fallback=false!../workers/DishWorker";
/** /**
* Waiter to handle conversations with the ChefWorker * Waiter to handle conversations with the ChefWorker
@ -24,8 +25,6 @@ class WorkerWaiter {
this.loaded = false; this.loaded = false;
this.chefWorkers = []; this.chefWorkers = [];
this.dishWorker = null;
this.maxWorkers = navigator.hardwareConcurrency || 4;
this.inputs = []; this.inputs = [];
this.inputNums = []; this.inputNums = [];
this.totalOutputs = 0; this.totalOutputs = 0;
@ -33,6 +32,19 @@ class WorkerWaiter {
this.bakeId = 0; this.bakeId = 0;
this.callbacks = {}; this.callbacks = {};
this.callbackID = 0; this.callbackID = 0;
this.maxWorkers = 1;
if (navigator.hardwareConcurrency !== undefined &&
navigator.hardwareConcurrency > 1) {
this.maxWorkers = navigator.hardwareConcurrency - 1;
}
// Store dishWorker action (getDishAs or getDishTitle)
this.dishWorker = {
worker: null,
currentAction: ""
};
this.dishWorkerQueue = [];
} }
/** /**
@ -48,30 +60,20 @@ class WorkerWaiter {
} }
/** /**
* Sets up a separate ChefWorker for performing dish operations. * Sets up a DishWorker to be used for performing Dish operations
* Using a separate worker so that we can run dish operations without
* affecting a bake which may be in progress.
*/ */
setupDishWorker() { setupDishWorker() {
if (this.dishWorker !== null) { if (this.dishWorker.worker !== null) {
this.dishWorker.terminate(); this.dishWorker.worker.terminate();
} }
log.debug("Adding new ChefWorker (DishWorker)"); log.debug("Adding new DishWorker");
this.dishWorker = new ChefWorker(); this.dishWorker.worker = new DishWorker();
this.dishWorker.addEventListener("message", this.handleChefMessage.bind(this)); this.dishWorker.worker.addEventListener("message", this.handleDishMessage.bind(this));
let docURL = document.location.href.split(/[#?]/)[0]; if (this.dishWorkerQueue.length > 0) {
const index = docURL.lastIndexOf("/"); this.postDishMessage(this.dishWorkerQueue.splice(0, 1));
if (index > 0) {
docURL = docURL.substring(0, index);
} }
this.dishWorker.postMessage({"action": "docURL", "data": docURL});
this.dishWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
} }
/** /**
@ -242,7 +244,6 @@ class WorkerWaiter {
} }
} }
/** /**
* Update the value of an output * Update the value of an output
* *
@ -607,6 +608,31 @@ class WorkerWaiter {
}); });
} }
/**
* Handler for messages sent back from DishWorker
*
* @param {MessageEvent} e
*/
handleDishMessage(e) {
const r = e.data;
log.debug(`Receiving ${r.action} from DishWorker`);
switch (r.action) {
case "dishReturned":
this.dishWorker.currentAction = "";
this.callbacks[r.data.id](r.data);
if (this.dishWorkerQueue.length > 0) {
this.postDishMessage(this.dishWorkerQueue.splice(0, 1)[0]);
}
break;
default:
log.error("Unrecognised message from DishWorker", e);
break;
}
}
/** /**
* Asks the ChefWorker to return the dish as the specified type * Asks the ChefWorker to return the dish as the specified type
* *
@ -619,9 +645,9 @@ class WorkerWaiter {
this.callbacks[id] = callback; this.callbacks[id] = callback;
if (this.dishWorker === null) this.setupDishWorker(); if (this.dishWorker.worker === null) this.setupDishWorker();
this.dishWorker.postMessage({ this.postDishMessage({
action: "getDishAs", action: "getDishAs",
data: { data: {
dish: dish, dish: dish,
@ -644,9 +670,9 @@ class WorkerWaiter {
this.callbacks[id] = callback; this.callbacks[id] = callback;
if (this.dishWorker === null) this.setupDishWorker(); if (this.dishWorker.worker === null) this.setupDishWorker();
this.dishWorker.postMessage({ this.postDishMessage({
action: "getDishTitle", action: "getDishTitle",
data: { data: {
dish: dish, dish: dish,
@ -656,6 +682,39 @@ class WorkerWaiter {
}); });
} }
/**
* Queues a message to be sent to the dishWorker
*
* @param {object} message
* @param {string} message.action
* @param {object} message.data
* @param {Dish} message.data.dish
* @param {number} message.data.id
*/
queueDishMessage(message) {
if (message.action === "getDishAs") {
this.dishWorkerQueue = [message].concat(this.dishWorkerQueue);
} else {
this.dishWorkerQueue.push(message);
}
}
/**
* Sends a message to the DishWorker
*
* @param {object} message
* @param {string} message.action
* @param {object} message.data
*/
postDishMessage(message) {
if (this.dishWorker.currentAction !== "") {
this.queueDishMessage(message);
} else {
this.dishWorker.currentAction = message.action;
this.dishWorker.worker.postMessage(message);
}
}
/** /**
* Sets the console log level in the workers. * Sets the console log level in the workers.
*/ */