Skip to content

Commit

Permalink
Use an AbortController to remove the temporary "error" handler for …
Browse files Browse the repository at this point in the history
…the worker
  • Loading branch information
Snuffleupagus committed Jun 15, 2024
1 parent 2d0e08f commit f3f88ee
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ class PDFWorker {
const worker = new Worker(workerSrc, { type: "module" });
const messageHandler = new MessageHandler("main", "worker", worker);
const terminateEarly = () => {
worker.removeEventListener("error", onWorkerError);
ac.abort();
messageHandler.destroy();
worker.terminate();
if (this.destroyed) {
Expand All @@ -2166,17 +2166,21 @@ class PDFWorker {
}
};

const onWorkerError = () => {
if (!this._webWorker) {
// Worker failed to initialize due to an error. Clean up and fall
// back to the fake worker.
terminateEarly();
}
};
worker.addEventListener("error", onWorkerError);
const ac = new AbortController();
worker.addEventListener(
"error",
() => {
if (!this._webWorker) {
// Worker failed to initialize due to an error. Clean up and fall
// back to the fake worker.
terminateEarly();
}
},
{ signal: ac.signal }
);

messageHandler.on("test", data => {
worker.removeEventListener("error", onWorkerError);
ac.abort();
if (this.destroyed || !data) {
terminateEarly();
return;
Expand All @@ -2189,7 +2193,7 @@ class PDFWorker {
});

messageHandler.on("ready", data => {
worker.removeEventListener("error", onWorkerError);
ac.abort();
if (this.destroyed) {
terminateEarly();
return;
Expand Down

0 comments on commit f3f88ee

Please sign in to comment.