mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-11 17:45:49 +02:00
Convert ChunkedStreamManager.prototype.sendRequest to an asynchronous method
This is not only shorter, but (in my opinion) it also simplifies the code. *Note:* In order to keep the *five* different `BasePDFStreamRangeReader` implementations consistent, we purposely don't re-factor the `PDFWorkerStreamRangeReader` class to support `for await...of` iteration.
This commit is contained in:
parent
b6d028419b
commit
59fbad617b
@ -293,46 +293,37 @@ class ChunkedStreamManager {
|
|||||||
this.msgHandler = args.msgHandler;
|
this.msgHandler = args.msgHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendRequest(begin, end) {
|
async sendRequest(begin, end) {
|
||||||
const rangeReader = this.pdfStream.getRangeReader(begin, end);
|
const rangeReader = this.pdfStream.getRangeReader(begin, end);
|
||||||
|
|
||||||
let chunks = [];
|
let chunks = [];
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const readChunk = ({ value, done }) => {
|
while (true) {
|
||||||
try {
|
const { value, done } = await rangeReader.read();
|
||||||
if (done) {
|
|
||||||
resolve(
|
|
||||||
chunks.length > 0 || !this.disableAutoFetch
|
|
||||||
? arrayBuffersToBytes(chunks)
|
|
||||||
: null
|
|
||||||
);
|
|
||||||
chunks = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
||||||
assert(
|
|
||||||
value instanceof ArrayBuffer,
|
|
||||||
"readChunk (sendRequest) - expected an ArrayBuffer."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
chunks.push(value);
|
|
||||||
rangeReader.read().then(readChunk, reject);
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
rangeReader.read().then(readChunk, reject);
|
|
||||||
}).then(data => {
|
|
||||||
if (this.aborted) {
|
if (this.aborted) {
|
||||||
|
chunks = null;
|
||||||
return; // Ignoring any data after abort.
|
return; // Ignoring any data after abort.
|
||||||
}
|
}
|
||||||
if (!data) {
|
if (done) {
|
||||||
// The range request wasn't dispatched, see the "GetRangeReader" handler
|
break;
|
||||||
// in the `src/display/api.js` file.
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
this.onReceiveData({ chunk: data.buffer, begin });
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||||
});
|
assert(
|
||||||
|
value instanceof ArrayBuffer,
|
||||||
|
"sendRequest - expected an ArrayBuffer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
chunks.push(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chunks.length === 0 && this.disableAutoFetch) {
|
||||||
|
// The range request wasn't dispatched, see the "GetRangeReader" handler
|
||||||
|
// in the `src/display/api.js` file.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = arrayBuffersToBytes(chunks);
|
||||||
|
chunks = null;
|
||||||
|
this.onReceiveData({ chunk: data.buffer, begin });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user