phetsims / calculus-grapher

"Calculus Grapher" is an educational simulation in HTML5, by PhET Interactive Simulations.
GNU General Public License v3.0
4 stars 4 forks source link

API improvement for GraphNode #277

Closed pixelzoom closed 1 year ago

pixelzoom commented 1 year ago

Related to #262 (Self code review)

There's a part of the GraphNode API that I keep bumping into, and it feels unnecessarily complicated. It's GraphNodeOptions.createCurveNode.

The default value in GraphNode.ts is:

      createCurveNode: ( chartTransform: ChartTransform, chartRectangleFill: TPaint, plotBounds: Bounds2 ) => new CurveNode( curve, chartTransform, {
        plotBoundsMethod: CalculusGrapherConstants.PLOT_BOUNDS_METHOD, // see https://github.com/phetsims/calculus-grapher/issues/210
        plotBounds: plotBounds, // see https://github.com/phetsims/calculus-grapher/issues/259
        stroke: graphType.strokeProperty,
        discontinuousPointsScatterPlotOptions: {
          fill: chartRectangleFill
        },
        tandem: providedOptions.tandem.createTandem( 'curveNode' )
      } ),

And the override in OriginalGraphNode.ts is:

   // Creates PhET-iO element 'graphsNode.originalCurveNode'
    const originalCurveNodeTandem = providedOptions.tandem.createTandem( 'originalCurveNode' );
    const createOriginalCurveNode = ( chartTransform: ChartTransform, chartRectangleFill: TPaint, plotBounds: Bounds2 ) =>
      new TransformedCurveNode( originalCurve, curveManipulationProperties, chartTransform, {
        // ... 22 lines of code
      } );

    const options = optionize<OriginalGraphNodeOptions, SelfOptions, GraphNodeOptions>()( {

      // GraphNodeOptions
      createCurveNode: createOriginalCurveNode,

And then this cast is needed in OriginalGraphNode.ts, which requires disabling ESlint:

    // We need to know that this.curveNode is of type TransformedCurveNode, as created by createCurveNode above.
    const originalCurveNode = this.curveNode as TransformedCurveNode;
    assert && assert( originalCurveNode instanceof TransformedCurveNode ); // eslint-disable-line no-simple-type-checking-assertions

I think it would be cleaner to:

This I shall investigate.

pixelzoom commented 1 year ago

Summary of the important bits in the above commit:

This looks much nicer to me. And I can sleep better without the // eslint-disable-line :)

@veillette have a look, let me know if you have questions/concerns, or feel free to close.

PS: I confirmed that Screenshot is still working correctly after this change.

veillette commented 1 year ago

I reviewed this. Our overriding of CurveNode was always a bit awkward. Much better. Cl;osing