Merge pull request #16032 from Snuffleupagus/less-arrayByteLength

Reduce usage of the `arrayByteLength` helper function
This commit is contained in:
Jonas Jenwald 2023-02-09 18:56:20 +01:00 committed by GitHub
commit 1fc8350795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 17 deletions

View File

@ -14,8 +14,8 @@
*/ */
import { import {
arrayByteLength,
arraysToBytes, arraysToBytes,
assert,
createPromiseCapability, createPromiseCapability,
} from "../shared/util.js"; } from "../shared/util.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
@ -291,21 +291,31 @@ class ChunkedStreamManager {
let chunks = [], let chunks = [],
loaded = 0; loaded = 0;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const readChunk = chunk => { const readChunk = ({ value, done }) => {
try { try {
if (!chunk.done) { if (done) {
const data = chunk.value; const chunkData = arraysToBytes(chunks);
chunks.push(data); chunks = null;
loaded += arrayByteLength(data); resolve(chunkData);
if (rangeReader.isStreamingSupported) {
this.onProgress({ loaded });
}
rangeReader.read().then(readChunk, reject);
return; return;
} }
const chunkData = arraysToBytes(chunks); if (
chunks = null; typeof PDFJSDev === "undefined" ||
resolve(chunkData); 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) { } catch (e) {
reject(e); reject(e);
} }

View File

@ -15,8 +15,8 @@
import { import {
AbortException, AbortException,
arrayByteLength,
arraysToBytes, arraysToBytes,
assert,
createPromiseCapability, createPromiseCapability,
getVerbosityLevel, getVerbosityLevel,
info, info,
@ -314,8 +314,17 @@ class WorkerMessageHandler {
cancelXHRs = null; cancelXHRs = null;
return; 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) { if (!fullRequest.isStreamingSupported) {
handler.send("DocProgress", { handler.send("DocProgress", {
loaded, loaded,
@ -328,7 +337,6 @@ class WorkerMessageHandler {
} else { } else {
cachedChunks.push(value); cachedChunks.push(value);
} }
fullRequest.read().then(readChunk, reject); fullRequest.read().then(readChunk, reject);
} catch (e) { } catch (e) {
reject(e); reject(e);

View File

@ -1115,7 +1115,6 @@ export {
AnnotationReviewState, AnnotationReviewState,
AnnotationStateModelType, AnnotationStateModelType,
AnnotationType, AnnotationType,
arrayByteLength,
arraysToBytes, arraysToBytes,
assert, assert,
BaseException, BaseException,