Commit bdc2552a authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Use EventHandler to look for nodes

Code clean-up. Does not change the behavior of Switch Access.

AX-Relnotes: n/a.
Bug: None.
Change-Id: I49b5eefd69c9165f9729927f85779b8bdacfc224
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2373290Reviewed-by: default avatarAbigail Klein <abigailbklein@google.com>
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Cr-Commit-Position: refs/heads/master@{#801612}
parent 408ea211
......@@ -12,7 +12,7 @@ class EventHandler {
* !Array<!chrome.automation.AutomationNode>} nodes
* @param {!chrome.automation.EventType |
* !Array<!chrome.automation.EventType>} types
* @param {!function(!chrome.automation.AutomationEvent)} callback
* @param {?function(!chrome.automation.AutomationEvent)} callback
* @param {{capture: (boolean|undefined), exactMatch: (boolean|undefined),
* listenOnce: (boolean|undefined), predicate:
* ((function(chrome.automation.AutomationEvent): boolean)|undefined)}}
......@@ -32,7 +32,7 @@ class EventHandler {
/** @private {!Array<!chrome.automation.EventType>} */
this.types_ = types instanceof Array ? types : [types];
/** @private {!function(!chrome.automation.AutomationEvent)} */
/** @private {?function(!chrome.automation.AutomationEvent)} */
this.callback_ = callback;
/** @private {boolean} */
......@@ -81,6 +81,11 @@ class EventHandler {
this.listening_ = false;
}
/** @param {?function(!chrome.automation.AutomationEvent)} callback */
setCallback(callback) {
this.callback_ = callback;
}
/**
* Changes what nodes are being listened to. Removes listeners from existing
* nodes before adding listeners on new nodes.
......@@ -138,6 +143,8 @@ class EventHandler {
this.stop();
}
this.callback_(event);
if (this.callback_) {
this.callback_(event);
}
}
}
......@@ -63,32 +63,27 @@ class SwitchAccess {
}
// If it's not currently in the tree, listen for changes to the desktop
// tree.
const onDesktopChildrenChanged = (event) => {
const eventHandler = new EventHandler(
desktop, chrome.automation.EventType.CHILDREN_CHANGED,
null /** callback */);
const onEvent = (event) => {
if (event.target.matches(findParams)) {
// If the event target is the node we're looking for, we've found it.
desktop.removeEventListener(
chrome.automation.EventType.CHILDREN_CHANGED,
onDesktopChildrenChanged, false);
eventHandler.stop();
foundCallback(event.target);
} else if (event.target.children.length > 0) {
// Otherwise, see if one of its children is the node we're looking for.
node = event.target.find(findParams);
if (node) {
desktop.removeEventListener(
chrome.automation.EventType.CHILDREN_CHANGED,
onDesktopChildrenChanged, false);
eventHandler.stop();
foundCallback(node);
}
}
};
// Note: Cannot use an EventHandler here because in some cases we want
// to run foundCallback on the event.target, and in some cases we want to
// run foundCallback on one of the target's children. We would need to
// recalculate the interesting child twice to use EventHandler.
desktop.addEventListener(
chrome.automation.EventType.CHILDREN_CHANGED, onDesktopChildrenChanged,
false);
eventHandler.setCallback(onEvent);
eventHandler.start();
}
/*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment