Re-factor the download/save-methods, on PDFViewerApplication, to make full use of async/await

In the next patch we'll need to be able to actually wait for saving to complete, hence it's necessary to slightly re-factor the `save`-method.

As part of these changes, we can reduce some duplication in the `save`-method and slightly improve the overall code. For consistency, the `download`-method is updated similarily to improve the code (this functionality is *very* old, even pre-dating the introduction of Promises in the code-base).
This commit is contained in:
Jonas Jenwald 2021-04-03 15:33:14 +02:00
parent 5cf116a958
commit 5b28a0bf97

View File

@ -949,62 +949,59 @@ const PDFViewerApplication = {
); );
}, },
download({ sourceEventType = "download" } = {}) { /**
function downloadByUrl() { * @private
downloadManager.downloadUrl(url, filename); */
} _ensureDownloadComplete() {
if (this.pdfDocument && this.downloadComplete) {
const downloadManager = this.downloadManager,
url = this.baseUrl,
filename = this._docFilename;
// When the PDF document isn't ready, or the PDF file is still downloading,
// simply download using the URL.
if (!this.pdfDocument || !this.downloadComplete) {
downloadByUrl();
return; return;
} }
throw new Error("PDF document not downloaded.");
},
this.pdfDocument async download({ sourceEventType = "download" } = {}) {
.getData() const url = this.baseUrl,
.then(function (data) { filename = this._docFilename;
const blob = new Blob([data], { type: "application/pdf" }); try {
downloadManager.download(blob, url, filename, sourceEventType); this._ensureDownloadComplete();
})
.catch(downloadByUrl); // Error occurred, try downloading with the URL. const data = await this.pdfDocument.getData();
const blob = new Blob([data], { type: "application/pdf" });
await this.downloadManager.download(blob, url, filename, sourceEventType);
} catch (reason) {
// When the PDF document isn't ready, or the PDF file is still
// downloading, simply download using the URL.
await this.downloadManager.downloadUrl(url, filename);
}
}, },
async save({ sourceEventType = "download" } = {}) { async save({ sourceEventType = "download" } = {}) {
if (this._saveInProgress) { if (this._saveInProgress) {
return; return;
} }
const downloadManager = this.downloadManager,
url = this.baseUrl,
filename = this._docFilename;
// When the PDF document isn't ready, or the PDF file is still downloading,
// simply download using the URL.
if (!this.pdfDocument || !this.downloadComplete) {
this.download({ sourceEventType });
return;
}
this._saveInProgress = true; this._saveInProgress = true;
await this.pdfScriptingManager.dispatchWillSave(); await this.pdfScriptingManager.dispatchWillSave();
this.pdfDocument const url = this.baseUrl,
.saveDocument(this.pdfDocument.annotationStorage) filename = this._docFilename;
.then(data => { try {
const blob = new Blob([data], { type: "application/pdf" }); this._ensureDownloadComplete();
downloadManager.download(blob, url, filename, sourceEventType);
}) const data = await this.pdfDocument.saveDocument(
.catch(() => { this.pdfDocument.annotationStorage
this.download({ sourceEventType }); );
}) const blob = new Blob([data], { type: "application/pdf" });
.finally(async () => {
await this.pdfScriptingManager.dispatchDidSave(); await this.downloadManager.download(blob, url, filename, sourceEventType);
this._saveInProgress = false; } catch (reason) {
}); // When the PDF document isn't ready, or the PDF file is still
// downloading, simply fallback to a "regular" download.
await this.download({ sourceEventType });
} finally {
await this.pdfScriptingManager.dispatchDidSave();
this._saveInProgress = false;
}
}, },
downloadOrSave(options) { downloadOrSave(options) {