Skip to content

Commit

Permalink
Update ammeter after circuit element deletion, see #418
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Oct 15, 2017
1 parent a144111 commit 2012a38
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions js/model/CircuitElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ define( function( require ) {
// length changes when switching between LIFELIKE |SCHEMATIC
this.chargePathLength = chargePathLength;

// The ammeter update is called after items are disposed but before corresponding views are disposed, so we must
// take care not to display current for any items that are pending deletion.
// See https://github.com/phetsims/circuit-construction-kit-common/issues/418
this.circuitElementDisposed = false;

tandem.addInstance( this, TObject, options );
}

Expand Down Expand Up @@ -202,6 +207,8 @@ define( function( require ) {
*/
dispose: function() {

this.circuitElementDisposed = true;

// Notify about intent to dispose first because dispose listeners may need to access state
this.disposeEmitter.emit();
this.disposeEmitter.removeAllListeners();
Expand Down
6 changes: 5 additions & 1 deletion js/view/AmmeterNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ define( function( require ) {
for ( var i = 0; i < layer.children.length; i++ ) {
var circuitElementNode = layer.children[ i ];
if ( circuitElementNode instanceof CircuitElementNode ) {
if ( circuitElementNode.containsSensorPoint( probeNode.translation ) ) {

// This is called between when the circuit element is disposed and when the corresponding view is disposed
// so we must take care not to visit circuit elements that have been disposed but still have a view
// see https://github.com/phetsims/circuit-construction-kit-common/issues/418
if ( !circuitElementNode.circuitElement.circuitElementDisposed && circuitElementNode.containsSensorPoint( probeNode.translation ) ) {
return circuitElementNode.circuitElement.currentProperty.get();
}
}
Expand Down

0 comments on commit 2012a38

Please sign in to comment.