-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.min.js
1 lines (1 loc) · 2.81 KB
/
client.min.js
1
var nanosocket=function(){"use strict";var n=function(){var n,e,t=new Promise(function(t,o){n=t,e=o});return arguments.length&&n(arguments[0]),t.resolve=n,t.reject=e,t},e=function n(e,t){return t instanceof Array&&(t=t.reduce(n,[])),(e=e||[]).concat(t)},t=function(n,e){return e in n},o=function(n,e,o,r){return n.host&&n.host.nodeName&&(n=n.host),e.name&&(o=e,e=e.name),!t(n,e)&&Object.defineProperty(n,e,{value:o,writable:r}),n[e]},r=function t(r){function c(n,e){return n.next?n.next(e):e instanceof Array?n.apply(r,e):n.call(r,e)}function u(n,e){for(var t=n.length;~--t;)e!=n[t]&&e!=n[t].fn&&e||n.splice(t,1)}function i(e,o){var r=t((o=o||{}).base||n());return r.i=0,r.li=[],r.fn=o.fn,r.parent=e,r.source=o.fn?r.parent.source:r,r.on("stop",function(){return r.type?r.parent.off(r.type,r):r.parent.off(r)}),r.each=function(n){var e=n.next?n:i(r,{fn:n});return r.li.push(e),e},r.pipe=function(n){return n(r)},r.map=function(n){return r.each(function(e,t,o){return o.next(n(e,t,o))})},r.filter=function(n){return r.each(function(e,t,o){return n(e,t,o)&&o.next(e)})},r.reduce=function(n,e){return r.each(function(t,o,r){return r.next(e=n(e,t,o,r))})},r.unpromise=function(){var n=i(r,{base:{},fn:function(e){return n.next(e)}});return r.li.push(n),n},r.next=function(n){return r.resolve&&r.resolve(n),r.li.length?r.li.map(function(e){return e.fn(n,e.i++,e)}):n},r.until=function(n){return n.each(function(){r.source.emit("stop")}),r},r.off=function(n){return u(r.li,n),r},r[Symbol.asyncIterator]=function(){return{next:()=>r.wait=new Promise(n=>{r.wait=!0,r.map((e,t,o)=>{delete r.wait,r.off(o),n({value:e,done:!1})}),r.emit("pull",r)})}},r}return r=r||{},o(r,"emit",function(n,t,o){for(var u=r.on[n.split(".")[0]]||[],i=[],f=0;f<u.length;f++)u[f].ns&&o&&!o(u[f].ns)||i.push(c(u[f].isOnce?u.splice(f--,1)[0]:u[f],t));for(f=0;f<r.on["*"].length;f++)i.push(c(r.on["*"][f],[n,t]));return i.reduce(e,[])},1),o(r,"once",function(n,e){return r.on(n,e,!0)},1),o(r,"off",function(n,e){return u(r.on[n]||[],e),e&&e.ns&&delete r.on[n]["$"+e.ns],r},1),o(r,"on",function(n,e,t){function o(n){return n.isOnce=t,n.type=c,f&&(r.on[c]["$"+(n.ns=f)]=n),a.push(n),n.next?n:r}var c=n.split(".")[0],f=n.split(".")[1],a=r.on[c]=r.on[c]||[],s="function"==typeof e?e:0;return!s&&f?(s=r.on[c]["$"+f])?s:o(i(r,e)):s||f?s&&f?o((u(a,r.on[c]["$"+f]||-1),s)):!(!s||f)&&o(s):o(i(r,e))},1),r.on["*"]=r.on["*"]||[],r};const{min:c,pow:u}=Math,i=(n,e)=>()=>{const{WebSocket:t,location:o,setTimeout:r}=window,c=new t(e);c.onopen=(()=>n.emit("connected",c)),c.onmessage=(e=>n.emit("recv",e.data)),c.onclose=(()=>{n.ready=n.once("connected"),n.emit("disconnected"),r(n.connect,f(++n.attempt))})},f=(n,e=100,t=1e4)=>c(t,e*u(2,n));return function(n=location.href.replace("http","ws")){const e=r({attempt:0});return e.ready=e.once("connected"),e.connect=i(e,n),e.connect(),e.send=(n=>e.ready.then(e=>e.send(n))),e}}();