Permalink
Browse files

Remove opt_capt from goog.events.removeAll.

Use goog.events.listeners_ as source of truth for
goog.events.removeAll when opt_obj is not specified. While
this is supposedly less performant, goog.events.removeAll() is
not meant to be used often and hence it should not matter if
it is performant or not. This should make it easier to
implement delegation to goog.events.Listenable.

R=nicksantos
DELTA=35 (17 added, 13 deleted, 5 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6045


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2404 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
1 parent 5c38616 commit 56d47fb92e874e81f123db762a0c216cd6c857f6 chrishenry@google.com committed Dec 20, 2012
Showing with 22 additions and 18 deletions.
  1. +6 −18 closure/goog/events/events.js
  2. +16 −0 closure/goog/events/events_test.html
@@ -171,6 +171,7 @@ goog.events.listen = function(src, type, listener, opt_capt, opt_handler) {
* false).
* @param {Object=} opt_handler Element in whose scope to call the listener.
* @return {?number} Unique key for the listener.
+ * @private
*/
goog.events.listen_ = function(
src, type, listener, callOnce, opt_capt, opt_handler) {
@@ -548,43 +549,30 @@ goog.events.cleanUp_ = function(type, capture, srcUid, listenerArray) {
*
* @param {Object=} opt_obj Object to remove listeners from.
* @param {string=} opt_type Type of event to, default is all types.
- * @param {boolean=} opt_capt Whether to remove the listeners from the capture
- * or bubble phase. If unspecified, will remove both.
* @return {number} Number of listeners removed.
*/
-goog.events.removeAll = function(opt_obj, opt_type, opt_capt) {
+goog.events.removeAll = function(opt_obj, opt_type) {
var count = 0;
var noObj = opt_obj == null;
var noType = opt_type == null;
- var noCapt = opt_capt == null;
- opt_capt = !!opt_capt;
if (!noObj) {
var srcUid = goog.getUid(/** @type {Object} */ (opt_obj));
if (goog.events.sources_[srcUid]) {
var sourcesArray = goog.events.sources_[srcUid];
for (var i = sourcesArray.length - 1; i >= 0; i--) {
var listener = sourcesArray[i];
- if ((noType || opt_type == listener.type) &&
- (noCapt || opt_capt == listener.capture)) {
+ if (noType || opt_type == listener.type) {
goog.events.unlistenByKey(listener.key);
count++;
}
}
}
} else {
- // Loop over the sources_ map instead of over the listeners_ since it is
- // smaller which results in fewer allocations.
- goog.object.forEach(goog.events.sources_, function(listeners) {
- for (var i = listeners.length - 1; i >= 0; i--) {
- var listener = listeners[i];
- if ((noType || opt_type == listener.type) &&
- (noCapt || opt_capt == listener.capture)) {
- goog.events.unlistenByKey(listener.key);
- count++;
- }
- }
+ goog.object.forEach(goog.events.listeners_, function(listener, key) {
+ goog.events.unlistenByKey(key);
+ count++;
});
}
@@ -511,6 +511,22 @@
assertEquals(0, listener.getCallCount());
}
+
+ function testRemoveAll() {
+ var listener = goog.testing.recordFunction();
+ var div = document.createElement('div');
+
+ goog.events.listenOnce(document.body, 'click', listener);
+ goog.events.listen(document.body, 'click', listener);
+ goog.events.listenOnce(div, 'click', listener);
+ goog.events.listen(div, 'click', listener);
+
+ goog.events.removeAll();
+
+ dispatchClick(document.body);
+ dispatchClick(div);
+ assertEquals(0, listener.getCallCount());
+ }
</script>
</body>
</html>

0 comments on commit 56d47fb

Please sign in to comment.