mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-02 04:17:24 +02:00
Convert the data reading in getPdfManager to be asynchronous
This is not only shorter, but (in my opinion) it also simplifies the code. *Note:* In order to keep the *five* different `BasePDFStreamReader` implementations consistent, we purposely don't re-factor the `PDFWorkerStreamReader` class to support `for await...of` iteration.
This commit is contained in:
parent
a4fcd830cc
commit
e8ab3cb335
@ -222,13 +222,13 @@ class WorkerMessageHandler {
|
|||||||
const pdfStream = new PDFWorkerStream({ msgHandler: handler }),
|
const pdfStream = new PDFWorkerStream({ msgHandler: handler }),
|
||||||
fullReader = pdfStream.getFullReader();
|
fullReader = pdfStream.getFullReader();
|
||||||
|
|
||||||
const pdfManagerCapability = Promise.withResolvers();
|
const { promise, resolve, reject } = Promise.withResolvers();
|
||||||
let newPdfManager,
|
let newPdfManager,
|
||||||
cachedChunks = [],
|
cachedChunks = [];
|
||||||
loaded = 0;
|
cancelXHRs = reason => pdfStream.cancelAllRequests(reason);
|
||||||
|
|
||||||
fullReader.headersReady
|
fullReader.headersReady
|
||||||
.then(function () {
|
.then(() => {
|
||||||
if (!fullReader.isRangeSupported) {
|
if (!fullReader.isRangeSupported) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -239,77 +239,73 @@ class WorkerMessageHandler {
|
|||||||
|
|
||||||
newPdfManager = new NetworkPdfManager(pdfManagerArgs);
|
newPdfManager = new NetworkPdfManager(pdfManagerArgs);
|
||||||
// There may be a chance that `newPdfManager` is not initialized for
|
// There may be a chance that `newPdfManager` is not initialized for
|
||||||
// the first few runs of `readchunk` block of code. Be sure to send
|
// the first few iterations of the `readData` code. Be sure to send
|
||||||
// all cached chunks, if any, to chunked_stream via pdf_manager.
|
// all cached chunks, if any, to chunked_stream via pdf_manager.
|
||||||
for (const chunk of cachedChunks) {
|
for (const chunk of cachedChunks) {
|
||||||
newPdfManager.sendProgressiveData(chunk);
|
newPdfManager.sendProgressiveData(chunk);
|
||||||
}
|
}
|
||||||
|
cachedChunks = null;
|
||||||
|
|
||||||
cachedChunks = [];
|
resolve(newPdfManager);
|
||||||
pdfManagerCapability.resolve(newPdfManager);
|
|
||||||
cancelXHRs = null;
|
cancelXHRs = null;
|
||||||
})
|
})
|
||||||
.catch(function (reason) {
|
.catch(reason => {
|
||||||
pdfManagerCapability.reject(reason);
|
reject(reason);
|
||||||
cancelXHRs = null;
|
cancelXHRs = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
new Promise(function (resolve, reject) {
|
async function readData() {
|
||||||
const readChunk = function ({ value, done }) {
|
let loaded = 0;
|
||||||
try {
|
|
||||||
ensureNotTerminated();
|
|
||||||
if (done) {
|
|
||||||
if (!newPdfManager) {
|
|
||||||
const pdfFile = arrayBuffersToBytes(cachedChunks);
|
|
||||||
cachedChunks = [];
|
|
||||||
|
|
||||||
if (length && pdfFile.length !== length) {
|
while (true) {
|
||||||
warn("reported HTTP length is different from actual");
|
const { value, done } = await fullReader.read();
|
||||||
}
|
ensureNotTerminated();
|
||||||
pdfManagerArgs.source = pdfFile;
|
|
||||||
|
|
||||||
newPdfManager = new LocalPdfManager(pdfManagerArgs);
|
if (done) {
|
||||||
pdfManagerCapability.resolve(newPdfManager);
|
break;
|
||||||
}
|
|
||||||
cancelXHRs = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
||||||
assert(
|
|
||||||
value instanceof ArrayBuffer,
|
|
||||||
"readChunk (getPdfManager) - expected an ArrayBuffer."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
loaded += value.byteLength;
|
|
||||||
|
|
||||||
if (!fullReader.isStreamingSupported) {
|
|
||||||
handler.send("DocProgress", {
|
|
||||||
loaded,
|
|
||||||
total: Math.max(loaded, fullReader.contentLength || 0),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newPdfManager) {
|
|
||||||
newPdfManager.sendProgressiveData(value);
|
|
||||||
} else {
|
|
||||||
cachedChunks.push(value);
|
|
||||||
}
|
|
||||||
fullReader.read().then(readChunk, reject);
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
}
|
||||||
};
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||||
fullReader.read().then(readChunk, reject);
|
assert(
|
||||||
}).catch(function (e) {
|
value instanceof ArrayBuffer,
|
||||||
pdfManagerCapability.reject(e);
|
"readData (getPdfManager) - expected an ArrayBuffer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
loaded += value.byteLength;
|
||||||
|
|
||||||
|
if (!fullReader.isStreamingSupported) {
|
||||||
|
handler.send("DocProgress", {
|
||||||
|
loaded,
|
||||||
|
total: Math.max(loaded, fullReader.contentLength || 0),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newPdfManager) {
|
||||||
|
newPdfManager.sendProgressiveData(value);
|
||||||
|
} else {
|
||||||
|
cachedChunks.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newPdfManager) {
|
||||||
|
const pdfFile = arrayBuffersToBytes(cachedChunks);
|
||||||
|
cachedChunks = null;
|
||||||
|
|
||||||
|
if (length && pdfFile.length !== length) {
|
||||||
|
warn("reported HTTP length is different from actual");
|
||||||
|
}
|
||||||
|
pdfManagerArgs.source = pdfFile;
|
||||||
|
|
||||||
|
newPdfManager = new LocalPdfManager(pdfManagerArgs);
|
||||||
|
resolve(newPdfManager);
|
||||||
|
}
|
||||||
|
cancelXHRs = null;
|
||||||
|
}
|
||||||
|
readData().catch(reason => {
|
||||||
|
reject(reason);
|
||||||
cancelXHRs = null;
|
cancelXHRs = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
cancelXHRs = reason => {
|
return promise;
|
||||||
pdfStream.cancelAllRequests(reason);
|
|
||||||
};
|
|
||||||
|
|
||||||
return pdfManagerCapability.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupDoc(data) {
|
function setupDoc(data) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user