Unify method return values in the ObjectLoader class

Given that all the methods are already asynchronous we can just use `await` more throughout this code, rather than having to explicitly return function-calls and `undefined`.
Note also how none of the `ObjectLoader.prototype.load` call-sites use the return value.
This commit is contained in:
Jonas Jenwald 2025-05-06 15:11:33 +02:00
parent 04400c588f
commit ef1ad675c2

View File

@ -54,17 +54,18 @@ function addChildren(node, nodesToVisit) {
* entire PDF document object graph to be traversed.
*/
class ObjectLoader {
refSet = null;
constructor(dict, keys, xref) {
this.dict = dict;
this.keys = keys;
this.xref = xref;
this.refSet = null;
}
async load() {
// Don't walk the graph if all the data is already loaded.
if (this.xref.stream.isDataLoaded) {
return undefined;
return;
}
const { keys, dict } = this;
@ -78,10 +79,12 @@ class ObjectLoader {
nodesToVisit.push(rawValue);
}
}
return this._walk(nodesToVisit);
await this.#walk(nodesToVisit);
this.refSet = null; // Everything is loaded, clear the cache.
}
async _walk(nodesToVisit) {
async #walk(nodesToVisit) {
const nodesToRevisit = [];
const pendingRequests = [];
// DFS walk of the object graph.
@ -99,11 +102,10 @@ class ObjectLoader {
currentNode = this.xref.fetch(currentNode);
} catch (ex) {
if (!(ex instanceof MissingDataException)) {
warn(`ObjectLoader._walk - requesting all data: "${ex}".`);
this.refSet = null;
warn(`ObjectLoader.#walk - requesting all data: "${ex}".`);
const { manager } = this.xref.stream;
return manager.requestAllChunks();
await this.xref.stream.manager.requestAllChunks();
return;
}
nodesToRevisit.push(currentNode);
pendingRequests.push({ begin: ex.begin, end: ex.end });
@ -139,11 +141,8 @@ class ObjectLoader {
this.refSet.remove(node);
}
}
return this._walk(nodesToRevisit);
await this.#walk(nodesToRevisit);
}
// Everything is loaded.
this.refSet = null;
return undefined;
}
}