Skip to content

Commit

Permalink
update histogram when data changes (for phet-io), see #42
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Oct 2, 2024
1 parent 3cb16c2 commit 86970fb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 7 additions & 6 deletions js/coins/model/CoinsExperimentSceneModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type SelfOptions = {
};
type CoinExperimentSceneModelOptions = SelfOptions & PickRequired<PhetioObjectOptions, 'tandem'>;

type MeasuredCoinStates = ClassicalCoinStates | QuantumCoinStates;

// constants

// allowed values for the number of coins to use in the multi-coin experiment
Expand All @@ -51,7 +53,7 @@ export default class CoinsExperimentSceneModel extends PhetioObject {
// The coins that are flipped/prepared and then measured during the experiment.
public readonly singleCoin: TwoStateSystem<ClassicalCoinStates> | TwoStateSystem<QuantumCoinStates>;

public readonly coinSet: TwoStateSystemSet<ClassicalCoinStates | QuantumCoinStates>;
public readonly coinSet: TwoStateSystemSet<ClassicalCoinStates> | TwoStateSystemSet<QuantumCoinStates>;

// The initial state of the coin(s) before any flipping or other experiment preparation occurs.
public readonly initialCoinStateProperty: Property<ClassicalCoinStates> | Property<QuantumUncollapsedCoinStates>;
Expand Down Expand Up @@ -136,11 +138,10 @@ export default class CoinsExperimentSceneModel extends PhetioObject {

// The scene is moving from preparation mode to measurement mode. Set the coins to be in the initial state
// chosen by the user. If these are quantum coins and the initial state is set to superposed, set an arbitrary
// initial state, which is okay because the values won't be shown to the user.
let initialState = this.initialCoinStateProperty.value;
if ( this.systemType === 'quantum' && initialState === 'superposed' ) {
initialState = 'up';
}
// initial state. This is okay because the values won't be shown to the user.
const initialState: MeasuredCoinStates = this.initialCoinStateProperty.value === 'superposed' ?
'up' :
this.initialCoinStateProperty.value;
// TODO: How can this be better and avoid the "as never" weirdness? See https://github.com/phetsims/quantum-measurement/issues/42.
this.singleCoin.setMeasurementValuesImmediate( initialState as never );
this.coinSet.setMeasurementValuesImmediate( initialState as never );
Expand Down
7 changes: 3 additions & 4 deletions js/common/model/TwoStateSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import TwoStateSystemSet, { TwoStateSystemSetOptions } from './TwoStateSystemSet
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import Property from '../../../../axon/js/Property.js';
import StringUnionIO from '../../../../tandem/js/types/StringUnionIO.js';
import NullableIO from '../../../../tandem/js/types/NullableIO.js';

type SelfOptions = EmptySelfOptions;
type TwoStateSystemOptions = SelfOptions & StrictOmit<TwoStateSystemSetOptions, 'maxNumberOfSystems'>;

export default class TwoStateSystem<T extends string> extends TwoStateSystemSet<T> {

// the value of most recent measurement, null indicates indeterminate
public readonly measuredValueProperty: Property<T | null>;
public readonly measuredValueProperty: Property<T>;

public constructor( stateValues: readonly T[],
initialState: T,
Expand All @@ -37,8 +36,8 @@ export default class TwoStateSystem<T extends string> extends TwoStateSystemSet<

this.measuredValueProperty = new Property( initialState, {
tandem: options.tandem.createTandem( 'measuredValueProperty' ),
phetioValueType: NullableIO( StringUnionIO( stateValues ) ),
validValues: [ ...stateValues, null ]
phetioValueType: StringUnionIO( stateValues ),
validValues: [ ...stateValues ]
} );

// Hook up to the data-changed emitter to update the data Property.
Expand Down
6 changes: 3 additions & 3 deletions js/common/model/TwoStateSystemSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type TwoStateSystemSetOptions = SelfOptions & PickRequired<PhetioObjectOp

export type StateSetMeasurementResult<T> = {
length: number;
measuredValues: Array<T | null>;
measuredValues: Array<T>;
};

// Define the time that it will take to prepare a measurement, in seconds. This is empirically determined.
Expand Down Expand Up @@ -267,7 +267,7 @@ export default class TwoStateSystemSet<T extends string> extends PhetioObject {
* Set the measurement values immediately to the provided value for all elements in this set without transitioning
* through the 'preparingToBeMeasured' state.
*/
public setMeasurementValuesImmediate( value: T ): void {
public setMeasurementValuesImmediate( whatIsUpHere: T ): void {

// Cancel any in-progress preparation.
if ( this.preparingToBeMeasuredTimeoutListener ) {
Expand All @@ -276,7 +276,7 @@ export default class TwoStateSystemSet<T extends string> extends PhetioObject {
}

// Set the seed value to the value that will incite its listener to update the data to the provided value.
const valueIndex = this.validValues.indexOf( value );
const valueIndex = this.validValues.indexOf( whatIsUpHere );
assert && assert( valueIndex === 0 || valueIndex === 1 );
this.seedProperty.value = valueIndex;

Expand Down

0 comments on commit 86970fb

Please sign in to comment.