From 03f3a0c347501e63b62be3349cf1d67aa866a725 Mon Sep 17 00:00:00 2001 From: zepumph Date: Thu, 18 Apr 2019 15:41:15 -0800 Subject: [PATCH] add defaults to extend call; don't assign to passed in options object; rename emitter-> action, https://github.com/phetsims/axon/issues/222 --- js/Action.js | 27 ++++++++++++++++++--------- js/Emitter.js | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/js/Action.js b/js/Action.js index f3df4af2..cd1904a2 100644 --- a/js/Action.js +++ b/js/Action.js @@ -43,24 +43,28 @@ define( require => { options = _.extend( { - // {Array.|null} - array of "validators" as defined by ValidatorDef.js + // {Array.} - array of "validators" as defined by ValidatorDef.js validators: EMPTY_ARRAY, // {boolean} @deprecated, only to support legacy emit1, emit2, emit3 calls. validationEnabled: true, - // phet-io + // phet-io - see PhetioObject.js for doc tandem: Tandem.optional, phetioState: false, - phetioType: ActionIOWithNoArgs // subtypes can override with EmitterIO([...]), see EmitterIO.js + phetioType: ActionIOWithNoArgs, // subtypes can override with ActionIO([...]), see ActionIO.js + phetioPlayback: PhetioObject.DEFAULT_OPTIONS.phetioPlayback, + phetioEventMetadata: PhetioObject.DEFAULT_OPTIONS.phetioEventMetadata }, options ); + const optionsSetByAction = {}; + // phetioPlayback events need to know the order the arguments occur in order to call EmitterIO.emit() // Indicate whether the event is for playback, but leave this "sparse"--only indicate when this happens to be true if ( options.phetioPlayback ) { - options.phetioEventMetadata = options.phetioEventMetadata || {}; - assert && assert( !options.phetioEventMetadata.hasOwnProperty( 'dataKeys' ), 'dataKeys should be supplied by Emitter, not elsewhere' ); - options.phetioEventMetadata.dataKeys = options.phetioType.elements.map( element => element.name ); + optionsSetByAction.phetioEventMetadata = options.phetioEventMetadata || {}; + assert && assert( !optionsSetByAction.phetioEventMetadata.hasOwnProperty( 'dataKeys' ), 'dataKeys should be supplied by Action, not elsewhere' ); + optionsSetByAction.phetioEventMetadata.dataKeys = options.phetioType.elements.map( element => element.name ); } // important to be before super call. OK to supply neither or one or the other, but not both. That is a NAND. @@ -74,7 +78,12 @@ define( require => { // use the phetioType's validators if provided, we know we aren't overwriting here because of the above assertion if ( phetioTypeSupplied ) { - options.validators = options.phetioType.validators; + optionsSetByAction.validators = options.phetioType.validators; + } + + // extend options with values set by Action, only if there are any to set + if ( Object.keys( optionsSetByAction ).length > 0 ) { + options = _.extend( {}, options, optionsSetByAction ); } super( options ); @@ -94,7 +103,7 @@ define( require => { options.validators.forEach( validator => { assert && assert( validator.validateOptionsOnValidateValue !== true, - 'emitter sets its own validateOptionsOnValidateValue for each argument type' + 'Action sets its own validateOptionsOnValidateValue for each argument type' ); validator.validateOptionsOnValidateValue = false; @@ -102,7 +111,7 @@ define( require => { // are shared between instances. Don't assume we "own" the validator if it came from the TypeIO. assert && !phetioTypeSupplied && Object.freeze( validator ); - // validate the options passed in to validate each emitter argument + // validate the options passed in to validate each Action argument assert && ValidatorDef.validateValidator( validator ); } ); diff --git a/js/Emitter.js b/js/Emitter.js index a6d5bfa6..aaf3b7d7 100644 --- a/js/Emitter.js +++ b/js/Emitter.js @@ -36,7 +36,7 @@ define( require => { // If validators and/or phetioType are supplied, the parent will check via assertions that supplied options // are correct. if ( options && !options.validators && !options.phetioType ) { - options.phetioType = EmitterIOWithNoArgs; + options = _.extend( {}, options, { phetioType: EmitterIOWithNoArgs } ); } super( null, options );