Closed pixelzoom closed 3 months ago
Here is a working implementation of the proposal above. I used a patch instead of branches:
This is also passing all precommit hooks (took 1m45s). @pixelzoom or @zepumph please review and feel free to commit if it passes review.
The patch is marginally better, but still provides a public API for modifying writableIsSettingPhetioStateProperty
.
Since PhetioStateEngine is the sole place where isSettingPhetioStateProperty should be set, a better implementation would be:
const isSettingPhetioStateProperty
in PhetioStateEngine.public static readonly isSettingPhetioStateProperty
.PhetioStateEngine. isSettingPhetioStateProperty
, i.e.:const isSettingPhetioStateProperty = PhetioStateEngine.isSettingPhetioStateProperty;
tandemNamespace.register( 'isSettingPhetioStateProperty', isSettingPhetioStateProperty );
export default isSettingPhetioStateProperty;
Below is a patch to demonstrate the implementation I described in https://github.com/phetsims/tandem/issues/311#issuecomment-2105322229. It's equivalent to the current implementation in that isSettingPhetioStateProperty
is still settable. To make it readonly, add a type specification to public static readonly isSettingPhetioStateProperty
. But I'm not sure what that type specification is, since I'm not familiar with the TinyProperty hierarchy. And the readonly type needs to support addListener
, which is used (exclusively) by PDL.
But I'm not sure what that type specification is, since I'm not familiar with the TinyProperty hierarchy. And the readonly type needs to support addListener, which is used (exclusively) by PDL.
Below is a patch that I believe addresses the problem.
In PhetioStateEngine:
const isSettingPhetioStateProperty = new TinyProperty( false );
...
public static readonly isSettingPhetioStateProperty: TReadOnlyProperty<boolean> = isSettingPhetioStateProperty;
Note that this requires PDL to use link
instead of addListener
-- which seems highly desirable. isSettingPhetioStateProperty
was formerly of type TinyProperty, which unfortunately inherits addListener
from TinyEmitter.
@samreid @zepumph please review, commit if this seems OK.
This proposed line is incompatible with our constraints:
In tandem/js/isSettingPhetioStateProperty.ts
import PhetioStateEngine from '../../phet-io/js/PhetioStateEngine.js';
phet-io cannot be imported from tandem, because phet-io is a private repo and not everyone (such as open source clients) can clone it.
Does it also follow that sims cannot import PhetioStateEngine.ts to use PhetioStateEngine.isSettingPhetioStateProperty
?
Correct, public code cannot statically import private code. Instead, we use a dynamic import in simLauncher like so: https://github.com/phetsims/joist/blob/main/js/simLauncher.ts#L23-L31
Then I guess your patch in https://github.com/phetsims/tandem/issues/311#issuecomment-2105311277 is as good as it's going to get. I'm OK with that.
@zepumph are you OK with @samreid's patch in https://github.com/phetsims/tandem/issues/311#issuecomment-2105311277?
Yes perfect. Thanks for discussing. I committed with @samreid this morning.
Thanks @zepumph, @pixelzoom want to review or close?
👍🏻
Noted while investigating https://github.com/phetsims/gas-properties/issues/240#issuecomment-2104909109, yet another problem that involves
isSettingPhetioStateProperty
.I find myself having to check
isSettingPhetioStateProperty
frequently when instrumenting sims, to short-circuit code that should not be called when restoring state. So I was surprised to find that this rather important (global!) Property allows me to set it, with no complains from tsc. For example:My understanding is that the only place that
isSettingPhetioStateProperty
should be set (and therefore the only place this should be settable) is in PhetioStateEngine.ts, specifically:Let's tighten this up so that
isSettingStateProperty
is read-only.