Introduce a "thumbnailrendered" event to simplify cleanup after thumbnail rendering (PR 12613 follow-up)

The way that the cleanup was implemented in PR 12613 has always bothered me slightly, since the `isPageCached`-method that I introduced there always felt quite out-of-place in the `IPDFLinkService`-implementations.
By introducing a new "thumbnailrendered" event, similar to the existing "pagerendered" one, we're able to move the cleanup handling into the `PDFViewer`-class instead.
This commit is contained in:
Jonas Jenwald 2023-05-25 14:29:30 +02:00
parent 6d8810b55c
commit bc8523ac29
6 changed files with 31 additions and 47 deletions

View file

@ -322,6 +322,15 @@ class PDFViewer {
}
this.#updateContainerHeightCss();
// Trigger API-cleanup, once thumbnail rendering has finished,
// if the relevant pageView is *not* cached in the buffer.
this.eventBus._on("thumbnailrendered", ({ pageNumber, pdfPage }) => {
const pageView = this._pages[pageNumber - 1];
if (!this.#buffer.has(pageView)) {
pdfPage?.cleanup();
}
});
}
get pagesCount() {
@ -1645,27 +1654,6 @@ class PDFViewer {
});
}
/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {
if (!this.pdfDocument) {
return false;
}
if (
!(
Number.isInteger(pageNumber) &&
pageNumber > 0 &&
pageNumber <= this.pagesCount
)
) {
console.error(`isPageCached: "${pageNumber}" is not a valid page.`);
return false;
}
const pageView = this._pages[pageNumber - 1];
return this.#buffer.has(pageView);
}
cleanup() {
for (const pageView of this._pages) {
if (pageView.renderingState !== RenderingStates.FINISHED) {