Skip to content

Commit

Permalink
Use Pool.ts, see #94
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Dec 19, 2024
1 parent 85407b5 commit 7cb9838
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
10 changes: 4 additions & 6 deletions js/least-squares-regression/view/GraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ export default class GraphNode extends Node {
graph.myLineResiduals.addItemAddedListener( addedResidualProperty => {

// Create and add the view representation for this residual.
// @ts-expect-error from Poolable mixin
const residualNode = ResidualLineAndSquareNode.createFromPool(
const residualNode = ResidualLineAndSquareNode.pool.create(
addedResidualProperty,
LeastSquaresRegressionConstants.MY_LINE_COLOR,
this.viewBounds,
Expand All @@ -100,7 +99,7 @@ export default class GraphNode extends Node {
// Add the removal listener for if and when this residual is removed from the model.
graph.myLineResiduals.addItemRemovedListener( function removalListener( removedResidualProperty ) {
if ( removedResidualProperty === addedResidualProperty ) {
residualNode.release();
residualNode.freeToPool();
residualsLayer.removeChild( residualNode );
graph.myLineResiduals.removeItemRemovedListener( removalListener );
}
Expand All @@ -112,8 +111,7 @@ export default class GraphNode extends Node {
graph.bestFitLineResiduals.addItemAddedListener( addedResidualProperty => {

// Create and add the view representation for this residual.
// @ts-expect-error from Poolable mixin
const residualNode = ResidualLineAndSquareNode.createFromPool(
const residualNode = ResidualLineAndSquareNode.pool.create(
addedResidualProperty,
LeastSquaresRegressionConstants.BEST_FIT_LINE_COLOR,
this.viewBounds,
Expand All @@ -134,7 +132,7 @@ export default class GraphNode extends Node {
this.bestFitResiduals.splice( index, 1 );
}

residualNode.release();
residualNode.freeToPool();
residualsLayer.removeChild( residualNode );
}
} );
Expand Down
32 changes: 16 additions & 16 deletions js/least-squares-regression/view/ResidualLineAndSquareNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/

import Property from '../../../../axon/js/Property.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import Bounds2 from '../../../../dot/js/Bounds2.js';
import { Shape } from '../../../../kite/js/imports.js';
import Poolable from '../../../../phet-core/js/Poolable.js';
import Pool from '../../../../phet-core/js/Pool.js';
import ModelViewTransform2 from '../../../../phetcommon/js/view/ModelViewTransform2.js';
import { Line, Node, Rectangle } from '../../../../scenery/js/imports.js';
import leastSquaresRegression from '../../leastSquaresRegression.js';
Expand All @@ -28,8 +29,8 @@ export default class ResidualLineAndSquareNode extends Node {
lineColor: { SQUARED_RESIDUAL_COLOR: string; RESIDUAL_COLOR: string },
private viewBounds: Bounds2,
private modelViewTransform: ModelViewTransform2,
private lineVisibilityProperty: Property<boolean>,
private squareVisibilityProperty: Property<boolean> ) {
private lineVisibilityProperty: TReadOnlyProperty<boolean>,
private squareVisibilityProperty: TReadOnlyProperty<boolean> ) {
super();

// create line and square residual with nominal values, will set the correct value later
Expand All @@ -53,7 +54,7 @@ export default class ResidualLineAndSquareNode extends Node {

this.updateLineAndSquareListener = this.updateLineAndSquare.bind( this );

this.set( residualProperty, lineColor, viewBounds, modelViewTransform, lineVisibilityProperty, squareVisibilityProperty );
this.initialize( residualProperty, lineColor, viewBounds, modelViewTransform, lineVisibilityProperty, squareVisibilityProperty );
}

/**
Expand Down Expand Up @@ -84,20 +85,19 @@ export default class ResidualLineAndSquareNode extends Node {
this.squareResidual.clipArea = Shape.bounds( this.viewBounds );
}

/**
* This used to be dispose() before we switched to Poolable, see https://github.com/phetsims/scenery/issues/601
*/
public release(): void {
public freeToPool(): void {

// unlink listeners
this.lineVisibilityProperty.unlink( this.lineVisibilityPropertyListener );
this.squareVisibilityProperty.unlink( this.squareVisibilityPropertyListener );
this.residualProperty.unlink( this.updateLineAndSquareListener );

// @ts-expect-error TODO: https://github.com/phetsims/least-squares-regression/issues/94
this.freeToPool(); // will throw ResidualLineAndSquareNode into the pool
// TypeScript doesn't need to know that we're using this for different types. When it is "active", it will be
// the correct type.
ResidualLineAndSquareNode.pool.freeToPool( this );
}

public set( residualProperty: Property<Residual>, lineColor: { SQUARED_RESIDUAL_COLOR: string; RESIDUAL_COLOR: string }, viewBounds: Bounds2, modelViewTransform: ModelViewTransform2, lineVisibilityProperty: Property<boolean>, squareVisibilityProperty: Property<boolean> ): ResidualLineAndSquareNode {
public initialize( residualProperty: Property<Residual>, lineColor: { SQUARED_RESIDUAL_COLOR: string; RESIDUAL_COLOR: string }, viewBounds: Bounds2, modelViewTransform: ModelViewTransform2, lineVisibilityProperty: TReadOnlyProperty<boolean>, squareVisibilityProperty: TReadOnlyProperty<boolean> ): ResidualLineAndSquareNode {
this.lineVisibilityProperty = lineVisibilityProperty;
this.squareVisibilityProperty = squareVisibilityProperty;
this.residualProperty = residualProperty;
Expand All @@ -115,10 +115,10 @@ export default class ResidualLineAndSquareNode extends Node {

return this; // for chaining
}
}

leastSquaresRegression.register( 'ResidualLineAndSquareNode', ResidualLineAndSquareNode );
public static readonly pool = new Pool( ResidualLineAndSquareNode, {
initialize: ResidualLineAndSquareNode.prototype.initialize
} );
}

Poolable.mixInto( ResidualLineAndSquareNode, {
initialize: ResidualLineAndSquareNode.prototype.set
} );
leastSquaresRegression.register( 'ResidualLineAndSquareNode', ResidualLineAndSquareNode );

0 comments on commit 7cb9838

Please sign in to comment.