Convert the ChunkedStreamManager.promisesByRequest 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:25:41 +02:00
parent dda7a5d1b7
commit 159e13c4e4

View File

@ -18,7 +18,6 @@ import {
arrayByteLength, arrayByteLength,
arraysToBytes, arraysToBytes,
createPromiseCapability, createPromiseCapability,
isEmptyObj,
} from "../shared/util.js"; } from "../shared/util.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
@ -321,7 +320,7 @@ class ChunkedStreamManager {
this._chunksNeededByRequest = new Map(); this._chunksNeededByRequest = new Map();
this._requestsByChunk = new Map(); this._requestsByChunk = new Map();
this.promisesByRequest = Object.create(null); this._promisesByRequest = new Map();
this.progressiveDataLength = 0; this.progressiveDataLength = 0;
this.aborted = false; this.aborted = false;
@ -397,7 +396,7 @@ class ChunkedStreamManager {
} }
const capability = createPromiseCapability(); const capability = createPromiseCapability();
this.promisesByRequest[requestId] = capability; this._promisesByRequest.set(requestId, capability);
const chunksToRequest = []; const chunksToRequest = [];
for (const chunk of chunksNeeded) { for (const chunk of chunksNeeded) {
@ -564,8 +563,8 @@ class ChunkedStreamManager {
} }
for (const requestId of loadedRequests) { for (const requestId of loadedRequests) {
const capability = this.promisesByRequest[requestId]; const capability = this._promisesByRequest.get(requestId);
delete this.promisesByRequest[requestId]; this._promisesByRequest.delete(requestId);
capability.resolve(); capability.resolve();
} }
@ -592,8 +591,8 @@ class ChunkedStreamManager {
if (this.pdfNetworkStream) { if (this.pdfNetworkStream) {
this.pdfNetworkStream.cancelAllRequests(reason); this.pdfNetworkStream.cancelAllRequests(reason);
} }
for (const requestId in this.promisesByRequest) { for (const [, capability] of this._promisesByRequest) {
this.promisesByRequest[requestId].reject(reason); capability.reject(reason);
} }
} }
} }