• Robert Knight's avatar
    Fix race condition when anchoring in a PDF during initial document load · a76e97c6
    Robert Knight authored
    Fix a race condition that could occur when the client tried to anchor
    annotations in a PDF while the PDF was still loading.
    
    PDF.js' `PDFViewer` class initializes its `PDFPageView` objects, as
    returned by `PDFViewer.getPageView`, asynchronously. While the page
    views are being initialized, `PDFViewer.pagesCount` returns 0 and
    `PDFViewer.getPageView` will return a nullish value. When loading has
    progressed further, `getPageView` returns a `PDFPageView` but with no
    `pdfPage` property. Once the page views are fully ready, a bubbling
    "pagesloaded" event is dispatched at the PDF viewer's container DOM
    element.
    
    The PDF anchoring code previously assumed that page views were always
    immediately available and they always had a `pdfPage` property. If this
    was not the case, anchoring would fail and all/many annotations would
    appear as orphans in the client. See the associated issue for ways to
    reproduce this locally with Via.
    
    Resolve the issue by making the `getPageView` helper which wraps
    `PDFViewer.getPageView` async, and block in that function until the page
    views are fully ready.
    
     - Make `getPageView` helper async and block if necessary until page
       views are ready.
     - Add a test to simulate scenarios where page views are not ready yet
     - Change the `quotePositionCache` cache to store page indexes rather
       than `PDFPageView` objects. This means that less code in the file
       needs to know about `PDFPageView` objects, and deal with the fact
       that they may not be immediately available. It also means it should
       be easier to adapt the code to future PDF.js API changes.
    
    Fixes #1330
    a76e97c6
pdf-test.js 10.8 KB