Skip to content

Commit

Permalink
minor updates, #1302
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Sep 6, 2022
1 parent b13641f commit cef8c3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
8 changes: 5 additions & 3 deletions js/LocalizedString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class LocalizedString {
* Take a state from getStateDelta, and apply it.
*/
public setStateDelta( state: LocalizedStringStateDelta ): void {
// Create potential new locales

// Create potential new locales (since locale-specific Properties are lazily created as needed
Object.keys( state ).forEach( locale => this.getLocaleSpecificProperty( locale ) );

this.usedLocales.forEach( locale => {
Expand Down Expand Up @@ -137,7 +138,8 @@ class LocalizedString {
}

private onLocaleOrderChange( localeOrder: string[] ): void {
// Do this in reverse order to AVOID infinite loops, e.g. if localeOrder1=ar,es localeOrder2=es,ar, then we

// Do this in reverse order to AVOID infinite loops (e.g. if localeOrder1=ar,es localeOrder2=es,ar) then we
// could have both TinyOverrideProperties pointing to each other, and they wouldn't be able to get a value
const locales = [
...this.usedLocales,
Expand All @@ -155,7 +157,7 @@ class LocalizedString {
}

/**
* Returns the locale-specific TProperty<string> for any locale (lazily creating it if necessary)
* Returns the locale-specific Property for any locale (lazily creating it if necessary)
*/
public getLocaleSpecificProperty( locale: string ): TProperty<string> {
if ( locale === 'en' ) {
Expand Down
23 changes: 12 additions & 11 deletions js/getStringModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';

// constants
const FALLBACK_LOCALE = 'en';
type PhetioID = string;

// Holds all of our localizedStrings, so that we can save our phet-io string change state
export const localizedStrings: LocalizedString[] = [];
Expand All @@ -35,7 +36,7 @@ window.phet.chipper.setAllStrings = ( str: string ) => {
const StringStateIOType = new IOType( 'StringStateIO', {
isValidValue: () => true,
toStateObject: () => {
const data: Record<string, LocalizedStringStateDelta> = {};
const data: Record<PhetioID, LocalizedStringStateDelta> = {};

localizedStrings.forEach( localizedString => {
const state = localizedString.getStateDelta();
Expand All @@ -46,30 +47,30 @@ const StringStateIOType = new IOType( 'StringStateIO', {
}
} );
return {
// Data nested for a valid schema
data: data
data: data // Data nested for a valid schema
};
},
stateSchema: {
data: ObjectLiteralIO
},
applyState: ( ( ignored, state ) => {
applyState: ( ignored, state ) => {

// When PhetioDynamicElementContainer elements such as PhetioGroup memers add localizedStrings, we wait until
// all of the members have been created before trying to set any of the strings.
const keys = Object.keys( state.data );
keys.forEach( key => {
const match = localizedStrings.find( localizedString => localizedString.property.tandem.phetioID === key );
// Every string in state has to be in localizedStrings to continue
Object.keys( state.data ).forEach( phetioID => {
const match = localizedStrings.find( localizedString => localizedString.property.tandem.phetioID === phetioID );

// When PhetioDynamicElementContainer elements such as PhetioGroup members add localizedStrings, we wait until
// all of the members have been created (populating localizedStrings) before trying to set any of the strings.
if ( !match ) {
throw new CouldNotYetDeserializeError();
}
} );

// We need to iterate through every string, since it might need to revert back to "initial" state
// We need to iterate through every string in this runtime, since it might need to revert back to "initial" state.
localizedStrings.forEach( localizedString => {
localizedString.setStateDelta( state.data[ localizedString.property.tandem.phetioID ] || {} );
} );
} )
}
} );

PhetioObject.create( {
Expand Down

0 comments on commit cef8c3f

Please sign in to comment.