Initialize the PDFWorker.#workerPorts WeakMap lazily

By default this WeakMap isn't needed, and it's simple enough to initialize it lazily instead.
This commit is contained in:
Jonas Jenwald 2023-08-19 16:18:38 +02:00
parent 2993c7725b
commit 988ce2820b

View File

@ -2061,7 +2061,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
* @param {PDFWorkerParameters} params - The worker initialization parameters.
*/
class PDFWorker {
static #workerPorts = new WeakMap();
static #workerPorts;
constructor({
name = null,
@ -2081,10 +2081,10 @@ class PDFWorker {
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
port
) {
if (PDFWorker.#workerPorts.has(port)) {
if (PDFWorker.#workerPorts?.has(port)) {
throw new Error("Cannot use more than one PDFWorker per port.");
}
PDFWorker.#workerPorts.set(port, this);
(PDFWorker.#workerPorts ||= new WeakMap()).set(port, this);
this._initializeFromPort(port);
return;
}
@ -2290,7 +2290,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();
@ -2308,7 +2308,7 @@ class PDFWorker {
if (!params?.port) {
throw new Error("PDFWorker.fromPort - invalid method signature.");
}
const cachedPort = this.#workerPorts.get(params.port);
const cachedPort = this.#workerPorts?.get(params.port);
if (cachedPort) {
if (cachedPort._pendingDestroy) {
throw new Error(