From 159e13c4e45b01b267e783cd2ba5e7e25cef33d9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 9 Jun 2020 16:25:41 +0200 Subject: [PATCH] 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. --- src/core/chunked_stream.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 7437ae391..f041f8bc0 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -18,7 +18,6 @@ import { arrayByteLength, arraysToBytes, createPromiseCapability, - isEmptyObj, } from "../shared/util.js"; import { MissingDataException } from "./core_utils.js"; @@ -321,7 +320,7 @@ class ChunkedStreamManager { this._chunksNeededByRequest = new Map(); this._requestsByChunk = new Map(); - this.promisesByRequest = Object.create(null); + this._promisesByRequest = new Map(); this.progressiveDataLength = 0; this.aborted = false; @@ -397,7 +396,7 @@ class ChunkedStreamManager { } const capability = createPromiseCapability(); - this.promisesByRequest[requestId] = capability; + this._promisesByRequest.set(requestId, capability); const chunksToRequest = []; for (const chunk of chunksNeeded) { @@ -564,8 +563,8 @@ class ChunkedStreamManager { } for (const requestId of loadedRequests) { - const capability = this.promisesByRequest[requestId]; - delete this.promisesByRequest[requestId]; + const capability = this._promisesByRequest.get(requestId); + this._promisesByRequest.delete(requestId); capability.resolve(); } @@ -592,8 +591,8 @@ class ChunkedStreamManager { if (this.pdfNetworkStream) { this.pdfNetworkStream.cancelAllRequests(reason); } - for (const requestId in this.promisesByRequest) { - this.promisesByRequest[requestId].reject(reason); + for (const [, capability] of this._promisesByRequest) { + capability.reject(reason); } } }