async'ify worker-util, reduce indirection

This commit is contained in:
tophf 2020-10-28 12:46:05 +03:00
parent 4764f91453
commit a81e1b8ac3
3 changed files with 33 additions and 47 deletions

View File

@ -2,9 +2,9 @@
'use strict'; 'use strict';
importScripts('/js/worker-util.js'); importScripts('/js/worker-util.js');
const {loadScript, createAPI} = workerUtil; const {loadScript} = workerUtil;
createAPI({ workerUtil.createAPI({
parseMozFormat(arg) { parseMozFormat(arg) {
loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js'); loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js');
return parseMozFormat(arg); return parseMozFormat(arg);

View File

@ -2,9 +2,9 @@
'use strict'; 'use strict';
importScripts('/js/worker-util.js'); importScripts('/js/worker-util.js');
const {createAPI, loadScript} = workerUtil; const {loadScript} = workerUtil;
createAPI({ workerUtil.createAPI({
csslint: (code, config) => { csslint: (code, config) => {
loadScript('/vendor-overwrites/csslint/parserlib.js', '/vendor-overwrites/csslint/csslint.js'); loadScript('/vendor-overwrites/csslint/parserlib.js', '/vendor-overwrites/csslint/csslint.js');
return CSSLint.verify(code, config).messages return CSSLint.verify(code, config).messages

View File

@ -1,17 +1,12 @@
/* global importScripts */
/* exported workerUtil */
'use strict'; 'use strict';
const workerUtil = (() => { const workerUtil = {
const loadedScripts = new Set();
return {createWorker, createAPI, loadScript, cloneError};
function createWorker({url, lifeTime = 300}) { createWorker({url, lifeTime = 300}) {
let worker; let worker;
let id; let id;
let timer; let timer;
const pendingResponse = new Map(); const pendingResponse = new Map();
return new Proxy({}, { return new Proxy({}, {
get: (target, prop) => get: (target, prop) =>
(...args) => { (...args) => {
@ -19,7 +14,7 @@ const workerUtil = (() => {
init(); init();
} }
return invoke(prop, args); return invoke(prop, args);
} },
}); });
function init() { function init() {
@ -34,10 +29,9 @@ const workerUtil = (() => {
worker = null; worker = null;
} }
function onMessage(e) { function onMessage({data: {id, data, error}}) {
const message = e.data; pendingResponse.get(id)[error ? 'reject' : 'resolve'](data);
pendingResponse.get(message.id)[message.error ? 'reject' : 'resolve'](message.data); pendingResponse.delete(id);
pendingResponse.delete(message.id);
if (!pendingResponse.size && lifeTime >= 0) { if (!pendingResponse.size && lifeTime >= 0) {
timer = setTimeout(uninit, lifeTime * 1000); timer = setTimeout(uninit, lifeTime * 1000);
} }
@ -47,36 +41,26 @@ const workerUtil = (() => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
pendingResponse.set(id, {resolve, reject}); pendingResponse.set(id, {resolve, reject});
clearTimeout(timer); clearTimeout(timer);
worker.postMessage({ worker.postMessage({id, action, args});
id,
action,
args
});
id++; id++;
}); });
} }
} },
function createAPI(methods) { createAPI(methods) {
self.onmessage = e => { self.onmessage = async ({data: {id, action, args}}) => {
const message = e.data; let data, error;
Promise.resolve() try {
.then(() => methods[message.action](...message.args)) data = await methods[action](...args);
.then(result => ({ } catch (err) {
id: message.id, error = true;
error: false, data = workerUtil.cloneError(err);
data: result }
})) self.postMessage({id, data, error});
.catch(err => ({
id: message.id,
error: true,
data: cloneError(err)
}))
.then(data => self.postMessage(data));
}; };
} },
function cloneError(err) { cloneError(err) {
return Object.assign({ return Object.assign({
name: err.name, name: err.name,
stack: err.stack, stack: err.stack,
@ -85,14 +69,16 @@ const workerUtil = (() => {
columnNumber: err.columnNumber, columnNumber: err.columnNumber,
fileName: err.fileName fileName: err.fileName
}, err); }, err);
} },
function loadScript(...scripts) { loadScript(...urls) {
const urls = scripts.filter(u => !loadedScripts.has(u)); urls = urls.filter(u => !workerUtil._loadedScripts.has(u));
if (!urls.length) { if (!urls.length) {
return; return;
} }
importScripts(...urls); self.importScripts(...urls);
urls.forEach(u => loadedScripts.add(u)); urls.forEach(u => workerUtil._loadedScripts.add(u));
} },
})();
_loadedScripts: new Set(),
};