[Pkg-javascript-commits] [sockjs-client] 98/434: Fixed bug in EventEmitter code, added some tests.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:06 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 117d9040c9cbe30dc1b736dd2b81f1179300370a
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Wed Aug 24 15:29:31 2011 +0100

    Fixed bug in EventEmitter code, added some tests.
---
 lib/reventtarget.js         | 13 +++++++------
 tests/html/src/tests.coffee | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lib/reventtarget.js b/lib/reventtarget.js
index c4431f3..a3b771a 100644
--- a/lib/reventtarget.js
+++ b/lib/reventtarget.js
@@ -1,7 +1,8 @@
-var REventTarget = function() {
-    this._listeners = {};
-};
+var REventTarget = function() {};
 REventTarget.prototype.addEventListener = function (eventType, listener) {
+    if(!this._listeners) {
+         this._listeners = {};
+    }
     if(!(eventType in this._listeners)) {
         this._listeners[eventType] = [];
     }
@@ -9,14 +10,14 @@ REventTarget.prototype.addEventListener = function (eventType, listener) {
     return true;
 };
 REventTarget.prototype.removeEventListener = function (eventType, listener) {
-    if(!(eventType in this._listeners)) {
+    if(!(this._listeners && (eventType in this._listeners))) {
         return false;
     }
     var arr = this._listeners[eventType];
     var idx = arr.indexOf(listener);
     if (idx !== -1) {
         if(arr.length > 1) {
-            this._listeners[eventType] = arr.slice(0, idx).concat( a.slice(idx+1) );
+            this._listeners[eventType] = arr.slice(0, idx).concat( arr.slice(idx+1) );
         } else {
             delete this._listeners[eventType];
         }
@@ -30,7 +31,7 @@ REventTarget.prototype.dispatchEvent = function (event) {
     if (typeof this['on'+t] !== 'undefined') {
         this['on'+t].apply(this, args);
     }
-    if (t in this._listeners) {
+    if (this._listeners && t in this._listeners) {
         for(var i=0; i < this._listeners[t].length; i++) {
             this._listeners[t][i].apply(this, args);
         }
diff --git a/tests/html/src/tests.coffee b/tests/html/src/tests.coffee
index 62efcbb..3970cb8 100644
--- a/tests/html/src/tests.coffee
+++ b/tests/html/src/tests.coffee
@@ -257,3 +257,18 @@ test "amending url", ->
 
     r = new SockJS('http://a:1/abc', [])
     equal(r._base_url, 'http://a:1/abc')
+
+
+test "EventEmitter", ->
+    expect(3)
+    r = new SockJS('//blah/abc', [])
+    r.addEventListener 'message', -> ok(true)
+    r.onmessage = -> fail(true)
+    bluff = -> fail(true)
+    r.addEventListener 'message', bluff
+    r.removeEventListener 'message', bluff
+    r.addEventListener 'message', bluff
+    r.addEventListener 'message', -> ok(true)
+    r.onmessage = -> ok(true)
+    r.removeEventListener 'message', bluff
+    r.dispatchEvent({type:'message'})

-- 
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