Convert PDFDocumentLoadingTask, in src/display/api.js, to an ES6 class

Also deprecates the `then` method, in favour of the `promise` getter.
This commit is contained in:
Jonas Jenwald 2018-11-08 13:40:06 +01:00
parent 5f15dc2023
commit ef8e5fd77c
14 changed files with 82 additions and 68 deletions

View File

@ -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();

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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;
})();

View File

@ -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) => {