Skip to content

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

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

Merged
merged 1 commit into from
Sep 22, 2021
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
23 changes: 12 additions & 11 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,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.
Expand All @@ -760,13 +762,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;
}
Expand Down Expand Up @@ -805,17 +807,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.
Expand Down