Re-factor the EventBus to allow servicing of "external" event listeners *after* the viewer components have updated

Since the goal has always been, essentially since the `EventBus` abstraction was added, to remove all dispatching of DOM events[1] from the viewer components this patch tries to address one thing that came up when updating the examples:
The DOM events are always dispatched last, and it's thus guaranteed that all internal event listeners have been invoked first.
However, there's no such guarantees with the general `EventBus` functionality and the order in which event listeners are invoked is *not* specified. With the promotion of the `EventBus` in the examples, over DOM events, it seems like a good idea to at least *try* to keep this ordering invariant[2] intact.

Obviously this won't prevent anyone from manually calling the new *internal* viewer component methods on the `EventBus`, but hopefully that won't be too common since any existing third-party code would obviously use the `on`/`off` methods and that all of the examples shows the *correct* usage (which should be similarily documented on the "Third party viewer usage" Wiki-page).

---
[1] Looking at the various Firefox-tests, I'm not sure that it'll be possible to (easily) re-write all of them to not rely on DOM events (since getting access to `PDFViewerApplication` might be generally difficult/messy depending on scopes).
In any case, even if technically feasible, it would most likely add *a lot* of complication that may not be desireable in the various Firefox-tests. All-in-all, I'd be fine with keeping the DOM events only for the `MOZCENTRAL` target and gated on `Cu.isInAutomation` (or similar) rather than a preference.

[2] I wouldn't expect any *real* bugs in a custom implementation, simply based on event ordering, but it nonetheless seem like a good idea if any "external" events are still handled last.
This commit is contained in:
Jonas Jenwald 2020-02-26 23:33:27 +01:00
parent 9a437a158f
commit 4a1b056c82
16 changed files with 168 additions and 124 deletions

View file

@ -69,17 +69,17 @@ class PDFHistory {
this._isViewerInPresentationMode = false;
// Ensure that we don't miss either a 'presentationmodechanged' or a
// 'pagesinit' event, by registering the listeners immediately.
this.eventBus.on("presentationmodechanged", evt => {
this.eventBus._on("presentationmodechanged", evt => {
this._isViewerInPresentationMode = evt.active || evt.switchInProgress;
});
this.eventBus.on("pagesinit", () => {
this.eventBus._on("pagesinit", () => {
this._isPagesLoaded = false;
const onPagesLoaded = evt => {
this.eventBus.off("pagesloaded", onPagesLoaded);
this.eventBus._off("pagesloaded", onPagesLoaded);
this._isPagesLoaded = !!evt.pagesCount;
};
this.eventBus.on("pagesloaded", onPagesLoaded);
this.eventBus._on("pagesloaded", onPagesLoaded);
});
}
@ -684,7 +684,7 @@ class PDFHistory {
pageHide: this._pageHide.bind(this),
};
this.eventBus.on("updateviewarea", this._boundEvents.updateViewarea);
this.eventBus._on("updateviewarea", this._boundEvents.updateViewarea);
window.addEventListener("popstate", this._boundEvents.popState);
window.addEventListener("pagehide", this._boundEvents.pageHide);
}
@ -696,7 +696,7 @@ class PDFHistory {
if (!this._boundEvents) {
return; // The event listeners were already removed.
}
this.eventBus.off("updateviewarea", this._boundEvents.updateViewarea);
this.eventBus._off("updateviewarea", this._boundEvents.updateViewarea);
window.removeEventListener("popstate", this._boundEvents.popState);
window.removeEventListener("pagehide", this._boundEvents.pageHide);