From ef1ad675c2bd15f157b9c1ad3d7301657802ad69 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 6 May 2025 15:11:33 +0200 Subject: [PATCH] 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. --- src/core/object_loader.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/core/object_loader.js b/src/core/object_loader.js index 6f75e4561..28763b25a 100644 --- a/src/core/object_loader.js +++ b/src/core/object_loader.js @@ -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; } }