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 { ...@@ -12,7 +12,7 @@ class EventHandler {
* !Array<!chrome.automation.AutomationNode>} nodes * !Array<!chrome.automation.AutomationNode>} nodes
* @param {!chrome.automation.EventType | * @param {!chrome.automation.EventType |
* !Array<!chrome.automation.EventType>} types * !Array<!chrome.automation.EventType>} types
* @param {!function(!chrome.automation.AutomationEvent)} callback * @param {?function(!chrome.automation.AutomationEvent)} callback
* @param {{capture: (boolean|undefined), exactMatch: (boolean|undefined), * @param {{capture: (boolean|undefined), exactMatch: (boolean|undefined),
* listenOnce: (boolean|undefined), predicate: * listenOnce: (boolean|undefined), predicate:
* ((function(chrome.automation.AutomationEvent): boolean)|undefined)}} * ((function(chrome.automation.AutomationEvent): boolean)|undefined)}}
...@@ -32,7 +32,7 @@ class EventHandler { ...@@ -32,7 +32,7 @@ class EventHandler {
/** @private {!Array<!chrome.automation.EventType>} */ /** @private {!Array<!chrome.automation.EventType>} */
this.types_ = types instanceof Array ? types : [types]; this.types_ = types instanceof Array ? types : [types];
/** @private {!function(!chrome.automation.AutomationEvent)} */ /** @private {?function(!chrome.automation.AutomationEvent)} */
this.callback_ = callback; this.callback_ = callback;
/** @private {boolean} */ /** @private {boolean} */
...@@ -81,6 +81,11 @@ class EventHandler { ...@@ -81,6 +81,11 @@ class EventHandler {
this.listening_ = false; 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 * Changes what nodes are being listened to. Removes listeners from existing
* nodes before adding listeners on new nodes. * nodes before adding listeners on new nodes.
...@@ -138,6 +143,8 @@ class EventHandler { ...@@ -138,6 +143,8 @@ class EventHandler {
this.stop(); this.stop();
} }
if (this.callback_) {
this.callback_(event); this.callback_(event);
} }
}
} }
...@@ -63,32 +63,27 @@ class SwitchAccess { ...@@ -63,32 +63,27 @@ class SwitchAccess {
} }
// If it's not currently in the tree, listen for changes to the desktop // If it's not currently in the tree, listen for changes to the desktop
// tree. // 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 (event.target.matches(findParams)) {
// If the event target is the node we're looking for, we've found it. // If the event target is the node we're looking for, we've found it.
desktop.removeEventListener( eventHandler.stop();
chrome.automation.EventType.CHILDREN_CHANGED,
onDesktopChildrenChanged, false);
foundCallback(event.target); foundCallback(event.target);
} else if (event.target.children.length > 0) { } else if (event.target.children.length > 0) {
// Otherwise, see if one of its children is the node we're looking for. // Otherwise, see if one of its children is the node we're looking for.
node = event.target.find(findParams); node = event.target.find(findParams);
if (node) { if (node) {
desktop.removeEventListener( eventHandler.stop();
chrome.automation.EventType.CHILDREN_CHANGED,
onDesktopChildrenChanged, false);
foundCallback(node); foundCallback(node);
} }
} }
}; };
// Note: Cannot use an EventHandler here because in some cases we want eventHandler.setCallback(onEvent);
// to run foundCallback on the event.target, and in some cases we want to eventHandler.start();
// 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);
} }
/* /*
......
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