Skip to content

Commit

Permalink
refactor action setting in Emitter, make Action.action private, #222
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Apr 19, 2019
1 parent db06752 commit efa4716
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions js/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ define( require => {
class Action extends PhetioObject {

/**
* @param {function|null} action - the function that is called when this Action occurs
* @param {function} action - the function that is called when this Action occurs
* @param {Object} [options]
*/
constructor( action, options ) {
Expand Down Expand Up @@ -120,8 +120,8 @@ define( require => {
!phetioTypeSupplied && Object.freeze( options.validators );
}

// @protected - can be supplied by subclasses after super constructor call completes
this.action = action;
// @private {function}
this._action = action;
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ define( require => {
* @public
*/
emit() {
assert && assert( typeof this.action === 'function', 'action should exist when emit is called' );
assert && assert( typeof this._action === 'function', 'action should exist when emit is called' );
if ( assert && this.validationEnabled ) {
assert( arguments.length === this.validators.length,
`Emitted unexpected number of args. Expected: ${this.validators.length} and received ${arguments.length}`
Expand All @@ -166,7 +166,7 @@ define( require => {
// handle phet-io data stream for the emitted event
this.isPhetioInstrumented() && this.phetioStartEvent( 'emitted', this.getPhetioData.apply( this, arguments ) );

this.action.apply( null, arguments );
this._action.apply( null, arguments );

this.isPhetioInstrumented() && this.phetioEndEvent();
}
Expand Down
12 changes: 6 additions & 6 deletions js/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ define( require => {
options = _.extend( {}, options, { phetioType: EmitterIOWithNoArgs } );
}

super( null, options );
super( function() {
assert && assert( self.tinyEmitter instanceof TinyEmitter,
'Emitter should not emit until after its constructor has completed' );

self.tinyEmitter.emit.apply( self.tinyEmitter, arguments );
}, options );

const self = this;

// @private - provide Emitter functionality via composition
this.tinyEmitter = new TinyEmitter();

// Set the action in the parent type now that we have self, use function to support arguments
this.action = function() {
self.tinyEmitter.emit.apply( self.tinyEmitter, arguments );
};
}

/**
Expand Down

0 comments on commit efa4716

Please sign in to comment.