Skip to content

Commit

Permalink
Convert the PDFDocumentProperties.open method to be async
Browse files Browse the repository at this point in the history
By using `await`, rather than chaining promises, this method becomes more compact and slightly easier to reason about (at least in my opinion).
  • Loading branch information
Snuffleupagus committed Nov 21, 2020
1 parent d3f7959 commit 4615815
Showing 1 changed file with 71 additions and 85 deletions.
156 changes: 71 additions & 85 deletions web/pdf_document_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PDFDocumentProperties {
/**
* Open the document properties overlay.
*/
open() {
async open() {
const freezeFieldData = data => {
Object.defineProperty(this, "fieldData", {
value: Object.freeze(data),
Expand All @@ -107,95 +107,81 @@ class PDFDocumentProperties {
});
};

Promise.all([
await Promise.all([
this.overlayManager.open(this.overlayName),
this._dataAvailableCapability.promise,
]).then(() => {
const currentPageNumber = this._currentPageNumber;
const pagesRotation = this._pagesRotation;
]);
const currentPageNumber = this._currentPageNumber;
const pagesRotation = this._pagesRotation;

// If the document properties were previously fetched (for this PDF file),
// just update the dialog immediately to avoid redundant lookups.
if (
this.fieldData &&
currentPageNumber === this.fieldData._currentPageNumber &&
pagesRotation === this.fieldData._pagesRotation
) {
this._updateUI();
return;
}
// If the document properties were previously fetched (for this PDF file),
// just update the dialog immediately to avoid redundant lookups.
if (
this.fieldData &&
currentPageNumber === this.fieldData._currentPageNumber &&
pagesRotation === this.fieldData._pagesRotation
) {
this._updateUI();
return;
}

// Get the document properties.
this.pdfDocument
.getMetadata()
.then(
({ info, metadata, contentDispositionFilename, contentLength }) => {
return Promise.all([
info,
metadata,
contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(contentLength),
this._parseDate(info.CreationDate),
this._parseDate(info.ModDate),
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this._parsePageSize(
getPageSizeInches(pdfPage),
pagesRotation
);
}),
this._parseLinearization(info.IsLinearized),
]);
}
)
.then(
([
info,
metadata,
fileName,
fileSize,
creationDate,
modDate,
pageSize,
isLinearized,
]) => {
freezeFieldData({
fileName,
fileSize,
title: info.Title,
author: info.Author,
subject: info.Subject,
keywords: info.Keywords,
creationDate,
modificationDate: modDate,
creator: info.Creator,
producer: info.Producer,
version: info.PDFFormatVersion,
pageCount: this.pdfDocument.numPages,
pageSize,
linearized: isLinearized,
_currentPageNumber: currentPageNumber,
_pagesRotation: pagesRotation,
});
this._updateUI();

// Get the correct fileSize, since it may not have been available
// or could potentially be wrong.
return this.pdfDocument.getDownloadInfo().then(downloadInfo => {
return this._parseFileSize(downloadInfo.length);
});
}
)
.then(fileSize => {
if (fileSize === this.fieldData.fileSize) {
return; // The fileSize has already been correctly set.
}
const data = Object.assign(Object.create(null), this.fieldData);
data.fileSize = fileSize;

freezeFieldData(data);
this._updateUI();
});
// Get the document properties.
const {
info,
/* metadata, */
contentDispositionFilename,
contentLength,
} = await this.pdfDocument.getMetadata();

const [
fileName,
fileSize,
creationDate,
modificationDate,
pageSize,
isLinearized,
] = await Promise.all([
contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(contentLength),
this._parseDate(info.CreationDate),
this._parseDate(info.ModDate),
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
}),
this._parseLinearization(info.IsLinearized),
]);

freezeFieldData({
fileName,
fileSize,
title: info.Title,
author: info.Author,
subject: info.Subject,
keywords: info.Keywords,
creationDate,
modificationDate,
creator: info.Creator,
producer: info.Producer,
version: info.PDFFormatVersion,
pageCount: this.pdfDocument.numPages,
pageSize,
linearized: isLinearized,
_currentPageNumber: currentPageNumber,
_pagesRotation: pagesRotation,
});
this._updateUI();

// Get the correct fileSize, since it may not have been available
// or could potentially be wrong.
const { length } = await this.pdfDocument.getDownloadInfo();
if (contentLength === length) {
return; // The fileSize has already been correctly set.
}
const data = Object.assign(Object.create(null), this.fieldData);
data.fileSize = await this._parseFileSize(length);

freezeFieldData(data);
this._updateUI();
}

/**
Expand Down

0 comments on commit 4615815

Please sign in to comment.