UNPKG

xrs

Version:

reactive server

2 lines (1 loc) 5.87 kB
var xrs=function(){"use strict";function n(n){return function(t){return t==n}}function t(n){return function(t){return x(t)(n)}}var e=function(){var n,t,e=new Promise(function(e,r){n=e,t=r});return arguments.length&&n(arguments[0]),e.resolve=n,e.reject=t,e},r=function n(t,e){return e instanceof Array&&(e=e.reduce(n,[])),(t=t||[]).concat(e)},o=function(n,t){return t in n},u=function(n,t,e,r){return n.host&&n.host.nodeName&&(n=n.host),t.name&&(e=t,t=t.name),!o(n,t)&&Object.defineProperty(n,t,{value:e,writable:r}),n[t]},i=function(){},c=function n(t,o){function c(n,e){return n.next?n.next(e):e instanceof Array?n.apply(t,e):n.call(t,e)}function f(n,t){for(var e=n.length;~--e;)t!=n[e]&&t!=n[e].fn&&t||(o.off||i)(n.splice(e,1)[0])}function s(t,r){var o=n((r=r||{}).base||e());return o.i=0,o.li=[],o.fn=r.fn,o.parent=t,o.source=r.fn?o.parent.source:o,o.on("stop",function(n){return o.type?o.parent.off(o.type,o):o.parent.off(o),o.reason=n}),o.each=function(n){var t=n.next?n:s(o,{fn:n});return o.li.push(t),t},o.pipe=function(n){return n(o)},o.map=function(n){return o.each(function(t,e,r){return r.next(n(t,e,r))})},o.filter=function(n){return o.each(function(t,e,r){return n(t,e,r)&&r.next(t)})},o.reduce=function(n,t){return o.each(function(e,r,o){return o.next(t=n(t,e,r,o))})},o.unpromise=function(){var n=s(o,{base:{},fn:function(t){return n.next(t)}});return o.li.push(n),n},o.next=function(n){return o.resolve&&o.resolve(n),o.li.length?o.li.map(function(t){return t.fn(n,t.i++,t)}):n},o.until=function(n){return(n.each||n.then).call(n,function(n){return o.source.emit("stop",n)}),o},o.off=function(n){return f(o.li,n),o},o.start=function(){return o.source.emit("start"),o},o[Symbol.asyncIterator]=function(){return{next:function(){return o.wait=new Promise(function(n){o.wait=!0,o.map(function(t,e,r){delete o.wait,o.off(r),n({value:t,done:!1})}),o.emit("pull",o)})}}},o}return t=t||{},o=o||{},u(t,"emit",function(n,e,o){for(var u=t.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],e));for(f=0;f<t.on["*"].length;f++)i.push(c(t.on["*"][f],[n,e]));return i.reduce(r,[])},1),u(t,"once",function(n,e){return t.on(n,e,!0)},1),u(t,"off",function(n,e){return f(t.on[n]||[],e),e&&e.ns&&delete t.on[n]["$"+e.ns],t},1),u(t,"on",function(n,e,r){function u(n){return n.isOnce=r,n.type=c,a&&(t.on[c]["$"+(n.ns=a)]=n),p.push(n),(o.on||i)(n),n.next?n:t}var c=n.split(".")[0],a=n.split(".")[1],p=t.on[c]=t.on[c]||[],l="function"==typeof e?e:0;return!l&&a?(l=t.on[c]["$"+a])?l:u(s(t,e)):l||a?l&&a?u((f(p,t.on[c]["$"+a]||-1),l)):!(!l||a)&&u(l):u(s(t,e))},1),t.on["*"]=t.on["*"]||[],t},f=function(n=location.href.replace("http","ws")){const t=c({attempt:0});return t.ready=t.once("connected"),t.connect=p(t,n),t.connect(),t.send=(n=>t.ready.then(t=>t.send(n))),t};const{min:s,pow:a}=Math,p=(n,t)=>()=>{const{WebSocket:e,location:r,setTimeout:o}=window,u=new e(t);u.onopen=(()=>n.emit("connected",u)),u.onmessage=(t=>n.emit("recv",t.data)),u.onclose=(()=>{n.ready=n.once("connected"),n.emit("disconnected"),o(n.connect,l(++n.attempt))})},l=(n,t=100,e=1e4)=>s(e,t*a(2,n));var d=n;n.fn=function(n){return"function"==typeof n},n.str=function(n){return"string"==typeof n},n.num=function(n){return"number"==typeof n},n.obj=function(n){return"object"==typeof n},n.lit=function(n){return n.constructor==Object},n.bol=function(n){return"boolean"==typeof n},n.truthy=function(n){return 1==!!n},n.falsy=function(n){return 0==!!n},n.arr=function(n){return n instanceof Array},n.null=function(n){return null===n},n.def=function(n){return void 0!==n},n.in=function(n){return function(t){return!!n&&(n.indexOf?~n.indexOf(t):t in n)}},n.promise=function(n){return n instanceof Promise},n.stream=function(n){return!(!n||!n.next)};var m=function(n){return Object.keys(d.obj(n)||d.fn(n)?n:{})},h=function(n){return n.__data__},v=function(n){return function(){return n}},y=function(n){return 0===n?"0":n?d.fn(n)?""+n:d.obj(n)?JSON.stringify(n):String(n):""},x=function n(t,e){var r=arguments.length>1,o=d.fn(t)?[]:y(t).split(".").filter(Boolean),u=o.shift();return function(i,c){var f={};return i?d.num(t)||t?d.arr(t)?(t.map(function(t){var r=n(t)(i);void 0!=(r=d.fn(e)?e(r):void 0==r?e:r)&&n(t,d.fn(r)?v(r):r)(f)}),f):i[t]||!o.length?r?(i[t]=d.fn(e)?e(i[t],c):e,i):d.fn(t)?t(i):i[t]:r?(n(o.join("."),e)(i[u]?i[u]:i[u]={}),i):n(o.join("."))(i[u]):r?function(n,t){return m(n).map(function(t){delete n[t]}),m(t).map(function(e){n[e]=t[e]}),n}(i,e):i:void 0}},b=t;t.parent=function(n){return h(this.parentNode)[n]};var w=function(n){return n?m(n).map(b(n)):[]};return function(n,t){return t={exports:{}},n(t,t.exports),t.exports}(function(n){n.exports=function({socket:n=f()}={}){n.id=0;const o=c({socket:n,send:r(n),get subscriptions(){return w(n.on).map(n=>n&&n[0]).filter(n=>n&&n.type&&"$"==n.type[0])}});return n.once("disconnected").map(()=>n.on("connected").map(e(o))),n.on("recv").map(t).each(({id:t,data:e})=>{const r=n.on[`$${t}`]&&n.on[`$${t}`][0];e.exec?e.exec(r,e.value):t?n.emit(`$${t}`,e):o.emit("recv",e)}),o};const t=n=>new Function(`return ${n}`)(),e=n=>()=>n.subscriptions.map(({subscription:t})=>n.socket.send(t)),r=(n,t)=>(e,u)=>{if(e instanceof window.Blob)return o(n,e,u);const i=y(++n.id),c=n.on(`$${i}`),f=(e,r=0)=>n.send(c.source.subscription=y({id:i,data:e,type:t})).then(()=>c.emit("sent",{id:i,count:r}));return e.next?e.map(f).source.emit("start"):f(e),c.source.once("stop").filter(n=>"CLOSED"!=n).map(()=>r(n,"UNSUBSCRIBE")(i).filter((n,t,e)=>e.source.emit("stop","CLOSED"))),c},o=(n,t,e,o=0,u=1024)=>{const i=c().on("recv"),f=e=>()=>o>=t.size?i.emit("sent",{id:e}):(n.send(t.slice(o,o+=u)),window.setTimeout(f(e)));return r(n,"BINARY")({size:t.size,meta:e}).on("sent",({id:n})=>f(n)()).on("progress",n=>i.emit("progress",{received:n,total:t.size})).map(i.next).source.until(i.once("stop")),i}})}();