Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add getListeners to Listenable interface.

R=nicksantos
DELTA=66 (62 added, 0 deleted, 4 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6043


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2402 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
commit f1a52cb6ac7329651d6d5de20a44316557daf9e9 1 parent e211eb9
chrishenry@google.com authored
View
16 closure/goog/events/eventtarget.js
@@ -356,6 +356,22 @@ goog.events.EventTarget.prototype.fireListeners = function(
};
+/** @override */
+goog.events.EventTarget.prototype.getListeners = function(type, capture) {
+ var listenerArray = this.eventTargetListeners_[type];
+ var rv = [];
+ if (listenerArray) {
+ for (var i = 0; i < listenerArray.length; ++i) {
+ var listenerObj = listenerArray[i];
+ if (listenerObj.capture == capture) {
+ rv.push(listenerObj);
+ }
+ }
+ }
+ return rv;
+};
+
+
/**
* Dispatches the given event on the ancestorsTree.
*
View
4 closure/goog/events/eventtarget_test.html
@@ -38,10 +38,14 @@
var dispatchEventFn = function(src, e) {
return src.dispatchEvent(e);
};
+ var getListenersFn = function(src, type, capture) {
+ return src.getListeners(type, capture);
+ };
goog.events.eventTargetTester.setUp(
listenFn, unlistenFn,
listenOnceFn, dispatchEventFn,
+ getListenersFn,
goog.events.eventTargetTester.KeyType.NUMBER,
goog.events.eventTargetTester.UnlistenReturnType.BOOLEAN);
}
View
1  closure/goog/events/eventtarget_via_googevents_test.html
@@ -25,6 +25,7 @@
goog.events.eventTargetTester.setUp(
goog.events.listen, goog.events.unlisten,
goog.events.listenOnce, goog.events.dispatchEvent,
+ goog.events.getListeners,
goog.events.eventTargetTester.KeyType.NUMBER,
goog.events.eventTargetTester.UnlistenReturnType.BOOLEAN);
}
View
1  closure/goog/events/eventtarget_via_w3cinterface_test.html
@@ -34,6 +34,7 @@
goog.events.eventTargetTester.setUp(
listenFn, unlistenFn,
null /* listenOnceFn */, dispatchEventFn,
+ null /* getListenersFn */,
goog.events.eventTargetTester.KeyType.UNDEFINED,
goog.events.eventTargetTester.UnlistenReturnType.UNDEFINED);
}
View
32 closure/goog/events/eventtargettester.js
@@ -44,6 +44,8 @@ goog.require('goog.testing.recordFunction');
* @param {Function} dispatchEventFn Function that, given the same
* signature as goog.events.dispatchEvent, will dispatch the event
* on the given event target.
+ * @param {Function} getListenersFn Function that, given the same
+ * signature as goog.events.getListeners, will retrieve listeners.
* @param {goog.events.eventTargetTester.KeyType} listenKeyType The
* key type returned by listen call.
* @param {goog.events.eventTargetTester.UnlistenReturnType}
@@ -53,12 +55,13 @@ goog.require('goog.testing.recordFunction');
* be set to false.
*/
goog.events.eventTargetTester.setUp = function(
- listenFn, unlistenFn, listenOnceFn, dispatchEventFn,
+ listenFn, unlistenFn, listenOnceFn, dispatchEventFn, getListenersFn,
listenKeyType, unlistenFnReturnType) {
listen = listenFn;
unlisten = unlistenFn;
listenOnce = listenOnceFn;
dispatchEvent = dispatchEventFn;
+ getListeners = getListenersFn;
keyType = listenKeyType;
unlistenReturnType = unlistenFnReturnType;
@@ -156,7 +159,7 @@ var EventType = {
};
-var listen, unlisten, listenOnce, dispatchEvent;
+var listen, unlisten, listenOnce, dispatchEvent, getListeners;
var keyType, unlistenReturnType;
var eventTargets, listeners;
@@ -791,3 +794,28 @@ function testUnlistenAfterListenOnce() {
assertNoOtherListenerIsCalled();
}
+
+
+function testGetListeners() {
+ if (!getListeners) {
+ return;
+ }
+
+ listen(eventTargets[0], EventType.A, listeners[0], true);
+ listen(eventTargets[0], EventType.A, listeners[1], true);
+ listen(eventTargets[0], EventType.A, listeners[2]);
+ listen(eventTargets[0], EventType.A, listeners[3]);
+
+ var l = getListeners(eventTargets[0], EventType.A, true);
+ assertEquals(2, l.length);
+ assertEquals(listeners[0], l[0].listener);
+ assertEquals(listeners[1], l[1].listener);
+
+ l = getListeners(eventTargets[0], EventType.A, false);
+ assertEquals(2, l.length);
+ assertEquals(listeners[2], l[0].listener);
+ assertEquals(listeners[3], l[1].listener);
+
+ l = getListeners(eventTargets[0], EventType.B, true);
+ assertEquals(0, l.length);
+}
View
16 closure/goog/events/listenable.js
@@ -129,7 +129,7 @@ goog.events.Listenable.prototype.dispatchEvent;
/**
- * Fires all registered listeners in this event target for the given
+ * Fires all registered listeners in this listenable for the given
* type and capture mode, passing them the given eventObject. This
* does not perform actual capture/bubble. Only implementors of the
* interface should be using this.
@@ -145,9 +145,21 @@ goog.events.Listenable.prototype.dispatchEvent;
goog.events.Listenable.prototype.fireListeners;
+/**
+ * Gets all listeners in this listenable for the given type and
+ * capture mode.
+ *
+ * @param {string} type The type of the listeners to fire.
+ * @param {boolean} capture The capture mode of the listeners to fire.
+ * @return {!Array.<goog.events.ListenableKey>} An array of registered
+ * listeners.
+ */
+goog.events.Listenable.prototype.getListeners;
+
+
/**
- * A key interface.
+ * An interface that describes a single registered listener.
* @interface
*/
goog.events.ListenableKey = function() {};
Please sign in to comment.
Something went wrong with that request. Please try again.