sinatra-cometio 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.4 2012-11-16
2
+
3
+ * removeListener on client side
4
+
1
5
  === 0.0.3 2012-11-16
2
6
 
3
7
  * emit/on "error" event on client side.
data/README.rdoc CHANGED
@@ -74,16 +74,27 @@ Client Side
74
74
 
75
75
  Server Side
76
76
 
77
- id = CometIO.on :chat do |data, from|
77
+ event_id = CometIO.on :chat do |data, from|
78
78
  puts "#{data} - from#{from}"
79
79
  end
80
- CometIO.removeListener id
80
+ CometIO.removeListener event_id
81
81
 
82
82
  or
83
83
 
84
84
  CometIO.removeListener :chat # remove all chat listener
85
85
 
86
86
 
87
+ Client Side
88
+
89
+ var event_id = io.on("error", function(err){
90
+ console.error("CometIO error : "err);
91
+ });
92
+ io.removeListener(event_id);
93
+
94
+ or
95
+
96
+ io.removeListener("error"); // remove all error listener
97
+
87
98
  == Sample App
88
99
 
89
100
  chat app
data/lib/js/cometio.js CHANGED
@@ -23,7 +23,7 @@ var CometIO = function(){
23
23
  };
24
24
 
25
25
  this.connect = function(){
26
- self.on("set_session_id", function(session){
26
+ self.on("__session_id", function(session){
27
27
  self.session = session;
28
28
  self.emit("connect", self.session);
29
29
  });
@@ -57,9 +57,14 @@ var CometIO = function(){
57
57
 
58
58
  this.events = new Array();
59
59
  this.on = function(type, listener){
60
- if(typeof listener === "function"){
61
- self.events.push({type: type, listener: listener});
62
- }
60
+ if(typeof listener !== "function") return;
61
+ var event_id = self.events.length > 0 ? 1 + self.events[self.events.length-1].id : 0
62
+ self.events.push({
63
+ id: event_id,
64
+ type: type,
65
+ listener: listener
66
+ });
67
+ return event_id;
63
68
  };
64
69
 
65
70
  this.emit = function(type, data){
@@ -69,4 +74,18 @@ var CometIO = function(){
69
74
  }
70
75
  };
71
76
 
77
+ this.removeListener = function(id_or_type){
78
+ for(var i = self.events.length-1; i >= 0; i--){
79
+ var e = self.events[i];
80
+ switch(typeof id_or_type){
81
+ case "number":
82
+ if(e.id == id_or_type) self.events.splice(i,1);
83
+ break
84
+ case "string":
85
+ if(e.type == id_or_type) self.events.splice(i,1);
86
+ break
87
+ }
88
+ }
89
+ };
90
+
72
91
  };
@@ -9,7 +9,7 @@ class CometIO
9
9
  def self.sessions
10
10
  @@sessions ||= Hash.new{|h,session_id|
11
11
  h[session_id] = {
12
- :queue => [{:type => :set_session_id, :data => session_id}],
12
+ :queue => [{:type => :__session_id, :data => session_id}],
13
13
  :stream => nil,
14
14
  :last => nil
15
15
  }
@@ -1,3 +1,3 @@
1
1
  module SinatraCometio
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-cometio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -194,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  segments:
196
196
  - 0
197
- hash: 3190168265440038000
197
+ hash: 3731248774010908495
198
198
  required_rubygems_version: !ruby/object:Gem::Requirement
199
199
  none: false
200
200
  requirements: