UNPKG

angular-actioncable

Version:
1 lines 10.7 kB
"use strict";var ngActionCable=angular.module("ngActionCable",["ngWebSocket"]);!function(){function ActionCableChannelFactory($q,$rootScope,ActionCableController,ActionCableWebsocket,ActionCableConfig,ActionCableSocketWrangler){function ActionCableChannel(channelName,channelParams){this.channelName=channelName,this.channelParams=channelParams||{},this._channelParamsString=JSON.stringify(this.channelParams),this.onMessageCallback=null,this.callbacks=this._websocketControllerActions()}return ActionCableChannel.prototype._websocketControllerActions=function(){return ActionCableController.actions[this.channelName]=ActionCableController.actions[this.channelName]||{},ActionCableController.actions[this.channelName][this._channelParamsString]=ActionCableController.actions[this.channelName][this._channelParamsString]||[],ActionCableController.actions[this.channelName][this._channelParamsString]},ActionCableChannel.prototype._subscriptionCount=function(){return this.callbacks.length},ActionCableChannel.prototype.subscribe=function(cb){var request;return"function"!=typeof cb?(console.error("0x01 Callback function was not defined on subscribe(). ActionCable channel: '"+this.channelName+"', params: '"+this._channelParamsString+"'"),$q.reject()):this.onMessageCallback?(console.error("0x02 This ActionCableChannel instance is already subscribed. ActionCable channel: '"+this.channelName+"', params: '"+this._channelParamsString+"'"),$q.reject()):(0===this._subscriptionCount()&&(request=ActionCableWebsocket.subscribe(this.channelName,this.channelParams)),this._addMessageCallback(cb),request||$q.resolve())},ActionCableChannel.prototype.unsubscribe=function(){var request;return this._removeMessageCallback(),0===this._subscriptionCount()&&(request=ActionCableWebsocket.unsubscribe(this.channelName,this.channelParams)),request||$q.resolve()},ActionCableChannel.prototype.send=function(message,action){return this.onMessageCallback?ActionCableWebsocket.send(this.channelName,this.channelParams,message,action):(console.error("0x03 You need to subscribe before you can send a message. ActionCable channel: '"+this.channelName+"', params: '"+this._channelParamsString+"'"),$q.reject())},ActionCableChannel.prototype.onConfirmSubscription=function(callback){ActionCableConfig.debug&&console.log("Callback","confirm_subscription:"+this.channelName),$rootScope.$on("confirm_subscription:"+this.channelName,callback)},ActionCableChannel.prototype._addMessageCallback=function(cb){this.onMessageCallback=cb,this.callbacks.push(this.onMessageCallback)},ActionCableChannel.prototype._removeMessageCallback=function(){for(var i=0;i<this.callbacks.length;i++)if(this.callbacks[i]===this.onMessageCallback)return this.callbacks.splice(i,1),this.onMessageCallback=null,!0;throw ActionCableConfig.debug&&(console.log("Callbacks:"),console.log(this.callbacks)),ActionCableConfig.debug&&(console.log("onMessageCallback:"),console.log(this.onMessageCallback)),"Can't find onMessageCallback in callbacks array to remove"},ActionCableChannel}angular.module("ngActionCable").factory("ActionCableChannel",["$q","$rootScope","ActionCableController","ActionCableWebsocket","ActionCableConfig","ActionCableSocketWrangler",ActionCableChannelFactory])}(),ngActionCable.factory("ActionCableConfig",function(){function actioncable_meta_tag_content(){for(var metaTags=document.getElementsByTagName("meta"),metaTagContent=!1,index=0;index<metaTags.length;index++)if(metaTags[index].hasAttribute("name")&&metaTags[index].hasAttribute("content")&&"action-cable-url"===metaTags[index].getAttribute("name")){metaTagContent=metaTags[index].getAttribute("content");break}return metaTagContent}var _wsUri,defaultWsUri="wss://please.add.an.actioncable.meta.tag.invalid:12345/path/to/cable",config={autoStart:!0,debug:!1,protocols:[]};return Object.defineProperty(config,"wsUri",{get:function(){return _wsUri=_wsUri||actioncable_meta_tag_content()||defaultWsUri},set:function(newWsUri){return _wsUri=newWsUri}}),config}),function(){function ActionCableController($rootScope,ActionCableConfig){function findActionCallbacksForChannel(channelName,params){return actions[channelName]&&actions[channelName][params]}function channel_from(message){if(message&&message.identifier)return JSON.parse(message.identifier).channel}function params_from(message){var paramsData=JSON.parse(message.identifier).data;return JSON.stringify(paramsData)}var actions={welcome:function(message){ActionCableConfig.debug&&console.log("Willkommen")},ping:function(message){ActionCableConfig.debug&&console.log("ActionCable ping")},_ping:function(message){ActionCableConfig.debug&&console.log("ActionCable 5.0.0.beta3 ping")},confirm_subscription:function(message){var identifier=JSON.parse(message.identifier),channel=identifier.channel;$rootScope.$broadcast("confirm_subscription:"+channel),ActionCableConfig.debug&&console.log("ActionCable confirm_subscription on channel: "+message.identifier)},ws_404:function(message){ActionCableConfig.debug&&console.log("ActionCable route not found: "+message)}},routeToActions=function(actionCallbacks,message){angular.forEach(actionCallbacks,function(func,id){func.apply(null,[message])})},route=function(message){if(actions[message.type])actions[message.type](message),"ping"==message.type&&methods.after_ping_callback();else if("_ping"==message.identifier)actions._ping(message),methods.after_ping_callback();else if(findActionCallbacksForChannel(channel_from(message),params_from(message))){var actionCallbacks=findActionCallbacksForChannel(channel_from(message),params_from(message));routeToActions(actionCallbacks,message.message)}else actions.ws_404(message)},methods={post:function(message){return route(message)},actions:actions,after_ping_callback:function(){}};return methods}angular.module("ngActionCable").factory("ActionCableController",["$rootScope","ActionCableConfig",ActionCableController])}(),ngActionCable.factory("ActionCableSocketWrangler",["$rootScope","ActionCableWebsocket","ActionCableConfig","ActionCableController",function($rootScope,ActionCableWebsocket,ActionCableConfig,ActionCableController){var reconnectIntervalTime=7537,timeoutTime=20143,websocket=ActionCableWebsocket,controller=ActionCableController,_live=!1,_connecting=!1,_reconnectTimeout=!1,setReconnectTimeout=function(){stopReconnectTimeout(),_reconnectTimeout=_reconnectTimeout||setTimeout(function(){ActionCableConfig.debug&&console.log("ActionCable connection might be dead; no pings received recently"),connection_dead()},timeoutTime+Math.floor(Math.random()*timeoutTime/5))},stopReconnectTimeout=function(){clearTimeout(_reconnectTimeout),_reconnectTimeout=!1};controller.after_ping_callback=function(){setReconnectTimeout()};var connectNow=function(){websocket.attempt_restart(),setReconnectTimeout()},startReconnectInterval=function(){_connecting=_connecting||setInterval(function(){connectNow()},reconnectIntervalTime+Math.floor(Math.random()*reconnectIntervalTime/5))},stopReconnectInterval=function(){clearInterval(_connecting),_connecting=!1,clearTimeout(_reconnectTimeout),_reconnectTimeout=!1},connection_dead=function(){_live&&startReconnectInterval(),ActionCableConfig.debug&&console.log("socket close"),$rootScope.$apply()};websocket.on_connection_close_callback=connection_dead;var connection_alive=function(){stopReconnectInterval(),setReconnectTimeout(),ActionCableConfig.debug&&console.log("socket open"),$rootScope.$apply()};websocket.on_connection_open_callback=connection_alive;var methods={start:function(){ActionCableConfig.debug&&console.info("Live STARTED"),_live=!0,startReconnectInterval(),connectNow()},stop:function(){ActionCableConfig.debug&&console.info("Live stopped"),_live=!1,stopReconnectInterval(),stopReconnectTimeout(),websocket.close()}};return Object.defineProperties(methods,{connected:{get:function(){return _live&&!_connecting}},connecting:{get:function(){return _live&&!!_connecting}},disconnected:{get:function(){return!_live}}}),ActionCableConfig.autoStart&&methods.start(),methods}]),ngActionCable.factory("ActionCableWebsocket",["$websocket","ActionCableController","ActionCableConfig",function($websocket,ActionCableController,ActionCableConfig){var settings={};Object.defineProperties(settings,{wsUrl:{get:function(){return ActionCableConfig.wsUri}},protocols:{get:function(){return ActionCableConfig.protocols}}});var methods,controller=ActionCableController,dataStream=null,connected=!1,currentChannels=[],close_connection=function(){dataStream&&(dataStream.close({force:!0}),dataStream=null,connected=!1)},subscribe_to=function(channel,data){return"undefined"==typeof data&&(data="N/A"),ActionCableConfig.debug&&console.log("-> subscribing to: "+channel),new_data_stream().send(JSON.stringify({command:"subscribe",identifier:JSON.stringify({channel:channel,data:data})}))},unsubscribe_from=function(channel,data){return"undefined"==typeof data&&(data="N/A"),ActionCableConfig.debug&&console.log("<- unsubscribing from: "+channel),new_data_stream().send(JSON.stringify({command:"unsubscribe",identifier:JSON.stringify({channel:channel,data:data})}))},send_to=function(channel,data,message,action){return"undefined"==typeof data&&(data="N/A"),ActionCableConfig.debug&&console.log("=> sending to: "+channel),new_data_stream().send(JSON.stringify({command:"message",identifier:JSON.stringify({channel:channel,data:data}),data:JSON.stringify({message:message,action:action})}))},new_data_stream=function(){return null===dataStream&&(dataStream=$websocket(settings.wsUrl,settings.protocols),dataStream.onClose(function(arg){close_connection(),connected=!1,methods.on_connection_close_callback()}),dataStream.onOpen(function(arg){connected=!0,currentChannels.forEach(function(channel){subscribe_to(channel.name,channel.data)}),methods.on_connection_open_callback()}),dataStream.onMessage(function(message){controller.post(JSON.parse(message.data))})),dataStream};return methods={connected:function(){return connected},attempt_restart:function(){return close_connection(),new_data_stream(),!0},currentChannels:currentChannels,close:function(){return close_connection()},on_connection_close_callback:function(){},on_connection_open_callback:function(){},subscribe:function(channel,data){return currentChannels.push({name:channel,data:data}),this.connected()&&subscribe_to(channel,data)},unsubscribe:function(channel,data){for(var i=0;i<currentChannels.length;i++)currentChannels[i].name===channel&&currentChannels.splice(i,1);return this.connected()&&unsubscribe_from(channel,data)},send:function(channel,data,message,action){return ActionCableConfig.debug&&console.log("send requested"),this.connected()&&send_to(channel,data,message,action)}}}]);