Skip to content

Commit

Permalink
Improve type safety and inference for Multilink, see phetsims/axon#395
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed May 14, 2022
1 parent f9cac32 commit 26543a9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion js/quadrilateral/model/QuadrilateralShapeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class QuadrilateralShapeModel {
this.vertexB.positionProperty,
this.vertexC.positionProperty,
this.vertexD.positionProperty ],
( position1: Vector2, position2: Vector2, position3: Vector2, position4: Vector2 ) => {
( position1, position2, position3, position4 ) => {

// Update Properties after Vertex positions have changed. Please note the usage of
// setDeferred for Vertex position Properties in this sim because it is important
Expand Down
4 changes: 2 additions & 2 deletions js/quadrilateral/view/CornerGuideNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CornerGuideNode extends Node {
lineDash: [ 5, 5 ]
} );

Property.multilink( [ vertex1.angleProperty, vertex1.positionProperty ], ( angle: number | null, position: Vector2 ) => {
Property.multilink( [ vertex1.angleProperty, vertex1.positionProperty ], ( angle, position ) => {
assert && assert( angle !== null, 'angleProperty needs to be defined to add listeners in CornerGuideNode' );
assert && assert( angle! > 0, 'CornerGuideNodes cannot support angles at or less than zero' );
const vertexCenter = vertex1.positionProperty.value;
Expand Down Expand Up @@ -135,7 +135,7 @@ class CornerGuideNode extends Node {

// listeners - This Node is only visible when "Angle Guides" are visible by the user and the angle is NOT a right
// angle. In that case, the RightAngleIndicatorNode will display the angle.
Property.multilink( [ visibleProperty, shapeModel.shapeNameProperty ], ( visible: boolean, shapeName: NamedQuadrilateral | null ) => {
Property.multilink( [ visibleProperty, shapeModel.shapeNameProperty ], ( visible, shapeName ) => {
const currentShape = shapeModel.shapeNameProperty.value;
this.visible = visible && currentShape !== NamedQuadrilateral.SQUARE && currentShape !== NamedQuadrilateral.RECTANGLE;
} );
Expand Down
4 changes: 2 additions & 2 deletions js/quadrilateral/view/ParallelsStaccatoSoundView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ class ParallelsStaccatoSoundView {

const leftRightMultilink = Property.multilink(
[ quadrilateralShapeModel.leftSide.tiltProperty, quadrilateralShapeModel.rightSide.tiltProperty ],
( leftTilt: number, rightTilt: number ) => {
( leftTilt, rightTilt ) => {
assert && assert( leftTilt !== Number.POSITIVE_INFINITY && rightTilt !== Number.POSITIVE_INFINITY, 'tilts cannot be infinite in the sound design' );
this.leftRightPopCoefficient = TILT_DIFFERENCE_TO_PITCHED_POP_COEFFICIENT.evaluate( Math.abs( leftTilt - rightTilt ) );
this.leftRightRelativePitch = TILT_DIFFERENCE_TO_PITCH.evaluate( Math.abs( leftTilt - rightTilt ) );
}
);
const topBottomMultilink = Property.multilink(
[ quadrilateralShapeModel.topSide.tiltProperty, quadrilateralShapeModel.bottomSide.tiltProperty ],
( topTilt: number, bottomTilt: number ) => {
( topTilt, bottomTilt ) => {
assert && assert( topTilt !== Number.POSITIVE_INFINITY && bottomTilt !== Number.POSITIVE_INFINITY, 'tilts cannot be infinite in the sound design' );
this.topBottomPopCoefficient = TILT_DIFFERENCE_TO_PITCHED_POP_COEFFICIENT.evaluate( Math.abs( topTilt - bottomTilt ) );
this.topBottomRelativePitch = TILT_DIFFERENCE_TO_PITCH.evaluate( Math.abs( topTilt - bottomTilt ) );
Expand Down
15 changes: 8 additions & 7 deletions js/quadrilateral/view/ParallelsVolumeSoundView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import soundManager from '../../../../tambo/js/soundManager.js';
import quadrilateral from '../../quadrilateral.js';
import QuadrilateralModel from '../model/QuadrilateralModel.js';
import QuadrilateralSoundOptionsModel, { SuccessSoundFile } from '../model/QuadrilateralSoundOptionsModel.js';
import Multilink from '../../../../axon/js/Multilink.js';
import { UnknownMultilink } from '../../../../axon/js/Multilink.js';
import Side from '../model/Side.js';
import WrappedAudioBuffer from '../../../../tambo/js/WrappedAudioBuffer.js';

Expand All @@ -29,8 +29,9 @@ class ParallelsVolumeSoundView {
private model: QuadrilateralModel;
private leftRightSideGenerator: null | SoundClipChord;
private topBottomSideGenerator: null | SoundClipChord;
private leftRightSideMultilink: null | Multilink<any[]>;
private topBottomSideMultilink: null | Multilink<any[]>;

private leftRightSideMultilink: UnknownMultilink | null;
private topBottomSideMultilink: UnknownMultilink | null;
private leftRightSideOutputLevel: number;
private topBottomSideOutputLevel: number;
private isPlaying: boolean;
Expand Down Expand Up @@ -176,12 +177,12 @@ class ParallelsVolumeSoundView {
this.leftRightSideGenerator.setOutputLevel( 0 );
this.topBottomSideGenerator.setOutputLevel( 0 );

this.leftRightSideMultilink = this.createTiltMultilink( this.model.quadrilateralShapeModel.leftSide, this.model.quadrilateralShapeModel.rightSide, ( outputLevel: number ) => {
this.leftRightSideMultilink = this.createTiltMultilink( this.model.quadrilateralShapeModel.leftSide, this.model.quadrilateralShapeModel.rightSide, outputLevel => {
assert && assert( this.leftRightSideGenerator, 'The SoundGenerator has been disposed of, you can no longer set its output level' );
this.leftRightSideOutputLevel = outputLevel;
this.leftRightSideGenerator!.outputLevel = outputLevel;
} );
this.topBottomSideMultilink = this.createTiltMultilink( this.model.quadrilateralShapeModel.topSide, this.model.quadrilateralShapeModel.bottomSide, ( outputLevel: number ) => {
this.topBottomSideMultilink = this.createTiltMultilink( this.model.quadrilateralShapeModel.topSide, this.model.quadrilateralShapeModel.bottomSide, outputLevel => {
assert && assert( this.leftRightSideGenerator, 'The SoundGenerator has been disposed of, you can no longer set its output level' );
this.topBottomSideOutputLevel = outputLevel;
this.topBottomSideGenerator!.outputLevel = outputLevel;
Expand Down Expand Up @@ -215,10 +216,10 @@ class ParallelsVolumeSoundView {
* Creates and returns (for disposal) a multilink that sets the output level of a SoundGenerator from the
* difference in tilts of two sides.
*/
public createTiltMultilink( sideA: Side, sideB: Side, applyOutputLevel: ( outputLevel: number ) => void ): Multilink<[ leftTilt: number, rightTilt: number ]> {
public createTiltMultilink( sideA: Side, sideB: Side, applyOutputLevel: ( outputLevel: number ) => void ): UnknownMultilink {
return Property.multilink(
[ sideA.tiltProperty, sideB.tiltProperty ],
( leftTilt: number, rightTilt: number ) => {
( leftTilt, rightTilt ) => {

assert && assert( leftTilt !== Number.POSITIVE_INFINITY && rightTilt !== Number.POSITIVE_INFINITY, 'tilts cannot be infinite in sound design' );
const outputLevel = TILT_DIFFERENCE_TO_OUTPUT_LEVEL.evaluate( Math.abs( leftTilt - rightTilt ) );
Expand Down
2 changes: 1 addition & 1 deletion js/quadrilateral/view/SideLengthAreaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SideLengthAreaNode extends Node {
dragSide.isPressedProperty,
dragSide.vertex1.isPressedProperty,
dragSide.vertex2.isPressedProperty
], ( equalToSaved: boolean, sidePressed: boolean, vertex1Pressed: boolean, vertex2Pressed: boolean ) => {
], ( equalToSaved, sidePressed, vertex1Pressed, vertex2Pressed ) => {

const showArc = ( sidePressed || ( vertex1Pressed && vertex2Pressed ) ) && shapeModel.isParallelogramProperty.value;

Expand Down
2 changes: 1 addition & 1 deletion js/quadrilateral/view/SideNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class SideNode extends Voicing( Path, 1 ) {
const lineNode = new LineNode( 0, 0, 0, 0 );

// listeners
Property.multilink( [ side.vertex1.positionProperty, side.vertex2.positionProperty ], ( vertex1Position: Vector2, vertex2Position: Vector2 ) => {
Property.multilink( [ side.vertex1.positionProperty, side.vertex2.positionProperty ], ( vertex1Position, vertex2Position ) => {

// create a single line that will then be divided into segments
const fullLine = new Line( vertex1Position, vertex2Position );
Expand Down

0 comments on commit 26543a9

Please sign in to comment.