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.
This commit is contained in:
Jonas Jenwald 2022-02-20 12:33:33 +01:00
parent beecde3229
commit f4712bc0ad

View File

@ -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() {