sinatra-rocketio-linda 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a21d349cf79dd595121baad063de1ef33c8b957
4
- data.tar.gz: d46fa4cb8f29feae458c0d9df8fae976b89cb5b2
3
+ metadata.gz: 2e7067ada4f786e31b6d1b0a8c8611bd7be70ac5
4
+ data.tar.gz: 4cacd159ab27e7be32ed95652a44e15efc8fe544
5
5
  SHA512:
6
- metadata.gz: 7395d4341d8a46eb373e0757ed0397e07f08b53245bcf2e178a8fbb26c0f83a262d0c8daeb4e2350d1154ae28df3d3bf9ad449bf9481b729763cfb542f38bf27
7
- data.tar.gz: 70ffc23c4d8b74555547a99fdbb91e7e85e4828cd56a72928a47942364fe13a61fe09dbe85d5de7207435349dab47fb2d28d3c4cf418f442404edd1a869527ca
6
+ metadata.gz: d4418b918c92b3da0e8546f32d05db5371137cb6f691c0cb2afbedbce1646ac5d62cab2338892b5b2dac6e16fc0550645b72a3278fbe2406c12b9c212bf56394
7
+ data.tar.gz: 9f507afde5216bb8f310c7bd20b1b3357c2b60f4f654c28468d4a2a3791b2874d415ee39693587c32f38724a639d7b38fb2e644795811b081e2e7b0ab1280807
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.9 2013-05-29
2
+
3
+ * bugfix callback_id duplication in client lib
4
+
1
5
  === 0.0.8 2013-05-27
2
6
 
3
7
  * generate JS lib for browser
data/Rakefile CHANGED
@@ -5,6 +5,14 @@ Rake::TestTask.new do |t|
5
5
  t.pattern = "test/test_*.rb"
6
6
  end
7
7
 
8
+ desc "Start test server"
9
+ task :test_server do
10
+ require File.expand_path 'test/app', File.dirname(__FILE__)
11
+ App.start
12
+ end
13
+
14
+ task :default => :test
15
+
8
16
  desc "generate JavaScript lib for browser"
9
17
  task :jslib do
10
18
  dest = "linda.js"
@@ -33,11 +41,3 @@ task :jslib do
33
41
  system "uglifyjs #{dest} >> #{dest_min}"
34
42
  puts " => #{dest_min}"
35
43
  end
