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'); var container = document.getElementById('pageContainer');
// Fetch the PDF document from the URL using promises. // 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. // Use a promise to fetch and render the next page.
var promise = Promise.resolve(); var promise = Promise.resolve();

View File

@ -37,11 +37,12 @@ var SCALE = 1.0;
var container = document.getElementById('pageContainer'); var container = document.getElementById('pageContainer');
// Loading document. // Loading document.
pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL, url: DEFAULT_URL,
cMapUrl: CMAP_URL, cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED, cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) { });
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, retrieving the page. // Document loaded, retrieving the page.
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) { return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
// Creating the page view with default parameters. // Creating the page view with default parameters.

View File

@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
}); });
// Loading document. // Loading document.
pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL, url: DEFAULT_URL,
cMapUrl: CMAP_URL, cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED, cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) { });
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and // Document loaded, specifying document for the viewer and
// the (optional) linkService. // the (optional) linkService.
pdfViewer.setDocument(pdfDocument); pdfViewer.setDocument(pdfDocument);

View File

@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
}); });
// Loading document. // Loading document.
pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL, url: DEFAULT_URL,
cMapUrl: CMAP_URL, cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED, cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) { });
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and // Document loaded, specifying document for the viewer and
// the (optional) linkService. // the (optional) linkService.
pdfSinglePageViewer.setDocument(pdfDocument); pdfSinglePageViewer.setDocument(pdfDocument);

View File

