diff --git a/phoenix-socket.js b/phoenix-socket.js index 79bef62..f1256f5 100644 --- a/phoenix-socket.js +++ b/phoenix-socket.js @@ -27,12 +27,8 @@ App.Phoenix.Socket = Ember.Controller.extend({ addTopic: function(channelName, topicName, channel, message, callback) { var topicKey = channelName + ":" + topicName; - if (this.get('channels').get(topicKey)) { - callback(this.get('channels').get(topicKey), true); - } else { - this.get('channels').set(topicKey, channel); - this.get('socket').join(channelName, topicName, message || {}, this.get('handleAddTopic').bind(this, topicKey, callback)); - } + this.get('channels').set(topicKey, channel); + this.get('socket').join(channelName, topicName, message || {}, this.get('handleAddTopic').bind(this, topicKey, callback)); }, handleAddTopic: function(topicKey, callback, channel) { channel.on("join", function(res) { diff --git a/phoenix-socket.min.js b/phoenix-socket.min.js index 665a4ed..e2d2701 100644 --- a/phoenix-socket.min.js +++ b/phoenix-socket.min.js @@ -1 +1 @@ -var PhoenixSocket=function(e){window.ENV=window.ENV||{};e=e||{};ENV.Phoenix={};ENV.Phoenix.endpoint=e.endpoint||location.protocol.match(/^https/)?"wss://"+location.host+"/ws":"ws://"+location.host+"/ws";ENV.Phoenix.appName=e.appName||"App";ENV.Phoenix.storeName=e.storeName||"DS";ENV.Phoenix.init=e.init||{doInit:true,channel:"data",topic:"data",params:null};var t=window[ENV.Phoenix.storeName],n=window[ENV.Phoenix.appName];n.Phoenix={};n.Phoenix.Socket=Ember.Controller.extend({socket:null,channels:null,init:function(){this.set("channels",Ember.Map.create());var e=new Phoenix.Socket(ENV.Phoenix.endpoint);e.onClose(this.get("handleClose").bind(this));this.set("socket",e)},addTopic:function(e,t,n,r,i){var s=e+":"+t;if(this.get("channels").get(s)){i(this.get("channels").get(s),true)}else{this.get("channels").set(s,n);this.get("socket").join(e,t,r||{},this.get("handleAddTopic").bind(this,s,i))}},handleAddTopic:function(e,t,n){n.on("join",function(e){t(n,e)}.bind(this));n.on("error",function(e){t("error",e)})},handleClose:function(e){this.get("channels").forEach(function(t){t.onClose(e)})}});n.Phoenix.Channel=t.PhoenixSocketAdapter=t.RESTAdapter.extend({needs:["phoenix","session"],_channel:null,_topic:null,_initialized:false,_transactions:{},_socket:null,_header:null,setSocket:function(e,t){this.set("_channel",e);this.set("_topic",t)},join:function(e){var t=n.Phoenix.Transaction.create({params:e});var r=this.get("_channel")+":"+this.get("_topic");this.container.lookup("service:phoenix").addTopic(this.get("_channel"),this.get("_topic"),this,e,this.get("onJoin").bind(this,t));return t.promise},onJoin:function(e,t,n){if(n.success){t.on(this.get("_topic"),this.get("onData").bind(this));this.set("_socket",t);this.set("_initialized",true);this.get("unloadQueue").call(this);e.success(n)}else{e.error(n)}e.destroy()},onData:function(e){var t=this.get("_transactions")[e.uuid];if(e.success){t.success(e.message)}else{t.error(e.message)}t.destroy();delete t},onClose:function(e){console.log("channel closed: "+this.get("_topic"))},unloadQueue:function(){var e=this.get("_transactions");for(var t in e){this.get("_socket").send(this.get("_topic"),e[t].payload())}},ajax:function(e,t,r){var i=n.Phoenix.Utils.generateUuid();var s=this.get("_transactions")[i]=n.Phoenix.Transaction.create({uuid:i,url:e,type:t,params:r,header:this.get("_header")});if(this.get("_initialized"))this.get("_socket").send(this.get("_topic"),s.payload());else this.get("join").call(this,{});return s.promise}});n.Phoenix.Transaction=Ember.Object.extend({uuid:null,url:null,type:null,params:null,header:null,promise:null,success:null,error:null,init:function(){var e=new Ember.RSVP.Promise(function(e,t){this.set("success",function(t){Ember.run(null,e,t)});this.set("error",function(e){Ember.run(null,t,e)})}.bind(this));this.set("promise",e)},payload:function(e,t,n){var r={uuid:this.get("uuid"),type:this.get("type"),params:this.get("params"),path:this.get("derivePath").call(this)};if(this.get("header"))r.header=this.get("header");return r},derivePath:function(){return this.get("url").replace(this.host,"")}});n.Phoenix.Utils={generateUuid:function(){var e=(new Date).getTime();var t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var n=(e+Math.random()*16)%16|0;e=Math.floor(e/16);return(t==="x"?n:n&7|8).toString(16)});return t}};Ember.onLoad("Ember.Application",function(e){e.initializer({name:"phoenix",initialize:function(e,r){r.register("service:phoenix",n.Phoenix.Socket,{singleton:true});n.ApplicationAdapter=t.PhoenixSocketAdapter;if(ENV.Phoenix.init){e.lookup("adapter:application").setSocket(ENV.Phoenix.init.channel,ENV.Phoenix.init.topic);if(ENV.Phoenix.init.doInit){e.lookup("adapter:application").join(ENV.Phoenix.init.params)}}}})})} \ No newline at end of file +var PhoenixSocket=function(e){window.ENV=window.ENV||{};e=e||{};ENV.Phoenix={};ENV.Phoenix.endpoint=e.endpoint||location.protocol.match(/^https/)?"wss://"+location.host+"/ws":"ws://"+location.host+"/ws";ENV.Phoenix.appName=e.appName||"App";ENV.Phoenix.storeName=e.storeName||"DS";ENV.Phoenix.init=e.init||{doInit:true,channel:"data",topic:"data",params:null};var t=window[ENV.Phoenix.storeName],n=window[ENV.Phoenix.appName];n.Phoenix={};n.Phoenix.Socket=Ember.Controller.extend({socket:null,channels:null,init:function(){this.set("channels",Ember.Map.create());var e=new Phoenix.Socket(ENV.Phoenix.endpoint);e.onClose(this.get("handleClose").bind(this));this.set("socket",e)},addTopic:function(e,t,n,r,i){var s=e+":"+t;this.get("channels").set(s,n);this.get("socket").join(e,t,r||{},this.get("handleAddTopic").bind(this,s,i))},handleAddTopic:function(e,t,n){n.on("join",function(e){t(n,e)}.bind(this));n.on("error",function(e){t("error",e)})},handleClose:function(e){this.get("channels").forEach(function(t){t.onClose(e)})}});n.Phoenix.Channel=t.PhoenixSocketAdapter=t.RESTAdapter.extend({needs:["phoenix","session"],_channel:null,_topic:null,_initialized:false,_transactions:{},_socket:null,_header:null,setSocket:function(e,t){this.set("_channel",e);this.set("_topic",t)},join:function(e){var t=n.Phoenix.Transaction.create({params:e});var r=this.get("_channel")+":"+this.get("_topic");this.container.lookup("service:phoenix").addTopic(this.get("_channel"),this.get("_topic"),this,e,this.get("onJoin").bind(this,t));return t.promise},onJoin:function(e,t,n){if(n.success){t.on(this.get("_topic"),this.get("onData").bind(this));this.set("_socket",t);this.set("_initialized",true);this.get("unloadQueue").call(this);e.success(n)}else{e.error(n)}e.destroy()},onData:function(e){var t=this.get("_transactions")[e.uuid];if(e.success){t.success(e.message)}else{t.error(e.message)}t.destroy();delete t},onClose:function(e){console.log("channel closed: "+this.get("_topic"))},unloadQueue:function(){var e=this.get("_transactions");for(var t in e){this.get("_socket").send(this.get("_topic"),e[t].payload())}},ajax:function(e,t,r){var i=n.Phoenix.Utils.generateUuid();var s=this.get("_transactions")[i]=n.Phoenix.Transaction.create({uuid:i,url:e,type:t,params:r,header:this.get("_header")});if(this.get("_initialized"))this.get("_socket").send(this.get("_topic"),s.payload());else this.get("join").call(this,{});return s.promise}});n.Phoenix.Transaction=Ember.Object.extend({uuid:null,url:null,type:null,params:null,header:null,promise:null,success:null,error:null,init:function(){var e=new Ember.RSVP.Promise(function(e,t){this.set("success",function(t){Ember.run(null,e,t)});this.set("error",function(e){Ember.run(null,t,e)})}.bind(this));this.set("promise",e)},payload:function(e,t,n){var r={uuid:this.get("uuid"),type:this.get("type"),params:this.get("params"),path:this.get("derivePath").call(this)};if(this.get("header"))r.header=this.get("header");return r},derivePath:function(){return this.get("url").replace(this.host,"")}});n.Phoenix.Utils={generateUuid:function(){var e=(new Date).getTime();var t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var n=(e+Math.random()*16)%16|0;e=Math.floor(e/16);return(t==="x"?n:n&7|8).toString(16)});return t}};Ember.onLoad("Ember.Application",function(e){e.initializer({name:"phoenix",initialize:function(e,r){r.register("service:phoenix",n.Phoenix.Socket,{singleton:true});n.ApplicationAdapter=t.PhoenixSocketAdapter;if(ENV.Phoenix.init){e.lookup("adapter:application").setSocket(ENV.Phoenix.init.channel,ENV.Phoenix.init.topic);if(ENV.Phoenix.init.doInit){e.lookup("adapter:application").join(ENV.Phoenix.init.params)}}}})})} \ No newline at end of file