Skip to content

Commit

Permalink
rename Emitter's argumentTypes -> validators, #212
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Feb 23, 2019
1 parent 6316bfc commit ca5138a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions js/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ define( require => {

options = _.extend( {

// {Array.<Object>|null} - array of "Validator Options" Objects that hold options for how to validate each
// argument, see ValidatorDef.js for details.
argumentTypes: EMPTY_ARRAY,
// {Array.<Object>|null} - array of "validators" as defined by ValidatorDef.js
validators: EMPTY_ARRAY,

tandem: Tandem.optional,
phetioState: false,
Expand Down Expand Up @@ -61,12 +60,13 @@ define( require => {

super( options );

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

if ( assert ) {

// Iterate through all argumentType validator options and make sure that they won't validate options on validating value
options.argumentTypes.forEach( validatorOptions => {
// Iterate through each validator and make sure that it won't validate options on validating value. This is
// mainly done for performance
options.validators.forEach( validatorOptions => {
assert && assert(
validatorOptions.validateOptionsOnValidateValue === undefined,
'emitter sets its own validateOptionsOnValidateValue for each argument type'
Expand All @@ -81,17 +81,17 @@ define( require => {
} );

// Changing after construction indicates a logic error
assert && Object.freeze( options.argumentTypes );
assert && Object.freeze( options.validators );
}

// @private {function|false}
this.validate = assert && function() {
assert(
arguments.length === options.argumentTypes.length,
`Emitted unexpected number of args. Expected: ${options.argumentTypes.length} and received ${arguments.length}`
arguments.length === options.validators.length,
`Emitted unexpected number of args. Expected: ${options.validators.length} and received ${arguments.length}`
);
for ( let i = 0; i < options.argumentTypes.length; i++ ) {
validate( arguments[ i ], options.argumentTypes[ i ] );
for ( let i = 0; i < options.validators.length; i++ ) {
validate( arguments[ i ], options.validators[ i ] );
}
};

Expand Down Expand Up @@ -231,7 +231,7 @@ define( require => {
* Emits a single event. This method is called many times in a simulation and must be well-optimized. Listeners
* are notified in the order they were added via addListener, though it is poor practice to rely on the order
* of listener notifications.
* @params - expected parameters are based on options.argumentTypes, see constructor
* @params - expected parameters are based on options.validators, see constructor
* @public
*/
emit() {
Expand Down
8 changes: 4 additions & 4 deletions js/EmitterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ define( require => {

assert.ok( true, 'Token test in case assertions are disabled, because each test must have at least one assert.' );
const e1 = new Emitter( {
argumentTypes: [ { valueType: 'number' } ]
validators: [ { valueType: 'number' } ]
} );

e1.emit( 1 );
Expand All @@ -34,7 +34,7 @@ define( require => {

// emitting with an object as parameter
const e2 = new Emitter( {
argumentTypes: [ { valueType: Emitter }, { valueType: Object }, { valueType: 'function' } ]
validators: [ { valueType: Emitter }, { valueType: Object }, { valueType: 'function' } ]
} );

e2.emit( new Emitter(), {}, () => {} );
Expand All @@ -50,7 +50,7 @@ define( require => {
}

const e3 = new Emitter( {
argumentTypes: [ { valueType: 'number' }, { isValidValue: v => v === null || typeof v === 'string' } ]
validators: [ { valueType: 'number' }, { isValidValue: v => v === null || typeof v === 'string' } ]
} );

e3.emit( 1, 'hi' );
Expand Down Expand Up @@ -124,7 +124,7 @@ define( require => {
const entries = [];

const emitter = new Emitter( {
argumentTypes: [ { valueType: 'string' } ]
validators: [ { valueType: 'string' } ]
} );

const a = arg => {
Expand Down

0 comments on commit ca5138a

Please sign in to comment.