Set the first pdfPage immediately in {BaseViewer, PDFThumbnailViewer}.setDocument

*This patch is simple enough that I almost feel like I'm overlooking some trivial reason why this would be a bad idea.*

Note how in `{BaseViewer, PDFThumbnailViewer}.setDocument` we're always getting the *first* `pdfPage` in order to initialize all pages/thumbnails.
However, once that's done the first `pdfPage` is simply ignored and rendering of the first page thus requires calling `PDFDocumentProxy.getPage` yet again. (And in the `BaseViewer` case, it's even done once more after `onePageRenderedCapability` is resolved.)

All in all, I cannot see why we cannot just immediately set the first `pdfPage` and thus avoid an early round-trip to the API in the `_ensurePdfPageLoaded` method before rendering can begin.
This commit is contained in:
Jonas Jenwald 2019-12-01 12:21:47 +01:00
parent 62ec8109b5
commit 6732df6aae
2 changed files with 29 additions and 7 deletions

View File

@ -421,9 +421,9 @@ class BaseViewer {
// Fetch a single page so we can get a viewport that will be the default
// viewport for all pages
firstPagePromise.then((pdfPage) => {
firstPagePromise.then((firstPdfPage) => {
let scale = this.currentScale;
let viewport = pdfPage.getViewport({ scale: scale * CSS_UNITS, });
const viewport = firstPdfPage.getViewport({ scale: scale * CSS_UNITS, });
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
let textLayerFactory = null;
if (this.textLayerMode !== TextLayerMode.DISABLE) {
@ -449,6 +449,14 @@ class BaseViewer {
});
this._pages.push(pageView);
}
// Set the first `pdfPage` immediately, since it's already loaded,
// rather than having to repeat the `PDFDocumentProxy.getPage` call in
// the `this._ensurePdfPageLoaded` method before rendering can start.
const firstPageView = this._pages[0];
if (firstPageView) {
firstPageView.setPdfPage(firstPdfPage);
this.linkService.cachePageRef(1, firstPdfPage.ref);
}
if (this._spreadMode !== SpreadMode.NONE) {
this._updateSpreadMode();
}
@ -469,8 +477,13 @@ class BaseViewer {
pagesCapability.resolve();
return;
}
let getPagesLeft = pagesCount;
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
let getPagesLeft = pagesCount - 1; // The first page was already loaded.
if (getPagesLeft <= 0) {
pagesCapability.resolve();
return;
}
for (let pageNum = 2; pageNum <= pagesCount; ++pageNum) {
pdfDocument.getPage(pageNum).then((pdfPage) => {
let pageView = this._pages[pageNum - 1];
if (!pageView.pdfPage) {

View File

@ -164,9 +164,9 @@ class PDFThumbnailViewer {
return;
}
pdfDocument.getPage(1).then((firstPage) => {
pdfDocument.getPage(1).then((firstPdfPage) => {
let pagesCount = pdfDocument.numPages;
let viewport = firstPage.getViewport({ scale: 1, });
const viewport = firstPdfPage.getViewport({ scale: 1, });
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
let thumbnail = new PDFThumbnailView({
container: this.container,
@ -179,6 +179,13 @@ class PDFThumbnailViewer {
});
this._thumbnails.push(thumbnail);
}
// Set the first `pdfPage` immediately, since it's already loaded,
// rather than having to repeat the `PDFDocumentProxy.getPage` call in
// the `this._ensurePdfPageLoaded` method before rendering can start.
const firstThumbnailView = this._thumbnails[0];
if (firstThumbnailView) {
firstThumbnailView.setPdfPage(firstPdfPage);
}
// Ensure that the current thumbnail is always highlighted on load.
const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
@ -235,7 +242,9 @@ class PDFThumbnailViewer {
return this._pagesRequests.get(thumbView);
}
const promise = this.pdfDocument.getPage(thumbView.id).then((pdfPage) => {
thumbView.setPdfPage(pdfPage);
if (!thumbView.pdfPage) {
thumbView.setPdfPage(pdfPage);
}
this._pagesRequests.delete(thumbView);
return pdfPage;
}).catch((reason) => {