phetsims / axon

Axon provides powerful and concise models for interactive simulations, based on observable Properties and related patterns.
MIT License
11 stars 8 forks source link

BooleanProperty doesn't support validValues. #359

Closed pixelzoom closed 3 years ago

pixelzoom commented 3 years ago

Sometimes.an API takes a Property, and you want to pass in a constant. So you create a Property that can only take 1 value, like:

    const dragBoundsProperty = new Property( options.dragBounds, {
      validValues: [ options.dragBounds ]
    } );

In Fourier, I have a chart that always auto-scales the y axis, and the chart's API takes a BooleanProperty. So I tried this:

    options.yAutoScaleProperty = new BooleanProperty( true, {
      validValues: [ true ]
    } );

... which fails with:

Assertion failed: BooleanProperty does not support validValues

I can't tell for sure, but it looks like this prohibition of validValues was in the first version of BooleanProperty, created by @samreid. Any reason why we can't change this?

The alternative is:

    options.yAutoScaleProperty = new Property( true, {
      validValues: [ true ]
    } );

... which would be OK, as long as I don't have to eventually instrument it for PhET-iO.

samreid commented 3 years ago

Fixed in the commit. I tested with new BooleanProperty(true,{validValues:['hello']}); and saw that it correctly failed. I tested with new BooleanProperty(true,{validValues:[true]}); and saw that it correctly succeeded. @pixelzoom can you please review and test in your context?

pixelzoom commented 3 years ago

Looks good, thanks. Tested with my use-case in Fourier, see the above commit. Closing.