From fac5f1f4e36855ff3f2938b87c0651f62e8f51bf Mon Sep 17 00:00:00 2001 From: Eric Marsh <108751850+ericmarsh995@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:27:21 -0700 Subject: [PATCH 1/4] add priority queue for objectURL rendering --- ui/library/src/context/PageRenderContext.ts | 23 ++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/ui/library/src/context/PageRenderContext.ts b/ui/library/src/context/PageRenderContext.ts index 5cf1a81..29270ab 100644 --- a/ui/library/src/context/PageRenderContext.ts +++ b/ui/library/src/context/PageRenderContext.ts @@ -139,13 +139,26 @@ export function usePageRenderContextProps({ ); React.useEffect(() => { - for (const pageNumber of visiblePageRatios.keys()) { - if (pageRenderStates.has(pageNumber)) { - continue; - } + const visiblePages = [...visiblePageRatios.keys()]; + if ( + !pdfDocProxy || + [...pageRenderStates.keys()].length === pdfDocProxy.numPages || + visiblePages.length === 0 + ) { + return; + } + + const visiblePagesNeighbors = [ + Math.max(1, visiblePages[0] - 1), + Math.min(pdfDocProxy.numPages, visiblePages[visiblePages.length - 1] + 1), + ]; + const allPages = Array.from({ length: pdfDocProxy.numPages }, (_, i) => i + 1); + const priorityQueue = new Set([...visiblePages, ...visiblePagesNeighbors, ...allPages]); + + for (const pageNumber of priorityQueue) { buildObjectURLForPage({ pageNumber }); } - }, [pageRenderStates, visiblePageRatios]); + }, [pageRenderStates, pdfDocProxy, visiblePageRatios]); // Flush page render states when scale changes React.useEffect(() => { From 21d3e9a25cfc4c76499f31bd707a0b2ab1250234 Mon Sep 17 00:00:00 2001 From: Eric Marsh <108751850+ericmarsh995@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:28:08 -0700 Subject: [PATCH 2/4] remove thumbnail code that was rendering objectURLs --- ui/library/src/components/thumbnails/Thumbnail.tsx | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ui/library/src/components/thumbnails/Thumbnail.tsx b/ui/library/src/components/thumbnails/Thumbnail.tsx index c750ad3..147479a 100644 --- a/ui/library/src/components/thumbnails/Thumbnail.tsx +++ b/ui/library/src/components/thumbnails/Thumbnail.tsx @@ -11,8 +11,7 @@ type Props = { }; export const Thumbnail: React.FunctionComponent = ({ pageNumber }: Props) => { - const { pageRenderStates, buildObjectURLForPage, getObjectURLForPage } = - React.useContext(PageRenderContext); + const { getObjectURLForPage } = React.useContext(PageRenderContext); const { isPageVisible, scrollToPage, visiblePageRatios } = React.useContext(ScrollContext); const [maxVisiblePageNumber, setMaxVisiblePageNumber] = React.useState>(null); const objectURL = getObjectURLForPage({ pageNumber }); @@ -31,10 +30,6 @@ export const Thumbnail: React.FunctionComponent = ({ pageNumber }: Props) parseInt(maxVisiblePageNumber) === pageNumber && isPageVisible({ pageNumber }); - React.useEffect(() => { - buildObjectURLForPage({ pageNumber }); - }, [pageRenderStates]); - const onClick = React.useCallback( event => { event.preventDefault(); From 8b5635bf88644f94063d85a7adbd403d63909bb1 Mon Sep 17 00:00:00 2001 From: Eric Marsh <108751850+ericmarsh995@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:35:38 -0700 Subject: [PATCH 3/4] export function for checking if all pages are rendered --- ui/library/src/context/PageRenderContext.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ui/library/src/context/PageRenderContext.ts b/ui/library/src/context/PageRenderContext.ts index 29270ab..464ee0b 100644 --- a/ui/library/src/context/PageRenderContext.ts +++ b/ui/library/src/context/PageRenderContext.ts @@ -16,6 +16,7 @@ export interface IPageRenderContext { pageRenderStates: PageNumberToRenderStateMap; getObjectURLForPage: (pageNumber: PageNumber) => Nullable; isBuildingObjectURLForPage: (pageNumber: PageNumber) => boolean; + isFinishedBuildingAllPagesObjectURLs: () => boolean; buildObjectURLForPage: (pageNumber: PageNumber) => Promise; } @@ -29,6 +30,10 @@ export const PageRenderContext = React.createContext({ logProviderWarning(`isBuildingObjectURLForPage(${JSON.stringify(args)})`, 'PageRenderContext'); return false; }, + isFinishedBuildingAllPagesObjectURLs: () => { + logProviderWarning(`isFinishedBuildingAllPagesObjectURLs()`, 'PageRenderContext'); + return false; + }, buildObjectURLForPage: args => { logProviderWarning(`buildObjectURLForPage(${JSON.stringify(args)})`, 'PageRenderContext'); return Promise.resolve(''); @@ -82,6 +87,16 @@ export function usePageRenderContextProps({ [pageRenderStates] ); + const isFinishedBuildingAllPagesObjectURLs = React.useCallback((): boolean => { + if (!pdfDocProxy) return false; + for (let pageNumber = 1; pageNumber <= pdfDocProxy.numPages; pageNumber++) { + if (!pageRenderStates.get(pageNumber)?.objectURL) { + return false; + } + } + return true; + }, [pdfDocProxy, pageRenderStates]); + const getObjectURLForPage = React.useCallback( ({ pageNumber, pageIndex }: PageNumber): Nullable => { if (typeof pageIndex === 'number') { @@ -179,6 +194,7 @@ export function usePageRenderContextProps({ pageRenderStates, getObjectURLForPage, isBuildingObjectURLForPage, + isFinishedBuildingAllPagesObjectURLs, buildObjectURLForPage, }; } From 2cd60f3f2eeca6fb11c7bd6e8ef6acb2f8848da6 Mon Sep 17 00:00:00 2001 From: Eric Marsh <108751850+ericmarsh995@users.noreply.github.com> Date: Tue, 11 Oct 2022 12:05:38 -0700 Subject: [PATCH 4/4] create getPriorityQueue and getNeighboringPages functions so that we can unit test --- ui/library/src/context/PageRenderContext.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ui/library/src/context/PageRenderContext.ts b/ui/library/src/context/PageRenderContext.ts index 464ee0b..be6b5fb 100644 --- a/ui/library/src/context/PageRenderContext.ts +++ b/ui/library/src/context/PageRenderContext.ts @@ -163,13 +163,7 @@ export function usePageRenderContextProps({ return; } - const visiblePagesNeighbors = [ - Math.max(1, visiblePages[0] - 1), - Math.min(pdfDocProxy.numPages, visiblePages[visiblePages.length - 1] + 1), - ]; - const allPages = Array.from({ length: pdfDocProxy.numPages }, (_, i) => i + 1); - const priorityQueue = new Set([...visiblePages, ...visiblePagesNeighbors, ...allPages]); - + const priorityQueue = getPriorityQueue(visiblePages, pdfDocProxy.numPages); for (const pageNumber of priorityQueue) { buildObjectURLForPage({ pageNumber }); } @@ -199,6 +193,17 @@ export function usePageRenderContextProps({ }; } +export function getNeighboringPages(pages: number[], numPages: number): number[] { + return [Math.max(1, pages[0] - 1), Math.min(numPages, pages[pages.length - 1] + 1)]; +} + +export function getPriorityQueue(visiblePages: number[], numPages: number): number[] { + const visiblePagesNeighbors = getNeighboringPages(visiblePages, numPages); + const allPages = Array.from({ length: numPages }, (_, i) => i + 1); + const priorityQueue = new Set([...visiblePages, ...visiblePagesNeighbors, ...allPages]); // put into set to remove duplicats + return Array.from(priorityQueue); // convert set to array +} + // This boost causes the rendered image to be scaled up by this amount const SCALE_BOOST = 2;