Skip to content

Commit

Permalink
Utilize async/await in PDFViewerApplication.load to reduce the …
Browse files Browse the repository at this point in the history
…number of Promises and temporary variables necessary when setting the initial document location
  • Loading branch information
Snuffleupagus committed Aug 11, 2018
1 parent df631ee commit d3885a6
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,6 @@ let PDFViewerApplication = {
}
}

let initialParams = {
bookmark: null,
hash: null,
};
let storePromise = store.getMultiple({
page: null,
zoom: DEFAULT_SCALE_VALUE,
Expand All @@ -953,8 +949,10 @@ let PDFViewerApplication = {
spreadMode: null,
}).catch(() => { /* Unable to read from storage; ignoring errors. */ });

Promise.all([storePromise, pageModePromise]).then(
([values = {}, pageMode]) => {
Promise.all([
storePromise, pageModePromise,
]).then(async ([values = {}, pageMode]) => {
const initialBookmark = this.initialBookmark;
// Initialize the default values, from user preferences.
const zoom = AppOptions.get('defaultZoomValue');
let hash = zoom ? `zoom=${zoom}` : null;
Expand All @@ -977,51 +975,39 @@ let PDFViewerApplication = {
// Always let the user preference/history take precedence.
sidebarView = sidebarView || apiPageModeToSidebarView(pageMode);
}
return {
hash,
rotation,
sidebarView,
scrollMode,
spreadMode,
};
}).then(({ hash, rotation, sidebarView, scrollMode, spreadMode, }) => {
initialParams.bookmark = this.initialBookmark;
initialParams.hash = hash;

this.setInitialView(hash, {
rotation, sidebarView, scrollMode, spreadMode,
});

// Make all navigation keys work on document load,
// unless the viewer is embedded in a web page.
if (!this.isViewerEmbedded) {
pdfViewer.focus();
}

return Promise.race([
// For documents with different page sizes, once all pages are resolved,
// ensure that the correct location becomes visible on load.
// (To reduce the risk, in very large and/or slow loading documents,
// that the location changes *after* the user has started interacting
// with the viewer, wait for either `pagesPromise` or a timeout.)
await Promise.race([
pagesPromise,
new Promise((resolve) => {
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
}),
]);
}).then(() => {
// For documents with different page sizes, once all pages are resolved,
// ensure that the correct location becomes visible on load.
// To reduce the risk, in very large and/or slow loading documents,
// that the location changes *after* the user has started interacting
// with the viewer, wait for either `pagesPromise` or a timeout above.

if (!initialParams.bookmark && !initialParams.hash) {
if (!initialBookmark && !hash) {
return;
}
if (pdfViewer.hasEqualPageSizes) {
return;
}
this.initialBookmark = initialParams.bookmark;
this.initialBookmark = initialBookmark;

// eslint-disable-next-line no-self-assign
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
this.setInitialView(initialParams.hash);
// Re-apply the initial document location.
this.setInitialView(hash);
}).then(function() {
// At this point, rendering of the initial page(s) should always have
// started (and may even have completed).
Expand Down

0 comments on commit d3885a6

Please sign in to comment.