Skip to content

Commit

Permalink
Move the dispatchDOMEvent functionality out from the EventBus and…
Browse files Browse the repository at this point in the history
… add a deprecation warning for the `eventBusDispatchToDOM` option/preference (PR 11631 follow-up)

It occured to me that similar to the `getGlobalEventBus` function, it's probably a good idea to *also* notify users of the fact that `eventBusDispatchToDOM` is now deprecated.

Rather than depending of the re-dispatching of internal events to the DOM, the default viewer can instead be used in e.g. the following way:
```javascript
document.addEventListener("webviewerloaded", function() {
  PDFViewerApplication.initializedPromise.then(function() {
    // The viewer has now been initialized, and its properties can be accessed.

    PDFViewerApplication.eventBus.on("pagerendered", function(event) {
      console.log("Has rendered page number: " + event.pageNumber);
    });
  });
});
```
  • Loading branch information
Snuffleupagus committed Mar 5, 2020
1 parent 3ed1bc9 commit 3e582ee
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,38 @@ const animationStarted = new Promise(function(resolve) {
window.requestAnimationFrame(resolve);
});

/**
* NOTE: Only used to support various PDF viewer tests in `mozilla-central`.
*/
function dispatchDOMEvent(eventName, args = null) {
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("MOZCENTRAL || TESTING")
) {
console.error(
"The `eventBusDispatchToDOM` option/preference is deprecated, " +
"use a manually created EventBus instance instead."
);
}
const details = Object.create(null);
if (args && args.length > 0) {
const obj = args[0];
for (const key in obj) {
const value = obj[key];
if (key === "source") {
if (value === window || value === document) {
return; // No need to re-dispatch (already) global events.
}
continue; // Ignore the `source` property.
}
details[key] = value;
}
}
const event = document.createEvent("CustomEvent");
event.initCustomEvent(eventName, true, true, details);
document.dispatchEvent(event);
}

/**
* Simple event bus for an application. Listeners are attached using the `on`
* and `off` methods. To raise an event, the `dispatch` method shall be used.
Expand Down Expand Up @@ -787,7 +819,7 @@ class EventBus {
if (!eventListeners || eventListeners.length === 0) {
if (this._dispatchToDOM) {
const args = Array.prototype.slice.call(arguments, 1);
this._dispatchDOMEvent(eventName, args);
dispatchDOMEvent(eventName, args);
}
return;
}
Expand Down Expand Up @@ -815,7 +847,7 @@ class EventBus {
externalListeners = null;
}
if (this._dispatchToDOM) {
this._dispatchDOMEvent(eventName, args);
dispatchDOMEvent(eventName, args);
}
}

Expand Down Expand Up @@ -848,29 +880,6 @@ class EventBus {
}
}
}

/**
* @private
*/
_dispatchDOMEvent(eventName, args = null) {
const details = Object.create(null);
if (args && args.length > 0) {
const obj = args[0];
for (const key in obj) {
const value = obj[key];
if (key === "source") {
if (value === window || value === document) {
return; // No need to re-dispatch (already) global events.
}
continue; // Ignore the `source` property.
}
details[key] = value;
}
}
const event = document.createEvent("CustomEvent");
event.initCustomEvent(eventName, true, true, details);
document.dispatchEvent(event);
}
}

let globalEventBus = null;
Expand Down

0 comments on commit 3e582ee

Please sign in to comment.