Slightly refactor and ES6-ify the code in ObjectLoader
This patch changes all `var` to `let`, and caches the array lengths in all loops. Also removes two unnecessary temporary variable assignments.
This commit is contained in:
parent
0c93dee0de
commit
f2fc9ee281
@ -1616,32 +1616,32 @@ var FileSpec = (function FileSpecClosure() {
|
|||||||
* 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.
|
||||||
*
|
*
|
||||||
* NOTE: It provides protection from circular references by keeping track of
|
* NOTE: It provides protection from circular references by keeping track of
|
||||||
* of loaded references. However, you must be careful not to load any graphs
|
* loaded references. However, you must be careful not to load any graphs
|
||||||
* that have references to the catalog or other pages since that will cause the
|
* that have references to the catalog or other pages since that will cause the
|
||||||
* entire PDF document object graph to be traversed.
|
* entire PDF document object graph to be traversed.
|
||||||
*/
|
*/
|
||||||
var ObjectLoader = (function() {
|
let ObjectLoader = (function() {
|
||||||
function mayHaveChildren(value) {
|
function mayHaveChildren(value) {
|
||||||
return isRef(value) || isDict(value) || isArray(value) || isStream(value);
|
return isRef(value) || isDict(value) || isArray(value) || isStream(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addChildren(node, nodesToVisit) {
|
function addChildren(node, nodesToVisit) {
|
||||||
var value;
|
let value;
|
||||||
if (isDict(node) || isStream(node)) {
|
if (isDict(node) || isStream(node)) {
|
||||||
var map;
|
let map;
|
||||||
if (isDict(node)) {
|
if (isDict(node)) {
|
||||||
map = node.map;
|
map = node.map;
|
||||||
} else {
|
} else {
|
||||||
map = node.dict.map;
|
map = node.dict.map;
|
||||||
}
|
}
|
||||||
for (var key in map) {
|
for (let key in map) {
|
||||||
value = map[key];
|
value = map[key];
|
||||||
if (mayHaveChildren(value)) {
|
if (mayHaveChildren(value)) {
|
||||||
nodesToVisit.push(value);
|
nodesToVisit.push(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isArray(node)) {
|
} else if (isArray(node)) {
|
||||||
for (var i = 0, ii = node.length; i < ii; i++) {
|
for (let i = 0, ii = node.length; i < ii; i++) {
|
||||||
value = node[i];
|
value = node[i];
|
||||||
if (mayHaveChildren(value)) {
|
if (mayHaveChildren(value)) {
|
||||||
nodesToVisit.push(value);
|
nodesToVisit.push(value);
|
||||||
@ -1659,8 +1659,7 @@ var ObjectLoader = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ObjectLoader.prototype = {
|
ObjectLoader.prototype = {
|
||||||
load: function ObjectLoader_load() {
|
load() {
|
||||||
var keys = this.keys;
|
|
||||||
this.capability = createPromiseCapability();
|
this.capability = createPromiseCapability();
|
||||||
// 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 instanceof ChunkedStream) ||
|
if (!(this.xref.stream instanceof ChunkedStream) ||
|
||||||
@ -1669,10 +1668,11 @@ var ObjectLoader = (function() {
|
|||||||
return this.capability.promise;
|
return this.capability.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let keys = this.keys;
|
||||||
this.refSet = new RefSet();
|
this.refSet = new RefSet();
|
||||||
// Setup the initial nodes to visit.
|
// Setup the initial nodes to visit.
|
||||||
var nodesToVisit = [];
|
let nodesToVisit = [];
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (let i = 0, ii = keys.length; i < ii; i++) {
|
||||||
nodesToVisit.push(this.obj[keys[i]]);
|
nodesToVisit.push(this.obj[keys[i]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1680,12 +1680,12 @@ var ObjectLoader = (function() {
|
|||||||
return this.capability.promise;
|
return this.capability.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
_walk: function ObjectLoader_walk(nodesToVisit) {
|
_walk(nodesToVisit) {
|
||||||
var nodesToRevisit = [];
|
let nodesToRevisit = [];
|
||||||
var pendingRequests = [];
|
let pendingRequests = [];
|
||||||
// DFS walk of the object graph.
|
// DFS walk of the object graph.
|
||||||
while (nodesToVisit.length) {
|
while (nodesToVisit.length) {
|
||||||
var currentNode = nodesToVisit.pop();
|
let currentNode = nodesToVisit.pop();
|
||||||
|
|
||||||
// Only references or chunked streams can cause missing data exceptions.
|
// Only references or chunked streams can cause missing data exceptions.
|
||||||
if (isRef(currentNode)) {
|
if (isRef(currentNode)) {
|
||||||
@ -1694,28 +1694,24 @@ var ObjectLoader = (function() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var ref = currentNode;
|
this.refSet.put(currentNode);
|
||||||
this.refSet.put(ref);
|
|
||||||
currentNode = this.xref.fetch(currentNode);
|
currentNode = this.xref.fetch(currentNode);
|
||||||
} catch (e) {
|
} catch (ex) {
|
||||||
if (!(e instanceof MissingDataException)) {
|
if (!(ex instanceof MissingDataException)) {
|
||||||
throw e;
|
throw ex;
|
||||||
}
|
}
|
||||||
nodesToRevisit.push(currentNode);
|
nodesToRevisit.push(currentNode);
|
||||||
pendingRequests.push({ begin: e.begin, end: e.end, });
|
pendingRequests.push({ begin: ex.begin, end: ex.end, });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (currentNode && currentNode.getBaseStreams) {
|
if (currentNode && currentNode.getBaseStreams) {
|
||||||
var baseStreams = currentNode.getBaseStreams();
|
let baseStreams = currentNode.getBaseStreams();
|
||||||
var foundMissingData = false;
|
let foundMissingData = false;
|
||||||
for (var i = 0; i < baseStreams.length; i++) {
|
for (let i = 0, ii = baseStreams.length; i < ii; i++) {
|
||||||
var stream = baseStreams[i];
|
let stream = baseStreams[i];
|
||||||
if (stream.getMissingChunks && stream.getMissingChunks().length) {
|
if (stream.getMissingChunks && stream.getMissingChunks().length) {
|
||||||
foundMissingData = true;
|
foundMissingData = true;
|
||||||
pendingRequests.push({
|
pendingRequests.push({ begin: stream.start, end: stream.end, });
|
||||||
begin: stream.start,
|
|
||||||
end: stream.end,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (foundMissingData) {
|
if (foundMissingData) {
|
||||||
@ -1728,16 +1724,15 @@ var ObjectLoader = (function() {
|
|||||||
|
|
||||||
if (pendingRequests.length) {
|
if (pendingRequests.length) {
|
||||||
this.xref.stream.manager.requestRanges(pendingRequests).then(() => {
|
this.xref.stream.manager.requestRanges(pendingRequests).then(() => {
|
||||||
nodesToVisit = nodesToRevisit;
|
for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) {
|
||||||
for (var i = 0; i < nodesToRevisit.length; i++) {
|
let node = nodesToRevisit[i];
|
||||||
var node = nodesToRevisit[i];
|
// Remove any reference nodes from the current `RefSet` so they
|
||||||
// Remove any reference nodes from the currrent refset so they
|
|
||||||
// aren't skipped when we revist them.
|
// aren't skipped when we revist them.
|
||||||
if (isRef(node)) {
|
if (isRef(node)) {
|
||||||
this.refSet.remove(node);
|
this.refSet.remove(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._walk(nodesToVisit);
|
this._walk(nodesToRevisit);
|
||||||
}, this.capability.reject);
|
}, this.capability.reject);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user