Skip to content

Commit

Permalink
Mixin parameter passing testing phetsims/chipper#1127
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Oct 25, 2021
1 parent c6f8ef0 commit b8bda94
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions js/wilder/view/Mixable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
*/

import Node from '../../../../scenery/js/nodes/Node.js';
import Text from '../../../../scenery/js/nodes/Text.js';
import wilder from '../../wilder.js';
import inheritance from '../../../../phet-core/js/inheritance.js';

/* eslint-disable */

function memoize<Key,Value>( func: ( k: Key ) => Value ) {
// Just memoizes first argument
function memoize<Key, Value>( func: ( k: Key, ...args: any[] ) => Value ) {
assert && assert( typeof func === 'function' );

const map = new Map<Key,Value>();
const map = new Map<Key, Value>();

return ( key: Key ) => {
return ( key: Key, ...args: any[] ) => {
if ( map.has( key ) ) {
return map.get( key )!;
}
else {
const value = func( key );
const value = func( key, ...args );
map.set( key, value );
return value;
}
Expand All @@ -31,21 +31,40 @@ function memoize<Key,Value>( func: ( k: Key ) => Value ) {

type Constructor<T = {}> = new ( ...args: any[] ) => T;

const Mixable = memoize( <SuperType extends Constructor>( type: SuperType ) => {
const MIXIN_PARAMETER_COUNT = 1;
const Mixable = memoize( <SuperType extends Constructor>( type: SuperType, superParameterCount: number ) => {
assert && assert( _.includes( inheritance( type ), Node ) );

const result = class extends type {
_someField: string;

// Call args to be (Node, ...args: any[])
constructor( ...args: any[] ) {
super( ...args );
const node = args[ 0 ] as Node;
assert && assert( node instanceof Node );
const superArguments = args.slice( MIXIN_PARAMETER_COUNT ).slice( 0, superParameterCount );
const nodeOptions = args[ superParameterCount + MIXIN_PARAMETER_COUNT ] as ( {} | undefined );

console.log( `mixable passed a node: ${node}` );
console.log( `mixable passed node options: ${JSON.stringify( nodeOptions )}` );

super( ...superArguments );

this._someField = 'Testing';

( this as unknown as Node ).addChild( node );

( this as unknown as Node ).mutate( nodeOptions );
}

get someField(): string { return this._someField; }

set someField( value: string ) { this._someField = value; }

/**
* @public
* @override
*/
setVisible( value: boolean ): this {
console.log( `our bounds: ${( this as unknown as Node ).bounds}` );

Expand All @@ -62,22 +81,26 @@ const Mixable = memoize( <SuperType extends Constructor>( type: SuperType ) => {
} );

/// THIS WILL BE IN EACH MIXING TYPE
class Mixed extends Mixable( Node ) {
class NodeMixed extends Mixable( Node, 0 ) {
constructor( node: Node, options?: {} ) {
// @ts-ignore
super( node, options );
}
}

class TextMixed extends Mixable( Text, 1 ) {
constructor( node: Node, text: string, options?: {} ) {
// @ts-ignore
super( node, text, options );
}
}

//// THIS IS A TEST USAGE
const m = new Mixed();
m.someField = 'foo';
m.pickable = true;

class SubMixed extends Mixed {

}
const s = new SubMixed();
s.someField = 'bar';
s.pickable = true;
const m = new NodeMixed( new Node(), { someField: 'foo' } );
console.log( `my foo: ${m.someField}` );
const t = new TextMixed( new Node(), 'This is TextMixed', { someField: 'bar' } );
console.log( `my bar: ${t.someField}` );

wilder.register( 'Mixable', Mixable );
export { Mixed, SubMixed };
export { NodeMixed, TextMixed };
export default Mixable;

0 comments on commit b8bda94

Please sign in to comment.