Skip to content

Commit

Permalink
added a check to avoid trying to remove a listener if it has already …
Browse files Browse the repository at this point in the history
…been removed, see #78
  • Loading branch information
jbphet committed Sep 10, 2019
1 parent 4b4384f commit 79ba0fa
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions js/soundManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ define( require => {
// context becomes more consistent across browsers, it may be possible to simplify this.
if ( !phetAudioContext.isStubbed ) {

// function to remove the listeners, used to avoid code duplication
const removeUserInteractionListeners = () => {
window.removeEventListener( 'touchstart', resumeAudioContext, false );
if ( Display.userGestureEmitter.hasListener( resumeAudioContext ) ) {
Display.userGestureEmitter.removeListener( resumeAudioContext );
}
};

// listener that resumes the audio context
const resumeAudioContext = () => {

Expand All @@ -186,20 +194,18 @@ define( require => {
phetAudioContext.resume()
.then( () => {
phet.log && phet.log( 'resume appears to have succeeded, phetAudioContext.state = ' + phetAudioContext.state );
window.removeEventListener( 'touchstart', resumeAudioContext, false );
Display.userGestureEmitter.removeListener( resumeAudioContext );
removeUserInteractionListeners();
} )
.catch( err => {
const errorMessage = 'error when trying to resume audio context, err = ' + err;
console.err( errorMessage );
alert( errorMessage );
assert && alert( errorMessage );
} );
}
else {

// audio context is already running, no need to listen anymore
window.removeEventListener( 'touchstart', resumeAudioContext, false );
Display.userGestureEmitter.removeListener( resumeAudioContext );
removeUserInteractionListeners();
}
};

Expand Down

0 comments on commit 79ba0fa

Please sign in to comment.