Convert the PDFDocumentProperties.open
method to be async
By using `await`, rather than chaining promises, this method becomes more compact and slightly easier to reason about (at least in my opinion).
This commit is contained in:
parent
d3f7959689
commit
4615815cc7
@ -97,7 +97,7 @@ class PDFDocumentProperties {
|
|||||||
/**
|
/**
|
||||||
* Open the document properties overlay.
|
* Open the document properties overlay.
|
||||||
*/
|
*/
|
||||||
open() {
|
async open() {
|
||||||
const freezeFieldData = data => {
|
const freezeFieldData = data => {
|
||||||
Object.defineProperty(this, "fieldData", {
|
Object.defineProperty(this, "fieldData", {
|
||||||
value: Object.freeze(data),
|
value: Object.freeze(data),
|
||||||
@ -107,95 +107,81 @@ class PDFDocumentProperties {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Promise.all([
|
await Promise.all([
|
||||||
this.overlayManager.open(this.overlayName),
|
this.overlayManager.open(this.overlayName),
|
||||||
this._dataAvailableCapability.promise,
|
this._dataAvailableCapability.promise,
|
||||||
]).then(() => {
|
]);
|
||||||
const currentPageNumber = this._currentPageNumber;
|
const currentPageNumber = this._currentPageNumber;
|
||||||
const pagesRotation = this._pagesRotation;
|
const pagesRotation = this._pagesRotation;
|
||||||
|
|
||||||
// If the document properties were previously fetched (for this PDF file),
|
// If the document properties were previously fetched (for this PDF file),
|
||||||
// just update the dialog immediately to avoid redundant lookups.
|
// just update the dialog immediately to avoid redundant lookups.
|
||||||
if (
|
if (
|
||||||
this.fieldData &&
|
this.fieldData &&
|
||||||
currentPageNumber === this.fieldData._currentPageNumber &&
|
currentPageNumber === this.fieldData._currentPageNumber &&
|
||||||
pagesRotation === this.fieldData._pagesRotation
|
pagesRotation === this.fieldData._pagesRotation
|
||||||
) {
|
) {
|
||||||
this._updateUI();
|
this._updateUI();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the document properties.
|
// Get the document properties.
|
||||||
this.pdfDocument
|
const {
|
||||||
.getMetadata()
|
info,
|
||||||
.then(
|
/* metadata, */
|
||||||
({ info, metadata, contentDispositionFilename, contentLength }) => {
|
contentDispositionFilename,
|
||||||
return Promise.all([
|
contentLength,
|
||||||
info,
|
} = await this.pdfDocument.getMetadata();
|
||||||
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
|
const [
|
||||||
// or could potentially be wrong.
|
fileName,
|
||||||
return this.pdfDocument.getDownloadInfo().then(downloadInfo => {
|
fileSize,
|
||||||
return this._parseFileSize(downloadInfo.length);
|
creationDate,
|
||||||
});
|
modificationDate,
|
||||||
}
|
pageSize,
|
||||||
)
|
isLinearized,
|
||||||
.then(fileSize => {
|
] = await Promise.all([
|
||||||
if (fileSize === this.fieldData.fileSize) {
|
contentDispositionFilename || getPDFFileNameFromURL(this.url),
|
||||||
return; // The fileSize has already been correctly set.
|
this._parseFileSize(contentLength),
|
||||||
}
|
this._parseDate(info.CreationDate),
|
||||||
const data = Object.assign(Object.create(null), this.fieldData);
|
this._parseDate(info.ModDate),
|
||||||
data.fileSize = fileSize;
|
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
|
||||||
|
return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
|
||||||
|
}),
|
||||||
|
this._parseLinearization(info.IsLinearized),
|
||||||
|
]);
|
||||||
|
|
||||||
freezeFieldData(data);
|
freezeFieldData({
|
||||||
this._updateUI();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user