[api-minor] Change EventBus.dispatch to only support *one* data-argument

This is consistent with how the `EventBus` has *always* been used internally in the viewer, and allows a slight simplification of the relevant code.
This commit is contained in:
Jonas Jenwald 2021-09-22 12:11:47 +02:00
parent 6381158855
commit af748050c0

View File

@ -742,13 +742,15 @@ class EventBus {
}); });
} }
dispatch(eventName) { /**
* @param {string} eventName
* @param {Object} data
*/
dispatch(eventName, data) {
const eventListeners = this._listeners[eventName]; const eventListeners = this._listeners[eventName];
if (!eventListeners || eventListeners.length === 0) { if (!eventListeners || eventListeners.length === 0) {
return; return;
} }
// Passing all arguments after the eventName to the listeners.
const args = Array.prototype.slice.call(arguments, 1);
let externalListeners; let externalListeners;
// Making copy of the listeners array in case if it will be modified // Making copy of the listeners array in case if it will be modified
// during dispatch. // during dispatch.
@ -760,13 +762,13 @@ class EventBus {
(externalListeners ||= []).push(listener); (externalListeners ||= []).push(listener);
continue; continue;
} }
listener.apply(null, args); listener(data);
} }
// Dispatch any "external" listeners *after* the internal ones, to give the // Dispatch any "external" listeners *after* the internal ones, to give the
// viewer components time to handle events and update their state first. // viewer components time to handle events and update their state first.
if (externalListeners) { if (externalListeners) {
for (const listener of externalListeners) { for (const listener of externalListeners) {
listener.apply(null, args); listener(data);
} }
externalListeners = null; externalListeners = null;
} }
@ -805,17 +807,16 @@ class EventBus {
* NOTE: Only used to support various PDF viewer tests in `mozilla-central`. * NOTE: Only used to support various PDF viewer tests in `mozilla-central`.
*/ */
class AutomationEventBus extends EventBus { class AutomationEventBus extends EventBus {
dispatch(eventName) { dispatch(eventName, data) {
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: AutomationEventBus.dispatch"); throw new Error("Not implemented: AutomationEventBus.dispatch");
} }
super.dispatch(...arguments); super.dispatch(eventName, data);
const details = Object.create(null); const details = Object.create(null);
if (arguments.length > 1) { if (data) {
const obj = arguments[1]; for (const key in data) {
for (const key in obj) { const value = data[key];
const value = obj[key];
if (key === "source") { if (key === "source") {
if (value === window || value === document) { if (value === window || value === document) {
return; // No need to re-dispatch (already) global events. return; // No need to re-dispatch (already) global events.