Convert the ChunkedStreamManager.requestsByChunk property to a Map

Compared to regular `Object`s, `Map`s have a number of advantageous properties: Of particular importance in this case is the built-in iteration support, and that determining if the structure is empty is easy.
This commit is contained in:
Jonas Jenwald 2020-06-09 16:06:26 +02:00
parent 17e23ffb33
commit dda7a5d1b7

View File

@ -320,7 +320,7 @@ class ChunkedStreamManager {
this.currRequestId = 0; this.currRequestId = 0;
this._chunksNeededByRequest = new Map(); this._chunksNeededByRequest = new Map();
this.requestsByChunk = Object.create(null); this._requestsByChunk = new Map();
this.promisesByRequest = Object.create(null); this.promisesByRequest = Object.create(null);
this.progressiveDataLength = 0; this.progressiveDataLength = 0;
this.aborted = false; this.aborted = false;
@ -401,11 +401,14 @@ class ChunkedStreamManager {
const chunksToRequest = []; const chunksToRequest = [];
for (const chunk of chunksNeeded) { for (const chunk of chunksNeeded) {
if (!(chunk in this.requestsByChunk)) { let requestIds = this._requestsByChunk.get(chunk);
this.requestsByChunk[chunk] = []; if (!requestIds) {
requestIds = [];
this._requestsByChunk.set(chunk, requestIds);
chunksToRequest.push(chunk); chunksToRequest.push(chunk);
} }
this.requestsByChunk[chunk].push(requestId); requestIds.push(requestId);
} }
if (!chunksToRequest.length) { if (!chunksToRequest.length) {
@ -521,8 +524,11 @@ class ChunkedStreamManager {
const loadedRequests = []; const loadedRequests = [];
for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) { for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
// The server might return more chunks than requested. // The server might return more chunks than requested.
const requestIds = this.requestsByChunk[curChunk] || []; const requestIds = this._requestsByChunk.get(curChunk);
delete this.requestsByChunk[curChunk]; if (!requestIds) {
continue;
}
this._requestsByChunk.delete(curChunk);
for (const requestId of requestIds) { for (const requestId of requestIds) {
const chunksNeeded = this._chunksNeededByRequest.get(requestId); const chunksNeeded = this._chunksNeededByRequest.get(requestId);
@ -539,7 +545,7 @@ class ChunkedStreamManager {
// If there are no pending requests, automatically fetch the next // If there are no pending requests, automatically fetch the next
// unfetched chunk of the PDF file. // unfetched chunk of the PDF file.
if (!this.disableAutoFetch && isEmptyObj(this.requestsByChunk)) { if (!this.disableAutoFetch && this._requestsByChunk.size === 0) {
let nextEmptyChunk; let nextEmptyChunk;
if (this.stream.numChunksLoaded === 1) { if (this.stream.numChunksLoaded === 1) {
// This is a special optimization so that after fetching the first // This is a special optimization so that after fetching the first