Skip to content

Commit

Permalink
Gracefully handle removeEventListener when event listener does not ex…
Browse files Browse the repository at this point in the history
…ist, see #6
  • Loading branch information
jessegreenberg committed Mar 9, 2022
1 parent 885bb53 commit 52cad1f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ class SpeechSynthesisUtterance {
*/
removeEventListener( eventType, callback ) {

// Gracefully handle when there is no event type/callback listener. DOM behavior is to do nothing if we try to
// remove an event listener that does not exist, see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
const listenerList = this.listenerMap.get( eventType );
if ( !listenerList || !listenerList.includes( callback ) ) {
throw new Error( 'Trying to remove listener that was not added' );
if ( listenerList && listenerList.includes( callback ) ) {
this.listenerMap.set( eventType, listenerList.filter( listener => listener !== callback ) );
}
else {
this.listenerMap.set( eventType, listenerList.filter( listener => listener !== callback ) );
Expand Down

0 comments on commit 52cad1f

Please sign in to comment.