Skip to content

Commit f302323

Browse files
Merge pull request #20627 from Snuffleupagus/ChunkedStream-onReceiveData-rm-copy
Improve progress reporting in `ChunkedStreamManager`, and prevent unnecessary data copy in `ChunkedStream.prototype.onReceiveData`
2 parents a0f3528 + b3cd042 commit f302323

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/core/chunked_stream.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { arrayBuffersToBytes, MissingDataException } from "./core_utils.js";
17-
import { assert } from "../shared/util.js";
17+
import { assert, MathClamp } from "../shared/util.js";
1818
import { Stream } from "./stream.js";
1919

2020
class ChunkedStream extends Stream {
@@ -70,6 +70,12 @@ class ChunkedStream extends Stream {
7070
throw new Error(`Bad end offset: ${end}`);
7171
}
7272

73+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
74+
assert(
75+
chunk instanceof ArrayBuffer,
76+
"onReceiveData - expected an ArrayBuffer."
77+
);
78+
}
7379
this.bytes.set(new Uint8Array(chunk), begin);
7480
const beginChunk = Math.floor(begin / chunkSize);
7581
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
@@ -85,6 +91,12 @@ class ChunkedStream extends Stream {
8591
let position = this.progressiveDataLength;
8692
const beginChunk = Math.floor(position / this.chunkSize);
8793

94+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
95+
assert(
96+
data instanceof ArrayBuffer,
97+
"onReceiveProgressiveData - expected an ArrayBuffer."
98+
);
99+
}
88100
this.bytes.set(new Uint8Array(data), position);
89101
position += data.byteLength;
90102
this.progressiveDataLength = position;
@@ -310,7 +322,7 @@ class ChunkedStreamManager {
310322
if (this.aborted) {
311323
return; // Ignoring any data after abort.
312324
}
313-
this.onReceiveData({ chunk: data, begin });
325+
this.onReceiveData({ chunk: data.buffer, begin });
314326
});
315327
}
316328

@@ -511,7 +523,11 @@ class ChunkedStreamManager {
511523
}
512524

513525
this.msgHandler.send("DocProgress", {
514-
loaded: stream.numChunksLoaded * chunkSize,
526+
loaded: MathClamp(
527+
stream.numChunksLoaded * chunkSize,
528+
stream.progressiveDataLength,
529+
length
530+
),
515531
total: length,
516532
});
517533
}

0 commit comments

Comments
 (0)