Skip to content

Commit

Permalink
don't redundantly set state, see #219
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Jun 3, 2024
1 parent fc30f50 commit aa3bf48
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions js/fair-share/model/FairShareModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import stepTimer from '../../../../axon/js/stepTimer.js';
import Plate from '../../common/model/Plate.js';
import Multilink from '../../../../axon/js/Multilink.js';
import TinyEmitter from '../../../../axon/js/TinyEmitter.js';
import isSettingPhetioStateProperty from '../../../../tandem/js/isSettingPhetioStateProperty.js';

type SelfOptions = EmptySelfOptions;
type FairShareModelOptions = SelfOptions & PickRequired<SharingModelOptions, 'tandem'>;
Expand Down Expand Up @@ -128,11 +129,9 @@ export default class FairShareModel extends SharingModel<Apple> {
} );

this.appleCollection.addItemAddedListener( apple => {
const index = this.appleCollection.indexOf( apple );

apple.isActiveProperty.value = true;
apple.fractionProperty.value = Fraction.ONE;
apple.moveTo( this.getCollectionPosition( index ), this.animateAddedSnacks );
apple.moveTo( this.getCollectionPosition( this.appleCollection.indexOf( apple ) ), this.animateAddedSnacks );
} );

// Set up plate-related behavior that is specific to the Fair Share screen.
Expand All @@ -156,6 +155,13 @@ export default class FairShareModel extends SharingModel<Apple> {
// plates and the collection area, and some of this motion is animated.
const handleModeChange = ( appleDistributionMode: DistributionMode, previousDistributionMode: DistributionMode | null ): void => {

// Do nothing if this is being called due to the setting of phet-io state. Why? Because the more granular state
// information, such as apple positions and fractional values, is already taken care of by instrumented Property
// instances, and trying to set it all again will likely mess up the state.
if ( isSettingPhetioStateProperty.value ) {
return;
}

// Make sure any leftover animations from previous mode changes are cleared.
this.finishInProgressAnimations();

Expand Down

0 comments on commit aa3bf48

Please sign in to comment.