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