Merge pull request #16852 from Snuffleupagus/less-workerPort
Initialize the `PDFWorker.#workerPorts` WeakMap lazily
This commit is contained in:
commit
a7d829155b
@ -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,
|
||||
@ -2077,11 +2077,14 @@ class PDFWorker {
|
||||
this._webWorker = null;
|
||||
this._messageHandler = null;
|
||||
|
||||
if (port) {
|
||||
if (PDFWorker.#workerPorts.has(port)) {
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
|
||||
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;
|
||||
}
|
||||
@ -2113,6 +2116,9 @@ class PDFWorker {
|
||||
}
|
||||
|
||||
_initializeFromPort(port) {
|
||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||
throw new Error("Not implemented: _initializeFromPort");
|
||||
}
|
||||
this._port = port;
|
||||
this._messageHandler = new MessageHandler("main", "worker", port);
|
||||
this._messageHandler.on("ready", function () {
|
||||
@ -2284,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();
|
||||
@ -2302,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(
|
||||
|
Loading…
Reference in New Issue
Block a user