From 719ad16834e6b8ed2d5fc554fb568f65bbcd1472 Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Tue, 8 Feb 2022 11:30:34 -0700 Subject: [PATCH] Mixable testing for https://github.com/phetsims/scenery/issues/1349 --- js/wilder/view/Mixable.ts | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/js/wilder/view/Mixable.ts b/js/wilder/view/Mixable.ts index b182462..7c66203 100644 --- a/js/wilder/view/Mixable.ts +++ b/js/wilder/view/Mixable.ts @@ -6,12 +6,13 @@ * @author Jonathan Olson */ -import { Node } from '../../../../scenery/js/imports.js'; -import { Text } from '../../../../scenery/js/imports.js'; +import { Node, Text } from '../../../../scenery/js/imports.js'; import wilder from '../../wilder.js'; import inheritance from '../../../../phet-core/js/inheritance.js'; import merge from '../../../../phet-core/js/merge.js'; import extend from '../../../../phet-core/js/extend.js'; +import Constructor from '../../../../phet-core/js/Constructor.js'; +import Property, { PropertyOptions } from '../../../../axon/js/Property.js'; // Just memoizes first argument. function memoize( func: ( k: Key, ...args: any[] ) => Value ) { @@ -31,7 +32,36 @@ function memoize( func: ( k: Key, ...args: any[] ) => Value ) { }; } -type Constructor = new ( ...args: any[] ) => T; +const GenericMix = memoize( ( type: SuperType ) => { + const result = class extends type { + _someField: string; + + constructor( ...args: any[] ) { + super( ...args ); + + this._someField = 'Testing'; + } + + get someField(): string { return this._someField; } + + set someField( value: string ) { this._someField = value; } + }; + + return result; +} ); + +class PropertyMixed extends GenericMix( Property ) { + constructor( value: T, options?: PropertyOptions ) { + super( value, options ); + } +} + +const pMixed1 = new PropertyMixed( 5 ); +console.log( pMixed1.value, pMixed1.someField ); + +const pMixed2 = new PropertyMixed( 'hi' ); +console.log( pMixed2.value, pMixed2.someField ); + const MIXIN_PARAMETER_COUNT = 1; const Mixable = memoize( ( type: SuperType, superParameterCount: number ) => {