36
-
37
- desc "Start test server"
38
- task :test_server do
39
- require File.expand_path 'test/app', File.dirname(__FILE__)
40
- App.start
41
- end
42
-
43
- task :default => :test
data/lib/js/linda.js CHANGED
@@ -13,6 +13,9 @@ var Linda = function(io, opts){
13
13
  this.name = name;
14
14
  this.linda = self;
15
15
  var space = this;
16
+ var make_callback_id = function(){
17
+ return new Date()-0+"_"+Math.floor(Math.random()*1000000);
18
+ };
16
19
  this.write = function(tuple, opts){
17
20
  if(tuple === null || typeof tuple !== "object") return;
18
21
  if(opts === null || typeof opts === "undefined") opts = {};
@@ -21,21 +24,21 @@ var Linda = function(io, opts){
21
24
  this.read = function(tuple, callback){
22
25
  if(tuple === null || typeof tuple !== "object") return;
23
26
  if(typeof callback !== "function") return;
24
- var callback_id = new Date()-0+"";
27
+ var callback_id = make_callback_id();
25
28
  self.io.once("__linda_read_callback_"+callback_id, callback);
26
29
  self.io.push("__linda_read", [space.name, tuple, callback_id]);
27
30
  };
28
31
  this.take = function(tuple, callback){
29
32
  if(tuple === null || typeof tuple !== "object") return;
30
33
  if(typeof callback !== "function") return;
31
- var callback_id = new Date()-0+"";
34
+ var callback_id = make_callback_id();
32
35
  self.io.once("__linda_take_callback_"+callback_id, callback);
33
36
  self.io.push("__linda_take", [space.name, tuple, callback_id]);
34
37
  };
35
38
  this.watch = function(tuple, callback){
36
39
  if(tuple === null || typeof tuple !== "object") return;
37
40
  if(typeof callback !== "function") return;
38
- var callback_id = new Date()-0+"";
41
+ var callback_id = make_callback_id();
39
42
  self.io.on("__linda_watch_callback_"+callback_id, callback);
40
43
  self.io.push("__linda_watch", [space.name, tuple, callback_id]);
41
44
  };
@@ -18,7 +18,7 @@ module Sinatra
18
18
  @tuplespace = Hash.new{|h,k|
19
19
  h[k] = Sinatra::RocketIO::Linda::Client::TupleSpace.new(k, self)
20
20
  }
21
- end
21
+ end
22
22
 
23
23
  def wait(&block)
24
24
  loop do
@@ -45,7 +45,7 @@ module Sinatra
45
45
  unless [Hash, Array].include? tuple.class
46
46
  raise ArgumentError, "tuple must be Array or Hash"
47
47
  end
48
- callback_id = "#{Time.now.to_i}#{Time.now.usec}"
48
+ callback_id = "#{Time.now.to_i}#{Time.now.usec}_#{(rand*1000000).to_i}"
49
49
  @linda.io.once "__linda_read_callback_#{callback_id}", &block
50
50
  @linda.io.push "__linda_read", [@name, tuple, callback_id]
51
51
  end
@@ -54,7 +54,7 @@ module Sinatra
54
54
  unless [Hash, Array].include? tuple.class
55
55
  raise ArgumentError, "tuple must be Array or Hash"
56
56
  end
57
- callback_id = "#{Time.now.to_i}#{Time.now.usec}"
57
+ callback_id = "#{Time.now.to_i}#{Time.now.usec}_#{(rand*1000000).to_i}"
58
58
  @linda.io.once "__linda_take_callback_#{callback_id}", &block
59
59
  @linda.io.push "__linda_take", [@name, tuple, callback_id]
60
60
  end
@@ -63,7 +63,7 @@ module Sinatra
63
63
  unless [Hash, Array].include? tuple.class
64
64
  raise ArgumentError, "tuple must be Array or Hash"
65
65
  end
66
- callback_id = "#{Time.now.to_i}#{Time.now.usec}"
66
+ callback_id = "#{Time.now.to_i}#{Time.now.usec}_#{(rand*1000000).to_i}"
67
67
  @linda.io.on "__linda_watch_callback_#{callback_id}", &block
68
68
  @linda.io.push "__linda_watch", [@name, tuple, callback_id]
69
69
  end
@@ -1,7 +1,7 @@
1
1
  module Sinatra
2
2
  module RocketIO
3
3
  module Linda
4
- VERSION = "0.0.8"
4
+ VERSION = "0.0.9"
5
5
  end
6
6
  end
7
7
  end
data/linda.js CHANGED
@@ -1,4 +1,4 @@
1
- // Linda.js v0.0.8 (rocketio v0.2.6)
1
+ // Linda.js v0.0.9 (rocketio v0.2.6)
2
2
  // https://github.com/shokai/sinatra-rocketio-linda
3
3
  // (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
4
4
  // The MIT License
@@ -17,6 +17,9 @@ var Linda = function(io, opts){
17
17
  this.name = name;
18
18
  this.linda = self;
19
19
  var space = this;
20
+ var make_callback_id = function(){
21
+ return new Date()-0+"_"+Math.floor(Math.random()*1000000);
22
+ };
20
23
  this.write = function(tuple, opts){
21
24
  if(tuple === null || typeof tuple !== "object") return;
22
25
  if(opts === null || typeof opts === "undefined") opts = {};
@@ -25,21 +28,21 @@ var Linda = function(io, opts){
25
28
  this.read = function(tuple, callback){
26
29
  if(tuple === null || typeof tuple !== "object") return;
27
30
  if(typeof callback !== "function") return;
28
- var callback_id = new Date()-0+"";
31
+ var callback_id = make_callback_id();
29
32
  self.io.once("__linda_read_callback_"+callback_id, callback);
30
33
  self.io.push("__linda_read", [space.name, tuple, callback_id]);
31
34
  };
32
35
  this.take = function(tuple, callback){
33
36
  if(tuple === null || typeof tuple !== "object") return;
34
37
  if(typeof callback !== "function") return;
35
- var callback_id = new Date()-0+"";
38
+ var callback_id = make_callback_id();
36
39
  self.io.once("__linda_take_callback_"+callback_id, callback);
37
40
  self.io.push("__linda_take", [space.name, tuple, callback_id]);
38
41
  };
39
42
  this.watch = function(tuple, callback){
40
43
  if(tuple === null || typeof tuple !== "object") return;
41
44
  if(typeof callback !== "function") return;
42
- var callback_id = new Date()-0+"";
45
+ var callback_id = make_callback_id();
43
46
  self.io.on("__linda_watch_callback_"+callback_id, callback);
44
47
  self.io.push("__linda_watch", [space.name, tuple, callback_id]);
45
48
  };
data/linda.min.js CHANGED
@@ -1,5 +1,5 @@
1
- // Linda.js v0.0.8 (rocketio v0.2.6)
1
+ // Linda.js v0.0.9 (rocketio v0.2.6)
2
2
  // https://github.com/shokai/sinatra-rocketio-linda
3
3
  // (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
4
4
  // The MIT License
5
- var Linda=function(io,opts){var self=this;this.io=null;if(io===null||typeof io==="undefined"){this.io=(new RocketIO).connect()}else{this.io=io}this.opts=opts||{};this.TupleSpace=function(name){if(name===null||typeof name!=="string")name="__default__";this.name=name;this.linda=self;var space=this;this.write=function(tuple,opts){if(tuple===null||typeof tuple!=="object")return;if(opts===null||typeof opts==="undefined")opts={};self.io.push("__linda_write",[space.name,tuple,opts])};this.read=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.once("__linda_read_callback_"+callback_id,callback);self.io.push("__linda_read",[space.name,tuple,callback_id])};this.take=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.once("__linda_take_callback_"+callback_id,callback);self.io.push("__linda_take",[space.name,tuple,callback_id])};this.watch=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.on("__linda_watch_callback_"+callback_id,callback);self.io.push("__linda_watch",[space.name,tuple,callback_id])}}};var RocketIO=function(opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.type=opts.type||null;this.session=opts.session||null;this.channel=null;if(typeof opts.channel!=="undefined"&&opts.channel!==null){this.channel=""+opts.channel}var setting={};this.io=null;var self=this;var ws_close_timer=null;self.on("__connect",function(session_id){self.session=session_id;self.io.push("__channel_id",self.channel);self.emit("connect")});this.connect=function(url){if(typeof url==="string"){$.getJSON(url+"/rocketio/settings",function(res){setting=res;connect_io()});return self}else{return connect_io()}};var connect_io=function(){self.io=function(){if(self.type==="comet")return;if(typeof WebSocketIO!=="function")return;var io=new WebSocketIO;if(typeof setting.websocket==="string")io.url=setting.websocket;io.session=self.session;return io.connect()}()||function(){if(typeof CometIO!=="function")return;var io=new CometIO;if(typeof setting.comet==="string")io.url=setting.comet;io.session=self.session;return io.connect()}();if(typeof self.io==="undefined"){setTimeout(function(){self.emit("error","WebSocketIO and CometIO are not available")},100);return self}if(self.io.url.match(/^ws:\/\/.+/))self.type="websocket";else if(self.io.url.match(/cometio/))self.type="comet";else self.type="unknown";self.io.on("*",function(event_name,args){if(event_name==="connect")event_name="__connect";self.emit(event_name,args)});ws_close_timer=setTimeout(function(){self.close();self.type="comet";connect_io()},3e3);self.once("connect",function(){if(ws_close_timer)clearTimeout(ws_close_timer);ws_close_timer=null});return self};this.close=function(){self.io.close()};this.push=function(type,data){self.io.push(type,data)}};var CometIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;var running=false;var self=this;var post_queue=[];var flush=function(){if(!running||post_queue.length<1)return;var post_data={json:JSON.stringify({session:self.session,events:post_queue})};$.ajax({url:self.url,data:post_data,success:function(data){},error:function(req,stat,e){self.emit("error","CometIO push error")},complete:function(e){},type:"POST",dataType:"json",timeout:1e4});post_queue=[]};setInterval(flush,1e3);this.push=function(type,data){if(!running||!self.session){self.emit("error","CometIO not connected");return}post_queue.push({type:type,data:data})};this.connect=function(){if(running)return self;self.on("__session_id",function(session){self.session=session;self.emit("connect",self.session)});running=true;get();return self};this.close=function(){running=false;self.removeListener("__session_id")};var get=function(){if(!running)return;$.ajax({url:self.url,data:{session:self.session},success:function(data_arr){if(data_arr!==null&&typeof data_arr=="object"&&!!data_arr.length){for(var i=0;i<data_arr.length;i++){var data=data_arr[i];if(data)self.emit(data.type,data.data)}}get()},error:function(req,stat,e){self.emit("error","CometIO get error");setTimeout(get,1e4)},complete:function(e){},type:"GET",dataType:"json",timeout:13e4})}};var WebSocketIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;this.websocket=null;this.connecting=false;var reconnect_timer_id=null;var running=false;var self=this;self.on("__session_id",function(session_id){self.session=session_id;self.emit("connect",self.session)});this.connect=function(){if(typeof WebSocket==="undefined"){self.emit("error","websocket not exists in this browser");return null}self.running=true;var url=self.session?self.url+"/session="+self.session:self.url;self.websocket=new WebSocket(url);self.websocket.onmessage=function(e){try{var data_=JSON.parse(e.data);self.emit(data_.type,data_.data)}catch(e){self.emit("error","WebSocketIO data parse error")}};self.websocket.onclose=function(){if(self.connecting){self.connecting=false;self.emit("disconnect")}if(self.running){reconnect_timer_id=setTimeout(self.connect,1e4)}};self.websocket.onopen=function(){self.connecting=true};return self};this.close=function(){clearTimeout(reconnect_timer_id);self.running=false;self.websocket.close()};this.push=function(type,data){if(!self.connecting){self.emit("error","websocket not connected");return}self.websocket.send(JSON.stringify({type:type,data:data,session:self.session}))}};var EventEmitter=function(){var self=this;this.apply=function(target,prefix){if(!prefix)prefix="";for(var func in self){if(self.hasOwnProperty(func)&&func!=="apply"){target[prefix+func]=this[func]}}};this.__events=new Array;this.on=function(type,listener,opts){if(typeof listener!=="function")return;var event_id=self.__events.length>0?1+self.__events[self.__events.length-1].id:0;var params={id:event_id,type:type,listener:listener};for(i in opts){if(!params[i])params[i]=opts[i]}self.__events.push(params);return event_id};this.once=function(type,listener){self.on(type,listener,{once:true})};this.emit=function(type,data){for(var i=0;i<self.__events.length;i++){var e=self.__events[i];switch(e.type){case type:e.listener(data);if(e.once)e.type=null;break;case"*":e.listener(type,data);if(e.once)e.type=null;break}}self.removeListener()};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":case"object":if(e.type===id_or_type)self.__events.splice(i,1);break}}}};if(typeof module!=="undefined"&&typeof module.exports!=="undefined"){module.exports=EventEmitter}
5
+ var Linda=function(io,opts){var self=this;this.io=null;if(io===null||typeof io==="undefined"){this.io=(new RocketIO).connect()}else{this.io=io}this.opts=opts||{};this.TupleSpace=function(name){if(name===null||typeof name!=="string")name="__default__";this.name=name;this.linda=self;var space=this;var make_callback_id=function(){return new Date-0+"_"+Math.floor(Math.random()*1e6)};this.write=function(tuple,opts){if(tuple===null||typeof tuple!=="object")return;if(opts===null||typeof opts==="undefined")opts={};self.io.push("__linda_write",[space.name,tuple,opts])};this.read=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=make_callback_id();self.io.once("__linda_read_callback_"+callback_id,callback);self.io.push("__linda_read",[space.name,tuple,callback_id])};this.take=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=make_callback_id();self.io.once("__linda_take_callback_"+callback_id,callback);self.io.push("__linda_take",[space.name,tuple,callback_id])};this.watch=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=make_callback_id();self.io.on("__linda_watch_callback_"+callback_id,callback);self.io.push("__linda_watch",[space.name,tuple,callback_id])}}};var RocketIO=function(opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.type=opts.type||null;this.session=opts.session||null;this.channel=null;if(typeof opts.channel!=="undefined"&&opts.channel!==null){this.channel=""+opts.channel}var setting={};this.io=null;var self=this;var ws_close_timer=null;self.on("__connect",function(session_id){self.session=session_id;self.io.push("__channel_id",self.channel);self.emit("connect")});this.connect=function(url){if(typeof url==="string"){$.getJSON(url+"/rocketio/settings",function(res){setting=res;connect_io()});return self}else{return connect_io()}};var connect_io=function(){self.io=function(){if(self.type==="comet")return;if(typeof WebSocketIO!=="function")return;var io=new WebSocketIO;if(typeof setting.websocket==="string")io.url=setting.websocket;io.session=self.session;return io.connect()}()||function(){if(typeof CometIO!=="function")return;var io=new CometIO;if(typeof setting.comet==="string")io.url=setting.comet;io.session=self.session;return io.connect()}();if(typeof self.io==="undefined"){setTimeout(function(){self.emit("error","WebSocketIO and CometIO are not available")},100);return self}if(self.io.url.match(/^ws:\/\/.+/))self.type="websocket";else if(self.io.url.match(/cometio/))self.type="comet";else self.type="unknown";self.io.on("*",function(event_name,args){if(event_name==="connect")event_name="__connect";self.emit(event_name,args)});ws_close_timer=setTimeout(function(){self.close();self.type="comet";connect_io()},3e3);self.once("connect",function(){if(ws_close_timer)clearTimeout(ws_close_timer);ws_close_timer=null});return self};this.close=function(){self.io.close()};this.push=function(type,data){self.io.push(type,data)}};var CometIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;var running=false;var self=this;var post_queue=[];var flush=function(){if(!running||post_queue.length<1)return;var post_data={json:JSON.stringify({session:self.session,events:post_queue})};$.ajax({url:self.url,data:post_data,success:function(data){},error:function(req,stat,e){self.emit("error","CometIO push error")},complete:function(e){},type:"POST",dataType:"json",timeout:1e4});post_queue=[]};setInterval(flush,1e3);this.push=function(type,data){if(!running||!self.session){self.emit("error","CometIO not connected");return}post_queue.push({type:type,data:data})};this.connect=function(){if(running)return self;self.on("__session_id",function(session){self.session=session;self.emit("connect",self.session)});running=true;get();return self};this.close=function(){running=false;self.removeListener("__session_id")};var get=function(){if(!running)return;$.ajax({url:self.url,data:{session:self.session},success:function(data_arr){if(data_arr!==null&&typeof data_arr=="object"&&!!data_arr.length){for(var i=0;i<data_arr.length;i++){var data=data_arr[i];if(data)self.emit(data.type,data.data)}}get()},error:function(req,stat,e){self.emit("error","CometIO get error");setTimeout(get,1e4)},complete:function(e){},type:"GET",dataType:"json",timeout:13e4})}};var WebSocketIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;this.websocket=null;this.connecting=false;var reconnect_timer_id=null;var running=false;var self=this;self.on("__session_id",function(session_id){self.session=session_id;self.emit("connect",self.session)});this.connect=function(){if(typeof WebSocket==="undefined"){self.emit("error","websocket not exists in this browser");return null}self.running=true;var url=self.session?self.url+"/session="+self.session:self.url;self.websocket=new WebSocket(url);self.websocket.onmessage=function(e){try{var data_=JSON.parse(e.data);self.emit(data_.type,data_.data)}catch(e){self.emit("error","WebSocketIO data parse error")}};self.websocket.onclose=function(){if(self.connecting){self.connecting=false;self.emit("disconnect")}if(self.running){reconnect_timer_id=setTimeout(self.connect,1e4)}};self.websocket.onopen=function(){self.connecting=true};return self};this.close=function(){clearTimeout(reconnect_timer_id);self.running=false;self.websocket.close()};this.push=function(type,data){if(!self.connecting){self.emit("error","websocket not connected");return}self.websocket.send(JSON.stringify({type:type,data:data,session:self.session}))}};var EventEmitter=function(){var self=this;this.apply=function(target,prefix){if(!prefix)prefix="";for(var func in self){if(self.hasOwnProperty(func)&&func!=="apply"){target[prefix+func]=this[func]}}};this.__events=new Array;this.on=function(type,listener,opts){if(typeof listener!=="function")return;var event_id=self.__events.length>0?1+self.__events[self.__events.length-1].id:0;var params={id:event_id,type:type,listener:listener};for(i in opts){if(!params[i])params[i]=opts[i]}self.__events.push(params);return event_id};this.once=function(type,listener){self.on(type,listener,{once:true})};this.emit=function(type,data){for(var i=0;i<self.__events.length;i++){var e=self.__events[i];switch(e.type){case type:e.listener(data);if(e.once)e.type=null;break;case"*":e.listener(type,data);if(e.once)e.type=null;break}}self.removeListener()};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":case"object":if(e.type===id_or_type)self.__events.splice(i,1);break}}}};if(typeof module!=="undefined"&&typeof module.exports!=="undefined"){module.exports=EventEmitter}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rocketio-linda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sho Hashimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-27 00:00:00.000000000 Z
11
+ date: 2013-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler