[Pkg-javascript-commits] [sockjs-client] 147/434: addEventListener doesn't check if listener was already added

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:10 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit ee5886edfd4862944a3ccaacf5343654bcda72b1
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Wed Sep 14 10:06:17 2011 +0100

    addEventListener doesn't check if listener was already added
---
 lib/reventtarget.js         | 18 +++++++++++++-----
 tests/html/src/tests.coffee | 11 ++++++++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/lib/reventtarget.js b/lib/reventtarget.js
index a88ead0..0b5eaeb 100644
--- a/lib/reventtarget.js
+++ b/lib/reventtarget.js
@@ -1,3 +1,6 @@
+/* Simplified implementation of DOM2 EventTarget.
+ *   http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
+ */
 var REventTarget = function() {};
 REventTarget.prototype.addEventListener = function (eventType, listener) {
     if(!this._listeners) {
@@ -6,12 +9,16 @@ REventTarget.prototype.addEventListener = function (eventType, listener) {
     if(!(eventType in this._listeners)) {
         this._listeners[eventType] = [];
     }
-    this._listeners[eventType].push(listener);
-    return true;
+    var arr = this._listeners[eventType];
+    if(utils.arrIndexOf(arr, listener) === -1) {
+        arr.push(listener);
+    }
+    return;
 };
+
 REventTarget.prototype.removeEventListener = function (eventType, listener) {
     if(!(this._listeners && (eventType in this._listeners))) {
-        return false;
+        return;
     }
     var arr = this._listeners[eventType];
     var idx = utils.arrIndexOf(arr, listener);
@@ -21,10 +28,11 @@ REventTarget.prototype.removeEventListener = function (eventType, listener) {
         } else {
             delete this._listeners[eventType];
         }
-        return true;
+        return;
     }
-    return false;
+    return;
 };
+
 REventTarget.prototype.dispatchEvent = function (event) {
     var t = event.type;
     var args = Array.prototype.slice.call(arguments, 0);
diff --git a/tests/html/src/tests.coffee b/tests/html/src/tests.coffee
index 0abb942..db31d84 100644
--- a/tests/html/src/tests.coffee
+++ b/tests/html/src/tests.coffee
@@ -302,7 +302,7 @@ test "amending url", ->
 
 
 test "EventEmitter", ->
-    expect(3)
+    expect(4)
     r = new SockJS('//blah/abc', [])
     r.addEventListener 'message', -> ok(true)
     r.onmessage = -> fail(true)
@@ -315,6 +315,15 @@ test "EventEmitter", ->
     r.removeEventListener 'message', bluff
     r.dispatchEvent({type:'message'})
 
+    # Adding the same eventlistener should be indempotent (sockjs-client #4).
+    single = -> ok(true)
+    r.addEventListener 'close', single
+    r.addEventListener 'close', single
+    r.dispatchEvent({type:'close'}) # 1 callback run
+    r.removeEventListener 'close', single
+    r.dispatchEvent({type:'close'}) # 0 runs
+
+
 chunking_test_factory = (counter) ->
     return ->
         expect(counter)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list