Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless message handlers #17335

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/core/worker.js
Expand Up @@ -87,8 +87,11 @@ class WorkerMessageHandler {
setVerbosityLevel(data.verbosity);
});

handler.on("GetDocRequest", function (data) {
return WorkerMessageHandler.createDocumentHandler(data, port);
handler.on("GetDocRequest", function ({ source, singleUse }) {
if (singleUse) {
handler.destroy();
}
return WorkerMessageHandler.createDocumentHandler(source, port);
});
}

Expand Down
15 changes: 13 additions & 2 deletions src/display/api.js
Expand Up @@ -216,6 +216,8 @@ const DefaultStandardFontDataFactory =
* when creating canvases. The default value is {new DOMCanvasFactory()}.
* @property {Object} [filterFactory] - A factory instance that will be used
* to create SVG filters when rendering some images on the main canvas.
* @property {boolean} [singleUse] - When true the worker will be able to load
* only one PDF document, using the `getDocument` method.
*/

/**
Expand Down Expand Up @@ -346,12 +348,13 @@ function getDocument(src) {
baseUrl: standardFontDataUrl,
});
}

if (!worker) {
const workerParams = {
verbosity,
port: GlobalWorkerOptions.workerPort,
singleUse: src.singleUse === true,
};

// Worker was not provided -- creating and owning our own. If message port
// is specified in global worker options, using it.
worker = workerParams.port
Expand Down Expand Up @@ -456,6 +459,12 @@ function getDocument(src) {
throw new Error("Loading aborted");
}

if (worker._singleUse) {
// We don't the messageHandler anymore.
worker._messageHandler.destroy();
worker._messageHandler = null;
}

const messageHandler = new MessageHandler(
docId,
workerId,
Expand Down Expand Up @@ -493,7 +502,7 @@ async function _fetchDocument(worker, source) {
}
const workerId = await worker.messageHandler.sendWithPromise(
"GetDocRequest",
source,
{ source, singleUse: worker._singleUse },
source.data ? [source.data.buffer] : null
);

Expand Down Expand Up @@ -2029,6 +2038,7 @@ class PDFWorker {
name = null,
port = null,
verbosity = getVerbosityLevel(),
singleUse = false,
} = {}) {
this.name = name;
this.destroyed = false;
Expand All @@ -2038,6 +2048,7 @@ class PDFWorker {
this._port = null;
this._webWorker = null;
this._messageHandler = null;
this._singleUse = singleUse;

if (
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
Expand Down
1 change: 1 addition & 0 deletions web/app.js
Expand Up @@ -1000,6 +1000,7 @@ const PDFViewerApplication = {
const loadingTask = getDocument({
...apiParams,
...args,
singleUse: true,
});
this.pdfLoadingTask = loadingTask;

Expand Down