Skip to content

Commit

Permalink
Add generics to GroupItemOptions Node and ToggleNode, see #846
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed May 10, 2023
1 parent b96fb1c commit b042e7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
6 changes: 3 additions & 3 deletions js/GroupItemOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Tandem from '../../tandem/js/Tandem.js';
import { Node } from '../../scenery/js/imports.js';

type GroupItemOptions = {
createNode: ( contentTandem: Tandem ) => Node;
type GroupItemOptions<T extends Node = Node> = {
createNode: ( contentTandem: Tandem ) => T;

// If PhET-iO instrumented, tandemName must be supplied to supply the instrumentation. Optional to support
// uninstrumented sims and demos.
Expand All @@ -24,7 +24,7 @@ export default GroupItemOptions;
/**
* Get the nodes for the GroupItemOptions
*/
export function getGroupItemNodes( array: GroupItemOptions[], tandem: Tandem ): Node[] {
export function getGroupItemNodes<T extends Node = Node>( array: GroupItemOptions<T>[], tandem: Tandem ): T[] {
return array.map( item => {

// @ts-expect-error - runtime check to prevent prior pattern, see https://github.com/phetsims/sun/issues/794
Expand Down
20 changes: 7 additions & 13 deletions js/ToggleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import sun from './sun.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import GroupItemOptions, { getGroupItemNodes } from './GroupItemOptions.js';

export type ToggleNodeElement<T> = {
export type ToggleNodeElement<T, N extends Node = Node> = {
value: T; // a value
} & GroupItemOptions;
} & GroupItemOptions<N>;

type SelfOptions = {

Expand All @@ -28,20 +28,12 @@ type SelfOptions = {

export type ToggleNodeOptions = SelfOptions & StrictOmit<NodeOptions, 'children'>;

export default class ToggleNode<T> extends Node {
export default class ToggleNode<T, N extends Node = Node> extends Node {

private readonly disposeToggleNode: () => void;
public readonly nodes: N[];

public constructor( valueProperty: TReadOnlyProperty<T>, elements: ToggleNodeElement<T>[], providedOptions?: ToggleNodeOptions ) {

// assert && assert( Array.isArray( elements ), 'elements should be an array' );
// if ( assert ) {
// elements.forEach( element => {
// const keys = _.keys( element );
// assert && assert( keys.length === 2, 'each element should have two keys' );
// assert && assert( keys[ 0 ] === 'value' || keys[ 1 ] === 'value', 'element should have a value key' );
// } );
// }
public constructor( valueProperty: TReadOnlyProperty<T>, elements: ToggleNodeElement<T, N>[], providedOptions?: ToggleNodeOptions ) {

const options = optionize<ToggleNodeOptions, SelfOptions, NodeOptions>()( {

Expand Down Expand Up @@ -79,6 +71,8 @@ export default class ToggleNode<T> extends Node {
// tandem: options.tandem.createTandem( 'valueProperty' )
// } );

this.nodes = nodes;

this.disposeToggleNode = function() {
valueProperty.unlink( valueListener );
nodes.forEach( node => node.dispose() );
Expand Down

0 comments on commit b042e7d

Please sign in to comment.