1
|
!function(n,e,t){"use strict";var a,r,o,l,s,u,c,i,g,d,f,h,x;n.MessageBus;r=function(){return"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g,function(n){var e;return e=16*Math.random()|0,("x"===n?e:3&e|8).toString(16)})}(),a=[],o=0,d=!1,f=[],x=0,h=n.jQuery;var v;!function(){for(var n=["","webkit","ms"],t=0;t<n.length;t++){var a=n[t],r=a+(""===a?"hidden":"Hidden");void 0!==e[r]&&(v=r)}}();var b=function(){return void 0!==v?e[v]:!e.hasFocus},p=null===(new XMLHttpRequest).onprogress,m=function(){return s.enableChunkedEncoding&&p};l=function(){return s.alwaysLongPoll||!b()};var P,E=0,y=0,L=function(n){var e=!1;if(!n)return!1;for(var t=0;t<n.length;t++){var r=n[t];e=!0;for(var o=0;o<a.length;o++){var l=a[o];if(l.channel===r.channel){l.last_id=r.message_id;try{l.func(r.data,r.global_id,r.message_id)}catch(n){console.log&&console.log("MESSAGE BUS FAIL: callback "+l.channel+" caused exception "+n.message)}}"/__status"===r.channel&&void 0!==r.data[l.channel]&&(l.last_id=r.data[l.channel])}}return e},S=function(n){if(o=0,!d)return L(n);if(n)for(var e=0;e<n.length;e++)f.push(n[e]);return!1};i=function(n,e){var t=!1,a=!1;P=new Date,y+=1,e.__seq=y;var r=l()&&s.enableLongPolling,u=r&&m();x>0&&(x--,u=!1);var c={"X-SILENCE-LOGGER":"true"};for(var i in s.headers)c[i]=s.headers[i];u||(c["Dont-Chunk"]="true");var d=u?"text":"json",f=function(n,t){var a=n.indexOf("\r\n|\r\n",t);if(-1===a)return t;var r=n.substring(t,a);r=r.replace(/\r\n\|\|\r\n/g,"\r\n|\r\n");try{S(JSON.parse(r))}catch(n){console.log&&(console.log("FAILED TO PARSE CHUNKED REPLY"),console.log(e))}return f(n,a+"\r\n|\r\n".length)},v=function(){s.longPoll&&(s.longPoll.abort(),x=30)};if(!s.ajax)throw new Error("Either jQuery or the ajax adapter must be loaded");return s.ajax({url:s.baseUrl+"message-bus/"+s.clientId+"/poll"+(r?"":"?dlp=t"),data:e,cache:!1,async:!0,dataType:d,type:"POST",headers:c,messageBus:{chunked:u,onProgressListener:function(n){var e=0,t=setTimeout(v,3e3);return n.onprogress=function(){clearTimeout(t),"application/json; charset=utf-8"===n.getResponseHeader("Content-Type")?u=!1:e=f(n.responseText,e)}}},xhr:function(){var n=h.ajaxSettings.xhr();return u?(this.messageBus.onProgressListener(n),n):n},success:function(n){u||("string"==typeof n&&(n=JSON.parse(n)),t=S(n))},error:function(n,e,t){"abort"===e?a=!0:(o+=1,E+=1)},complete:function(){var e;try{t||a?e=100:(e=s.callbackInterval,o>2?e*=o:l()||(e=s.backgroundCallbackInterval),e>s.maxPollInterval&&(e=s.maxPollInterval),(e-=new Date-P)<100&&(e=100))}catch(n){console.log&&n.message&&console.log("MESSAGE BUS FAIL: "+n.message)}g=setTimeout(function(){g=null,n()},e),s.longPoll=null}})},s={enableChunkedEncoding:!0,enableLongPolling:!0,callbackInterval:15e3,backgroundCallbackInterval:6e4,maxPollInterval:18e4,callbacks:a,clientId:r,alwaysLongPoll:!1,baseUrl:"/",headers:{},ajax:h&&h.ajax,noConflict:function(){return n.MessageBus=n.MessageBus.previousMessageBus,this},diagnostics:function(){console.log("Stopped: "+c+" Started: "+u),console.log("Current callbacks"),console.log(a),console.log("Total ajax calls: "+y+" Recent failure count: "+o+" Total failures: "+E),console.log("Last ajax call: "+(new Date-P)/1e3+" seconds ago")},pause:function(){d=!0},resume:function(){d=!1,L(f),f=[]},stop:function(){c=!0,u=!1},start:function(){var t,r;u||(u=!0,c=!1,t=function(){var n;if(!c)if(0!==a.length){n={};for(var e=0;e<a.length;e++)n[a[e].channel]=a[e].last_id;s.longPoll=i(t,n)}else r||(r=setTimeout(function(){r=null,t()},500))},e.addEventListener&&"hidden"in e&&(s.visibilityEvent=n.document.addEventListener("visibilitychange",function(){e.hidden||s.longPoll||!g||(clearTimeout(g),g=null,t())})),t())},status:function(){if(d)return"paused";if(u)return"started";if(c)return"stopped";throw"Cannot determine current status"},subscribe:function(n,e,t){return u||c||s.start(),("number"!=typeof t||t<-1)&&(t=-1),a.push({channel:n,func:e,last_id:t}),s.longPoll&&s.longPoll.abort(),e},unsubscribe:function(n,e){var t;-1!==n.indexOf("*",n.length-1)&&(n=n.substr(0,n.length-1),t=!0);for(var r=!1,o=a.length-1;o>=0;o--){var l,u=a[o];!(l=t?u.channel.substr(0,n.length)!==n:u.channel!==n)&&e&&u.func!==e&&(l=!0),l||(a.splice(o,1),r=!0)}return r&&s.longPoll&&s.longPoll.abort(),r}},n.MessageBus=s}(window,document);
|