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.
This commit is contained in:
Jonas Jenwald 2019-11-03 14:58:40 +01:00
parent e7f24bd5b1
commit 04497bcb3c

View File

@ -2067,7 +2067,6 @@ let ObjectLoader = (function() {
this.keys = keys; this.keys = keys;
this.xref = xref; this.xref = xref;
this.refSet = null; this.refSet = null;
this.capability = null;
} }
ObjectLoader.prototype = { ObjectLoader.prototype = {
@ -2078,7 +2077,6 @@ let ObjectLoader = (function() {
this.xref.stream.allChunksLoaded()) { this.xref.stream.allChunksLoaded()) {
return undefined; return undefined;
} }
this.capability = createPromiseCapability();
let { keys, dict, } = this; let { keys, dict, } = this;
this.refSet = new RefSet(); this.refSet = new RefSet();
@ -2091,12 +2089,10 @@ let ObjectLoader = (function() {
nodesToVisit.push(rawValue); nodesToVisit.push(rawValue);
} }
} }
return this._walk(nodesToVisit);
this._walk(nodesToVisit);
return this.capability.promise;
}, },
_walk(nodesToVisit) { async _walk(nodesToVisit) {
let nodesToRevisit = []; let nodesToRevisit = [];
let pendingRequests = []; let pendingRequests = [];
// DFS walk of the object graph. // DFS walk of the object graph.
@ -2139,22 +2135,21 @@ let ObjectLoader = (function() {
} }
if (pendingRequests.length) { if (pendingRequests.length) {
this.xref.stream.manager.requestRanges(pendingRequests).then(() => { await this.xref.stream.manager.requestRanges(pendingRequests);
for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) {
let node = nodesToRevisit[i]; for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) {
// Remove any reference nodes from the current `RefSet` so they let node = nodesToRevisit[i];
// aren't skipped when we revist them. // Remove any reference nodes from the current `RefSet` so they
if (node instanceof Ref) { // aren't skipped when we revist them.
this.refSet.remove(node); if (node instanceof Ref) {
} this.refSet.remove(node);
} }
this._walk(nodesToRevisit); }
}, this.capability.reject); return this._walk(nodesToRevisit);
return;
} }
// Everything is loaded. // Everything is loaded.
this.refSet = null; this.refSet = null;
this.capability.resolve(); return undefined;
}, },
}; };