Skip to content

Commit 8013537

Browse files
Merge pull request mozilla#10636 from Snuffleupagus/PDFDocumentProxy-destroy
Small clean-up of the `PDFDocumentProxy.destroy` method and related code
2 parents 0abd0bc + 24fc4f8 commit 8013537

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

src/display/api.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ class PDFDataRangeTransport {
574574
* properties that can be read synchronously.
575575
*/
576576
class PDFDocumentProxy {
577-
constructor(pdfInfo, transport, loadingTask) {
578-
this.loadingTask = loadingTask;
579-
577+
constructor(pdfInfo, transport) {
580578
this._pdfInfo = pdfInfo;
581579
this._transport = transport;
582580
}
@@ -761,6 +759,13 @@ class PDFDocumentProxy {
761759
get loadingParams() {
762760
return this._transport.loadingParams;
763761
}
762+
763+
/**
764+
* @return {PDFDocumentLoadingTask} The loadingTask for the current document.
765+
*/
766+
get loadingTask() {
767+
return this._transport.loadingTask;
768+
}
764769
}
765770

766771
/**
@@ -1827,9 +1832,8 @@ class WorkerTransport {
18271832
}, this);
18281833

18291834
messageHandler.on('GetDoc', function({ pdfInfo, }) {
1830-
this.numPages = pdfInfo.numPages;
1831-
this.pdfDocument = new PDFDocumentProxy(pdfInfo, this, loadingTask);
1832-
loadingTask._capability.resolve(this.pdfDocument);
1835+
this._numPages = pdfInfo.numPages;
1836+
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
18331837
}, this);
18341838

18351839
messageHandler.on('PasswordRequest', function(exception) {
@@ -2130,7 +2134,7 @@ class WorkerTransport {
21302134

21312135
getPage(pageNumber) {
21322136
if (!Number.isInteger(pageNumber) ||
2133-
pageNumber <= 0 || pageNumber > this.numPages) {
2137+
pageNumber <= 0 || pageNumber > this._numPages) {
21342138
return Promise.reject(new Error('Invalid page request'));
21352139
}
21362140

test/unit/api_spec.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ describe('api', function() {
954954
return _checkCanLoad(false, filename, options);
955955
}
956956
afterEach(function(done) {
957-
if (loadingTask) {
957+
if (loadingTask && !loadingTask.destroyed) {
958958
loadingTask.destroy().then(done);
959959
} else {
960960
done();
@@ -1385,15 +1385,13 @@ describe('api', function() {
13851385
// A PDF using the Arial font.
13861386
var pdf3 = buildGetDocumentParams('issue6068.pdf');
13871387
var loadingTasks = [];
1388-
var pdfDocuments = [];
13891388

13901389
// Render the first page of the given PDF file.
13911390
// Fulfills the promise with the base64-encoded version of the PDF.
13921391
async function renderPDF(filename) {
13931392
const loadingTask = getDocument(filename);
13941393
loadingTasks.push(loadingTask);
13951394
const pdf = await loadingTask.promise;
1396-
pdfDocuments.push(pdf);
13971395
const page = await pdf.getPage(1);
13981396
const viewport = page.getViewport({ scale: 1.2, });
13991397
const canvasAndCtx = CanvasFactory.create(viewport.width,
@@ -1413,18 +1411,10 @@ describe('api', function() {
14131411
// Issue 6205 reported an issue with font rendering, so clear the loaded
14141412
// fonts so that we can see whether loading PDFs in parallel does not
14151413
// cause any issues with the rendered fonts.
1416-
var destroyPromises = pdfDocuments.map(function(pdfDocument) {
1417-
return pdfDocument.destroy();
1418-
});
1419-
1420-
// Destroy the workers.
1421-
var destroyPromises2 = loadingTasks.map(function(loadingTask) {
1414+
const destroyPromises = loadingTasks.map(function(loadingTask) {
14221415
return loadingTask.destroy();
14231416
});
1424-
1425-
Promise.all(destroyPromises.concat(destroyPromises2)).then(function() {
1426-
done();
1427-
});
1417+
Promise.all(destroyPromises).then(done);
14281418
});
14291419

14301420
it('should correctly render PDFs in parallel', function(done) {

0 commit comments

Comments
 (0)