Refactor ObjectLoader to use Dicts correctly, rather than abusing their internal properties

The `ObjectLoader` currently takes an Object as input, despite actually working with `Dict`s internally. This means that at the (two) existing call-sites, we're passing in the "private" `Dict.map` property directly.

Doing this seems like an anti-pattern, and we could (and even should) simply provide the actual `Dict` when creating an `ObjectLoader` instance.
Accessing properties stored in the `Dict` is now done using the intended methods instead, in particular `getRaw` which (as the name suggests) doesn't do any de-referencing, thus maintaining the current functionality of the code.

The only functional change in this patch is that `ObjectLoader.load` will now ignore empty nodes, such that `ObjectLoader._walk` only needs to deal with nodes that are known to contain data. (This lets us skip, among other checks, meaningless `addChildren` function calls.)
This commit is contained in:
Jonas Jenwald 2017-06-13 10:22:11 +02:00
parent f2fc9ee281
commit 3a20fd165f
3 changed files with 20 additions and 23 deletions

View File

@ -390,9 +390,8 @@ var Annotation = (function AnnotationClosure() {
if (!resources) { if (!resources) {
return; return;
} }
var objectLoader = new ObjectLoader(resources.map, let objectLoader = new ObjectLoader(resources, keys, resources.xref);
keys,
resources.xref);
return objectLoader.load().then(function() { return objectLoader.load().then(function() {
return resources; return resources;
}); });

View File

@ -186,9 +186,8 @@ var Page = (function PageClosure() {
this.resourcesPromise = this.pdfManager.ensure(this, 'resources'); this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
} }
return this.resourcesPromise.then(() => { return this.resourcesPromise.then(() => {
var objectLoader = new ObjectLoader(this.resources.map, let objectLoader = new ObjectLoader(this.resources, keys, this.xref);
keys,
this.xref);
return objectLoader.load(); return objectLoader.load();
}); });
}, },

View File

@ -1610,7 +1610,7 @@ var FileSpec = (function FileSpecClosure() {
})(); })();
/** /**
* A helper for loading missing data in object graphs. It traverses the graph * A helper for loading missing data in `Dict` graphs. It traverses the graph
* depth first and queues up any objects that have missing data. Once it has * depth first and queues up any objects that have missing data. Once it has
* has traversed as many objects that are available it attempts to bundle the * has traversed as many objects that are available it attempts to bundle the
* missing data requests and then resume from the nodes that weren't ready. * missing data requests and then resume from the nodes that weren't ready.
@ -1626,23 +1626,18 @@ let ObjectLoader = (function() {
} }
function addChildren(node, nodesToVisit) { function addChildren(node, nodesToVisit) {
let value;
if (isDict(node) || isStream(node)) { if (isDict(node) || isStream(node)) {
let map; let dict = isDict(node) ? node : node.dict;
if (isDict(node)) { let dictKeys = dict.getKeys();
map = node.map; for (let i = 0, ii = dictKeys.length; i < ii; i++) {
} else { let rawValue = dict.getRaw(dictKeys[i]);
map = node.dict.map; if (mayHaveChildren(rawValue)) {
} nodesToVisit.push(rawValue);
for (let key in map) {
value = map[key];
if (mayHaveChildren(value)) {
nodesToVisit.push(value);
} }
} }
} else if (isArray(node)) { } else if (isArray(node)) {
for (let i = 0, ii = node.length; i < ii; i++) { for (let i = 0, ii = node.length; i < ii; i++) {
value = node[i]; let value = node[i];
if (mayHaveChildren(value)) { if (mayHaveChildren(value)) {
nodesToVisit.push(value); nodesToVisit.push(value);
} }
@ -1650,8 +1645,8 @@ let ObjectLoader = (function() {
} }
} }
function ObjectLoader(obj, keys, xref) { function ObjectLoader(dict, keys, xref) {
this.obj = obj; this.dict = dict;
this.keys = keys; this.keys = keys;
this.xref = xref; this.xref = xref;
this.refSet = null; this.refSet = null;
@ -1668,12 +1663,16 @@ let ObjectLoader = (function() {
return this.capability.promise; return this.capability.promise;
} }
let keys = this.keys; let { keys, dict, } = this;
this.refSet = new RefSet(); this.refSet = new RefSet();
// Setup the initial nodes to visit. // Setup the initial nodes to visit.
let nodesToVisit = []; let nodesToVisit = [];
for (let i = 0, ii = keys.length; i < ii; i++) { for (let i = 0, ii = keys.length; i < ii; i++) {
nodesToVisit.push(this.obj[keys[i]]); let rawValue = dict.getRaw(keys[i]);
// Skip nodes that are guaranteed to be empty.
if (rawValue !== undefined) {
nodesToVisit.push(rawValue);
}
} }
this._walk(nodesToVisit); this._walk(nodesToVisit);