Skip to content

Commit

Permalink
Some cleanup in Input for pointerAddedEmitter instead of pointerAdded…
Browse files Browse the repository at this point in the history
…Listeners, see #1116
  • Loading branch information
jonathanolson committed Mar 12, 2021
1 parent b7ca30e commit 92ace56
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 33 deletions.
34 changes: 4 additions & 30 deletions js/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
*/

import Action from '../../../axon/js/Action.js';
import TinyEmitter from '../../../axon/js/TinyEmitter.js';
import Vector2 from '../../../dot/js/Vector2.js';
import cleanArray from '../../../phet-core/js/cleanArray.js';
import merge from '../../../phet-core/js/merge.js';
Expand Down Expand Up @@ -254,8 +255,8 @@ class Input {
// @public {Array.<Pointer>} - All active pointers.
this.pointers = [];

// TODO: replace this with an Emitter
this.pointerAddedListeners = [];
// @public {TinyEmitter.<Pointer>}
this.pointerAddedEmitter = new TinyEmitter();

// @public {boolean} - Whether we are currently firing events. We need to track this to handle re-entrant cases
// like https://github.com/phetsims/balloons-and-static-electricity/issues/406.
Expand Down Expand Up @@ -939,34 +940,7 @@ class Input {
addPointer( pointer ) {
this.pointers.push( pointer );

// Callback for showing pointer events. Optimized for performance.
if ( this.pointerAddedListeners.length ) {
for ( let i = 0; i < this.pointerAddedListeners.length; i++ ) {
this.pointerAddedListeners[ i ]( pointer );
}
}
}

/**
* Add a listener to be called when a Pointer is added.
* TODO: Just use an emitter
* @public
* @param {function} listener
*/
addPointerAddedListener( listener ) {
this.pointerAddedListeners.push( listener );
}

/**
* Remove a listener being called when a Pointer is added.
* @public
* @param listener
*/
removePointerAddedListener( listener ) {
const index = this.pointerAddedListeners.indexOf( listener );
if ( index !== -1 ) {
this.pointerAddedListeners.splice( index, 1 );
}
this.pointerAddedEmitter.emit( pointer );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/listeners/SwipeListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SwipeListener {
this.handleDown( event );
}
};
input.addPointerAddedListener( pointer => {
input.pointerAddedEmitter.addListener( pointer => {
if ( this.enabled ) {
pointer.addInputListener( this.handleEventListener, true );
}
Expand Down
4 changes: 2 additions & 2 deletions js/overlays/PointerOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class PointerOverlay {
svg.appendChild( circle );
this.pointerSVGContainer.appendChild( svg );
};
display._input.addPointerAddedListener( this.pointerAdded );
display._input.pointerAddedEmitter.addListener( this.pointerAdded );

//if there is already a mouse, add it here
// TODO: if there already other non-mouse touches, could be added here
Expand All @@ -124,7 +124,7 @@ class PointerOverlay {
* @public
*/
dispose() {
this.display._input.removePointerAddedListener( this.pointerAdded );
this.display._input.pointerAddedEmitter.removeListener( this.pointerAdded );
}

/**
Expand Down

0 comments on commit 92ace56

Please sign in to comment.