Skip to content

Commit 44a6e0c

Browse files
committed
Add a PDFViewerApplication.initializedPromise property to allow (easier) tracking of when the default viewer has been initialized
This complements the existing `PDFViewerApplication.initialized` boolean property, and may be helpful for custom implementations of the default viewer. This will thus provide users of the default viewer an alternative to setting the preference to dispatch events to the DOM (and listen for the "localized" event), since they can instead use: ```javascript document.addEventListener("webviewerloaded", function() { PDFViewerApplication.initializedPromise.then(function() { // The viewer has now been initialized. }) }); ``` Note that in order to avoid manually tracking the initialization state *twice*, this implementation purposely uses the `PromiseCapability` functionality to handle both `PDFViewerApplication.initialized` and `PDFViewerApplication.initializedPromise` internally.
1 parent 64351ca commit 44a6e0c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

web/app.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
import { AppOptions, OptionKind } from "./app_options.js";
3939
import {
4040
build,
41+
createPromiseCapability,
4142
getDocument,
4243
getFilenameFromUrl,
4344
GlobalWorkerOptions,
@@ -128,7 +129,7 @@ class DefaultExternalServices {
128129

129130
const PDFViewerApplication = {
130131
initialBookmark: document.location.hash.substring(1),
131-
initialized: false,
132+
_initializedCapability: createPromiseCapability(),
132133
fellback: false,
133134
appConfig: null,
134135
pdfDocument: null,
@@ -215,7 +216,7 @@ const PDFViewerApplication = {
215216
this.eventBus.dispatch("localized", { source: this });
216217
});
217218

218-
this.initialized = true;
219+
this._initializedCapability.resolve();
219220
},
220221

221222
/**
@@ -476,6 +477,14 @@ const PDFViewerApplication = {
476477
this.initialize(config).then(webViewerInitialized);
477478
},
478479

480+
get initialized() {
481+
return this._initializedCapability.settled;
482+
},
483+
484+
get initializedPromise() {
485+
return this._initializedCapability.promise;
486+
},
487+
479488
zoomIn(ticks) {
480489
if (this.pdfViewer.isInPresentationMode) {
481490
return;

0 commit comments

Comments
 (0)