Skip to content

Commit

Permalink
Inline the helper method in PDFLinkService.goToDestination
Browse files Browse the repository at this point in the history
We no longer need the helper method to *potentially* call itself once data is available, and can instead take full advantage of async/await by inlining the code.
  • Loading branch information
Snuffleupagus committed Apr 29, 2024
1 parent 3052e99 commit fa69d9a
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,44 +131,59 @@ class PDFLinkService {
return this.pdfDocument ? this.pdfViewer.isInPresentationMode : false;
}

#goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
/**
* This method will, when available, also update the browser history.
*
* @param {string|Array} dest - The named, or explicit, PDF destination.
*/
async goToDestination(dest) {
if (!this.pdfDocument) {
return;
}
let namedDest, explicitDest, pageNumber;
if (typeof dest === "string") {
namedDest = dest;
explicitDest = await this.pdfDocument.getDestination(dest);
} else {
namedDest = null;
explicitDest = await dest;
}
if (!Array.isArray(explicitDest)) {
console.error(
`goToDestination: "${explicitDest}" is not a valid destination array, for dest="${dest}".`
);
return;
}
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
const destRef = explicitDest[0];
let pageNumber;
const [destRef] = explicitDest;

if (typeof destRef === "object" && destRef !== null) {
pageNumber = this._cachedPageNumber(destRef);

if (!pageNumber) {
// Fetch the page reference if it's not yet available. This could
// only occur during loading, before all pages have been resolved.
this.pdfDocument
.getPageIndex(destRef)
.then(pageIndex => {
this.cachePageRef(pageIndex + 1, destRef);
this.#goToDestinationHelper(rawDest, namedDest, explicitDest);
})
.catch(() => {
console.error(
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
`a valid page reference, for dest="${rawDest}".`
);
});
return;
try {
pageNumber = (await this.pdfDocument.getPageIndex(destRef)) + 1;
this.cachePageRef(pageNumber, destRef);
} catch {
console.error(
`goToDestination: "${destRef}" is not a valid page reference, for dest="${dest}".`
);
return;
}
}
} else if (Number.isInteger(destRef)) {
pageNumber = destRef + 1;
} else {
console.error(
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
`a valid destination reference, for dest="${rawDest}".`
`goToDestination: "${destRef}" is not a valid destination reference, for dest="${dest}".`
);
return;
}
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
console.error(
`PDFLinkService.#goToDestinationHelper: "${pageNumber}" is not ` +
`a valid page number, for dest="${rawDest}".`
`goToDestination: "${pageNumber}" is not a valid page number, for dest="${dest}".`
);
return;
}
Expand All @@ -187,33 +202,6 @@ class PDFLinkService {
});
}

/**
* This method will, when available, also update the browser history.
*
* @param {string|Array} dest - The named, or explicit, PDF destination.
*/
async goToDestination(dest) {
if (!this.pdfDocument) {
return;
}
let namedDest, explicitDest;
if (typeof dest === "string") {
namedDest = dest;
explicitDest = await this.pdfDocument.getDestination(dest);
} else {
namedDest = null;
explicitDest = await dest;
}
if (!Array.isArray(explicitDest)) {
console.error(
`PDFLinkService.goToDestination: "${explicitDest}" is not ` +
`a valid destination array, for dest="${dest}".`
);
return;
}
this.#goToDestinationHelper(dest, namedDest, explicitDest);
}

/**
* This method will, when available, also update the browser history.
*
Expand Down

0 comments on commit fa69d9a

Please sign in to comment.