Re-factor PDFViewerApplication.load such that {PDFViewer, PDFThumbnailViewer}.setDocument happens slightly earlier

The `BaseViewer.setDocument` method in particular is necessary for rendering to start when the viewer loads, hence you obviously want that to happen as soon as possible and without any unnecessary delays.
Unfortunately *some* API calls need to be done before that, note existing comments, however the `ViewHistory` initialization (and subsequent fetching of data) in particular can be moved slightly without any adverse effects.

As part of testing I've used logging with `performance.now()` inserted in various parts of this code, and there's *obviously* no discernible changes between `master` and this patch for e.g. rendering starting in the viewer.

*Note:* The vast majority of this patch is simple indentation changes, which were forced by Prettier (and done automatically with `gulp lint --fix`).
This commit is contained in:
Jonas Jenwald 2020-03-21 14:37:03 +01:00
parent 08f9718a37
commit 9f70bca12c

View File

@ -1040,8 +1040,6 @@ const PDFViewerApplication = {
this.toolbar.setPagesCount(pdfDocument.numPages, false); this.toolbar.setPagesCount(pdfDocument.numPages, false);
this.secondaryToolbar.setPagesCount(pdfDocument.numPages); this.secondaryToolbar.setPagesCount(pdfDocument.numPages);
const store = (this.store = new ViewHistory(pdfDocument.fingerprint));
let baseDocumentUrl; let baseDocumentUrl;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
baseDocumentUrl = null; baseDocumentUrl = null;
@ -1060,10 +1058,9 @@ const PDFViewerApplication = {
const pdfThumbnailViewer = this.pdfThumbnailViewer; const pdfThumbnailViewer = this.pdfThumbnailViewer;
pdfThumbnailViewer.setDocument(pdfDocument); pdfThumbnailViewer.setDocument(pdfDocument);
firstPagePromise.then(pdfPage => { const storedPromise = (this.store = new ViewHistory(
this.loadingBar.setWidth(this.appConfig.viewerContainer); pdfDocument.fingerprint
))
const storePromise = store
.getMultiple({ .getMultiple({
page: null, page: null,
zoom: DEFAULT_SCALE_VALUE, zoom: DEFAULT_SCALE_VALUE,
@ -1076,23 +1073,20 @@ const PDFViewerApplication = {
}) })
.catch(() => { .catch(() => {
/* Unable to read from storage; ignoring errors. */ /* Unable to read from storage; ignoring errors. */
return Object.create(null);
}); });
firstPagePromise.then(pdfPage => {
this.loadingBar.setWidth(this.appConfig.viewerContainer);
Promise.all([ Promise.all([
animationStarted, animationStarted,
storePromise, storedPromise,
pageLayoutPromise, pageLayoutPromise,
pageModePromise, pageModePromise,
openActionPromise, openActionPromise,
]) ])
.then( .then(async ([timeStamp, stored, pageLayout, pageMode, openAction]) => {
async ([
timeStamp,
values = {},
pageLayout,
pageMode,
openAction,
]) => {
const viewOnLoad = AppOptions.get("viewOnLoad"); const viewOnLoad = AppOptions.get("viewOnLoad");
this._initializePdfHistory({ this._initializePdfHistory({
@ -1111,22 +1105,21 @@ const PDFViewerApplication = {
let scrollMode = AppOptions.get("scrollModeOnLoad"); let scrollMode = AppOptions.get("scrollModeOnLoad");
let spreadMode = AppOptions.get("spreadModeOnLoad"); let spreadMode = AppOptions.get("spreadModeOnLoad");
if (values.page && viewOnLoad !== ViewOnLoad.INITIAL) { if (stored.page && viewOnLoad !== ViewOnLoad.INITIAL) {
hash = hash =
`page=${values.page}&zoom=${zoom || values.zoom},` + `page=${stored.page}&zoom=${zoom || stored.zoom},` +
`${values.scrollLeft},${values.scrollTop}`; `${stored.scrollLeft},${stored.scrollTop}`;
rotation = parseInt(values.rotation, 10); rotation = parseInt(stored.rotation, 10);
// Always let user preferences take precedence over the view // Always let user preference take precedence over the view history.
// history.
if (sidebarView === SidebarView.UNKNOWN) { if (sidebarView === SidebarView.UNKNOWN) {
sidebarView = values.sidebarView | 0; sidebarView = stored.sidebarView | 0;
} }
if (scrollMode === ScrollMode.UNKNOWN) { if (scrollMode === ScrollMode.UNKNOWN) {
scrollMode = values.scrollMode | 0; scrollMode = stored.scrollMode | 0;
} }
if (spreadMode === SpreadMode.UNKNOWN) { if (spreadMode === SpreadMode.UNKNOWN) {
spreadMode = values.spreadMode | 0; spreadMode = stored.spreadMode | 0;
} }
} }
// Always let the user preference/view history take precedence. // Always let the user preference/view history take precedence.
@ -1151,12 +1144,10 @@ const PDFViewerApplication = {
} }
// For documents with different page sizes, once all pages are // For documents with different page sizes, once all pages are
// resolved, ensure that the correct location becomes visible on // resolved, ensure that the correct location becomes visible on load.
// load.
// (To reduce the risk, in very large and/or slow loading documents, // (To reduce the risk, in very large and/or slow loading documents,
// that the location changes *after* the user has started // that the location changes *after* the user has started interacting
// interacting with the viewer, wait for either `pagesPromise` or // with the viewer, wait for either `pagesPromise` or a timeout.)
// a timeout.)
await Promise.race([ await Promise.race([
pagesPromise, pagesPromise,
new Promise(resolve => { new Promise(resolve => {
@ -1175,8 +1166,7 @@ const PDFViewerApplication = {
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue; pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
// Re-apply the initial document location. // Re-apply the initial document location.
this.setInitialView(hash); this.setInitialView(hash);
} })
)
.catch(() => { .catch(() => {
// Ensure that the document is always completely initialized, // Ensure that the document is always completely initialized,
// even if there are any errors thrown above. // even if there are any errors thrown above.