Skip to content

Slightly reduce asynchronicity in the Catalog.getPageDict method (PR 14338 follow-up) #14370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ class Catalog {
this.xref = xref;

this._catDict = xref.getCatalogObj();
if (!isDict(this._catDict)) {
if (!(this._catDict instanceof Dict)) {
throw new FormatError("Catalog object is not a dictionary.");
}
// Given that `XRef.parse` will both fetch *and* validate the /Pages-entry,
// the following call must always succeed here:
this.toplevelPagesDict; // eslint-disable-line no-unused-expressions

this._actualNumPages = null;

this.fontCache = new RefSetCache();
Expand Down Expand Up @@ -1089,8 +1093,13 @@ class Catalog {

getPageDict(pageIndex) {
const capability = createPromiseCapability();
const nodesToVisit = [this._catDict.getRaw("Pages")];
const nodesToVisit = [this.toplevelPagesDict];
const visitedNodes = new RefSet();

const pagesRef = this._catDict.getRaw("Pages");
if (pagesRef instanceof Ref) {
visitedNodes.put(pagesRef);
}
const xref = this.xref,
pageKidsCountCache = this.pageKidsCountCache;
let currentPageIndex = 0;
Expand All @@ -1099,7 +1108,7 @@ class Catalog {
while (nodesToVisit.length) {
const currentNode = nodesToVisit.pop();

if (isRef(currentNode)) {
if (currentNode instanceof Ref) {
const count = pageKidsCountCache.get(currentNode);
// Skip nodes where the page can't be.
if (count >= 0 && currentPageIndex + count <= pageIndex) {
Expand Down Expand Up @@ -1139,7 +1148,7 @@ class Catalog {
}

// Must be a child page dictionary.
if (!isDict(currentNode)) {
if (!(currentNode instanceof Dict)) {
capability.reject(
new FormatError(
"Page dictionary kid reference points to wrong type of object."
Expand Down Expand Up @@ -1228,6 +1237,11 @@ class Catalog {
getAllPageDicts(recoveryMode = false) {
const queue = [{ currentNode: this.toplevelPagesDict, posInKids: 0 }];
const visitedNodes = new RefSet();

const pagesRef = this._catDict.getRaw("Pages");
if (pagesRef instanceof Ref) {
visitedNodes.put(pagesRef);
}
const map = new Map();
let pageIndex = 0;

Expand Down