@ -28,11 +28,12 @@
// //
// Asynchronous download PDF // Asynchronous download PDF
// //
pdfjsLib.getDocument(url).then(function getPdfHelloWorld(pdf) { var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
// //
// Fetch the first page // Fetch the first page
// //
pdf.getPage(1).then(function getPageHelloWorld(page) { pdf.getPage(1).then(function(page) {
var scale = 1.5; var scale = 1.5;
var viewport = page.getViewport(scale); 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 // 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. // 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. // Fetch the first page.
pdf.getPage(1).then(function getPageHelloWorld(page) { pdf.getPage(1).then(function(page) {
var scale = 1.5; var scale = 1.5;
var viewport = page.getViewport(scale); var viewport = page.getViewport(scale);

View File

@ -117,7 +117,8 @@
/** /**
* Asynchronously downloads PDF. * Asynchronously downloads PDF.
*/ */
pdfjsLib.getDocument(url).then(function (pdfDoc_) { var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_; pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages; 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 // Will be using promises to load document, pages and misc data instead of
// callback. // callback.
pdfjsLib.getDocument(pdfPath).then(function (doc) { var loadingTask = pdfjsLib.getDocument(pdfPath);
loadingTask.promise.then(function(doc) {
var numPages = doc.numPages; var numPages = doc.numPages;
console.log('# Document Loaded'); console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages); 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)); var rawData = new Uint8Array(fs.readFileSync(pdfURL));
// Load the PDF file. // 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.'); console.log('# PDF document loaded.');
// Get the first page. // 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 // Will be using promises to load document, pages and misc data instead of
// callback. // callback.
pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
data: data, data: data,
// Try to export JPEG images directly if they don't need any further processing. // Try to export JPEG images directly if they don't need any further processing.
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
}).then(function (doc) { });
loadingTask.promise.then(function(doc) {
var numPages = doc.numPages; var numPages = doc.numPages;
console.log('# Document Loaded'); console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages); console.log('Number of Pages: ' + numPages);

View File

@ -51,11 +51,12 @@ document.addEventListener('pagesinit', function () {
}); });
// Loading document. // Loading document.
pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL, url: DEFAULT_URL,
cMapUrl: CMAP_URL, cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED, cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) { });
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and // Document loaded, specifying document for the viewer and
// the (optional) linkService. // the (optional) linkService.
pdfViewer.setDocument(pdfDocument); pdfViewer.setDocument(pdfDocument);

View File

@ -49,7 +49,8 @@ function buildSVG(viewport, textContent) {
function pageLoaded() { function pageLoaded() {
// Loading document and page text content // 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) { pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
var viewport = page.getViewport(PAGE_SCALE); var viewport = page.getViewport(PAGE_SCALE);
page.getTextContent().then(function (textContent) { page.getTextContent().then(function (textContent) {

View File

@ -15,10 +15,11 @@
/* globals requirejs, __non_webpack_require__ */ /* globals requirejs, __non_webpack_require__ */
import { import {
assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException, assert, createPromiseCapability, deprecated, getVerbosityLevel, info,
isArrayBuffer, isSameOrigin, MissingPDFException, NativeImageDecoding, InvalidPDFException, isArrayBuffer, isSameOrigin, MissingPDFException,
PasswordException, setVerbosityLevel, shadow, stringToBytes, NativeImageDecoding, PasswordException, setVerbosityLevel, shadow,
UnexpectedResponseException, UnknownErrorException, unreachable, URL, warn stringToBytes, UnexpectedResponseException, UnknownErrorException,
unreachable, URL, warn
} from '../shared/util'; } from '../shared/util';
import { import {
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript, DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript,
@ -427,56 +428,55 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
* @class * @class
* @alias PDFDocumentLoadingTask * @alias PDFDocumentLoadingTask
*/ */
var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
var nextDocumentId = 0; let nextDocumentId = 0;
/** @constructs PDFDocumentLoadingTask */ /** @constructs PDFDocumentLoadingTask */
function PDFDocumentLoadingTask() { class PDFDocumentLoadingTask {
this._capability = createPromiseCapability(); constructor() {
this._transport = null; this._capability = createPromiseCapability();
this._worker = null; this._transport = null;
this._worker = null;
/** /**
* Unique document loading task id -- used in MessageHandlers. * Unique document loading task id -- used in MessageHandlers.
* @type {string} * @type {string}
*/ */
this.docId = 'd' + (nextDocumentId++); this.docId = 'd' + (nextDocumentId++);
/** /**
* Shows if loading task is destroyed. * Shows if loading task is destroyed.
* @type {boolean} * @type {boolean}
*/ */
this.destroyed = false; this.destroyed = false;
/** /**
* Callback to request a password if wrong or no password was provided. * Callback to request a password if wrong or no password was provided.
* The callback receives two parameters: function that needs to be called * The callback receives two parameters: function that needs to be called
* with new password and reason (see {PasswordResponses}). * with new password and reason (see {PasswordResponses}).
*/ */
this.onPassword = null; this.onPassword = null;
/** /**
* Callback to be able to monitor the loading progress of the PDF file * Callback to be able to monitor the loading progress of the PDF file
* (necessary to implement e.g. a loading bar). The callback receives * (necessary to implement e.g. a loading bar). The callback receives
* an {Object} with the properties: {number} loaded and {number} total. * an {Object} with the properties: {number} loaded and {number} total.
*/ */
this.onProgress = null; this.onProgress = null;
/** /**
* Callback to when unsupported feature is used. The callback receives * Callback to when unsupported feature is used. The callback receives
* an {UNSUPPORTED_FEATURES} argument. * an {UNSUPPORTED_FEATURES} argument.
*/ */
this.onUnsupportedFeature = null; this.onUnsupportedFeature = null;
} }
PDFDocumentLoadingTask.prototype =
/** @lends PDFDocumentLoadingTask.prototype */ {
/** /**
* @return {Promise} * @return {Promise}
*/ */
get promise() { get promise() {
return this._capability.promise; return this._capability.promise;
}, }
/** /**
* Aborts all network requests and destroys worker. * Aborts all network requests and destroys worker.
@ -486,7 +486,7 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
destroy() { destroy() {
this.destroyed = true; this.destroyed = true;
var transportDestroyed = !this._transport ? Promise.resolve() : const transportDestroyed = !this._transport ? Promise.resolve() :
this._transport.destroy(); this._transport.destroy();
return transportDestroyed.then(() => { return transportDestroyed.then(() => {
this._transport = null; this._transport = null;
@ -495,7 +495,7 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
this._worker = null; this._worker = null;
} }
}); });
}, }
/** /**
* Registers callbacks to indicate the document loading completion. * 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 * @return {Promise} A promise that is resolved after the onFulfilled or
* onRejected callback. * 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 this.promise.then.apply(this.promise, arguments);
}, }
}; }
return PDFDocumentLoadingTask; 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; let absoluteUrl = new URL(task.file, window.location).href;
try { try {
pdfjsLib.getDocument({ const loadingTask = pdfjsLib.getDocument({
url: absoluteUrl, url: absoluteUrl,
password: task.password, password: task.password,
nativeImageDecoderSupport: task.nativeImageDecoderSupport, nativeImageDecoderSupport: task.nativeImageDecoderSupport,
@ -367,7 +367,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
disableRange: task.disableRange, disableRange: task.disableRange,
disableAutoFetch: !task.enableAutoFetch, disableAutoFetch: !task.enableAutoFetch,
pdfBug: true, pdfBug: true,
}).then((doc) => { });
loadingTask.promise.then((doc) => {
task.pdfDoc = doc; task.pdfDoc = doc;
this._nextPage(task, failure); this._nextPage(task, failure);
}, (err) => { }, (err) => {