Skip to content

Commit ab672f0

Browse files
committed
Replace PDFWorker.fromPort with a generic PDFWorker.create method
This allows us to simply invoke `PDFWorker.create` unconditionally from the `getDocument` function, without having to manually check if a global `workerPort` is available first.
1 parent 278fc06 commit ab672f0

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/display/api.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ import {
3838
PrintAnnotationStorage,
3939
SerializableEmpty,
4040
} from "./annotation_storage.js";
41-
import { FontFaceObject, FontLoader } from "./font_loader.js";
4241
import {
42+
deprecated,
4343
isDataScheme,
4444
isValidFetchUrl,
4545
PageViewport,
4646
RenderingCancelledException,
4747
StatTimer,
4848
} from "./display_utils.js";
49+
import { FontFaceObject, FontLoader } from "./font_loader.js";
4950
import { MessageHandler, wrapReason } from "../shared/message_handler.js";
5051
import {
5152
NodeCanvasFactory,
@@ -383,15 +384,12 @@ function getDocument(src = {}) {
383384
};
384385

385386
if (!worker) {
386-
const workerParams = {
387-
verbosity,
388-
port: GlobalWorkerOptions.workerPort,
389-
};
390387
// Worker was not provided -- creating and owning our own. If message port
391388
// is specified in global worker options, using it.
392-
worker = workerParams.port
393-
? PDFWorker.fromPort(workerParams)
394-
: new PDFWorker(workerParams);
389+
worker = PDFWorker.create({
390+
verbosity,
391+
port: GlobalWorkerOptions.workerPort,
392+
});
395393
task._worker = worker;
396394
}
397395

@@ -2131,6 +2129,16 @@ class PDFWorker {
21312129
new Blob([wrapper], { type: "text/javascript" })
21322130
);
21332131
};
2132+
2133+
this.fromPort = params => {
2134+
deprecated(
2135+
"`PDFWorker.fromPort` - please use `PDFWorker.create` instead."
2136+
);
2137+
if (!params?.port) {
2138+
throw new Error("PDFWorker.fromPort - invalid method signature.");
2139+
}
2140+
return this.create(params);
2141+
};
21342142
}
21352143

21362144
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
@@ -2364,15 +2372,12 @@ class PDFWorker {
23642372
* @param {PDFWorkerParameters} params - The worker initialization parameters.
23652373
* @returns {PDFWorker}
23662374
*/
2367-
static fromPort(params) {
2368-
if (!params?.port) {
2369-
throw new Error("PDFWorker.fromPort - invalid method signature.");
2370-
}
2371-
const cachedPort = this.#workerPorts.get(params.port);
2375+
static create(params) {
2376+
const cachedPort = this.#workerPorts.get(params?.port);
23722377
if (cachedPort) {
23732378
if (cachedPort._pendingDestroy) {
23742379
throw new Error(
2375-
"PDFWorker.fromPort - the worker is being destroyed.\n" +
2380+
"PDFWorker.create - the worker is being destroyed.\n" +
23762381
"Please remember to await `PDFDocumentLoadingTask.destroy()`-calls."
23772382
);
23782383
}

test/unit/api_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ describe("api", function () {
10821082
getDocument(tracemonkeyGetDocumentParams);
10831083
}).toThrow(
10841084
new Error(
1085-
"PDFWorker.fromPort - the worker is being destroyed.\n" +
1085+
"PDFWorker.create - the worker is being destroyed.\n" +
10861086
"Please remember to await `PDFDocumentLoadingTask.destroy()`-calls."
10871087
)
10881088
);

0 commit comments

Comments
 (0)