mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10:08 +02:00
Re-factor the EventBus
and isInAutomation
handling (PR 11655 follow-up)
Rather than forcing the "regular" `EventBus` to check and handle `isInAutomation` for every `dispatch` call, we can take advantage of subclassing instead. Hence this PR introduces a new `AutomationEventBus` class, which extends `EventBus`, and is used by the default viewer when `isInAutomation === true`.
This commit is contained in:
parent
7082ff9bf8
commit
0e92f995c9
3 changed files with 45 additions and 51 deletions
|
@ -708,43 +708,13 @@ const animationStarted = new Promise(function (resolve) {
|
|||
window.requestAnimationFrame(resolve);
|
||||
});
|
||||
|
||||
/**
|
||||
* NOTE: Only used to support various PDF viewer tests in `mozilla-central`.
|
||||
*/
|
||||
function dispatchDOMEvent(eventName, args = null) {
|
||||
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) {
|
||||
throw new Error("Not implemented: dispatchDOMEvent");
|
||||
}
|
||||
const details = Object.create(null);
|
||||
if (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`
|
||||
* and `off` methods. To raise an event, the `dispatch` method shall be used.
|
||||
*/
|
||||
class EventBus {
|
||||
constructor(options) {
|
||||
constructor() {
|
||||
this._listeners = Object.create(null);
|
||||
|
||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("MOZCENTRAL")) {
|
||||
this._isInAutomation = options?.isInAutomation === true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -774,13 +744,6 @@ class EventBus {
|
|||
dispatch(eventName) {
|
||||
const eventListeners = this._listeners[eventName];
|
||||
if (!eventListeners || eventListeners.length === 0) {
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("MOZCENTRAL")) &&
|
||||
this._isInAutomation
|
||||
) {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
dispatchDOMEvent(eventName, args);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Passing all arguments after the eventName to the listeners.
|
||||
|
@ -806,12 +769,6 @@ class EventBus {
|
|||
}
|
||||
externalListeners = null;
|
||||
}
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("MOZCENTRAL")) &&
|
||||
this._isInAutomation
|
||||
) {
|
||||
dispatchDOMEvent(eventName, args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -843,6 +800,36 @@ class EventBus {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: Only used to support various PDF viewer tests in `mozilla-central`.
|
||||
*/
|
||||
class AutomationEventBus extends EventBus {
|
||||
dispatch(eventName) {
|
||||
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) {
|
||||
throw new Error("Not implemented: AutomationEventBus.dispatch");
|
||||
}
|
||||
super.dispatch(...arguments);
|
||||
|
||||
const details = Object.create(null);
|
||||
if (arguments.length > 1) {
|
||||
const obj = arguments[1];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
function clamp(v, min, max) {
|
||||
return Math.min(Math.max(v, min), max);
|
||||
}
|
||||
|
@ -1012,6 +999,7 @@ export {
|
|||
apiPageLayoutToSpreadMode,
|
||||
apiPageModeToSidebarView,
|
||||
approximateFraction,
|
||||
AutomationEventBus,
|
||||
AutoPrintRegExp,
|
||||
backtrackBeforeAllVisibleElements, // only exported for testing
|
||||
binarySearchFirstItem,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue