Skip to content

Commit

Permalink
Merge pull request #12822 from Snuffleupagus/scripting-contentLength
Browse files Browse the repository at this point in the history
Improve the `PDFViewerApplication._contentLength` handling in the viewer, related mostly to scripting
  • Loading branch information
timvandermeij committed Jan 7, 2021
2 parents 5bde4b7 + 7d3632d commit ac5168a
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,8 @@ const PDFViewerApplication = {
load(pdfDocument) {
this.pdfDocument = pdfDocument;

pdfDocument.getDownloadInfo().then(() => {
pdfDocument.getDownloadInfo().then(({ length }) => {
this._contentLength = length; // Ensure that the correct length is used.
this.downloadComplete = true;
this.loadingBar.hide();

Expand Down Expand Up @@ -1498,6 +1499,25 @@ const PDFViewerApplication = {
return; // The document was closed while the metadata resolved.
}
}
if (!this._contentLength) {
// Always waiting for the entire PDF document to be loaded will, most
// likely, delay sandbox-creation too much in the general case for all
// PDF documents which are not provided as binary data to the API.
// Hence we'll simply have to trust that the `contentLength` (as provided
// by the server), when it exists, is accurate enough here.
await new Promise(resolve => {
this.eventBus._on(
"documentloaded",
evt => {
resolve();
},
{ once: true }
);
});
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the downloadInfo resolved.
}
}

const updateFromSandbox = ({ detail }) => {
const { id, command, value } = detail;
Expand Down Expand Up @@ -1622,20 +1642,6 @@ const PDFViewerApplication = {
window.addEventListener(name, listener);
}

if (!this._contentLength) {
// Always waiting for the entire PDF document to be loaded will, most
// likely, delay sandbox-creation too much in the general case for all
// PDF documents which are not provided as binary data to the API.
// Hence we'll simply have to trust that the `contentLength` (as provided
// by the server), when it exists, is accurate enough here.
const { length } = await pdfDocument.getDownloadInfo();

if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the download info resolved.
}
this._contentLength = length;
}

try {
await scripting.createSandbox({
objects,
Expand Down Expand Up @@ -1761,7 +1767,7 @@ const PDFViewerApplication = {
this.documentInfo = info;
this.metadata = metadata;
this._contentDispositionFilename = contentDispositionFilename;
this._contentLength = contentLength;
this._contentLength ??= contentLength; // See `getDownloadInfo`-call above.

// Provides some basic debug information
console.log(
Expand Down

0 comments on commit ac5168a

Please sign in to comment.