lib/js/cometio.js in sinatra-cometio-0.0.3 vs lib/js/cometio.js in sinatra-cometio-0.0.4

- old
+ new

@@ -21,11 +21,11 @@ } ); }; this.connect = function(){ - self.on("set_session_id", function(session){ + self.on("__session_id", function(session){ self.session = session; self.emit("connect", self.session); }); self.get(); return self; @@ -55,18 +55,37 @@ ); }; this.events = new Array(); this.on = function(type, listener){ - if(typeof listener === "function"){ - self.events.push({type: type, listener: listener}); - } + if(typeof listener !== "function") return; + var event_id = self.events.length > 0 ? 1 + self.events[self.events.length-1].id : 0 + self.events.push({ + id: event_id, + type: type, + listener: listener + }); + return event_id; }; this.emit = function(type, data){ for(var i = 0; i < self.events.length; i++){ var e = self.events[i]; if(e.type == type) e.listener(data); + } + }; + + this.removeListener = function(id_or_type){ + for(var i = self.events.length-1; i >= 0; i--){ + var e = self.events[i]; + switch(typeof id_or_type){ + case "number": + if(e.id == id_or_type) self.events.splice(i,1); + break + case "string": + if(e.type == id_or_type) self.events.splice(i,1); + break + } } }; };