From 04497bcb3c8df5a04565ef60a37245d73a17b4e1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 3 Nov 2019 14:58:40 +0100 Subject: [PATCH] Re-factor the `ObjectLoader._walk` method to be properly asynchronous Rather than having to store a `PromiseCapability` on the `ObjectLoader` instances, we can simply convert `_walk` to be `async` and thus have the same functionality with native JavaScript instead. --- src/core/obj.js | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/core/obj.js b/src/core/obj.js index dd844b6da..283ea9aba 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -2067,7 +2067,6 @@ let ObjectLoader = (function() { this.keys = keys; this.xref = xref; this.refSet = null; - this.capability = null; } ObjectLoader.prototype = { @@ -2078,7 +2077,6 @@ let ObjectLoader = (function() { this.xref.stream.allChunksLoaded()) { return undefined; } - this.capability = createPromiseCapability(); let { keys, dict, } = this; this.refSet = new RefSet(); @@ -2091,12 +2089,10 @@ let ObjectLoader = (function() { nodesToVisit.push(rawValue); } } - - this._walk(nodesToVisit); - return this.capability.promise; + return this._walk(nodesToVisit); }, - _walk(nodesToVisit) { + async _walk(nodesToVisit) { let nodesToRevisit = []; let pendingRequests = []; // DFS walk of the object graph. @@ -2139,22 +2135,21 @@ let ObjectLoader = (function() { } if (pendingRequests.length) { - this.xref.stream.manager.requestRanges(pendingRequests).then(() => { - for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) { - let node = nodesToRevisit[i]; - // Remove any reference nodes from the current `RefSet` so they - // aren't skipped when we revist them. - if (node instanceof Ref) { - this.refSet.remove(node); - } + await this.xref.stream.manager.requestRanges(pendingRequests); + + for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) { + let node = nodesToRevisit[i]; + // Remove any reference nodes from the current `RefSet` so they + // aren't skipped when we revist them. + if (node instanceof Ref) { + this.refSet.remove(node); } - this._walk(nodesToRevisit); - }, this.capability.reject); - return; + } + return this._walk(nodesToRevisit); } // Everything is loaded. this.refSet = null; - this.capability.resolve(); + return undefined; }, };