From f4712bc0ad967af3b153e760749da935a7b1f448 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 20 Feb 2022 12:33:33 +0100 Subject: [PATCH] Simplify the data stored on `PDFObjects`-instances The manually tracked `resolved`-property is no longer necessary, since the same information is now directly available on all `PromiseCapability`-instances. Furthermore, since the `PDFObjects.resolve` method is not documented as accepting e.g. only Object-data, we probably shouldn't resolve the `PromiseCapability` with the `data` and instead only store it on the `PDFObjects`-instance.[1] --- [1] While Objects are passed by reference in JavaScript, other primitives such as e.g. strings are passed by value and the current implementation *could* thus lead to increased memory usage. Given how we're using `PDFObjects` in the PDF.js code-base none of this should be an issue, but it still cannot hurt to change this. --- src/display/api.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 26a3d3389..e99ff17d5 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -3062,7 +3062,6 @@ class PDFObjects { return (this.#objs[objId] = { capability: createPromiseCapability(), data: null, - resolved: false, }); } @@ -3078,7 +3077,8 @@ class PDFObjects { // If there is a callback, then the get can be async and the object is // not required to be resolved right now. if (callback) { - this.#ensureObj(objId).capability.promise.then(callback); + const obj = this.#ensureObj(objId); + obj.capability.promise.then(() => callback(obj.data)); return null; } // If there isn't a callback, the user expects to get the resolved data @@ -3086,7 +3086,7 @@ class PDFObjects { const obj = this.#objs[objId]; // If there isn't an object yet or the object isn't resolved, then the // data isn't ready yet! - if (!obj?.resolved) { + if (!obj?.capability.settled) { throw new Error(`Requesting object that isn't resolved yet ${objId}.`); } return obj.data; @@ -3094,7 +3094,7 @@ class PDFObjects { has(objId) { const obj = this.#objs[objId]; - return obj?.resolved || false; + return obj?.capability.settled || false; } /** @@ -3102,10 +3102,8 @@ class PDFObjects { */ resolve(objId, data = null) { const obj = this.#ensureObj(objId); - - obj.resolved = true; obj.data = data; - obj.capability.resolve(data); + obj.capability.resolve(); } clear() {