Skip to content

Commit

Permalink
dispose, #71
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Mar 7, 2024
1 parent 94a59ec commit 71bab4c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/implementation-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ and sim-specific) to the browser console.
### Memory Management

* **Dynamic allocation:** Most objects in this sim are allocated at startup, and exist for the lifetime of the
simulation. The exception is `Electron`, which are dynamically created when a coil is modified (see number of loops
simulation. The exception is `Electron`, dynamically created when a coil is modified (see number of loops
and loop area). Electrons do not need to be stateful, and the number of Electrons is derived.

* **Listeners**: Unless otherwise noted in the code, all uses of `link`, `addListener`, etc. do NOT need a
Expand Down
8 changes: 8 additions & 0 deletions js/common/model/Coil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,20 @@ export default class Coil extends PhetioObject {
( numberOfLoops, loopRadius, frontColor, middleColor, backColor ) =>
Coil.createCoilSegments( numberOfLoops, loopRadius, this.wireWidth, this.loopSpacing, frontColor, middleColor, backColor ) );

// When a new set of CoilSegments is created, dispose of the old set.
this.coilSegmentsProperty.lazyLink( ( newCoilSegments, oldCoilSegments ) =>
oldCoilSegments.forEach( coilSegment => coilSegment.dispose() ) );

this.electronsProperty = new DerivedProperty( [ this.coilSegmentsProperty ],
coilSegments => this.createElectrons( coilSegments ), {
// Erroneously identifies options to new Electron in createElectrons as dependencies.
strictAxonDependencies: false
} );

// When a new set of Electrons is created, dispose of the old set.
this.electronsProperty.lazyLink( ( newElectrons, oldElectrons ) =>
oldElectrons.forEach( electron => electron.dispose() ) );

this.electronsMovedEmitter = new Emitter();
}

Expand Down
4 changes: 4 additions & 0 deletions js/common/model/CoilSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export default class CoilSegment {
public evaluate( t: number, returnVector?: Vector2 ): Vector2 {
return this.curve.evaluate( t, returnVector );
}

public dispose(): void {
// Nothing to do currently. But this class is allocated dynamically, so keep this method as a bit of defensive programming.
}
}

faradaysElectromagneticLab.register( 'CoilSegment', CoilSegment );
4 changes: 4 additions & 0 deletions js/common/model/Electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export default class Electron {
this.speedScaleProperty = options.speedScaleProperty;
}

public dispose(): void {
// Nothing to do currently. But this class is allocated dynamically, so keep this method as a bit of defensive programming.
}

/**
* Gets the layer that this electron currently occupies.
*/
Expand Down
5 changes: 5 additions & 0 deletions js/common/model/QuadraticBezierSpline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import faradaysElectromagneticLab from '../../faradaysElectromagneticLab.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import { Shape } from '../../../../kite/js/imports.js';
import Disposable from '../../../../axon/js/Disposable.js';


export default class QuadraticBezierSpline {
Expand Down Expand Up @@ -59,6 +60,10 @@ export default class QuadraticBezierSpline {
.moveToPoint( this.startPoint )
.quadraticCurveToPoint( this.controlPoint, this.endPoint );
}

public dispose(): void {
Disposable.assertNotDisposable();
}
}

faradaysElectromagneticLab.register( 'QuadraticBezierSpline', QuadraticBezierSpline );
2 changes: 1 addition & 1 deletion js/common/view/ElectronsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ElectronSpriteInstance extends SpriteInstance {
}

public dispose(): void {
// Nothing to do currently. But keep this method as a bit of defensive programming.
// Nothing to do currently. But this class is allocated dynamically, so keep this method as a bit of defensive programming.
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/common/view/FieldNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class CompassNeedleSpriteInstance extends SpriteInstance {
}

public dispose(): void {
// Nothing to do currently. But keep this method as a bit of defensive programming for rebuild method.
// Nothing to do currently. But this class is allocated dynamically, so keep this method as a bit of defensive programming.
}

/**
Expand Down

0 comments on commit 71bab4c

Please sign in to comment.