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