Skip to content

Commit

Permalink
Use Property listener instead of setTimeout, see phetsims/phet-info#59
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 11, 2019
1 parent 557765f commit 96243e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
31 changes: 23 additions & 8 deletions js/model/CircuitElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,15 @@ define( require => {

this.startPositionProperty.link( this.vertexMovedListener );
this.endPositionProperty.link( this.vertexMovedListener );
this.startVertexProperty.lazyLink( this.linkVertexListener );
this.endVertexProperty.lazyLink( this.linkVertexListener );

// @public - named so it doesn't collide with the specified voltageProperty in Battery or ACVoltage
this.voltageDifferenceProperty = new NumberProperty( this.computeVoltageDifference() );

// @private
this.vertexVoltageListener = () => this.voltageDifferenceProperty.set( this.computeVoltageDifference() );

this.startVertexProperty.link( this.linkVertexListener );
this.endVertexProperty.link( this.linkVertexListener );

// @public (read-only by clients, writable-by-subclasses) {number} the distance the charges must take to get to the
// other side of the component. This is typically the distance between vertices, but not for light bulbs. This
Expand All @@ -169,12 +176,13 @@ define( require => {
}

/**
* Returns the difference in voltage between the end and start vertices.
* Determine the voltage difference between end vertex and start vertex
* @returns {number}
* @public
* @private
*/
getVoltage() {
return this.endVertexProperty.value.voltageProperty.value - this.startVertexProperty.value.voltageProperty.value;
computeVoltageDifference() {
return this.endVertexProperty.value.voltageProperty.value -
this.startVertexProperty.value.voltageProperty.value;
}

/**
Expand All @@ -187,16 +195,20 @@ define( require => {

// These guards prevent errors from the bad transient state caused by the Circuit.flip causing the same Vertex
// to be both start and end at the same time.
if ( oldVertex.positionProperty.hasListener( this.vertexMovedListener ) ) {
if ( oldVertex && oldVertex.positionProperty.hasListener( this.vertexMovedListener ) ) {
oldVertex.positionProperty.unlink( this.vertexMovedListener );
}
if ( !newVertex.positionProperty.hasListener( this.vertexMovedListener ) ) {
newVertex.positionProperty.lazyLink( this.vertexMovedListener );
}

if ( !oldVertex.positionProperty.get().equals( newVertex.positionProperty.get() ) ) {
if ( oldVertex && !oldVertex.positionProperty.get().equals( newVertex.positionProperty.get() ) ) {
this.vertexMovedEmitter.emit();
}

this.voltageDifferenceProperty.set( this.computeVoltageDifference() );
oldVertex && oldVertex.voltageProperty.unlink( this.vertexVoltageListener );
newVertex.voltageProperty.link( this.vertexVoltageListener );
}

/**
Expand Down Expand Up @@ -252,6 +264,9 @@ define( require => {
this.startPositionProperty.hasListener( this.vertexMovedListener ) && this.startPositionProperty.unlink( this.vertexMovedListener );
this.endPositionProperty.hasListener( this.vertexMovedListener ) && this.endPositionProperty.unlink( this.vertexMovedListener );

this.startVertexProperty.value.voltageProperty.unlink( this.vertexVoltageListener );
this.endVertexProperty.value.voltageProperty.unlink( this.vertexVoltageListener );

super.dispose();
}

Expand Down
17 changes: 12 additions & 5 deletions js/view/CapacitorCircuitElementNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ define( require => {
}
};

// TODO: DynamicProperty for getting the voltages? Or step during model step?
setInterval( () => {
const value = Util.linear( -9, 9, -V, V, capacitor.getVoltage() );
circuit.capacitor.plateChargeProperty.value = -value;
}, 10 );
const modelViewTransform = new YawPitchModelViewTransform3();
const plateChargeVisibleProperty = new BooleanProperty( true );
const electricFieldVisibleProperty = new BooleanProperty( true );
Expand All @@ -117,6 +112,9 @@ define( require => {
orientation: Orientation.HORIZONTAL // so the "-" charges are upside-up in the default orientation
} );

const voltageToPlateCharge = v => circuit.capacitor.plateChargeProperty.set( -Util.linear( -9, 9, -V, V, v ) );
capacitor.voltageDifferenceProperty.link( voltageToPlateCharge );

lifelikeNode.mutate( {
scale: 0.45,
rotation: -Math.PI / 2
Expand Down Expand Up @@ -200,6 +198,15 @@ define( require => {
const bottomPlateCenterToGlobal = this.capacitorCircuitElementLifelikeNode.getBottomPlateClipShapeToGlobal();
rightWireStub.clipArea = bottomPlateCenterToGlobal.transformed( rightWireStub.getGlobalToLocalMatrix() );
} );

// @private
this.disposeCapacitorCircuitElementNode = () => capacitor.voltageDifferenceProperty.unlink( voltageToPlateCharge );
}

// @public
dispose() {
this.disposeCapacitorCircuitElementNode();
super.dispose();
}

/**
Expand Down

0 comments on commit 96243e4

Please sign in to comment.