phetsims / axon

Axon provides powerful and concise models for interactive simulations, based on observable Properties and related patterns.
MIT License
11 stars 8 forks source link

Avoid duplication in DerivedProperty boilerplate and make sure all dependencies registered #441

Closed samreid closed 3 months ago

samreid commented 1 year ago

While reviewing https://github.com/phetsims/center-and-variability/issues/433, I saw that each property in a DerivedProperty is listed 3x times:

    this.isKeyboardSelectArrowVisibleProperty = new DerivedProperty( [ this.focusedSoccerBallProperty,
        this.isSoccerBallKeyboardGrabbedProperty, this.isKeyboardFocusedProperty, this.hasKeyboardSelectedDifferentBallProperty ],
      ( focusedSoccerBall, isSoccerBallGrabbed, isKeyboardFocused, hasKeyboardSelectedDifferentBall ) => {
        return focusedSoccerBall !== null && !isSoccerBallGrabbed && isKeyboardFocused && !hasKeyboardSelectedDifferentBall;
      } );

There is also the opportunity to forget to register certain properties in the listeners. I have seen code like this (someOtherProperty) not registered as a dependency:

    this.isKeyboardSelectArrowVisibleProperty = new DerivedProperty( [ this.focusedSoccerBallProperty,
        this.isSoccerBallKeyboardGrabbedProperty, this.isKeyboardFocusedProperty, this.hasKeyboardSelectedDifferentBallProperty ],
      ( focusedSoccerBall, isSoccerBallGrabbed, isKeyboardFocused, hasKeyboardSelectedDifferentBall ) => {
        return  someOtherProperty.value && focusedSoccerBall !== null && !isSoccerBallGrabbed && isKeyboardFocused && !hasKeyboardSelectedDifferentBall ;
      } );
pixelzoom commented 8 months ago

@zepumph Asked a great question at dev meeting today: What are we planning to do about the cases where we've opted out of dependency checking using strictAxonDependencies: false. At the time, I thought we only had a handful of these, as listed in https://github.com/phetsims/axon/issues/441#issuecomment-1898593809. Then I discovered that we actually have 47 occurrences, most of which were added by @samreid during development.

I just spent 1+ hour creating repo-specific GitHub issues for those 47 occurrences -- they are linked immediately above.

And now I'm hitting a problem in scenery, related to boundsProperty, over which I have no control -- see https://github.com/phetsims/faradays-electromagnetic-lab/issues/57. And I'm expecting that I'll easily burn another couple of hours on this.

So... While this has identified some legitimate bugs, and seem especially important for dynamic strings, I'm having second thoughts about this feature. And I'm starting to feel like I don't want to spend more time on it.

@samreid thoughts?

samreid commented 8 months ago

I'm not planning to work on this until I meet up with @pixelzoom. I reached out on slack.

jonathanolson commented 8 months ago

Ran into an issue in https://github.com/phetsims/scenery/issues/1600.

samreid commented 8 months ago

Regarding https://github.com/phetsims/axon/issues/441#issuecomment-1899494717, I wrote a proposal to skip strict axon dependency testing if an alternate listener order is specified. Those notes are in https://github.com/phetsims/faradays-electromagnetic-lab/issues/57#issuecomment-1909089735. Can this issue be closed?

pixelzoom commented 8 months ago

@samreid said:

... I wrote a proposal to skip strict axon dependency testing if an alternate listener order is specified.

Over in https://github.com/phetsims/faradays-electromagnetic-lab/issues/57#issuecomment-1910945878, we implemented that proposal.

Looks like everything else is done here, so closing.

samreid commented 3 months ago

From today's dev meeting:

CM: DerivedProperty strictAxonDependencies has been causing more problems than it’s solving. Should we consider removing it, or making it opt-in? JO: Can it be more opt in? Hasn’t found anything important (true positive) for me. MK: One case can break the feature, so if we make it opt-in many things will start failing. So we should go all in our all out. CM: opting in for certain cases may be ok, at specific call sites. But the things I’m hitting are just in common code and I have no way to address it. We don’t know why my usage of boundsProperty recently started failing. If some dependences were tinyProperty then it got shut off by default. MK: So if we turn it off globally, but allow opting in on a Property-by-Property basis, then it could be useful in certain cases. SR calls for a vote: SR: delete CM: needs more discussion JO: needs more discussion JB: needs more discussion JG: needs more discussion MK: this could be a default in DerivedStringProperty, and opt-in everywhere JG: thumbs up MK: Or maybe DerivedProperty could check if any dependency is a DerivedStringProperty Update vote: SR: in the votes, there are 4 votes for “eliminate completely” one vote for “maybe keep under certain circumstances” No objections to deleting. SR: I’ll delete it. MK: You will not be missed.

samreid commented 3 months ago

After my local changes, I tested a few sims and wrappers. The precommit hooks say we are OK:

node chipper/js/scripts/precommit-hook-multi.js
detected changed repos: area-model-common, axon, beers-law-lab, center-and-variability, chipper, collision-lab, density-buoyancy-common, energy-skate-park, expression-exchange, faradays-electromagnetic-lab, fractions-common, gas-properties, gravity-and-orbits, joist, keplers-laws, masses-and-springs, number-compare, number-line-distance, number-play, ph-scale, phetmarks, scenery-phet, sun, unit-rates
area-model-common: Success
axon: Success
beers-law-lab: Success
center-and-variability: Success
chipper: Success
collision-lab: Success
density-buoyancy-common: Success
energy-skate-park: Success
expression-exchange: Success
faradays-electromagnetic-lab: Success
fractions-common: Success
gas-properties: Success
gravity-and-orbits: Success
joist: Success
keplers-laws: Success
masses-and-springs: Success
number-compare: Success
number-line-distance: Success
number-play: Success
ph-scale: Success
phetmarks: Success
scenery-phet: Success
sun: Success
unit-rates: Success
Done in 295116ms

I'll spot check the delta one more time before pushing.

samreid commented 3 months ago

OK I removed the strictAxonDependencies feature. Closing.

zepumph commented 3 months ago

Thank you for the quick turnaround here! I appreciate it.