Skip to content

Commit

Permalink
Simplify logic for checking phetioType and validators, see #204
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 27, 2019
1 parent d096e5b commit 3d5f7b4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions js/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ define( require => {
class Emitter extends PhetioObject {
constructor( options ) {

const phetioTypeSupplied = options && options.hasOwnProperty( 'phetioType' );
const validatorsSupplied = options && options.hasOwnProperty( 'validators' );

options = _.extend( {

// {Array.<Object>|null} - array of "validators" as defined by ValidatorDef.js
Expand Down Expand Up @@ -61,10 +64,11 @@ define( require => {
// keep track of if we get the validators from options, or the phetioType
let validatorsFromTypeIO = false;

// important to be before super call
const phetioTypeSupplied = options.phetioType !== EmitterIOWithNoArgs;
assert && assert( !( phetioTypeSupplied && options.validators.length > 0 ),
'use either phetioType or validators, not both, see EmitterIO to set validators on an instrumented Emitter' );
// important to be before super call. OK to supply neither or one or the other, but not both. That is a NAND.
assert && assert(
!( phetioTypeSupplied && validatorsSupplied ),
'use either phetioType or validators, not both, see EmitterIO to set validators on an instrumented Emitter'
);

// use the phetioType's validators if provided, we know we aren't overwriting here because of the above assertion
if ( phetioTypeSupplied ) {
Expand All @@ -74,7 +78,7 @@ define( require => {

super( options );

validate( options.validators, { valueType: Array } );
assert && validate( options.validators, { valueType: Array } );

if ( assert ) {

Expand Down

0 comments on commit 3d5f7b4

Please sign in to comment.