Merge pull request #14058 from Snuffleupagus/EventBus-data

[api-minor] Change `EventBus.dispatch` to only support *one* data-argument
This commit is contained in:
Tim van der Meij 2021-09-22 22:26:32 +02:00 committed by GitHub
commit 1bef4e596c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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