From be2b1d5d2a3376de531db6909004bfc09cda231c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 16 Mar 2022 13:04:47 +0100 Subject: [PATCH] [src/display/api.js] Simplify the `sendTest` function, used with Worker initialization (PR 14291 follow-up) Given that we now only use Workers when `postMessage` transfers are supported, there's really no point in trying to send a "test" message *without* transfers present. Hence, if `postMessage` transfers are not supported by the browser, we'll now fallback to "fake" Workers immediately instead. The comment about Opera is also removed, since it was originally added back in PR 983 and mentions Opera `11.60` [which was released in 2011](https://en.wikipedia.org/wiki/History_of_the_Opera_web_browser#Version_11). --- src/core/worker.js | 5 ++--- src/display/api.js | 13 +++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index 5ec40561d..04fc03abe 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -75,9 +75,8 @@ class WorkerMessageHandler { } testMessageProcessed = true; - // Ensure that `TypedArray`s can be sent to the worker, - // and that `postMessage` transfers are supported. - handler.send("test", data instanceof Uint8Array && data[0] === 255); + // Ensure that `TypedArray`s can be sent to the worker. + handler.send("test", data instanceof Uint8Array); }); handler.on("configure", function wphConfigure(data) { diff --git a/src/display/api.js b/src/display/api.js index b743a0ec0..df5ad2a8b 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2167,16 +2167,9 @@ class PDFWorker { }); const sendTest = () => { - const testObj = new Uint8Array([255]); - // Some versions of Opera throw a DATA_CLONE_ERR on serializing the - // typed array. Also, checking if we can use transfers. - try { - messageHandler.send("test", testObj, [testObj.buffer]); - } catch (ex) { - warn("Cannot use postMessage transfers."); - testObj[0] = 0; - messageHandler.send("test", testObj); - } + const testObj = new Uint8Array(); + // Ensure that we can use `postMessage` transfers. + messageHandler.send("test", testObj, [testObj.buffer]); }; // It might take time for the worker to initialize. We will try to send