From 196f7d7df142db257e42c75a7bf87060d3aa5065 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 2 Nov 2024 11:06:30 +0100 Subject: [PATCH] Inline the `flushChunks` helper function, used in `getPdfManager` on the worker-thread - This helper function has only a single call-site, and the function is fairly short. - It'll only be invoked if range requests are *disabled*, or if the entire PDF manages to load *before* the headers are resolved (which is very unlikely). Hence, by default, this helper function is not invoked. - By inlining the code we're able to utilize the existing error-handling at the call-site, rather than having to duplicate it, which further reduces the size of this code. Finally, while slightly unrelated, this patch also adds optional chaining in one spot in the file (PR 16424 follow-up). --- src/core/worker.js | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index a8e0c42306233..2b6c9ac85222c 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -227,7 +227,8 @@ class WorkerMessageHandler { } let pdfStream, - cachedChunks = []; + cachedChunks = [], + loaded = 0; try { pdfStream = new PDFWorkerStream(handler); } catch (ex) { @@ -263,30 +264,22 @@ class WorkerMessageHandler { cancelXHRs = null; }); - let loaded = 0; - const flushChunks = function () { - const pdfFile = arrayBuffersToBytes(cachedChunks); - if (length && pdfFile.length !== length) { - warn("reported HTTP length is different from actual"); - } - // the data is array, instantiating directly from it - try { - pdfManagerArgs.source = pdfFile; - - newPdfManager = new LocalPdfManager(pdfManagerArgs); - pdfManagerCapability.resolve(newPdfManager); - } catch (ex) { - pdfManagerCapability.reject(ex); - } - cachedChunks = []; - }; new Promise(function (resolve, reject) { const readChunk = function ({ value, done }) { try { ensureNotTerminated(); if (done) { if (!newPdfManager) { - flushChunks(); + const pdfFile = arrayBuffersToBytes(cachedChunks); + cachedChunks = []; + + if (length && pdfFile.length !== length) { + warn("reported HTTP length is different from actual"); + } + pdfManagerArgs.source = pdfFile; + + newPdfManager = new LocalPdfManager(pdfManagerArgs); + pdfManagerCapability.resolve(newPdfManager); } cancelXHRs = null; return; @@ -854,9 +847,7 @@ class WorkerMessageHandler { } else { clearGlobalCaches(); } - if (cancelXHRs) { - cancelXHRs(new AbortException("Worker was terminated.")); - } + cancelXHRs?.(new AbortException("Worker was terminated.")); for (const task of WorkerTasks) { waitOn.push(task.finished);