Skip to content

Commit

Permalink
Convert from validate to validators and validationEnabled, see #204
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 27, 2019
1 parent 3d5f7b4 commit 66483a4
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions js/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ define( require => {

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

// @private - Note: one test indicates stripping this out via assert && in builds may save around 300kb heap
this.validators = options.validators;

// @private - short circuit for emit1, emit2, emit3. Can be removed when those are gone.
this.validationEnabled = true;

if ( assert ) {

// Iterate through each validator and make sure that it won't validate options on validating value. This is
Expand All @@ -103,17 +109,6 @@ define( require => {
assert && !validatorsFromTypeIO && Object.freeze( options.validators );
}

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

// @private {function[]} - the listeners that will be called on emit
this.listeners = [];

Expand Down Expand Up @@ -254,7 +249,16 @@ define( require => {
* @public
*/
emit() {
assert && this.validate && this.validate.apply( null, arguments );
if ( assert && this.validationEnabled ) {
assert(
arguments.length === this.validators.length,
`Emitted unexpected number of args. Expected: ${this.validators.length} and received ${arguments.length}`
);
for ( let i = 0; i < this.validators.length; i++ ) {
validate( arguments[ i ], this.validators[ i ] );
}
}

assert && this.first && assert( this.listeners.indexOf( this.first ) === 0, 'first should be at the beginning' );
assert && this.last && assert( this.listeners.indexOf( this.last ) === this.listeners.length - 1, 'last should be ' +
'at the end' );
Expand Down Expand Up @@ -284,7 +288,7 @@ define( require => {
* @deprecated - please use emit()
*/
emit1( arg0 ) {
this.validate = null;
this.validationEnabled = false; // Disable validation until emit() is used properly
this.emit( arg0 );
}

Expand All @@ -296,7 +300,7 @@ define( require => {
* @deprecated - please use emit()
*/
emit2( arg0, arg1 ) {
this.validate = null;
this.validationEnabled = false; // Disable validation until emit() is used properly
this.emit( arg0, arg1 );
}

Expand All @@ -309,7 +313,7 @@ define( require => {
* @deprecated - please use emit()
*/
emit3( arg0, arg1, arg2 ) {
this.validate = null;
this.validationEnabled = false; // Disable validation until emit() is used properly
this.emit( arg0, arg1, arg2 );
}

Expand Down

0 comments on commit 66483a4

Please sign in to comment.