Convert Catalog.getPageDict to an async method

This makes it possible to remove the internal `next` helper function, and also gets rid of the need to manually resolve/reject a `PromiseCapability`.
This commit is contained in:
Jonas Jenwald 2019-11-04 16:02:08 +01:00
parent 668650d564
commit 0d89006bf1

View File

@ -14,10 +14,9 @@
*/ */
import { import {
assert, bytesToString, createPromiseCapability, createValidAbsoluteUrl, assert, bytesToString, createValidAbsoluteUrl, FormatError, info,
FormatError, info, InvalidPDFException, isBool, isNum, isString, InvalidPDFException, isBool, isNum, isString, PermissionFlag, shadow,
PermissionFlag, shadow, stringToPDFString, stringToUTF8String, unreachable, stringToPDFString, stringToUTF8String, unreachable, warn
warn
} from '../shared/util'; } from '../shared/util';
import { import {
clearPrimitiveCaches, Cmd, Dict, isCmd, isDict, isName, isRef, isRefsEqual, clearPrimitiveCaches, Cmd, Dict, isCmd, isDict, isName, isRef, isRefsEqual,
@ -680,13 +679,11 @@ class Catalog {
}); });
} }
getPageDict(pageIndex) { async getPageDict(pageIndex) {
const capability = createPromiseCapability();
const nodesToVisit = [this.catDict.getRaw('Pages')]; const nodesToVisit = [this.catDict.getRaw('Pages')];
const xref = this.xref, pageKidsCountCache = this.pageKidsCountCache; const xref = this.xref, pageKidsCountCache = this.pageKidsCountCache;
let count, currentPageIndex = 0; let count, currentPageIndex = 0;
function next() {
while (nodesToVisit.length) { while (nodesToVisit.length) {
const currentNode = nodesToVisit.pop(); const currentNode = nodesToVisit.pop();
@ -697,8 +694,8 @@ class Catalog {
currentPageIndex += count; currentPageIndex += count;
continue; continue;
} }
const obj = await xref.fetchAsync(currentNode);
xref.fetchAsync(currentNode).then(function(obj) {
if (isDict(obj, 'Page') || (isDict(obj) && !obj.has('Kids'))) { if (isDict(obj, 'Page') || (isDict(obj) && !obj.has('Kids'))) {
if (pageIndex === currentPageIndex) { if (pageIndex === currentPageIndex) {
// Cache the Page reference, since it can *greatly* improve // Cache the Page reference, since it can *greatly* improve
@ -707,24 +704,19 @@ class Catalog {
if (currentNode && !pageKidsCountCache.has(currentNode)) { if (currentNode && !pageKidsCountCache.has(currentNode)) {
pageKidsCountCache.put(currentNode, 1); pageKidsCountCache.put(currentNode, 1);
} }
capability.resolve([obj, currentNode]); return [obj, currentNode];
} else {
currentPageIndex++;
next();
} }
return; currentPageIndex++;
continue;
} }
nodesToVisit.push(obj); nodesToVisit.push(obj);
next(); continue;
}, capability.reject);
return;
} }
// Must be a child page dictionary. // Must be a child page dictionary.
if (!isDict(currentNode)) { if (!isDict(currentNode)) {
capability.reject(new FormatError( throw new FormatError(
'Page dictionary kid reference points to wrong type of object.')); 'Page dictionary kid reference points to wrong type of object.');
return;
} }
count = currentNode.get('Count'); count = currentNode.get('Count');
@ -750,16 +742,12 @@ class Catalog {
if (isName(currentNode.get('Type'), 'Page') || if (isName(currentNode.get('Type'), 'Page') ||
(!currentNode.has('Type') && currentNode.has('Contents'))) { (!currentNode.has('Type') && currentNode.has('Contents'))) {
if (currentPageIndex === pageIndex) { if (currentPageIndex === pageIndex) {
capability.resolve([currentNode, null]); return [currentNode, null];
return;
} }
currentPageIndex++; currentPageIndex++;
continue; continue;
} }
throw new FormatError('Page dictionary kids object is not an array.');
capability.reject(new FormatError(
'Page dictionary kids object is not an array.'));
return;
} }
// Always check all `Kids` nodes, to avoid getting stuck in an empty // Always check all `Kids` nodes, to avoid getting stuck in an empty
@ -769,10 +757,7 @@ class Catalog {
nodesToVisit.push(kids[last]); nodesToVisit.push(kids[last]);
} }
} }
capability.reject(new Error(`Page index ${pageIndex} not found.`)); throw new Error(`Page index ${pageIndex} not found.`);
}
next();
return capability.promise;
} }
getPageIndex(pageRef) { getPageIndex(pageRef) {