Skip to content

Commit

Permalink
Merge pull request #13943 from Snuffleupagus/api-more-async
Browse files Browse the repository at this point in the history
Use `async` a bit more in the API
  • Loading branch information
timvandermeij authored Aug 29, 2021
2 parents a270bae + ce3f5ea commit 954e1a1
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,13 @@ function getDocument(src) {
* @param {Object} source
* @param {PDFDataRangeTransport} pdfDataRangeTransport
* @param {string} docId - Unique document ID, used in `MessageHandler`.
* @returns {Promise} A promise that is resolved when the worker ID of the
* `MessageHandler` is known.
* @returns {Promise<string>} A promise that is resolved when the worker ID of
* the `MessageHandler` is known.
* @private
*/
function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
async function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
if (worker.destroyed) {
return Promise.reject(new Error("Worker was destroyed"));
throw new Error("Worker was destroyed");
}

if (pdfDataRangeTransport) {
Expand All @@ -479,8 +479,9 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
source.contentDispositionFilename =
pdfDataRangeTransport.contentDispositionFilename;
}
return worker.messageHandler
.sendWithPromise("GetDocRequest", {
const workerId = await worker.messageHandler.sendWithPromise(
"GetDocRequest",
{
docId,
apiVersion:
typeof PDFJSDev !== "undefined" && !PDFJSDev.test("TESTING")
Expand Down Expand Up @@ -508,13 +509,13 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
standardFontDataUrl: source.useWorkerFetch
? source.standardFontDataUrl
: null,
})
.then(function (workerId) {
if (worker.destroyed) {
throw new Error("Worker was destroyed");
}
return workerId;
});
}
);

if (worker.destroyed) {
throw new Error("Worker was destroyed");
}
return workerId;
}

/**
Expand Down Expand Up @@ -587,19 +588,15 @@ class PDFDocumentLoadingTask {
* @returns {Promise<void>} A promise that is resolved when destruction is
* completed.
*/
destroy() {
async destroy() {
this.destroyed = true;
await this._transport?.destroy();

const transportDestroyed = !this._transport
? Promise.resolve()
: this._transport.destroy();
return transportDestroyed.then(() => {
this._transport = null;
if (this._worker) {
this._worker.destroy();
this._worker = null;
}
});
this._transport = null;
if (this._worker) {
this._worker.destroy();
this._worker = null;
}
}
}

Expand Down Expand Up @@ -2898,13 +2895,9 @@ class WorkerTransport {
}

getPageIndex(ref) {
return this.messageHandler
.sendWithPromise("GetPageIndex", {
ref,
})
.catch(function (reason) {
return Promise.reject(new Error(reason));
});
return this.messageHandler.sendWithPromise("GetPageIndex", {
ref,
});
}

getAnnotations(pageIndex, intent) {
Expand Down

0 comments on commit 954e1a1

Please sign in to comment.