Skip to content

Commit

Permalink
combine EnumerationIO and RichEnumerationIO, phetsims/phet-core#97
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jan 13, 2022
1 parent d29d677 commit a915fb0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions js/RichEnumerationIO.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Copyright 2021-2022, University of Colorado Boulder

/**
* @author Sam Reid (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import EnumerationValue from '../../phet-core/js/EnumerationValue.js';
import IRichEnumeration, { RichEnumerationContainer } from '../../phet-core/js/IRichEnumeration.js';
import IOType from '../../tandem/js/types/IOType.js';
import StateSchema from '../../tandem/js/types/StateSchema.js';
import axon from './axon.js';

// Cache each parameterized IOType so that it is only created once.
const cache = new Map<IRichEnumeration<any>, IOType>();

const RichEnumerationIO = <T extends EnumerationValue>( enumerationContainer: RichEnumerationContainer<T> ): IOType => {
const joinKeys = ( keys: string[] ) => keys.join( '|' );

const EnumerationIO = <T extends EnumerationValue>( enumerationContainer: RichEnumerationContainer<T> ): IOType => {
const enumeration = enumerationContainer.enumeration;

// This caching implementation should be kept in sync with the other parametric IO Type caching implementations.
Expand All @@ -19,7 +28,7 @@ const RichEnumerationIO = <T extends EnumerationValue>( enumerationContainer: Ri
const keys = enumeration.keys;
const values = enumeration.values;

cache.set( enumeration, new IOType( `RichEnumerationIO(${keys.join( '|' )})`, {
cache.set( enumeration, new IOType( `EnumerationIO(${joinKeys( keys )})`, {
validValues: values,
documentation: `Possible values: ${keys.join( ', ' )}.${additionalDocs}`,
toStateObject: ( t: T ) => enumeration.getKey( t ),
Expand All @@ -28,7 +37,7 @@ const RichEnumerationIO = <T extends EnumerationValue>( enumerationContainer: Ri
assert && assert( keys.includes( stateObject ), `Unrecognized value: ${stateObject}` );
return enumeration.getValue( stateObject )!;
},
stateSchema: StateSchema.asValue( `${keys.join( '|' )}`, {
stateSchema: StateSchema.asValue( `${joinKeys( keys )}`, {
isValidValue: ( key: string ) => keys.includes( key )
} )
} ) );
Expand All @@ -37,4 +46,5 @@ const RichEnumerationIO = <T extends EnumerationValue>( enumerationContainer: Ri
return cache.get( enumeration )!;
};

export default RichEnumerationIO;
axon.register( 'EnumerationIO', EnumerationIO );
export default EnumerationIO;

0 comments on commit a915fb0

Please sign in to comment.