Skip to content

Commit

Permalink
Merge pull request #12613 from Snuffleupagus/isPageCached
Browse files Browse the repository at this point in the history
Trigger cleanup, once rendering has finished, in `PDFThumbnailView.draw`
  • Loading branch information
timvandermeij authored Nov 12, 2020
2 parents 85de01e + 4a9994b commit 59b3560
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
40 changes: 38 additions & 2 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ function PDFPageViewBuffer(size) {
data.shift().destroy();
}
};

this.has = function (view) {
return data.includes(view);
};
}

function isSameScale(oldScale, newScale) {
Expand Down Expand Up @@ -1096,9 +1100,15 @@ class BaseViewer {
if (!this.pdfDocument) {
return false;
}
if (pageNumber < 1 || pageNumber > this.pagesCount) {
if (
!(
Number.isInteger(pageNumber) &&
pageNumber > 0 &&
pageNumber <= this.pagesCount
)
) {
console.error(
`${this._name}.isPageVisible: "${pageNumber}" is out of bounds.`
`${this._name}.isPageVisible: "${pageNumber}" is not a valid page.`
);
return false;
}
Expand All @@ -1107,6 +1117,32 @@ class BaseViewer {
});
}

/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {
if (!this.pdfDocument || !this._buffer) {
return false;
}
if (
!(
Number.isInteger(pageNumber) &&
pageNumber > 0 &&
pageNumber <= this.pagesCount
)
) {
console.error(
`${this._name}.isPageCached: "${pageNumber}" is not a valid page.`
);
return false;
}
const pageView = this._pages[pageNumber - 1];
if (!pageView) {
return false;
}
return this._buffer.has(pageView);
}

cleanup() {
for (let i = 0, ii = this._pages.length; i < ii; i++) {
if (
Expand Down
5 changes: 5 additions & 0 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class IPDFLinkService {
* @param {number} pageNumber
*/
isPageVisible(pageNumber) {}

/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ class PDFLinkService {
isPageVisible(pageNumber) {
return this.pdfViewer.isPageVisible(pageNumber);
}

/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {
return this.pdfViewer.isPageCached(pageNumber);
}
}

function isValidExplicitDestination(dest) {
Expand Down Expand Up @@ -609,6 +616,13 @@ class SimpleLinkService {
isPageVisible(pageNumber) {
return true;
}

/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {
return true;
}
}

export { PDFLinkService, SimpleLinkService };
10 changes: 10 additions & 0 deletions web/pdf_thumbnail_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ class PDFThumbnailView {
finishRenderTask(error);
}
);
// Only trigger cleanup, once rendering has finished, when the current
// pageView is *not* cached on the `BaseViewer`-instance.
resultPromise.finally(() => {
const pageCached = this.linkService.isPageCached(this.id);
if (pageCached) {
return;
}
this.pdfPage?.cleanup();
});

return resultPromise;
}

Expand Down

0 comments on commit 59b3560

Please sign in to comment.