Skip to content

Commit

Permalink
Introduce a "thumbnailrendered" event to simplify cleanup after thumb…
Browse files Browse the repository at this point in the history
…nail 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.
  • Loading branch information
Snuffleupagus committed May 26, 2023
1 parent 6d8810b commit bc8523a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
1 change: 1 addition & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ const PDFViewerApplication = {
if (appConfig.sidebar?.thumbnailView) {
this.pdfThumbnailViewer = new PDFThumbnailViewer({
container: appConfig.sidebar.thumbnailView,
eventBus,
renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService,
l10n: this.l10n,
Expand Down
5 changes: 0 additions & 5 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ class IPDFLinkService {
* @param {Object} pageRef - reference to the page.
*/
cachePageRef(pageNum, pageRef) {}

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

/**
Expand Down
14 changes: 0 additions & 14 deletions web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,6 @@ class PDFLinkService {
return this.#pagesRefCache.get(refStr) || null;
}

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

static #isValidExplicitDestination(dest) {
if (!Array.isArray(dest)) {
return false;
Expand Down Expand Up @@ -737,13 +730,6 @@ class SimpleLinkService {
* @param {Object} pageRef - reference to the page.
*/
cachePageRef(pageNum, pageRef) {}

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

export { LinkTarget, PDFLinkService, SimpleLinkService };
15 changes: 9 additions & 6 deletions web/pdf_thumbnail_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

/** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IL10n} IL10n */
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
/** @typedef {import("./interfaces").IRenderableView} IRenderableView */
Expand All @@ -29,6 +30,7 @@ const THUMBNAIL_WIDTH = 98; // px
/**
* @typedef {Object} PDFThumbnailViewOptions
* @property {HTMLDivElement} container - The viewer element.
* @property {EventBus} eventBus - The application event bus.
* @property {number} id - The thumbnail's unique ID (normally its number).
* @property {PageViewport} defaultViewport - The page viewport.
* @property {Promise<OptionalContentConfig>} [optionalContentConfigPromise] -
Expand Down Expand Up @@ -81,6 +83,7 @@ class PDFThumbnailView {
*/
constructor({
container,
eventBus,
id,
defaultViewport,
optionalContentConfigPromise,
Expand All @@ -100,6 +103,7 @@ class PDFThumbnailView {
this._optionalContentConfigPromise = optionalContentConfigPromise || null;
this.pageColors = pageColors || null;

this.eventBus = eventBus;
this.linkService = linkService;
this.renderingQueue = renderingQueue;

Expand Down Expand Up @@ -314,12 +318,11 @@ class PDFThumbnailView {
canvas.width = 0;
canvas.height = 0;

// Only trigger cleanup, once rendering has finished, when the current
// pageView is *not* cached on the `BaseViewer`-instance.
const pageCached = this.linkService.isPageCached(this.id);
if (!pageCached) {
this.pdfPage?.cleanup();
}
this.eventBus.dispatch("thumbnailrendered", {
source: this,
pageNumber: this.id,
pdfPage: this.pdfPage,
});
});

return resultPromise;
Expand Down
13 changes: 12 additions & 1 deletion web/pdf_thumbnail_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

/** @typedef {import("../src/display/api").PDFDocumentProxy} PDFDocumentProxy */
/** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IL10n} IL10n */
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
// eslint-disable-next-line max-len
Expand All @@ -35,6 +36,7 @@ const THUMBNAIL_SELECTED_CLASS = "selected";
* @typedef {Object} PDFThumbnailViewerOptions
* @property {HTMLDivElement} container - The container for the thumbnail
* elements.
* @property {EventBus} eventBus - The application event bus.
* @property {IPDFLinkService} linkService - The navigation/linking service.
* @property {PDFRenderingQueue} renderingQueue - The rendering queue object.
* @property {IL10n} l10n - Localization service.
Expand All @@ -50,8 +52,16 @@ class PDFThumbnailViewer {
/**
* @param {PDFThumbnailViewerOptions} options
*/
constructor({ container, linkService, renderingQueue, l10n, pageColors }) {
constructor({
container,
eventBus,
linkService,
renderingQueue,
l10n,
pageColors,
}) {
this.container = container;
this.eventBus = eventBus;
this.linkService = linkService;
this.renderingQueue = renderingQueue;
this.l10n = l10n;
Expand Down Expand Up @@ -209,6 +219,7 @@ class PDFThumbnailViewer {
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
const thumbnail = new PDFThumbnailView({
container: this.container,
eventBus: this.eventBus,
id: pageNum,
defaultViewport: viewport.clone(),
optionalContentConfigPromise,
Expand Down
30 changes: 9 additions & 21 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bc8523a

Please sign in to comment.