Refactor a number of methods in PDFViewerApplication to be async rather than manually returning Promises

*Ignoring whitespace changes is probably necessary, in order for the diff to be readable.*
This commit is contained in:
Jonas Jenwald 2018-07-30 17:41:39 +02:00
parent a60963f882
commit 3eba7ea267

View File

@ -133,23 +133,22 @@ let PDFViewerApplication = {
contentDispositionFilename: null,
// Called once when the document is loaded.
initialize(appConfig) {
async initialize(appConfig) {
this.preferences = this.externalServices.createPreferences();
this.appConfig = appConfig;
return this._readPreferences().then(() => {
return this._parseHashParameters();
}).then(() => {
return this._initializeL10n();
}).then(() => {
await this._readPreferences();
await this._parseHashParameters();
await this._initializeL10n();
if (this.isViewerEmbedded &&
AppOptions.get('externalLinkTarget') === LinkTarget.NONE) {
// Prevent external links from "replacing" the viewer,
// when it's embedded in e.g. an <iframe> or an <object>.
AppOptions.set('externalLinkTarget', LinkTarget.TOP);
}
return this._initializeViewerComponents();
}).then(() => {
await this._initializeViewerComponents();
// Bind the various event handlers *after* the viewer has been
// initialized, to prevent errors if an event arrives too soon.
this.bindEvents();
@ -164,13 +163,12 @@ let PDFViewerApplication = {
});
this.initialized = true;
});
},
/**
* @private
*/
_readPreferences() {
async _readPreferences() {
// A subset of the Preferences that `AppOptions`, for compatibility reasons,
// is allowed to override if the `AppOptions` values matches the ones below.
const OVERRIDES = {
@ -180,24 +178,27 @@ let PDFViewerApplication = {
textLayerMode: TextLayerMode.DISABLE,
};
return this.preferences.getAll().then(function(prefs) {
try {
const prefs = await this.preferences.getAll();
for (let name in prefs) {
if ((name in OVERRIDES) && AppOptions.get(name) === OVERRIDES[name]) {
continue;
}
AppOptions.set(name, prefs[name]);
}
}, function(reason) { });
} catch (reason) { }
},
/**
* @private
*/
_parseHashParameters() {
async _parseHashParameters() {
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('PRODUCTION') &&
!AppOptions.get('pdfBugEnabled')) {
return;
}
const waitOn = [];
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ||
AppOptions.get('pdfBugEnabled')) {
// Special debugging flags in the hash section of the URL.
let hash = document.location.hash.substring(1);
let hashParams = parseQueryString(hash);
@ -221,15 +222,13 @@ let PDFViewerApplication = {
hashParams['disablefontface'] === 'true');
}
if ('disablehistory' in hashParams) {
AppOptions.set('disableHistory',
hashParams['disablehistory'] === 'true');
AppOptions.set('disableHistory', hashParams['disablehistory'] === 'true');
}
if ('webgl' in hashParams) {
AppOptions.set('enableWebGL', hashParams['webgl'] === 'true');
}
if ('useonlycsszoom' in hashParams) {
AppOptions.set('useOnlyCssZoom',
hashParams['useonlycsszoom'] === 'true');
AppOptions.set('useOnlyCssZoom', hashParams['useonlycsszoom'] === 'true');
}
if ('verbosity' in hashParams) {
AppOptions.set('verbosity', hashParams['verbosity'] | 0);
@ -262,7 +261,6 @@ let PDFViewerApplication = {
PDFJSDev.test('!PRODUCTION || GENERIC')) && 'locale' in hashParams) {
AppOptions.set('locale', hashParams['locale']);
}
}
return Promise.all(waitOn);
},
@ -270,22 +268,20 @@ let PDFViewerApplication = {
/**
* @private
*/
_initializeL10n() {
async _initializeL10n() {
this.l10n = this.externalServices.createL10n({
locale: AppOptions.get('locale'),
});
return this.l10n.getDirection().then((dir) => {
const dir = await this.l10n.getDirection();
document.getElementsByTagName('html')[0].dir = dir;
});
},
/**
* @private
*/
_initializeViewerComponents() {
let { appConfig, } = this;
async _initializeViewerComponents() {
const appConfig = this.appConfig;
return new Promise((resolve, reject) => {
this.overlayManager = new OverlayManager();
const dispatchToDOM = AppOptions.get('eventBusDispatchToDOM');
@ -426,8 +422,6 @@ let PDFViewerApplication = {
this.pdfSidebarResizer = new PDFSidebarResizer(appConfig.sidebarResizer,
eventBus, this.l10n);
resolve(undefined);
});
},
run(config) {
@ -582,12 +576,12 @@ let PDFViewerApplication = {
* @returns {Promise} - Returns the promise, which is resolved when all
* destruction is completed.
*/
close() {
async close() {
let errorWrapper = this.appConfig.errorWrapper.container;
errorWrapper.setAttribute('hidden', 'true');
if (!this.pdfLoadingTask) {
return Promise.resolve();
return;
}
let promise = this.pdfLoadingTask.destroy();
@ -632,13 +626,10 @@ let PDFViewerApplication = {
* @returns {Promise} - Returns the promise, which is resolved when document
* is opened.
*/
open(file, args) {
async open(file, args) {
if (this.pdfLoadingTask) {
// We need to destroy already opened document.
return this.close().then(() => {
// ... and repeat the open() call.
return this.open(file, args);
});
await this.close();
}
// Set the necessary global worker parameters, using the available options.
const workerParameters = AppOptions.getAll('worker');
@ -1629,14 +1620,14 @@ function webViewerInitialized() {
PDFViewerApplication.pdfSidebar.toggle();
});
Promise.resolve().then(function() {
try {
webViewerOpenFileViaURL(file);
}).catch(function(reason) {
} catch (reason) {
PDFViewerApplication.l10n.get('loading_error', null,
'An error occurred while loading the PDF.').then((msg) => {
PDFViewerApplication.error(msg, reason);
});
});
}
}
let webViewerOpenFileViaURL;