Skip to content

Commit

Permalink
Use deriveAny, see #75
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jul 27, 2022
1 parent 93bf54e commit 329c404
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions js/intro/model/IntroModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,23 @@ export default class IntroModel extends MeanShareAndBalanceModel {

// This value is derived from the water levels in all the cups, but cannot be modeled as a DerivedProperty since
// the number of cups varies
// @ts-ignore - .map() does not preserve a property of .length required for DerivedProperty
this.meanProperty = new DerivedProperty( this.waterCup3DArray.map( waterCup => waterCup.waterLevelProperty ),
() => calculateMean( this.getActive3DCups().map( waterCup3D => waterCup3D.waterLevelProperty.value ) ),
const dependencies = [
...this.waterCup3DArray.map( waterCup => waterCup.waterLevelProperty ),
...this.waterCup3DArray.map( waterCup => waterCup.isActiveProperty )
];

// map() does not preserve a property of .length required for DerivedProperty
this.meanProperty = DerivedProperty.deriveAny( dependencies,
() => {
const mean = calculateMean( this.getActive3DCups().map( waterCup3D => waterCup3D.waterLevelProperty.value ) );
assert && assert( mean >= MeanShareAndBalanceConstants.CUP_RANGE_MIN && mean <= MeanShareAndBalanceConstants.CUP_RANGE_MAX, 'mean out of bounds: ' + mean );
return mean;
},
{
tandem: options.tandem.createTandem( 'meanProperty' ),
phetioDocumentation: 'The ground truth water level mean.',
phetioReadOnly: true,
phetioType: DerivedProperty.DerivedPropertyIO( NumberIO ),
range: new Range( MeanShareAndBalanceConstants.CUP_RANGE_MIN, MeanShareAndBalanceConstants.CUP_RANGE_MAX )
phetioType: DerivedProperty.DerivedPropertyIO( NumberIO )
} );

// add/remove water cups and pipes according to number spinner
Expand Down

0 comments on commit 329c404

Please sign in to comment.