From 2da6c6173a0ed6bedc1dad8a5fffac27faaf5259 Mon Sep 17 00:00:00 2001 From: samreid Date: Tue, 2 Apr 2019 19:01:41 -0600 Subject: [PATCH] Eliminate index from Events/Node off/offStatic, see https://github.com/phetsims/axon/issues/199 --- js/Events.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/js/Events.js b/js/Events.js index 9c1acb34..3713e3b9 100644 --- a/js/Events.js +++ b/js/Events.js @@ -67,45 +67,39 @@ define( function( require ) { }, /** - * Remove a listener added with on() from the specified event type. Does nothing if the listener did not exist. + * Remove a listener added with on() from the specified event type. * @param {string} eventName the name for the event channel * @param {function} callback + * @param {boolean} [assertListenerExists] - if true, will throw errors if the listener doesn't exist * @public */ - off: function( eventName, callback ) { + off: function( eventName, callback, assertListenerExists = true ) { assert && assert( typeof eventName === 'string', 'eventName should be a string' ); assert && assert( typeof callback === 'function', 'callback should be a function' ); + assert && assertListenerExists && assert( this._eventListeners[ eventName ], 'eventName should be defined' ); - var index = -1; - if ( this._eventListeners[ eventName ] ) { - index = this._eventListeners[ eventName ].indexOf( callback ); - if ( index !== -1 ) { - this._eventListeners[ eventName ].splice( index, 1 ); - } - } + const index = this._eventListeners[ eventName ].indexOf( callback ); + assert && assertListenerExists && assert( index >= 0, 'listener should be here' ); - return index; // so we can tell if we actually removed a listener + this._eventListeners[ eventName ].splice( index, 1 ); }, /** - * Remove a listener added with onStatic() from the specified event type. Does nothing if the listener did not exist. + * Remove a listener added with onStatic() from the specified event type. * @param {string} eventName the name for the event channel * @param {function} callback + * @param {boolean} [assertListenerExists] - if true, will throw errors if the listener doesn't exist * @public */ - offStatic: function( eventName, callback ) { + offStatic: function( eventName, callback, assertListenerExists = true ) { assert && assert( typeof eventName === 'string', 'eventName should be a string' ); assert && assert( typeof callback === 'function', 'callback should be a function' ); + assert && assertListenerExists && assert( this._staticEventListeners[ eventName ], 'eventName should exist' ); - var index = -1; - if ( this._staticEventListeners[ eventName ] ) { - index = this._staticEventListeners[ eventName ].indexOf( callback ); - if ( index !== -1 ) { - this._staticEventListeners[ eventName ].splice( index, 1 ); - } - } + const index = this._staticEventListeners[ eventName ].indexOf( callback ); + assert && assertListenerExists && assert( index >= 0, 'listener not found' ); - return index; // so we can tell if we actually removed a listener + this._staticEventListeners[ eventName ].splice( index, 1 ); }, /**