420733b93a
* add Patch CSP option * show style version, size, and update age in manager * add scope selector to style search in manager * keep scroll position and selections in tab's session * directly install usercss from raw github links * ditch localStorage, use on-demand SessionStore proxy * simplify localization * allow <code> tag in i18n-html * keep nodes in HTML templates * API.getAllStyles is actually faster with code untouched * fix fitToContent when applies-to is taller than window * dedupe linter.enableForEditor calls * prioritize visible CMs in refreshOnViewListener * don't scroll to last style on editing a new one * delay colorview for invisible CMs * eslint comma-dangle error + autofix files * styleViaXhr: also toggle for disableAll pref * styleViaXhr: allow cookies for sandbox CSP * simplify notes in options * simplify getStylesViaXhr * oldUI fixups: * remove separator before 1st applies-to * center name bubbles * fix updateToc focus on a newly added section * fix fitToContent when cloning section * remove CSS `contain` as it makes no difference * replace overrides with declarative CSS + code cosmetics * simplify adjustWidth and make it work in FF
85 lines
1.9 KiB
JavaScript
85 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const workerUtil = {
|
|
|
|
createWorker({url, lifeTime = 300}) {
|
|
let worker;
|
|
let id;
|
|
let timer;
|
|
const pendingResponse = new Map();
|
|
return new Proxy({}, {
|
|
get: (target, prop) =>
|
|
(...args) => {
|
|
if (!worker) {
|
|
init();
|
|
}
|
|
return invoke(prop, args);
|
|
},
|
|
});
|
|
|
|
function init() {
|
|
id = 0;
|
|
worker = new Worker(url);
|
|
worker.onmessage = onMessage;
|
|
}
|
|
|
|
function uninit() {
|
|
worker.onmessage = null;
|
|
worker.terminate();
|
|
worker = null;
|
|
}
|
|
|
|
function onMessage({data: {id, data, error}}) {
|
|
pendingResponse.get(id)[error ? 'reject' : 'resolve'](data);
|
|
pendingResponse.delete(id);
|
|
if (!pendingResponse.size && lifeTime >= 0) {
|
|
timer = setTimeout(uninit, lifeTime * 1000);
|
|
}
|
|
}
|
|
|
|
function invoke(action, args) {
|
|
return new Promise((resolve, reject) => {
|
|
pendingResponse.set(id, {resolve, reject});
|
|
clearTimeout(timer);
|
|
worker.postMessage({id, action, args});
|
|
id++;
|
|
});
|
|
}
|
|
},
|
|
|
|
createAPI(methods) {
|
|
self.onmessage = async ({data: {id, action, args}}) => {
|
|
let data, error;
|
|
try {
|
|
data = await methods[action](...args);
|
|
} catch (err) {
|
|
error = true;
|
|
data = workerUtil.cloneError(err);
|
|
}
|
|
self.postMessage({id, data, error});
|
|
};
|
|
},
|
|
|
|
cloneError(err) {
|
|
return Object.assign({
|
|
name: err.name,
|
|
stack: err.stack,
|
|
message: err.message,
|
|
lineNumber: err.lineNumber,
|
|
columnNumber: err.columnNumber,
|
|
fileName: err.fileName,
|
|
}, err);
|
|
},
|
|
|
|
loadScript(...urls) {
|
|
urls = urls.filter(u => !workerUtil._loadedScripts.has(u));
|
|
if (!urls.length) {
|
|
return;
|
|
}
|
|
self.importScripts(...urls);
|
|
urls.forEach(u => workerUtil._loadedScripts.add(u));
|
|
},
|
|
|
|
_loadedScripts: new Set(),
|
|
};
|