Merge pull request #16852 from Snuffleupagus/less-workerPort

Initialize the `PDFWorker.#workerPorts` WeakMap lazily
This commit is contained in:
Tim van der Meij 2023-08-20 12:45:41 +02:00 committed by GitHub
commit a7d829155b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2061,7 +2061,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
* @param {PDFWorkerParameters} params - The worker initialization parameters. * @param {PDFWorkerParameters} params - The worker initialization parameters.
*/ */
class PDFWorker { class PDFWorker {
static #workerPorts = new WeakMap(); static #workerPorts;
constructor({ constructor({
name = null, name = null,
@ -2077,11 +2077,14 @@ class PDFWorker {
this._webWorker = null; this._webWorker = null;
this._messageHandler = null; this._messageHandler = null;
if (port) { if (
if (PDFWorker.#workerPorts.has(port)) { (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
port
) {
if (PDFWorker.#workerPorts?.has(port)) {
throw new Error("Cannot use more than one PDFWorker per 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); this._initializeFromPort(port);
return; return;
} }
@ -2113,6 +2116,9 @@ class PDFWorker {
} }
_initializeFromPort(port) { _initializeFromPort(port) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: _initializeFromPort");
}
this._port = port; this._port = port;
this._messageHandler = new MessageHandler("main", "worker", port); this._messageHandler = new MessageHandler("main", "worker", port);
this._messageHandler.on("ready", function () { this._messageHandler.on("ready", function () {
@ -2284,7 +2290,7 @@ class PDFWorker {
this._webWorker.terminate(); this._webWorker.terminate();
this._webWorker = null; this._webWorker = null;
} }
PDFWorker.#workerPorts.delete(this._port); PDFWorker.#workerPorts?.delete(this._port);
this._port = null; this._port = null;
if (this._messageHandler) { if (this._messageHandler) {
this._messageHandler.destroy(); this._messageHandler.destroy();
@ -2302,7 +2308,7 @@ class PDFWorker {
if (!params?.port) { if (!params?.port) {
throw new Error("PDFWorker.fromPort - invalid method signature."); 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) {
if (cachedPort._pendingDestroy) { if (cachedPort._pendingDestroy) {
throw new Error( throw new Error(