-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix-socket.min.js
1 lines (1 loc) · 3.71 KB
/
phoenix-socket.min.js
1
(function(e){e.PhoenixSocket=function(t){e.ENV=e.ENV||{};t=t||{};ENV.Phoenix={};ENV.Phoenix.endpoint=t.endpoint||location.protocol.match(/^https/)?"wss://"+location.host+"/ws":"ws://"+location.host+"/ws";ENV.Phoenix.appName=t.appName||"App";ENV.Phoenix.storeName=t.storeName||"DS";ENV.Phoenix.init=t.init||{doInit:true,channel:"data",topic:"data",params:null};var n=e[ENV.Phoenix.storeName],r=e[ENV.Phoenix.appName];r.Phoenix={};r.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){this.get("channels").set(e,t);this.get("socket").join(e,n||{},this.get("handleAddTopic").bind(this,e,r))},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)})}});r.Phoenix.Channel=n.PhoenixSocketAdapter=n.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);this.set("_topicKey",e+":"+t)},join:function(e){var t=r.Phoenix.Transaction.create({params:e});this.container.lookup("service:phoenix").addTopic(this.get("_topicKey"),this,e,this.get("onJoin").bind(this,t));return t.promise},leave:function(e){this.set("_initialized",false);this.get("_socket").leave(e)},onJoin:function(e,t,n){if(n.success){t.on(this.get("_topicKey"),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("_topicKey"),e[t].payload())}},ajax:function(e,t,n){var i=r.Phoenix.Utils.generateUuid();var s=this.get("_transactions")[i]=r.Phoenix.Transaction.create({uuid:i,url:e,type:t,params:n,header:this.get("_header")});if(this.get("_initialized"))this.get("_socket").send(this.get("_topicKey"),s.payload());else{this.get("join").call(this,{}).catch(function(e){s.error({error:"Failed to join channel. Try rejoining.",msg:e})})}return s.promise}});r.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,"")}});r.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,t){t.register("service:phoenix",r.Phoenix.Socket,{singleton:true});r.ApplicationAdapter=n.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)}}}})})}})(window)