Merge pull request #11667 from Snuffleupagus/move-dispatchDOMEvent
Add a deprecation warning for the `eventBusDispatchToDOM` option/preference (PR 11631 follow-up)
This commit is contained in:
commit
001b0b270b
@ -716,7 +716,7 @@ function waitOnEventOrTimeout({ target, name, delay = 0 }) {
|
|||||||
|
|
||||||
function handler(type) {
|
function handler(type) {
|
||||||
if (target instanceof EventBus) {
|
if (target instanceof EventBus) {
|
||||||
target.off(name, eventHandler);
|
target._off(name, eventHandler);
|
||||||
} else {
|
} else {
|
||||||
target.removeEventListener(name, eventHandler);
|
target.removeEventListener(name, eventHandler);
|
||||||
}
|
}
|
||||||
@ -729,7 +729,7 @@ function waitOnEventOrTimeout({ target, name, delay = 0 }) {
|
|||||||
|
|
||||||
const eventHandler = handler.bind(null, WaitOnType.EVENT);
|
const eventHandler = handler.bind(null, WaitOnType.EVENT);
|
||||||
if (target instanceof EventBus) {
|
if (target instanceof EventBus) {
|
||||||
target.on(name, eventHandler);
|
target._on(name, eventHandler);
|
||||||
} else {
|
} else {
|
||||||
target.addEventListener(name, eventHandler);
|
target.addEventListener(name, eventHandler);
|
||||||
}
|
}
|
||||||
@ -756,6 +756,29 @@ const animationStarted = new Promise(function(resolve) {
|
|||||||
window.requestAnimationFrame(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`
|
* 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.
|
* and `off` methods. To raise an event, the `dispatch` method shall be used.
|
||||||
@ -764,6 +787,17 @@ class EventBus {
|
|||||||
constructor({ dispatchToDOM = false } = {}) {
|
constructor({ dispatchToDOM = false } = {}) {
|
||||||
this._listeners = Object.create(null);
|
this._listeners = Object.create(null);
|
||||||
this._dispatchToDOM = dispatchToDOM === true;
|
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."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -787,7 +821,7 @@ class EventBus {
|
|||||||
if (!eventListeners || eventListeners.length === 0) {
|
if (!eventListeners || eventListeners.length === 0) {
|
||||||
if (this._dispatchToDOM) {
|
if (this._dispatchToDOM) {
|
||||||
const args = Array.prototype.slice.call(arguments, 1);
|
const args = Array.prototype.slice.call(arguments, 1);
|
||||||
this._dispatchDOMEvent(eventName, args);
|
dispatchDOMEvent(eventName, args);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -815,7 +849,7 @@ class EventBus {
|
|||||||
externalListeners = null;
|
externalListeners = null;
|
||||||
}
|
}
|
||||||
if (this._dispatchToDOM) {
|
if (this._dispatchToDOM) {
|
||||||
this._dispatchDOMEvent(eventName, args);
|
dispatchDOMEvent(eventName, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -829,7 +863,7 @@ class EventBus {
|
|||||||
}
|
}
|
||||||
eventListeners.push({
|
eventListeners.push({
|
||||||
listener,
|
listener,
|
||||||
external: options ? options.external : false,
|
external: (options && options.external) === true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
let globalEventBus = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user