Skip to content

Commit

Permalink
Merge pull request #16032 from Snuffleupagus/less-arrayByteLength
Browse files Browse the repository at this point in the history
Reduce usage of the `arrayByteLength` helper function
  • Loading branch information
Snuffleupagus authored Feb 9, 2023
2 parents 972744a + 96d338e commit 1fc8350
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
36 changes: 23 additions & 13 deletions src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/

import {
arrayByteLength,
arraysToBytes,
assert,
createPromiseCapability,
} from "../shared/util.js";
import { MissingDataException } from "./core_utils.js";
Expand Down Expand Up @@ -291,21 +291,31 @@ class ChunkedStreamManager {
let chunks = [],
loaded = 0;
return new Promise((resolve, reject) => {
const readChunk = chunk => {
const readChunk = ({ value, done }) => {
try {
if (!chunk.done) {
const data = chunk.value;
chunks.push(data);
loaded += arrayByteLength(data);
if (rangeReader.isStreamingSupported) {
this.onProgress({ loaded });
}
rangeReader.read().then(readChunk, reject);
if (done) {
const chunkData = arraysToBytes(chunks);
chunks = null;
resolve(chunkData);
return;
}
const chunkData = arraysToBytes(chunks);
chunks = null;
resolve(chunkData);
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
value instanceof ArrayBuffer,
"readChunk (sendRequest) - expected an ArrayBuffer."
);
}
loaded += value.byteLength;

if (rangeReader.isStreamingSupported) {
this.onProgress({ loaded });
}

chunks.push(value);
rangeReader.read().then(readChunk, reject);
} catch (e) {
reject(e);
}
Expand Down
14 changes: 11 additions & 3 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import {
AbortException,
arrayByteLength,
arraysToBytes,
assert,
createPromiseCapability,
getVerbosityLevel,
info,
Expand Down Expand Up @@ -314,8 +314,17 @@ class WorkerMessageHandler {
cancelXHRs = null;
return;
}
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
value instanceof ArrayBuffer,
"readChunk (getPdfManager) - expected an ArrayBuffer."
);
}
loaded += value.byteLength;

loaded += arrayByteLength(value);
if (!fullRequest.isStreamingSupported) {
handler.send("DocProgress", {
loaded,
Expand All @@ -328,7 +337,6 @@ class WorkerMessageHandler {
} else {
cachedChunks.push(value);
}

fullRequest.read().then(readChunk, reject);
} catch (e) {
reject(e);
Expand Down
1 change: 0 additions & 1 deletion src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,6 @@ export {
AnnotationReviewState,
AnnotationStateModelType,
AnnotationType,
arrayByteLength,
arraysToBytes,
assert,
BaseException,
Expand Down

0 comments on commit 1fc8350

Please sign in to comment.