From ef8e5fd77c809b6589716a5f1d5f68e24862c3e6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 8 Nov 2018 13:40:06 +0100 Subject: [PATCH] Convert `PDFDocumentLoadingTask`, in `src/display/api.js`, to an ES6 class Also deprecates the `then` method, in favour of the `promise` getter. --- examples/acroforms/acroforms.js | 3 +- examples/components/pageviewer.js | 5 +- examples/components/simpleviewer.js | 5 +- examples/components/singlepageviewer.js | 5 +- examples/learning/helloworld.html | 5 +- examples/learning/helloworld64.html | 5 +- examples/learning/prevnext.html | 3 +- examples/node/getinfo.js | 3 +- examples/node/pdf2png/pdf2png.js | 3 +- examples/node/pdf2svg.js | 5 +- examples/svgviewer/viewer.js | 5 +- examples/text-only/pdf2svg.js | 3 +- src/display/api.js | 95 +++++++++++++------------ test/driver.js | 5 +- 14 files changed, 82 insertions(+), 68 deletions(-) diff --git a/examples/acroforms/acroforms.js b/examples/acroforms/acroforms.js index 0e708f3cb..795e992b4 100644 --- a/examples/acroforms/acroforms.js +++ b/examples/acroforms/acroforms.js @@ -24,7 +24,8 @@ var DEFAULT_SCALE = 1.0; var container = document.getElementById('pageContainer'); // Fetch the PDF document from the URL using promises. -pdfjsLib.getDocument(DEFAULT_URL).then(function (doc) { +var loadingTask = pdfjsLib.getDocument(DEFAULT_URL); +loadingTask.promise.then(function(doc) { // Use a promise to fetch and render the next page. var promise = Promise.resolve(); diff --git a/examples/components/pageviewer.js b/examples/components/pageviewer.js index 3eaabc6c8..df69fdad0 100644 --- a/examples/components/pageviewer.js +++ b/examples/components/pageviewer.js @@ -37,11 +37,12 @@ var SCALE = 1.0; var container = document.getElementById('pageContainer'); // Loading document. -pdfjsLib.getDocument({ +var loadingTask = pdfjsLib.getDocument({ url: DEFAULT_URL, cMapUrl: CMAP_URL, cMapPacked: CMAP_PACKED, -}).then(function(pdfDocument) { +}); +loadingTask.promise.then(function(pdfDocument) { // Document loaded, retrieving the page. return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) { // Creating the page view with default parameters. diff --git a/examples/components/simpleviewer.js b/examples/components/simpleviewer.js index 1f801bbd2..b0b268dee 100644 --- a/examples/components/simpleviewer.js +++ b/examples/components/simpleviewer.js @@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () { }); // Loading document. -pdfjsLib.getDocument({ +var loadingTask = pdfjsLib.getDocument({ url: DEFAULT_URL, cMapUrl: CMAP_URL, cMapPacked: CMAP_PACKED, -}).then(function(pdfDocument) { +}); +loadingTask.promise.then(function(pdfDocument) { // Document loaded, specifying document for the viewer and // the (optional) linkService. pdfViewer.setDocument(pdfDocument); diff --git a/examples/components/singlepageviewer.js b/examples/components/singlepageviewer.js index d4b5da3e7..d6157f4b1 100644 --- a/examples/components/singlepageviewer.js +++ b/examples/components/singlepageviewer.js @@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () { }); // Loading document. -pdfjsLib.getDocument({ +var loadingTask = pdfjsLib.getDocument({ url: DEFAULT_URL, cMapUrl: CMAP_URL, cMapPacked: CMAP_PACKED, -}).then(function(pdfDocument) { +}); +loadingTask.promise.then(function(pdfDocument) { // Document loaded, specifying document for the viewer and // the (optional) linkService. pdfSinglePageViewer.setDocument(pdfDocument); diff --git a/examples/learning/helloworld.html b/examples/learning/helloworld.html index c4a6456d2..c73f0329d 100644 --- a/examples/learning/helloworld.html +++ b/examples/learning/helloworld.html @@ -28,11 +28,12 @@ // // Asynchronous download PDF // - pdfjsLib.getDocument(url).then(function getPdfHelloWorld(pdf) { + var loadingTask = pdfjsLib.getDocument(url); + loadingTask.promise.then(function(pdf) { // // Fetch the first page // - pdf.getPage(1).then(function getPageHelloWorld(page) { + pdf.getPage(1).then(function(page) { var scale = 1.5; var viewport = page.getViewport(scale); diff --git a/examples/learning/helloworld64.html b/examples/learning/helloworld64.html index f32d69000..d316a6a06 100644 --- a/examples/learning/helloworld64.html +++ b/examples/learning/helloworld64.html @@ -39,9 +39,10 @@ // Opening PDF by passing its binary data as a string. It is still preferable // to use Uint8Array, but string or array-like structure will work too. - pdfjsLib.getDocument({data: pdfData}).then(function getPdfHelloWorld(pdf) { + var loadingTask = pdfjsLib.getDocument({data: pdfData}); + loadingTask.promise.then(function(pdf) { // Fetch the first page. - pdf.getPage(1).then(function getPageHelloWorld(page) { + pdf.getPage(1).then(function(page) { var scale = 1.5; var viewport = page.getViewport(scale); diff --git a/examples/learning/prevnext.html b/examples/learning/prevnext.html index df1098882..32fc72476 100644 --- a/examples/learning/prevnext.html +++ b/examples/learning/prevnext.html @@ -117,7 +117,8 @@ /** * Asynchronously downloads PDF. */ - pdfjsLib.getDocument(url).then(function (pdfDoc_) { + var loadingTask = pdfjsLib.getDocument(url); + loadingTask.promise.then(function(pdfDoc_) { pdfDoc = pdfDoc_; document.getElementById('page_count').textContent = pdfDoc.numPages; diff --git a/examples/node/getinfo.js b/examples/node/getinfo.js index 61034cfa3..72eeff48e 100644 --- a/examples/node/getinfo.js +++ b/examples/node/getinfo.js @@ -17,7 +17,8 @@ var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf'; // Will be using promises to load document, pages and misc data instead of // callback. -pdfjsLib.getDocument(pdfPath).then(function (doc) { +var loadingTask = pdfjsLib.getDocument(pdfPath); +loadingTask.promise.then(function(doc) { var numPages = doc.numPages; console.log('# Document Loaded'); console.log('Number of Pages: ' + numPages); diff --git a/examples/node/pdf2png/pdf2png.js b/examples/node/pdf2png/pdf2png.js index ae739a35b..08e06aea3 100644 --- a/examples/node/pdf2png/pdf2png.js +++ b/examples/node/pdf2png/pdf2png.js @@ -57,7 +57,8 @@ var pdfURL = '../../../web/compressed.tracemonkey-pldi-09.pdf'; var rawData = new Uint8Array(fs.readFileSync(pdfURL)); // Load the PDF file. -pdfjsLib.getDocument(rawData).then(function (pdfDocument) { +var loadingTask = pdfjsLib.getDocument(rawData); +loadingTask.promise.then(function(pdfDocument) { console.log('# PDF document loaded.'); // Get the first page. diff --git a/examples/node/pdf2svg.js b/examples/node/pdf2svg.js index 4c59cb78c..fe3372bdb 100644 --- a/examples/node/pdf2svg.js +++ b/examples/node/pdf2svg.js @@ -84,11 +84,12 @@ function writeSvgToFile(svgElement, filePath) { // Will be using promises to load document, pages and misc data instead of // callback. -pdfjsLib.getDocument({ +var loadingTask = pdfjsLib.getDocument({ data: data, // Try to export JPEG images directly if they don't need any further processing. nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY -}).then(function (doc) { +}); +loadingTask.promise.then(function(doc) { var numPages = doc.numPages; console.log('# Document Loaded'); console.log('Number of Pages: ' + numPages); diff --git a/examples/svgviewer/viewer.js b/examples/svgviewer/viewer.js index bcea1fa08..433d981be 100644 --- a/examples/svgviewer/viewer.js +++ b/examples/svgviewer/viewer.js @@ -51,11 +51,12 @@ document.addEventListener('pagesinit', function () { }); // Loading document. -pdfjsLib.getDocument({ +var loadingTask = pdfjsLib.getDocument({ url: DEFAULT_URL, cMapUrl: CMAP_URL, cMapPacked: CMAP_PACKED, -}).then(function(pdfDocument) { +}); +loadingTask.promise.then(function(pdfDocument) { // Document loaded, specifying document for the viewer and // the (optional) linkService. pdfViewer.setDocument(pdfDocument); diff --git a/examples/text-only/pdf2svg.js b/examples/text-only/pdf2svg.js index 64395cdeb..27094c46e 100644 --- a/examples/text-only/pdf2svg.js +++ b/examples/text-only/pdf2svg.js @@ -49,7 +49,8 @@ function buildSVG(viewport, textContent) { function pageLoaded() { // Loading document and page text content - pdfjsLib.getDocument({url: PDF_PATH}).then(function (pdfDocument) { + var loadingTask = pdfjsLib.getDocument({url: PDF_PATH}); + loadingTask.promise.then(function(pdfDocument) { pdfDocument.getPage(PAGE_NUMBER).then(function (page) { var viewport = page.getViewport(PAGE_SCALE); page.getTextContent().then(function (textContent) { diff --git a/src/display/api.js b/src/display/api.js index a3ff31530..57ee38f99 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -15,10 +15,11 @@ /* globals requirejs, __non_webpack_require__ */ import { - assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException, - isArrayBuffer, isSameOrigin, MissingPDFException, NativeImageDecoding, - PasswordException, setVerbosityLevel, shadow, stringToBytes, - UnexpectedResponseException, UnknownErrorException, unreachable, URL, warn + assert, createPromiseCapability, deprecated, getVerbosityLevel, info, + InvalidPDFException, isArrayBuffer, isSameOrigin, MissingPDFException, + NativeImageDecoding, PasswordException, setVerbosityLevel, shadow, + stringToBytes, UnexpectedResponseException, UnknownErrorException, + unreachable, URL, warn } from '../shared/util'; import { DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript, @@ -427,56 +428,55 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) { * @class * @alias PDFDocumentLoadingTask */ -var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { - var nextDocumentId = 0; +const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { + let nextDocumentId = 0; /** @constructs PDFDocumentLoadingTask */ - function PDFDocumentLoadingTask() { - this._capability = createPromiseCapability(); - this._transport = null; - this._worker = null; + class PDFDocumentLoadingTask { + constructor() { + this._capability = createPromiseCapability(); + this._transport = null; + this._worker = null; - /** - * Unique document loading task id -- used in MessageHandlers. - * @type {string} - */ - this.docId = 'd' + (nextDocumentId++); + /** + * Unique document loading task id -- used in MessageHandlers. + * @type {string} + */ + this.docId = 'd' + (nextDocumentId++); - /** - * Shows if loading task is destroyed. - * @type {boolean} - */ - this.destroyed = false; + /** + * Shows if loading task is destroyed. + * @type {boolean} + */ + this.destroyed = false; - /** - * Callback to request a password if wrong or no password was provided. - * The callback receives two parameters: function that needs to be called - * with new password and reason (see {PasswordResponses}). - */ - this.onPassword = null; + /** + * Callback to request a password if wrong or no password was provided. + * The callback receives two parameters: function that needs to be called + * with new password and reason (see {PasswordResponses}). + */ + this.onPassword = null; - /** - * Callback to be able to monitor the loading progress of the PDF file - * (necessary to implement e.g. a loading bar). The callback receives - * an {Object} with the properties: {number} loaded and {number} total. - */ - this.onProgress = null; + /** + * Callback to be able to monitor the loading progress of the PDF file + * (necessary to implement e.g. a loading bar). The callback receives + * an {Object} with the properties: {number} loaded and {number} total. + */ + this.onProgress = null; - /** - * Callback to when unsupported feature is used. The callback receives - * an {UNSUPPORTED_FEATURES} argument. - */ - this.onUnsupportedFeature = null; - } + /** + * Callback to when unsupported feature is used. The callback receives + * an {UNSUPPORTED_FEATURES} argument. + */ + this.onUnsupportedFeature = null; + } - PDFDocumentLoadingTask.prototype = - /** @lends PDFDocumentLoadingTask.prototype */ { /** * @return {Promise} */ get promise() { return this._capability.promise; - }, + } /** * Aborts all network requests and destroys worker. @@ -486,7 +486,7 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { destroy() { this.destroyed = true; - var transportDestroyed = !this._transport ? Promise.resolve() : + const transportDestroyed = !this._transport ? Promise.resolve() : this._transport.destroy(); return transportDestroyed.then(() => { this._transport = null; @@ -495,7 +495,7 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { this._worker = null; } }); - }, + } /** * Registers callbacks to indicate the document loading completion. @@ -505,11 +505,12 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { * @return {Promise} A promise that is resolved after the onFulfilled or * onRejected callback. */ - then: function PDFDocumentLoadingTask_then(onFulfilled, onRejected) { + then(onFulfilled, onRejected) { + deprecated('PDFDocumentLoadingTask.then method, ' + + 'use the `promise` getter instead.'); return this.promise.then.apply(this.promise, arguments); - }, - }; - + } + } return PDFDocumentLoadingTask; })(); diff --git a/test/driver.js b/test/driver.js index d9b3f1543..ae23dee2c 100644 --- a/test/driver.js +++ b/test/driver.js @@ -358,7 +358,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars let absoluteUrl = new URL(task.file, window.location).href; try { - pdfjsLib.getDocument({ + const loadingTask = pdfjsLib.getDocument({ url: absoluteUrl, password: task.password, nativeImageDecoderSupport: task.nativeImageDecoderSupport, @@ -367,7 +367,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars disableRange: task.disableRange, disableAutoFetch: !task.enableAutoFetch, pdfBug: true, - }).then((doc) => { + }); + loadingTask.promise.then((doc) => { task.pdfDoc = doc; this._nextPage(task, failure); }, (err) => {