Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a deprecation warning for the eventBusDispatchToDOM option/preference (PR 11631 follow-up) #11667

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function waitOnEventOrTimeout({ target, name, delay = 0 }) {

function handler(type) {
if (target instanceof EventBus) {
target.off(name, eventHandler);
target._off(name, eventHandler);
} else {
target.removeEventListener(name, eventHandler);
}
Expand All @@ -729,7 +729,7 @@ function waitOnEventOrTimeout({ target, name, delay = 0 }) {

const eventHandler = handler.bind(null, WaitOnType.EVENT);
if (target instanceof EventBus) {
target.on(name, eventHandler);
target._on(name, eventHandler);
} else {
target.addEventListener(name, eventHandler);
}
Expand All @@ -756,6 +756,29 @@ 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) {
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 All @@ -764,6 +787,17 @@ class EventBus {
constructor({ dispatchToDOM = false } = {}) {
this._listeners = Object.create(null);
this._dispatchToDOM = dispatchToDOM === true;

if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("MOZCENTRAL || TESTING") &&
dispatchToDOM
) {
console.error(
"The `eventBusDispatchToDOM` option/preference is deprecated, " +
"add event listeners to the EventBus instance rather than the DOM."
);
}
}

/**
Expand All @@ -787,7 +821,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 +849,7 @@ class EventBus {
externalListeners = null;
}
if (this._dispatchToDOM) {
this._dispatchDOMEvent(eventName, args);
dispatchDOMEvent(eventName, args);
}
}

Expand All @@ -829,7 +863,7 @@ class EventBus {
}
eventListeners.push({
listener,
external: options ? options.external : false,
external: (options && options.external) === true,
});
}

Expand All @@ -848,29 +882,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