diff --git a/src/lib/ThumbnailsSidebar.js b/src/lib/ThumbnailsSidebar.js index aef1272ed..c0bb3ed23 100644 --- a/src/lib/ThumbnailsSidebar.js +++ b/src/lib/ThumbnailsSidebar.js @@ -39,6 +39,9 @@ class ThumbnailsSidebar { /** @property {Object} - Cache for the thumbnail image elements */ thumbnailImageCache; + /** @property {Array} - The ID values returned by the call to window.requestAnimationFrame() */ + animationFrameRequestIds; + /** * [constructor] * @@ -46,6 +49,7 @@ class ThumbnailsSidebar { * @param {PDFViewer} pdfViewer - the PDFJS viewer */ constructor(element, pdfViewer) { + this.animationFrameRequestIds = []; this.anchorEl = element; this.currentThumbnails = []; this.pdfViewer = pdfViewer; @@ -132,6 +136,10 @@ class ThumbnailsSidebar { * @return {void} */ destroy() { + if (this.animationFrameRequestIds.length > 0) { + this.animationFrameRequestIds.forEach(id => cancelAnimationFrame(id)); + } + if (this.virtualScroller) { this.virtualScroller.destroy(); this.virtualScroller = null; @@ -289,19 +297,21 @@ class ThumbnailsSidebar { * @return {void} */ requestThumbnailImage(itemIndex, thumbnailEl) { - requestAnimationFrame(() => { - this.createThumbnailImage(itemIndex).then(imageEl => { - // Promise will resolve with null if create image request was already in progress - if (imageEl) { - // Appends to the thumbnail nav element - thumbnailEl.lastChild.appendChild(imageEl); - thumbnailEl.classList.add(CLASS_BOX_PREVIEW_THUMBNAIL_IMAGE_LOADED); - } - - // After generating the thumbnail image, render the next one - this.renderNextThumbnailImage(); - }); - }); + this.animationFrameRequestIds.push( + requestAnimationFrame(() => { + this.createThumbnailImage(itemIndex).then(imageEl => { + // Promise will resolve with null if create image request was already in progress + if (imageEl) { + // Appends to the thumbnail nav element + thumbnailEl.lastChild.appendChild(imageEl); + thumbnailEl.classList.add(CLASS_BOX_PREVIEW_THUMBNAIL_IMAGE_LOADED); + } + + // After generating the thumbnail image, render the next one + this.renderNextThumbnailImage(); + }); + }), + ); } /** @@ -311,10 +321,6 @@ class ThumbnailsSidebar { * @return {Promise} - promise reolves with the image HTMLElement or null if generation is in progress */ createThumbnailImage(itemIndex) { - if (!this.thumbnailImageCache) { - return Promise.resolve(null); - } - const cacheEntry = this.thumbnailImageCache.get(itemIndex); // If this thumbnail has already been cached, use it