-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Factor out onButtonEvent method from controlers #3169
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
Conversation
5b1e264 to
a7b9510
Compare
ngokevin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a plan to eventually make this a registerController API? Otherwise it might eventually become hard to follow code if they all implement a tangle of utils.
src/utils/controls.js
Outdated
| @@ -0,0 +1,16 @@ | |||
| /** | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a tracked-controls utils where all the controllers share code already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense for me to separate tracked-controls from the higher level controls utils
|
|
||
| // Update colors. | ||
| color = evtName === 'up' ? this.data.buttonColor : this.data.buttonHighlightColor; | ||
| if (Array.isArray(buttonName)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need the array handler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think we need it. I don’t see it used anywhere. I believe we went ahead of ourselves and thought we would neef multiple events when a button is pressed. I can reintroduce but I couldn’t find where this code path is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that was originally there for buttons that were aliased to trigger more than one named event (e.g. 'menu' and 'app') but then we resolved the naming such that there were no duplicates. So it should not currently be used anywhere
|
I think is a bit premature to do a registerController API. There’s plenty of ad-hoc logic to deal with the specifics of each controller and I don’t feel ready for abstraction. Factoring out a few functions will clean up and make code testable. We can reevaluate an |
src/components/daydream-controls.js
Outdated
| @@ -1,5 +1,7 @@ | |||
| var registerComponent = require('../core/component').registerComponent; | |||
| var bind = require('../utils/bind'); | |||
| var controlsUtils = require('../utils/controls'); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have controlsUtils and controllerUtils, could be confusing
src/utils/controls.js
Outdated
| * @param {object} component - reference to the component | ||
| * @param {string} hand - handedness of the controller: left or right. | ||
| */ | ||
| module.exports.onButtonEvent = function (id, evtName, component, hand) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concerned about passing in component and having a utility call a bunch of the component's methods.
For example, we did this with checkControllerPresentAndSetup, I found it hard to follow. The utils depends on component.el, component.controllerPresent, component.addEventListeners, component.controllerEventsActive, component.injectTrackedControls, component.removeEventListeners. There's a huge amount of interface expected to be available.
Since we want to make available these shared methods and have the controllers implement this common interface, we might want to think early about a higher level API/prototype mixin early. Splitting out to a util makes it seem refactored, but the util and component are heavily coupled and not any more testable. It might be the wrong road to go down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that although the attempt to refactor into utils allowed code reuse, it made it nearly impossible to understand what things were doing without a pretty thorough look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're correct. It's essentially a mixin but passing the this as an argument rather than this.foo(). I want to stay focused on the task of improving controls. My goal is to easily be able to add thumbpadup, thumbstickup methods without having to copy code across controllers. There will be just three methods factored out from controllers. In a separate PR we can think of a mixin mechanism that consolidates checkControllerPresentAndSetup and these factored out methods in a more sustainable pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the utils improves the code quality of the controls since it makes things hard to understand, I'd favor copy and pasting code. Ideally, if we're refactoring, we might as well refactor in the right direction. We can merge something temporary if we agree on the mechanism and someone promises to do it soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps not agree on the specific code, but that we can have a mixin-style refactor instead soon after you are able to finish the thumbpad/stick work. Then mostly OK with something temporary, these methods are at least less coupled than the tracked-controls utils.
ngokevin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, we can go ahead. It's a sideways move, we can do a bigger refactor later.
a7b9510 to
58017c7
Compare
|
I consolidated the new methods in |
It factors out the
onButtonEventmethod common to all controls. Other methods can be also factored out likeonButtonChangedoronAxisMoved. I will do follow up PRs with those. I started withonButtonEventfor easier review