freescout/freescout-dist/public/js/polycast/polycast.min.js

1 line
4.4 KiB
JavaScript
Vendored

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Polycast=e():t.Polycast=e()}(this,function(){return function(t){function e(s){if(n[s])return n[s].exports;var i=n[s]={exports:{},id:s,loaded:!1};return t[s].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){!function(){this.Polycast=function(){this.options={},this.channels={},this.timeout=null,this.connected=!1,this.events={};var t={url:null,polling:5,token:null};if(arguments[0]&&"object"==typeof arguments[0])this.options=this.extend(t,arguments[0]);else{if(!arguments[0]||"string"!=typeof arguments[0])throw"Polycast url must be defined!";if(arguments[1]&&"object"==typeof arguments[1]){var e=this.extend({url:arguments[0]},arguments[1]);this.options=this.extend(t,e)}else this.options=this.extend(t,{url:arguments[0]})}return this.init(),this},this.Polycast.prototype={init:function(){var t=this,e=this.serialize({polling:this.options.polling,_token:this.options.token}),n=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");return n.open("POST",this.options.url+"/connect"),n.onreadystatechange=function(){n.readyState>3&&200===n.status&&(response=JSON.parse(n.responseText),"success"==response.status&&(t.connected=!0,t.setTime(response.time),t.setTimeout(),console.log("Polycast connection established!"),t.fire("connect",t)))},n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),n.send(e),this},reconnect:function(){return this.connected?void 0:(this.init(),this)},on:function(t,e){return void 0===this.events[t]&&(this.events[t]=[]),this.events[t].push(e),this},fire:function(t,e){void 0===this.events[t]&&(this.events[t]=[]);for(var n in this.events[t])if(this.events[t].hasOwnProperty(n)){var s=this.events[t][n];s(e)}},disconnect:function(){return this.connected=!1,clearTimeout(this.timeout),this.timeout=null,this.fire("disconnect",this),this},extend:function(t,e){var n;for(n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t},setTime:function(t){this.options.time=t},setTimeout:function(){var t=this;this.timeout=setTimeout(function(){t.fetch()},1e3*this.options.polling)},fetch:function(){this.request()},request:function(){var t=this,e={};for(var n in this.channels)if(this.channels.hasOwnProperty(n)){void 0===e[n]&&(e[n]=[]);for(var s=0;s<this.channels[n].length;s++){var i=this.channels[n][s],o=i.events;for(var r in o)e[n].push(r)}}var a={time:this.options.time,channels:e,_token:this.options.token},h=this.serialize(a),c=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");return c.open("POST",this.options.url+"/receive"),c.onreadystatechange=function(){c.readyState>3&&200===c.status&&t.parseResponse(c.responseText)},c.setRequestHeader("X-Requested-With","XMLHttpRequest"),c.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),c.send(h),c},serialize:function(t,e){var n=[];for(var s in t)if(t.hasOwnProperty(s)){var i=e?e+"["+s+"]":s,o=t[s];n.push("object"==typeof o?this.serialize(o,i):encodeURIComponent(i)+"="+encodeURIComponent(o))}return n.join("&")},parseResponse:function(t){if(t=JSON.parse(t),"success"==t.status){this.setTime(t.time);for(var e in t.payloads)if(t.payloads.hasOwnProperty(e))for(i=0;i<t.payloads[e].channels.length;++i){var n=t.payloads[e].channels[i];for(index=0;index<this.channels[n].length;++index)this.channels[n][index].fire(t.payloads[e])}this.setTimeout()}},subscribe:function(t){var e=new PolycastChannel({channel:t});return void 0===this.channels[t]&&(this.channels[t]=[]),this.channels[t].push(e),e}},this.PolycastChannel=function(){this.options={},this.events={};var t={channel:null};if(!arguments[0]||"object"!=typeof arguments[0])throw"Polycast channel options must be defined!";this.options=this.extend(t,arguments[0])},this.PolycastChannel.prototype={init:function(){return this},extend:function(t,e){var n;for(n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t},on:function(t,e){return void 0===this.events[t]&&(this.events[t]=[]),this.events[t].push(e),this},fire:function(t){for(var e in this.events)if(this.events.hasOwnProperty(e)&&e==t.event){var n=this.events[e];0!=t.delay?setTimeout(function(){n[0](t.payload,t)},1e3*t.delay):n[0](t.payload,t)}}},this.Polycast.version="1.0.0",this.PolycastChannel.version="1.0.0",t.exports=this.Polycast}()}])});