Remove the PDFPageView.stats property, and fetch it manually only when debugging is enabled

Given that the default viewer only uses the "page stats" when debugging is enabled, it seems much simpler and more straightforward to simply query the API *directly* when this information is actually required. That way, there's a bit less information that needs to be stored/updated on each `PDFPageView`-instance.

Finally, since the `EventBus` now exists, we no longer need to handle the "page stats"-case in the regular listeners in `web/app.js`, but can instead add special "page stats"-listeners only when debugging is enabled.
This commit is contained in:
Jonas Jenwald 2020-09-23 14:39:14 +02:00
parent 9efc1784b2
commit e46055a92c
2 changed files with 28 additions and 15 deletions

View File

@ -1775,6 +1775,13 @@ const PDFViewerApplication = {
eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
if (AppOptions.get("pdfBug")) {
_boundEvents.reportPageStatsPDFBug = reportPageStatsPDFBug;
eventBus._on("pagerendered", _boundEvents.reportPageStatsPDFBug);
eventBus._on("pagechanging", _boundEvents.reportPageStatsPDFBug);
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._on("fileinputchange", webViewerFileInputChange);
eventBus._on("openfile", webViewerOpenFile);
@ -1855,6 +1862,13 @@ const PDFViewerApplication = {
eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
if (_boundEvents.reportPageStatsPDFBug) {
eventBus._off("pagerendered", _boundEvents.reportPageStatsPDFBug);
eventBus._off("pagechanging", _boundEvents.reportPageStatsPDFBug);
_boundEvents.reportPageStatsPDFBug = null;
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._off("fileinputchange", webViewerFileInputChange);
eventBus._off("openfile", webViewerOpenFile);
@ -1966,6 +1980,20 @@ function loadAndEnablePDFBug(enabledTabs) {
});
}
function reportPageStatsPDFBug({ pageNumber }) {
if (typeof Stats === "undefined" || !Stats.enabled) {
return;
}
const pageView = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
const pageStats = pageView && pageView.pdfPage && pageView.pdfPage.stats;
if (!pageStats) {
return;
}
Stats.add(pageNumber, pageStats);
}
function webViewerInitialized() {
const appConfig = PDFViewerApplication.appConfig;
let file;
@ -2150,10 +2178,6 @@ function webViewerPageRendered({ pageNumber, timestamp, error }) {
thumbnailView.setImage(pageView);
}
if (typeof Stats !== "undefined" && Stats.enabled && pageView.stats) {
Stats.add(pageNumber, pageView.stats);
}
if (error) {
PDFViewerApplication.l10n
.get(
@ -2534,14 +2558,6 @@ function webViewerPageChanging(evt) {
if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(page);
}
// We need to update stats.
if (typeof Stats !== "undefined" && Stats.enabled) {
const pageView = PDFViewerApplication.pdfViewer.getPageView(page - 1);
if (pageView && pageView.stats) {
Stats.add(page, pageView.stats);
}
}
}
function webViewerVisibilityChange(evt) {

View File

@ -139,7 +139,6 @@ class PDFPageView {
scale: this.scale * CSS_UNITS,
rotation: totalRotation,
});
this.stats = pdfPage.stats;
this.reset();
}
@ -524,8 +523,6 @@ class PDFPageView {
}
this._resetZoomLayer(/* removeFromDOM = */ true);
this.stats = pdfPage.stats;
this.eventBus.dispatch("pagerendered", {
source: this,
pageNumber: this.id,