Convert the getPdfManager function to be asynchronous

This is fairly old code, and by making the function `async` we can handle initialization errors "automatically" without the need for try-catch statements.
This commit is contained in:
Jonas Jenwald 2024-11-22 17:49:43 +01:00
parent 1f6cc85134
commit 8ec399d7e1

View File

@ -189,7 +189,7 @@ class WorkerMessageHandler {
return { numPages, fingerprints, htmlForXfa }; return { numPages, fingerprints, htmlForXfa };
} }
function getPdfManager({ async function getPdfManager({
data, data,
password, password,
disableAutoFetch, disableAutoFetch,
@ -211,32 +211,20 @@ class WorkerMessageHandler {
password, password,
rangeChunkSize, rangeChunkSize,
}; };
const pdfManagerCapability = Promise.withResolvers();
let newPdfManager;
if (data) { if (data) {
try { pdfManagerArgs.source = data;
pdfManagerArgs.source = data;
newPdfManager = new LocalPdfManager(pdfManagerArgs); return new LocalPdfManager(pdfManagerArgs);
pdfManagerCapability.resolve(newPdfManager);
} catch (ex) {
pdfManagerCapability.reject(ex);
}
return pdfManagerCapability.promise;
} }
const pdfStream = new PDFWorkerStream(handler),
fullRequest = pdfStream.getFullReader();
let pdfStream, const pdfManagerCapability = Promise.withResolvers();
let newPdfManager,
cachedChunks = [], cachedChunks = [],
loaded = 0; loaded = 0;
try {
pdfStream = new PDFWorkerStream(handler);
} catch (ex) {
pdfManagerCapability.reject(ex);
return pdfManagerCapability.promise;
}
const fullRequest = pdfStream.getFullReader();
fullRequest.headersReady fullRequest.headersReady
.then(function () { .then(function () {
if (!fullRequest.isRangeSupported) { if (!fullRequest.isRangeSupported) {
@ -315,7 +303,7 @@ class WorkerMessageHandler {
cancelXHRs = null; cancelXHRs = null;
}); });
cancelXHRs = function (reason) { cancelXHRs = reason => {
pdfStream.cancelAllRequests(reason); pdfStream.cancelAllRequests(reason);
}; };