From af748050c07ed09fea8f75de4ef6823c4fafa63d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 22 Sep 2021 12:11:47 +0200 Subject: [PATCH] [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. --- web/ui_utils.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/web/ui_utils.js b/web/ui_utils.js index f8a8037e1..3deacb513 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -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. @@ -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; } @@ -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.