-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.min.js
1 lines (1 loc) · 10.1 KB
/
client.min.js
1
var sync=function(){"use strict";var n=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};var t=function n(t,e){return e instanceof Array&&(e=e.reduce(n,[])),(t=t||[]).concat(e)},e=function(n,t,e,r){return n.host&&n.host.nodeName&&(n=n.host),t.name&&(e=t,t=t.name),!function(n,t){return t in n}(n,t)&&Object.defineProperty(n,t,{value:e,writable:r}),n[t]},r=function(){},u=function u(o,i){return i=i||{},e(o=o||{},"emit",function(n,e,r){for(var u=o.on[n.split(".")[0]]||[],i=[],f=0;f<u.length;f++)u[f].ns&&r&&!r(u[f].ns)||i.push(c(u[f].isOnce?u.splice(f--,1)[0]:u[f],e));for(var f=0;f<o.on["*"].length;f++)i.push(c(o.on["*"][f],[n,e]));return i.reduce(t,[])},1),e(o,"once",function(n,t){return o.on(n,t,!0)},1),e(o,"off",function(n,t){f(o.on[n]||[],t),t&&t.ns&&delete o.on[n]["$"+t.ns];return o},1),e(o,"on",function(n,t,e){var u=n.split(".")[0],c=n.split(".")[1],a=o.on[u]=o.on[u]||[],p="function"==typeof t?t:0;return!p&&c?(p=o.on[u]["$"+c])?p:l(s(o,t)):p||c?p&&c?l((f(a,o.on[u]["$"+c]||-1),p)):!(!p||c)&&l(p):l(s(o,t));function l(n){return n.isOnce=e,n.type=u,c&&(o.on[u]["$"+(n.ns=c)]=n),a.push(n),(i.on||r)(n),n.next?n:o}},1),o.on["*"]=o.on["*"]||[],o;function c(n,t){return n.next?n.next(t):t instanceof Array?n.apply(o,t):n.call(o,t)}function f(n,t){for(var e=n.length;~--e;)t!=n[e]&&t!=n[e].fn&&t||(i.off||r)(n.splice(e,1)[0])}function s(t,e){var r=u((e=e||{}).base||n());return r.i=0,r.li=[],r.fn=e.fn,r.parent=t,r.source=e.fn?r.parent.source:r,r.on("stop",function(n){return r.type?r.parent.off(r.type,r):r.parent.off(r),r.reason=n}),r.each=function(n){var t=n.next?n:s(r,{fn:n});return r.li.push(t),t},r.pipe=function(n){return n(r)},r.map=function(n){return r.each(function(t,e,r){return r.next(n(t,e,r))})},r.filter=function(n){return r.each(function(t,e,r){return n(t,e,r)&&r.next(t)})},r.reduce=function(n,t){return r.each(function(e,r,u){return u.next(t=n(t,e,r,u))})},r.unpromise=function(){var n=s(r,{base:{},fn:function(t){return n.next(t)}});return r.li.push(n),n},r.next=function(n){return r.resolve&&r.resolve(n),r.li.length?r.li.map(function(t){return t.fn(n,t.i++,t)}):n},r.until=function(n){return n?n.each?n.each(r.stop):n.then?n.then(r.stop):n.call?r.filter(n).map(r.stop):0:0},r.off=function(n){return f(r.li,n),r},r.start=function(n){return r.until(n),r.source.emit("start"),r},r.stop=function(n){return r.source.emit("stop",n)},r[Symbol.asyncIterator]=function(){return{next:function(){return r.wait=new Promise(function(n){r.wait=!0,r.map(function(t,e,u){delete r.wait,r.off(u),n({value:t,done:!1})}),r.emit("pull",r)})}}},r}},o=Math.min,i=Math.pow,c=function(n,t){return function(){var e=window.WebSocket,r=window.setTimeout,u=new e(t);u.onopen=function(){return n.emit("connected",u)},u.onmessage=function(t){return n.emit("recv",t.data)},u.onclose=function(){n.ready=n.once("connected"),n.emit("disconnected"),r(n.connect,f(++n.attempt))}}},f=function(n,t,e){return void 0===t&&(t=100),void 0===e&&(e=1e4),o(e,t*i(2,n))},s=a;function a(n){return function(t){return t==n}}a.fn=function(n){return"function"==typeof n},a.str=function(n){return"string"==typeof n},a.num=function(n){return"number"==typeof n},a.obj=function(n){return"object"==typeof n},a.lit=function(n){return n.constructor==Object},a.bol=function(n){return"boolean"==typeof n},a.truthy=function(n){return 1==!!n},a.falsy=function(n){return 0==!!n},a.arr=function(n){return n instanceof Array},a.null=function(n){return null===n},a.def=function(n){return void 0!==n},a.in=function(n){return function(t){return!!n&&(n.indexOf?~n.indexOf(t):t in n)}},a.promise=function(n){return n instanceof Promise},a.stream=function(n){return!(!n||!n.next)};var p=function(n){return Object.keys(s.obj(n)||s.fn(n)?n:{})},l=function(n){return n.__data__},d=function(n){return function(){return n}},m=function(n){return 0===n?"0":n?s.fn(n)?""+n:s.obj(n)?JSON.stringify(n):String(n):""},v=function n(t,e){var r=arguments.length>1,u=s.fn(t)?[]:m(t).split(".").filter(Boolean),o=u.shift();return function(i,c){var f={};return i?s.num(t)||t?s.arr(t)?(t.map(function(t){var r=n(t)(i);null!=(r=s.fn(e)?e(r):null==r?e:r)&&n(t,s.fn(r)?d(r):r)(f)}),f):i[t]||!u.length?r?(i[t]=s.fn(e)?e(i[t],c):e,i):s.fn(t)?t(i):i[t]:r?(n(u.join("."),e)(i[o]?i[o]:i[o]={}),i):n(u.join("."))(i[o]):r?function(n,t){return p(n).map(function(t){delete n[t]}),p(t).map(function(e){n[e]=t[e]}),n}(i,e):i:void 0}},b=h;function h(n){return function(t){return v(t)(n)}}h.parent=function(n){return l(this.parentNode)[n]};var y=function(n){void 0===n&&(n={});var t=n.socket;void 0===t&&(t=function(n){void 0===n&&(n=location.href.replace("http","ws"));var t=u({attempt:0});return t.ready=t.once("connected"),t.connect=c(t,n),t.connect(),t.send=function(n){return t.ready.then(function(t){return t.send(n)})},t}()),t.id=0;var e=u({socket:t,send:w(t),get subscriptions(){return(n=t.on,n?p(n).map(b(n)):[]).map(function(n){return n&&n[0]}).filter(function(n){return n&&n.type&&"$"==n.type[0]});var n}});return t.once("disconnected").map(function(){return t.on("connected").map(x(e))}),t.on("recv").map(g).each(function(n){var r=n.id,u=n.data,o=t.on["$"+r]&&t.on["$"+r][0];u.exec?u.exec(o,u.value):r?t.emit("$"+r,u):e.emit("recv",u)}),e},g=function(n){return new Function("return "+n)()},x=function(n){return function(){return n.subscriptions.map(function(t){var e=t.subscription;return n.socket.send(e)})}},w=function(n,t){return function(e,r){if(e instanceof window.Blob)return j(n,e,r);var u=m(++n.id),o=n.on("$"+u),i=function(e,r){return void 0===r&&(r=0),n.send(o.source.subscription=m({id:u,data:e,type:t})).then(function(){return o.emit("sent",{id:u,count:r})})};return e.next?e.map(i).source.emit("start"):i(e),o.source.once("stop").filter(function(n){return"CLOSED"!=n}).map(function(){return w(n,"UNSUBSCRIBE")(u).filter(function(n,t,e){return e.source.emit("stop","CLOSED")})}),o}},j=function(n,t,e,r,o){void 0===r&&(r=0),void 0===o&&(o=1024);var i=u().on("recv"),c=function(e){return function(){return r>=t.size?i.emit("sent",{id:e}):(n.send(t.slice(r,r+=o)),window.setTimeout(c(e)))}};return w(n,"BINARY")({size:t.size,meta:e}).on("sent",function(n){var t=n.id;return c(t)()}).on("progress",function(n){return i.emit("progress",{received:n,total:t.size})}).map(i.next).source.until(i.once("stop")),i},O={arr:function(n){return Array.prototype.slice.call(n,0)},obj:function(n){var t="id";return 1==arguments.length?(t=n,e):e.apply(this,arguments);function e(n,e,r){return 0===r&&(n={}),n[s.fn(t)?t(e,r):e[t]]=e,n}}};var k={add:function(n,t,e){s.arr(n)?n.splice(t,0,e):n[t]=e},update:function(n,t,e){if(s.num(t)||t)n[t]=e;else{if(!s.obj(e))return!0;for(var r in n)delete n[r];for(var r in e)n[r]=e[r]}},remove:function(n,t){s.arr(n)?n.splice(t,1):delete n[t]}},S=JSON.stringify,B=JSON.parse,$=function(n,t){return function(r,o,i){if(!s.obj(r)&&!s.fn(r))return r;if(!s.obj(n)){var c=o||r.log||[],f=r;if(s.def(i)||(i=c.max||0),i||(c=[]),i<0&&(c=c.concat(null)),i>0){var a=S(r);f=B(a),c=c.concat({type:"update",value:B(a),time:c.length})}return e(c,"max",i),f.log?f.log=c:e(u(f,null),"log",c,1),f}return!!s.def(n.key)&&(!!function n(t,e,r,u){var o=r.shift();if(!k[e])return!1;if(r.length){if(!(o in t)){if("remove"==e)return!0;t[o]={}}return n(t[o],e,r,u)}return!k[e](t,o,u)}(r,n.type,(n.key=""+n.key).split(".").filter(Boolean),n.value)&&(r.log&&r.log.max&&r.log.push((n.time=r.log.length,r.log.max>0?n:null)),!t&&r.emit&&r.emit("change",n),r))}};var z=function(n){var t=n.server;return function(n,e,r){return n instanceof Blob?t.send(n,e):s.obj(n)?t.send(n):t.send({name:n,type:e,value:r})}},N=function(n){return function(t,e){return n.subscribe(t,e).filter(function(n,t,e){return e.source.emit("stop")}).start()}},A=function(n,t,e){return function(r){u&&r.name&&u!=r.name&&n.link(u,r.name);var u=r.name=r.name||t;return r.type||(r.type="update"),s.def(e)&&(r.key=e+"."+m(r.key)),r.key||"update"!=r.type?$(r)(n.resources[u]?n(u):n(u,{})):n(C(r)),n.change=r,v(e)(n(u))}},P=function(n){return function(t,e){if(s.arr(t))return I(t.map(function(t){return n.subscribe(t,e)})).map(function(n){return t.reduce(function(t,e,r){return t[e]=n[r],t},{})});if(n.subscriptions[t]=n.subscriptions[t]||{},s.arr(e))return I(e.map(function(e){return n.subscribe(t,e)})).map(function(){return v(e)(n(t))});var r=u().on("subscription");r.on("stop").each(function(){o.subs.splice(o.subs.indexOf(r),1),function(n,t){1===arguments.length?setTimeout(n):setTimeout(t,n)}(1e3,function(){o.subs.length||(o.source.emit("stop"),n.subscriptions[t][e]=void 0,r.emit("end"))})}),n.subscriptions[t][e]&&r.on("start").map(function(){return v(e)(n(t))}).filter(s.def).map(function(n){return r.next(n)});var o=n.subscriptions[t][e]=n.subscriptions[t][e]||n.send(t,"SUBSCRIBE",e).map(A(n,t,e)).each(function(t){[].concat(o.subs).map(function(n){return n.next(t)}),delete n.change});return o.subs=o.subs||[],o.subs.push(r),r}},E=function(n){return function(e,r){var u=++n.upload.id,o={},i=0,c=function(){if(!f.length)return!0;var t=f.shift(),e=t.field,r=t.filename,o=t.i,a=t.blob;return n.send(a,{filename:r,field:e,i:o,index:u}).on("progress",function(n){var t=n.received;return s.emit("progress",{total:i,received:i-(a.size-t)-f.reduce(function(n,t){return n+t.blob.size},0)})}).then(c)},f=p(r).map(function(n){return o[n]=r[n],n}).filter(function(n){return r[n]instanceof FileList}).map(function(n){return o[n]=[],O.arr(r[n]).map(function(n){return i+=n.size,n}).map(function(t,e){return{field:n,filename:t.name,i:e,blob:t,sent:0}})}).reduce(t,[]),s=n.send({files:f.length,type:"PREUPLOAD",fields:o,index:u,size:i,name:e}).once("sent",c);return s}},C=function(n){var t=n.name,e=n.value;return{name:t,headers:n.headers,body:e}},I=function(n){var t=u().on("merged");return t.streams=n,n.map(function(e){return e.each(function(r){e.latest=r;var u=n.map(function(n){return n.latest});u.every(s.def)&&t.next(u)})}),t.once("start").map(function(){return n.map(function(n){return n.source.emit("start")})}),t.once("stop").map(function(){return n.map(function(n){return n.source.emit("stop")})}),t};return function(n,t,e){void 0===t&&(t={}),void 0===e&&(e={});var r=e.xrs;return void 0===r&&(r=y),n.server=r(),n.send=z(n),n.subscribe=P(n),n.subscriptions={},n.get=N(n),n.upload=E(n),n.upload.id=0,n.server.on("recv").map(function(t,e,r){return A(n)(t,e,r)}),n}}();