From 1aa49d9063400d05ca940644ae650a58390b6c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Beaufort?= Date: Wed, 31 Jan 2024 11:51:21 +0100 Subject: [PATCH] Handle messageerror, remove es6, check raf only for dedicated worker --- index.js | 22 +++++++++++----------- worker-import.js | 3 --- worker-module.js | 16 ---------------- worker.js | 3 +++ 4 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 worker-import.js delete mode 100644 worker-module.js diff --git a/index.js b/index.js index 4e771ee..e591e66 100644 --- a/index.js +++ b/index.js @@ -339,6 +339,14 @@ class WorkerHelper { }); this._worker.onmessage = (e) => { const {id, data} = e.data; + if (data == "messageerror") { + this._bad = true; + // resolve all existing promises + this._promisesByIdMap.forEach(({resolve}) => { + resolve({}); + }); + return; + } this._messagesByIdMap.set(id, data); this._process(id); }; @@ -440,21 +448,13 @@ async function checkWorkers(workerType) { } } - addSupportsRow('requestAnimationFrame', rAF); + if (workerType === "dedicated") { + addSupportsRow('requestAnimationFrame', rAF); + } addSupportsRow('transferControlToOffscreen', offscreen); addSupportsRow('OffscreenCanvas', offscreenSupported); addSupportsRow('CanvasRenderingContext2D', twoD); - let moduleSupport = false; - try { - const workerModule = new WorkerHelper('worker-module.js'); - const data = await workerModule.getMessage('ping'); - moduleSupport = true; - } catch (e) { - // - } - addSupportsRow('es6 modules', moduleSupport); - addElemToDocument(el('table', { className: 'worker' }, [ el('tbody', {}, mapLikeToTableRows(obj, false)), ])); diff --git a/worker-import.js b/worker-import.js deleted file mode 100644 index 55a033d..0000000 --- a/worker-import.js +++ /dev/null @@ -1,3 +0,0 @@ -const msg = "imported"; - -export default msg; \ No newline at end of file diff --git a/worker-module.js b/worker-module.js deleted file mode 100644 index 4dd6fc3..0000000 --- a/worker-module.js +++ /dev/null @@ -1,16 +0,0 @@ -import msg from './worker-import.js'; - -const handlers = { - ping(id) { - postMessage({id, data: {msg}}); - }, -}; - -self.onmessage = function(e) { - const {command, id, data} = e.data; - const handler = handlers[command]; - if (!handler) { - throw new Error(`unknown command: ${command}`); - } - handler(id, data); -}; \ No newline at end of file diff --git a/worker.js b/worker.js index d205446..d1d6577 100644 --- a/worker.js +++ b/worker.js @@ -62,4 +62,7 @@ self.onconnect = function(e) { port.onmessage = function(event) { handleMessage.call(port, event); }; + port.onmessageerror = function() { + port.postMessage({data: 'messageerror'}); + }; };