Skip to content

Commit

Permalink
Convert Emitter, Action, PhetioGroup to TypeScript, see phetsims/tand…
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 11, 2022
1 parent ffe69e2 commit 8ac9a09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
30 changes: 23 additions & 7 deletions js/model/Circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ class Circuit {
readonly resistorGroup: PhetioGroup<Resistor>;
readonly fuseGroup: PhetioGroup<Fuse>;
readonly seriesAmmeterGroup: PhetioGroup<SeriesAmmeter>;
readonly highResistanceLightBulbGroup: PhetioGroup<LightBulb>
readonly highResistanceLightBulbGroup: PhetioGroup<LightBulb>;
readonly capacitorGroup: PhetioGroup<Capacitor>;
readonly inductorGroup: PhetioGroup<Inductor>;
readonly switchGroup: PhetioGroup<Switch>
readonly switchGroup: PhetioGroup<Switch>;
readonly lightBulbGroup: PhetioGroup<LightBulb>;
private readonly realLightBulbGroup: PhetioGroup<LightBulb>;
private readonly groups: PhetioGroup<CircuitElement>[];
Expand Down Expand Up @@ -282,7 +282,7 @@ class Circuit {
// More sanity checks for the listeners
assert && assert( !vertex.positionProperty.hasListener( emitCircuitChanged ), 'Listener should be removed' );

vertex.isSelectedProperty.unlink( vertex.vertexSelectedPropertyListener );
vertex.isSelectedProperty.unlink( vertex.vertexSelectedPropertyListener! );
vertex.vertexSelectedPropertyListener = null;
} );

