From a915fb0605e1141d3a35c03406850010a64afac9 Mon Sep 17 00:00:00 2001 From: zepumph Date: Wed, 12 Jan 2022 16:53:31 -0700 Subject: [PATCH] combine EnumerationIO and RichEnumerationIO, https://github.com/phetsims/phet-core/issues/97 --- js/RichEnumerationIO.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/js/RichEnumerationIO.ts b/js/RichEnumerationIO.ts index 7c727497..444a9e40 100644 --- a/js/RichEnumerationIO.ts +++ b/js/RichEnumerationIO.ts @@ -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, IOType>(); -const RichEnumerationIO = ( enumerationContainer: RichEnumerationContainer ): IOType => { +const joinKeys = ( keys: string[] ) => keys.join( '|' ); + +const EnumerationIO = ( enumerationContainer: RichEnumerationContainer ): IOType => { const enumeration = enumerationContainer.enumeration; // This caching implementation should be kept in sync with the other parametric IO Type caching implementations. @@ -19,7 +28,7 @@ const RichEnumerationIO = ( 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 ), @@ -28,7 +37,7 @@ const RichEnumerationIO = ( 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 ) } ) } ) ); @@ -37,4 +46,5 @@ const RichEnumerationIO = ( enumerationContainer: Ri return cache.get( enumeration )!; }; -export default RichEnumerationIO; \ No newline at end of file +axon.register( 'EnumerationIO', EnumerationIO ); +export default EnumerationIO; \ No newline at end of file