From d5c9be341d344a31bf90bccbe9ba5aa37b023d01 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 16 Mar 2022 12:55:01 +0100 Subject: [PATCH 1/2] [src/display/api.js] Use private static class fields, rather than `shadow`ed getter work-arounds (PR 13813, 13882 follow-up) At the time private static class fields were to new, however that's no longer an issue and we can thus (ever so slightly) simplify the code. --- src/display/api.js | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 37317c50526fd..b743a0ec0535d 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -533,9 +533,7 @@ async function _fetchDocument(worker, source, pdfDataRangeTransport, docId) { * after which individual pages can be rendered. */ class PDFDocumentLoadingTask { - static get idCounters() { - return shadow(this, "idCounters", { doc: 0 }); - } + static #docId = 0; constructor() { this._capability = createPromiseCapability(); @@ -546,7 +544,7 @@ class PDFDocumentLoadingTask { * Unique identifier for the document loading task. * @type {string} */ - this.docId = `d${PDFDocumentLoadingTask.idCounters.doc++}`; + this.docId = `d${PDFDocumentLoadingTask.#docId++}`; /** * Whether the loading task is destroyed or not. @@ -2017,16 +2015,14 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { * @param {PDFWorkerParameters} params - The worker initialization parameters. */ class PDFWorker { - static get _workerPorts() { - return shadow(this, "_workerPorts", new WeakMap()); - } + static #workerPorts = new WeakMap(); constructor({ name = null, port = null, verbosity = getVerbosityLevel(), } = {}) { - if (port && PDFWorker._workerPorts.has(port)) { + if (port && PDFWorker.#workerPorts.has(port)) { throw new Error("Cannot use more than one PDFWorker per port."); } @@ -2040,7 +2036,7 @@ class PDFWorker { this._messageHandler = null; if (port) { - PDFWorker._workerPorts.set(port, this); + PDFWorker.#workerPorts.set(port, this); this._initializeFromPort(port); return; } @@ -2245,7 +2241,7 @@ class PDFWorker { this._webWorker.terminate(); this._webWorker = null; } - PDFWorker._workerPorts.delete(this._port); + PDFWorker.#workerPorts.delete(this._port); this._port = null; if (this._messageHandler) { this._messageHandler.destroy(); @@ -2260,8 +2256,8 @@ class PDFWorker { if (!params?.port) { throw new Error("PDFWorker.fromPort - invalid method signature."); } - if (this._workerPorts.has(params.port)) { - return this._workerPorts.get(params.port); + if (this.#workerPorts.has(params.port)) { + return this.#workerPorts.get(params.port); } return new PDFWorker(params); } @@ -3196,9 +3192,7 @@ class RenderTask { * @ignore */ class InternalRenderTask { - static get canvasInUse() { - return shadow(this, "canvasInUse", new WeakSet()); - } + static #canvasInUse = new WeakSet(); constructor({ callback, @@ -3251,14 +3245,14 @@ class InternalRenderTask { return; } if (this._canvas) { - if (InternalRenderTask.canvasInUse.has(this._canvas)) { + if (InternalRenderTask.#canvasInUse.has(this._canvas)) { throw new Error( "Cannot use the same canvas during multiple render() operations. " + "Use different canvas or ensure previous operations were " + "cancelled or completed." ); } - InternalRenderTask.canvasInUse.add(this._canvas); + InternalRenderTask.#canvasInUse.add(this._canvas); } if (this._pdfBug && globalThis.StepperManager?.enabled) { @@ -3298,7 +3292,7 @@ class InternalRenderTask { this.gfx.endDrawing(); } if (this._canvas) { - InternalRenderTask.canvasInUse.delete(this._canvas); + InternalRenderTask.#canvasInUse.delete(this._canvas); } this.callback( error || @@ -3364,7 +3358,7 @@ class InternalRenderTask { if (this.operatorList.lastChunk) { this.gfx.endDrawing(); if (this._canvas) { - InternalRenderTask.canvasInUse.delete(this._canvas); + InternalRenderTask.#canvasInUse.delete(this._canvas); } this.callback(); } From be2b1d5d2a3376de531db6909004bfc09cda231c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 16 Mar 2022 13:04:47 +0100 Subject: [PATCH 2/2] [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 5ec40561da80c..04fc03abefb38 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 b743a0ec0535d..df5ad2a8b850c 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