Expand Down Expand Up @@ -346,21 +346,21 @@ class Circuit {

// Create vertices for the API validated/baseline circuit elements. These are not present in the vertexGroup and
// hence not transmitted in the state.
const createVertices = ( length: number ) => {
const createVertices: ( l: number ) => [ Vertex, Vertex ] = ( length: number ) => {
const startPosition = new Vector2( -1000, 0 );
return [ new Vertex( startPosition ), new Vertex( startPosition.plusXY( length, 0 ) ) ];
};

// @public {PhetioGroup}
this.wireGroup = new PhetioGroup( ( tandem, startVertex, endVertex ) => {
this.wireGroup = new PhetioGroup<Wire, Vertex, Vertex>( ( tandem, startVertex, endVertex ) => {
return new Wire( startVertex, endVertex, this.wireResistivityProperty, tandem );
}, () => createVertices( WIRE_LENGTH ), {
phetioType: PhetioGroup.PhetioGroupIO( CircuitElement.CircuitElementIO ),
tandem: tandem.createTandem( 'wireGroup' )
} );

// @public {PhetioGroup}
this.batteryGroup = new PhetioGroup( ( tandem, startVertex, endVertex ) => {
this.batteryGroup = new PhetioGroup<Battery, Vertex, Vertex>( ( tandem, startVertex, endVertex ) => {
return new Battery( startVertex, endVertex, this.sourceResistanceProperty, 'normal',
tandem );
}, () => createVertices( BATTERY_LENGTH ), {
Expand Down Expand Up @@ -390,10 +390,12 @@ class Circuit {
} );

// @public {PhetioGroup}
this.resistorGroup = new PhetioGroup(
this.resistorGroup = new PhetioGroup<Resistor, Vertex, Vertex, ResistorType>(
( tandem, startVertex, endVertex, resistorType ) => resistorType === ResistorType.DOG ?
new Dog( startVertex, endVertex, tandem ) :
new Resistor( startVertex, endVertex, resistorType, tandem ),

// @ts-ignore
() => {
const argumentArray: any[] = createVertices( ResistorType.RESISTOR.length );
argumentArray.push( ResistorType.RESISTOR );
Expand Down Expand Up @@ -474,19 +476,33 @@ class Circuit {
tandem: this.includeLabElements ? tandem.createTandem( 'realLightBulbGroup' ) : Tandem.OPT_OUT
} );

// @ts-ignore
this.groups = [
// @ts-ignore
this.wireGroup,
// @ts-ignore
this.batteryGroup,
// @ts-ignore
this.highVoltageBatteryGroup,
// @ts-ignore
this.acVoltageGroup,
// @ts-ignore
this.resistorGroup,
// @ts-ignore
this.fuseGroup,
// @ts-ignore
this.capacitorGroup,
// @ts-ignore
this.inductorGroup,
// @ts-ignore
this.switchGroup,
// @ts-ignore
this.lightBulbGroup,
// @ts-ignore
this.realLightBulbGroup,
// @ts-ignore
this.highResistanceLightBulbGroup,
// @ts-ignore
this.seriesAmmeterGroup
];
this.dirty = false;
Expand Down
2 changes: 1 addition & 1 deletion js/model/Vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Vertex extends PhetioObject {
readonly blackBoxInterfaceProperty: Property<boolean>;
readonly insideTrueBlackBoxProperty: Property<boolean>;
readonly relayerEmitter: Emitter<[]>;
private readonly vertexSelectedPropertyListener: null;
vertexSelectedPropertyListener: ( ( selected: boolean ) => void ) | null;
isDragged: boolean;
static VertexIO: IOType;
outerWireStub: boolean;
Expand Down
26 changes: 13 additions & 13 deletions js/view/CircuitLayerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,42 +329,42 @@ class CircuitLayerNode extends Node {
};

initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Wire, this.wireLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new WireNode( screenView, this, circuitElement as Wire, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new WireNode( screenView, this, circuitElement as Wire, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.wireGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: tandem.createTandem( 'wireNodeGroup' ),
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Battery && e.batteryType === 'normal', this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new BatteryNode( screenView, this, circuitElement as Battery, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new BatteryNode( screenView, this, circuitElement as Battery, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.batteryGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: tandem.createTandem( 'batteryNodeGroup' ),
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Battery && e.batteryType === 'high-voltage', this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new BatteryNode( screenView, this, circuitElement as Battery, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new BatteryNode( screenView, this, circuitElement as Battery, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.batteryGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeLabElements ? tandem.createTandem( 'highVoltageBatteryNodeGroup' ) : Tandem.OPT_OUT,
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof LightBulb && !e.highResistance, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new CCKCLightBulbNode( screenView, this, circuitElement as LightBulb, this.model.isValueDepictionEnabledProperty, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new CCKCLightBulbNode( screenView, this, circuitElement as LightBulb, this.model.isValueDepictionEnabledProperty, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.lightBulbGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: tandem.createTandem( 'lightBulbNodeGroup' ),
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof LightBulb && e.highResistance, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new CCKCLightBulbNode( screenView, this, circuitElement as LightBulb, this.model.isValueDepictionEnabledProperty, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new CCKCLightBulbNode( screenView, this, circuitElement as LightBulb, this.model.isValueDepictionEnabledProperty, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.lightBulbGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeLabElements ? tandem.createTandem( 'highResistanceLightBulbNodeGroup' ) : Tandem.OPT_OUT,
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Resistor, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => {
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => {
if ( circuitElement instanceof Dog ) {
return new DogNode( screenView, this, circuitElement as Dog, this.model.viewTypeProperty, tandem );
}
Expand All @@ -378,42 +378,42 @@ class CircuitLayerNode extends Node {
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Capacitor, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new CapacitorCircuitElementNode( screenView, this, circuitElement as Capacitor, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new CapacitorCircuitElementNode( screenView, this, circuitElement as Capacitor, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.capacitorGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeACElements ? tandem.createTandem( 'capacitorNodeGroup' ) : Tandem.OPT_OUT,
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof SeriesAmmeter, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new SeriesAmmeterNode( screenView, this, circuitElement as SeriesAmmeter, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new SeriesAmmeterNode( screenView, this, circuitElement as SeriesAmmeter, tandem ),
() => [ this.circuit.seriesAmmeterGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeLabElements ? tandem.createTandem( 'seriesAmmeterNodeGroup' ) : Tandem.OPT_OUT,
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Switch, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new SwitchNode( screenView, this, circuitElement as Switch, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new SwitchNode( screenView, this, circuitElement as Switch, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.switchGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: tandem.createTandem( 'switchNodeGroup' ),
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof ACVoltage, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new ACVoltageNode( screenView, this, circuitElement as ACVoltage, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new ACVoltageNode( screenView, this, circuitElement as ACVoltage, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.acVoltageGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeACElements ? tandem.createTandem( 'acVoltageNodeGroup' ) : Tandem.OPT_OUT,
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Fuse, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new FuseNode( screenView, this, circuitElement as Fuse, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new FuseNode( screenView, this, circuitElement as Fuse, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.fuseGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: tandem.createTandem( 'fuseNodeGroup' ),
supportsDynamicState: false
} ) );
initializeCircuitElementType( ( e: CircuitElement ) => e instanceof Inductor, this.fixedCircuitElementLayer,
new PhetioGroup( ( tandem: Tandem, circuitElement: CircuitElement ) => new InductorNode( screenView, this, circuitElement as Inductor, this.model.viewTypeProperty, tandem ),
new PhetioGroup<CircuitElementNode, CircuitElement>( ( tandem: Tandem, circuitElement: CircuitElement ) => new InductorNode( screenView, this, circuitElement as Inductor, this.model.viewTypeProperty, tandem ),
() => [ this.circuit.inductorGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: circuit.includeACElements ? tandem.createTandem( 'inductorNodeGroup' ) : Tandem.OPT_OUT,
Expand Down Expand Up @@ -453,7 +453,7 @@ class CircuitLayerNode extends Node {
}
} );

const vertexNodeGroup = new PhetioGroup<VertexNode>( ( tandem, vertex: Vertex ) => {
const vertexNodeGroup = new PhetioGroup<VertexNode, Vertex>( ( tandem, vertex: Vertex ) => {
return new VertexNode( this, vertex, tandem );
}, [ circuit.vertexGroup.archetype ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
Expand Down

0 comments on commit 8ac9a09

Please sign in to comment.