Skip to content

Commit

Permalink
Merge pull request #18061 from Snuffleupagus/api-report-Stats
Browse files Browse the repository at this point in the history
Slightly re-factor how the viewer initializes debug-only functionality
  • Loading branch information
timvandermeij committed May 13, 2024
2 parents df23679 + 5fbc5ba commit 0347e59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
10 changes: 8 additions & 2 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1510,8 +1510,14 @@ class PDFPageProxy {
internalRenderTask.capability.resolve();
}

this._stats?.timeEnd("Rendering");
this._stats?.timeEnd("Overall");
if (this._stats) {
this._stats.timeEnd("Rendering");
this._stats.timeEnd("Overall");

if (globalThis.Stats?.enabled) {
globalThis.Stats.add(this.pageNumber, this._stats);
}
}
};

const internalRenderTask = new InternalRenderTask({
Expand Down
60 changes: 22 additions & 38 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,27 @@ const PDFViewerApplication = {
const { mainContainer, viewerContainer } = this.appConfig,
params = parseQueryString(hash);

const loadPDFBug = async () => {
if (this._PDFBug) {
return;
}
const { PDFBug } =
typeof PDFJSDev === "undefined"
? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method
: await __non_webpack_import__(AppOptions.get("debuggerSrc"));

this._PDFBug = PDFBug;
};

if (params.get("disableworker") === "true") {
try {
await loadFakeWorker();
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");

if (typeof PDFJSDev === "undefined") {
globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js");
} else {
await __non_webpack_import__(PDFWorker.workerSrc);
}
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
}
Expand Down Expand Up @@ -306,7 +324,7 @@ const PDFViewerApplication = {
case "hover":
viewerContainer.classList.add(`textLayer-${params.get("textlayer")}`);
try {
await loadPDFBug(this);
await loadPDFBug();
this._PDFBug.loadCSS();
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
Expand All @@ -315,12 +333,11 @@ const PDFViewerApplication = {
}
}
if (params.has("pdfbug")) {
AppOptions.set("pdfBug", true);
AppOptions.set("fontExtraProperties", true);
AppOptions.setAll({ pdfBug: true, fontExtraProperties: true });

const enabled = params.get("pdfbug").split(",");
try {
await loadPDFBug(this);
await loadPDFBug();
this._PDFBug.init(mainContainer, enabled);
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
Expand Down Expand Up @@ -1898,10 +1915,6 @@ const PDFViewerApplication = {
signal,
});

if (AppOptions.get("pdfBug")) {
eventBus._on("pagerendered", reportPageStatsPDFBug, { signal });
eventBus._on("pagechanging", reportPageStatsPDFBug, { signal });
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._on("fileinputchange", webViewerFileInputChange, { signal });
eventBus._on("openfile", webViewerOpenFile, { signal });
Expand Down Expand Up @@ -2168,35 +2181,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
};
}

async function loadFakeWorker() {
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");

if (typeof PDFJSDev === "undefined") {
globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js");
return;
}
await __non_webpack_import__(PDFWorker.workerSrc);
}

async function loadPDFBug(self) {
const { PDFBug } =
typeof PDFJSDev === "undefined"
? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method
: await __non_webpack_import__(AppOptions.get("debuggerSrc"));

self._PDFBug = PDFBug;
}

function reportPageStatsPDFBug({ pageNumber }) {
if (!globalThis.Stats?.enabled) {
return;
}
const pageView = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats);
}

function webViewerPageRender({ pageNumber }) {
// If the page is (the most) visible when it starts rendering,
// ensure that the page number input loading indicator is displayed.
Expand Down

0 comments on commit 0347e59

Please sign in to comment.