From 103b83ebafccb595799879292b8d660ba6634383 Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Thu, 3 Mar 2022 21:06:13 -0700 Subject: [PATCH] Convert method to emitter, see https://github.com/phetsims/center-and-spread/issues/45 --- js/common/model/CASModel.ts | 15 +++++++++------ js/common/model/SoccerModel.ts | 25 ++++++++++++------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/js/common/model/CASModel.ts b/js/common/model/CASModel.ts index 39e7348d..dbe502b7 100644 --- a/js/common/model/CASModel.ts +++ b/js/common/model/CASModel.ts @@ -71,6 +71,9 @@ class CASModel { private lastHighlightAnimationStepTime: number; readonly isMedianAnimationCompleteProperty: BooleanProperty; + // TODO: See if TypeScript 4.6 will let us initialize more things here + protected readonly objectValueBecameNonNullEmitter: Emitter<[ CASObject ]>; + constructor( objectType: CASObjectType, maxNumberOfObjects: number, providedOptions: CASModelOptions ) { const options = optionize( { @@ -176,7 +179,7 @@ class CASModel { if ( options.includeCards ) { this.cardModelGroup.createNextElement( casObject ); } - this.objectValueBecameNonNull( casObject ); + this.objectValueBecameNonNullEmitter.emit( casObject ); casObject.valueProperty.unlink( listener ); // Only create the card once, then no need to listen further } }; @@ -214,6 +217,11 @@ class CASModel { this.isMedianAnimationCompleteProperty.value = false; } } ); + + this.objectValueBecameNonNullEmitter = new Emitter<[ CASObject ]>( { + parameters: [ { valueType: CASObject } ] + } ); + this.objectValueBecameNonNullEmitter.addListener( () => this.updateAnimation() ); } updateMeanAndMedian(): void { @@ -336,11 +344,6 @@ class CASModel { } ); } - // TODO: Should this be an emitter? We say yes. - protected objectValueBecameNonNull( casObject: CASObject ): void { - this.updateAnimation(); - } - /** * Clears out the data and the cards */ diff --git a/js/common/model/SoccerModel.ts b/js/common/model/SoccerModel.ts index 74cd0b20..05635b5a 100644 --- a/js/common/model/SoccerModel.ts +++ b/js/common/model/SoccerModel.ts @@ -97,6 +97,18 @@ class SoccerModel extends CASModel { numberOfRemainingKickableObjects => numberOfRemainingKickableObjects > 0 ); this.currentDistribution = SoccerModel.chooseDistribution(); + + this.objectValueBecameNonNullEmitter.addListener( casObject => { + + // If the soccer player that kicked that ball was still in line when the ball lands, they can leave the line now. + if ( this.soccerPlayerGroup.includes( this.ballPlayerMap.get( casObject )! ) ) { + this.advanceLine(); + } + + if ( this.numberOfRemainingObjectsProperty.value > 0 && this.nextBallToKickProperty.value === null ) { + this.nextBallToKickProperty.value = this.createBall(); + } + } ); } static chooseDistribution(): number[] { @@ -267,19 +279,6 @@ class SoccerModel extends CASModel { } } - protected objectValueBecameNonNull( casObject: CASObject ): void { - super.objectValueBecameNonNull( casObject ); - - // If the soccer player that kicked that ball was still in line when the ball lands, they can leave the line now. - if ( this.soccerPlayerGroup.includes( this.ballPlayerMap.get( casObject )! ) ) { - this.advanceLine(); - } - - if ( this.numberOfRemainingObjectsProperty.value > 0 && this.nextBallToKickProperty.value === null ) { - this.nextBallToKickProperty.value = this.createBall(); - } - } - // When a ball lands, or when the next player is supposed to kick (before the ball lands), move the line forward private advanceLine(): void {