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

NumberProperty typescript updates #387

Closed zepumph closed 2 years ago

zepumph commented 2 years ago

This file is not up to our standards. I'll update documentation, options pattern, and whatever else I see while I am here for https://github.com/phetsims/studio/issues/253

zepumph commented 2 years ago

@samreid will you please take a quick look at the commits, and then the file and see if there is anything more to do?

samreid commented 2 years ago

Great commits, thanks! Some more questions about NumberProperty:

But should it be changed to read like so? Same with validateNumberAndRangeProperty.

  // @readonly, but cannot set as such because it is set by PhET-iO state.
  step?: number;

For this code, @chrisklus and I have recommended separating the statics from the instance variables:

  private readonly disposeNumberProperty: () => void;
  static NumberPropertyIO: IOType;
  private readonly resetNumberProperty: () => void;
    // This puts validation at notification time instead of at value setting time. This is especially helpful as it
    // pertains to Property.prototype.setDeferred(), and setting a range and value together.
    this.validateNumberAndRangeProperty && this.link( value => this.validateNumberProperty() );

Anyways, I just tried to jot down many thoughts for this issue. Not all are equally important and some should probably be moved to side issues. Happy to discuss further.

samreid commented 2 years ago

Please also be aware of the removal of step in https://github.com/phetsims/axon/issues/386

zepumph commented 2 years ago

This pattern feels problematic, but I don't know a better way to implement it:

It is @jonathanolson's code, and at one point I spent ~30 minutes poking around with other ideas and couldn't find anything. It doesn't bother me too much, but it is nice, when possible, to not defer to assumptions about runtime conditions.

Like we discussed yesterday, more validation should be moved into the validator instead of in separate links so it can be leveraged by studio value checking:

Better code has been committed from https://github.com/phetsims/studio/issues/253

Everything else was committed. Anything else here?

jonathanolson commented 2 years ago

Maybe it's an alternative to mixins?

It's more descriptive and on the type-end. Mixins would presumably change the behavior. If we have a DerivedProperty or DynamicProperty, they can be massaged into the correct type.

samreid commented 2 years ago

It seems this issue is ready to close.