Permalink
Browse files

Ensure that unlisten/unlistenByKey does not fail when called

on disposed EventTarget. This turns out to be pretty common
when you have a disposed event target stored in an
EventHandler.

R=nicksantos
DELTA=21 (17 added, 4 deleted, 0 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6199


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2469 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
1 parent 6e8cc25 commit 1c8cd970d992d664b96624094ed1dc03f965f8e2 chrishenry@google.com committed Jan 27, 2013
Showing with 17 additions and 4 deletions.
  1. +0 −4 closure/goog/events/eventtarget.js
  2. +17 −0 closure/goog/events/eventtargettester.js
@@ -302,8 +302,6 @@ goog.events.EventTarget.prototype.listenInternal_ = function(
/** @override */
goog.events.EventTarget.prototype.unlisten = function(
type, listener, opt_useCapture, opt_listenerScope) {
- goog.asserts.assert(!this.isDisposed());
-
if (!(type in this.eventTargetListeners_)) {
return false;
}
@@ -323,8 +321,6 @@ goog.events.EventTarget.prototype.unlisten = function(
/** @override */
goog.events.EventTarget.prototype.unlistenByKey = function(key) {
- goog.asserts.assert(!this.isDisposed());
-
var type = key.type;
if (!(type in this.eventTargetListeners_)) {
return false;
@@ -386,6 +386,23 @@ function testDisposingEventTargetRemovesListeners() {
}
+/**
+ * Unlisten/unlistenByKey should still work after disposal. There are
+ * many circumstances when this is actually necessary. For example, a
+ * user may have listened to an event target and stored the key
+ * (e.g. in a goog.events.EventHandler) and only unlisten after the
+ * target has been disposed.
+ */
+function testUnlistenWorksAfterDisposal() {
+ var key = listen(eventTargets[0], EventType.A, listeners[0]);
+ goog.dispose(eventTargets[0]);
+ unlisten(eventTargets[0], EventType.A, listeners[1]);
+ if (unlistenByKey) {
+ unlistenByKey(eventTargets[0], key);
+ }
+}
+
+
function testRemovingListener() {
var ret1 = unlisten(eventTargets[0], EventType.A, listeners[0]);
listen(eventTargets[0], EventType.A, listeners[0]);

0 comments on commit 1c8cd97

Please sign in to comment.