Merge pull request #18992 from Snuffleupagus/getPdfManager-inline-flushChunks

Inline the `flushChunks` helper function, used in `getPdfManager` on the worker-thread
This commit is contained in:
Tim van der Meij 2024-11-02 18:58:29 +01:00 committed by GitHub
commit e930f3030c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -227,7 +227,8 @@ class WorkerMessageHandler {
} }
let pdfStream, let pdfStream,
cachedChunks = []; cachedChunks = [],
loaded = 0;
try { try {
pdfStream = new PDFWorkerStream(handler); pdfStream = new PDFWorkerStream(handler);
} catch (ex) { } catch (ex) {
@ -263,30 +264,22 @@ class WorkerMessageHandler {
cancelXHRs = null; 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) { new Promise(function (resolve, reject) {
const readChunk = function ({ value, done }) { const readChunk = function ({ value, done }) {
try { try {
ensureNotTerminated(); ensureNotTerminated();
if (done) { if (done) {
if (!newPdfManager) { 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; cancelXHRs = null;
return; return;
@ -854,9 +847,7 @@ class WorkerMessageHandler {
} else { } else {
clearGlobalCaches(); clearGlobalCaches();
} }
if (cancelXHRs) { cancelXHRs?.(new AbortException("Worker was terminated."));
cancelXHRs(new AbortException("Worker was terminated."));
}
for (const task of WorkerTasks) { for (const task of WorkerTasks) {
waitOn.push(task.finished); waitOn.push(task.finished);