UNPKG

@gooddollar/gun

Version:

A realtime, decentralized, offline-first, graph data synchronization engine.

1 lines 38.1 kB
!function(){function G(e,t){return t?require(e):e.slice?G[o(e)]:function(t,n){e(t={exports:{}}),G[o(n)]=t.exports};function o(t){return t.split("/").slice(-1).toString().replace(".js","")}}var b;"undefined"!=typeof module&&(b=module),G(function(t){var n,l,p={};function e(t,n){v(this,n)&&void 0!==this[n]||(this[n]=t)}function o(t,n){var e=this.n;if(!e||!(n===e||g(e)&&v(e,n)))return void 0!==n||void 0}function h(t,n){if(2===arguments.length)return h.r=h.r||{},void(h.r[t]=n);h.r=h.r||[],h.r.push(t)}p.fn={is:function(t){return!!t&&"function"==typeof t}},p.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},p.num={is:function(t){return!d(t)&&(0<=t-parseFloat(t)+1||1/0===t||-1/0===t)}},p.text={is:function(t){return"string"==typeof t}},p.text.ify=function(t){return p.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},p.text.random=function(t,n){var e="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";0<t;)e+=n.charAt(Math.floor(Math.random()*n.length)),t--;return e},p.text.match=function(t,n){var e,o;return"string"==typeof t&&("string"==typeof n&&(n={"=":n}),t===(e=(n=n||{})["="]||n["*"]||n[">"]||n["<"])||o===n["="]&&(e=n["*"]||n[">"]||n["<"],t.slice(0,(e||"").length)===e||o===n["*"]&&(o!==n[">"]&&o!==n["<"]?t>=n[">"]&&t<=n["<"]:o!==n[">"]&&t>=n[">"]||o!==n["<"]&&t<=n["<"])))},p.text.hash=function(t,n){if("string"==typeof t){if(n=n||0,!t.length)return n;for(var e=0,o=t.length;e<o;++e)n=(n<<5)-n+t.charCodeAt(e),n|=0;return n}},p.list={is:function(t){return t instanceof Array}},p.list.slit=Array.prototype.slice,p.list.sort=function(e){return function(t,n){return t&&n?(t=t[e])<(n=n[e])?-1:n<t?1:0:0}},p.list.map=function(t,n,e){return r(t,n,e)},p.list.index=1,p.obj={is:function(t){return!!t&&(t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1])}},p.obj.put=function(t,n,e){return(t||{})[n]=e,t},p.obj.has=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},p.obj.del=function(t,n){if(t)return t[n]=null,delete t[n],t},p.obj.as=function(t,n,e,o){return t[n]=t[n]||(o===e?{}:e)},p.obj.ify=function(n){if(g(n))return n;try{n=JSON.parse(n)}catch(t){n={}}return n},p.obj.to=function(t,n){return r(t,e,n=n||{}),n},p.obj.copy=function(t){return t?JSON.parse(JSON.stringify(t)):t},p.obj.empty=function(t,n){return!t||!r(t,o,{n:n})},l=Object.keys,Object.keys=Object.keys||function(t){return n(t,function(t,n,e){e(n)})},p.obj.map=n=function(t,n,e){var o,i,r,a,u,s=0,f="function"==typeof n;if(h.r=o,l&&g(t)&&(a=l(t),u=!0),e=e||{},d(t)||a)for(i=(a||t).length;s<i;s++){var c=s+p.list.index;if(f){if((r=u?n.call(e,t[a[s]],a[s],h):n.call(e,t[s],c,h))!==o)return r}else if(n===t[u?a[s]:s])return a?a[s]:c}else for(s in t)if(f){if(v(t,s)&&(r=e?n.call(e,t[s],s,h):n(t[s],s,h))!==o)return r}else if(n===t[s])return s;return f?h.r:p.list.index?0:-1},p.time={},p.time.is=function(t){return t?t instanceof Date:+(new Date).getTime()};p.fn.is;var d=p.list.is,i=p.obj,g=i.is,v=i.has,r=i.map;t.exports=p})(G,"./type"),G(function(t){t.exports=function t(n,e,o){if(!n)return{to:t};n=(this.tag||(this.tag={}))[n]||(this.tag[n]={tag:n,to:t._={next:function(t){var n;(n=this.to)&&n.next(t)}}});if("function"!=typeof e)return(n=n.to)&&void 0!==e&&n.next(e),n;var i={off:t.off||(t.off=function(){if(this.next===t._.next)return!0;this===this.the.last&&(this.the.last=this.back),this.to.back=this.back,this.next=t._.next,this.back.to=this.to,this.the.last===this.the&&delete this.on.tag[this.the.tag]}),to:t._,next:e,the:n,on:this,as:o};return(i.back=n.last||n).to=i,n.last=i}})(G,"./onto"),G(function(t){var i="undefined"!=typeof setImmediate?setImmediate:setTimeout,r=[];t.exports=setTimeout.puff=function(t){r.length?r.push(t):(r=[t],i(function t(n){n=n||+new Date;for(var e,o=0;o<9&&(e=r[o++]);)e();if(console.STAT&&console.STAT(n,new Date-n,"puff"),e&&!(new Date-n))return t(n);(r=r.slice(o)).length&&i(t,0)},0))}})(G,"./puff"),G(function(t){if("undefined"==typeof JSON)throw new Error("JSON is not included in this browser. Please load it first: ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js");var r=JSON.stringify;t.exports=function(t,n,e,o,i){if(t<n)return{defer:!0};if(n<e)return{historical:!0};if(e<n)return{converge:!0,incoming:!0};if(n===e){if((o=r(o)||"")===(i=r(i)||""))return{state:!0};if(o<i)return{converge:!0,current:!0};if(i<o)return{converge:!0,incoming:!0}}return{err:"Invalid CRDT Data: "+o+" to "+i+" at "+n+" to "+e+"!"}}})(G,"./HAM"),G(function(t){var n=G("./type"),e={};function o(t,n){return!this.id&&n==i&&u(t)?void(this.id=t):this.id=!1}e.is=function(t){return void 0!==t&&(null===t||t!==1/0&&(!!(u(t)||r(t)||a(t))||(e.link.is(t)||!1)))},e.link=e.rel={_:"#"},e.link.is=function(t){if(t&&t[i]&&!t._&&f(t)){var n={};if(l(t,o,n),n.id)return n.id}return!1},e.link.ify=function(t){return c({},i,t)},n.obj.has._=".";var i=e.link._,r=n.bi.is,a=n.num.is,u=n.text.is,s=n.obj,f=s.is,c=s.put,l=s.map;t.exports=e})(G,"./val"),G(function(t){var n=G("./type"),i=G("./val"),r={_:"_"};function a(t,n){if(n!==r._)return!i.is(t)||void(this.cb&&this.cb.call(this.as,t,n,this.n,this.s))}function o(t,n){var e,o=this.o;o.map?void 0===(e=o.map.call(this.as,t,""+n,o.node))?s(o.node,n):o.node&&(o.node[n]=e):i.is(t)&&(o.node[n]=t)}r.soul=function(t,n){return t&&t._&&t._[n||l]},r.soul.ify=function(t,n){return n="string"==typeof n?{soul:n}:n||{},(t=t||{})._=t._||{},t._[l]=n.soul||t._[l]||c(),t},r.soul._=i.link._,r.is=function(t,n,e){var o;return!!u(t)&&(!!(o=r.soul(t))&&!f(t,a,{as:e,cb:n,s:o,n:t}))},r.ify=function(t,n,e){return n?"string"==typeof n?n={soul:n}:"function"==typeof n&&(n={map:n}):n={},n.map&&(n.node=n.map.call(e,t,void 0,n.node||{})),(n.node=r.soul.ify(n.node||{},n))&&f(t,o,{o:n,as:e}),n.node};var e=n.obj,u=e.is,s=e.del,f=e.map,c=n.text.random,l=r.soul._;t.exports=r})(G,"./node"),G(function(t){var n=G("./type"),a=G("./node");function u(){var t=+new Date;return e=e<t?(o=0,t+u.drift):t+(o+=1)/i+u.drift}n.time.is;var e=-1/0,o=0,i=1e3,r="undefined"!=typeof performance&&(performance.timing&&performance);r&&r.timing&&r.timing.navigationStart||(r=!1),u._=">";function s(t,n){m!==n&&u.ify(this.o,n,this.s)}u.drift=0,u.is=function(t,n,e){var o=n&&t&&t[m]&&t[m][">"]||e;if(o)return g(o=o[n])?o:-1/0},u.lex=function(){return u().toString(36).replace(".","")},u.ify=function(t,n,e,o,i){if(!t||!t[m]){if(!i)return;t=a.soul.ify(t,i)}var r=c(t[m],">");return void 0!==n&&n!==m&&(g(e)&&(r[n]=e),void 0!==o&&(t[n]=o)),t},u.to=function(t,n,e){var o=(t||{})[n];return p(o)&&(o=d(o)),u.ify(e,n,u.is(t,n),o,a.soul(t))},u.map=function(i,r,a){var t=p(t=i||r)?t:null;return i=v(i=i||r)?i:null,t&&!i?(r=g(r)?r:u(),t[m]=t[m]||{},h(t,s,{o:t,s:r}),t):(a=a||p(r)?r:void 0,r=g(r)?r:u(),function(t,n,e,o){if(!i)return s.call({o:e,s:r},t,n),t;i.call(a||this||{},t,n,e,o),l(e,n)&&void 0===e[n]||s.call({o:e,s:r},t,n)})};var f=n.obj,c=f.as,l=f.has,p=f.is,h=f.map,d=f.copy,g=n.num.is,v=n.fn.is,m=a._;t.exports=u})(G,"./state"),G(function(t){var a=G("./type"),u=G("./val"),s=G("./node"),i={};function r(t,n){if(!t||n!==s.soul(t)||!s.is(t,this.fn,this.as))return!0;this.cb&&(e.n=t,e.as=this.as,this.cb.call(e.as,t,n,e))}function e(t){t&&s.is(e.n,t,e.as)}function f(t,n){var e;return(e=function(t,n){var e,o=t.seen,i=o.length;for(;i--;)if(e=o[i],n.obj===e.obj)return e;o.push(n)}(t,n))?e:(n.env=t,n.soul=c,s.ify(n.obj,o,n)&&(n.link=n.link||u.link.ify(s.soul(n.node)),n.obj!==t.shell&&(t.graph[u.link.is(n.link)]=n.node)),n)}function o(t,n,e){var o,i,r=this,a=r.env;if(s._===n&&v(t,u.link._))return e._;if(o=l(t,n,e,r,a)){if(n||(r.node=r.node||e||{},v(t,s._)&&s.soul(t)&&(r.node._=y(t._)),r.node=s.soul.ify(r.node,u.link.is(r.link)),r.link=r.link||u.link.ify(s.soul(r.node))),(i=a.map)&&(i.call(a.as||{},t,n,e,r),v(e,n))){if(t=e[n],h===t)return void g(e,n);if(!(o=l(t,n,e,r,a)))return}if(!n)return r.node;if(!0===o)return t;if((i=f(a,{obj:t,path:r.path.concat(n)})).node)return i.link}}function c(t){var n=this,e=u.link.is(n.link),o=n.env.graph;n.link=n.link||u.link.ify(t),n.link[u.link._]=t,n.node&&n.node[s._]&&(n.node[s._][u.link._]=t),v(o,e)&&(o[t]=o[e],g(o,e))}function l(t,n,e,o,i){var r;return!!u.is(t)||(d(t)?1:(r=i.invalid)?l(t=r.call(i.as||{},t,n,e),n,e,o,i):(i.err="Invalid value at '"+o.path.concat(n).join(".")+"'!",void(a.list.is(t)&&(i.err+=" Use `.set(item)` instead of an Array."))))}function p(t,n){var e,o;if(s._!==n)(e=u.link.is(t))?(o=this.opt.seen[e])?this.obj[n]=o:this.obj[n]=this.opt.seen[e]=i.to(this.graph,e,this.opt):this.obj[n]=t;else{if(m(t,u.link._))return;this.obj[n]=y(t)}}i.is=function(t,n,e,o){return!(!t||!d(t)||m(t))&&!b(t,r,{cb:n,fn:e,as:o})},i.ify=function(t,n,e){var o={path:[],obj:t};return n?"string"==typeof n?n={soul:n}:"function"==typeof n&&(n.map=n):n={},"string"==typeof e&&(n.soul=n.soul||e,e=h),n.soul&&(o.link=u.link.ify(n.soul)),n.shell=(e||{}).shell,n.graph=n.graph||{},n.seen=n.seen||[],n.as=n.as||e,f(n,o),n.root=o.node,n.graph},i.node=function(t){var n=s.soul(t);if(n)return k({},n,t)},i.to=function(t,n,e){if(t){var o={};return e=e||{seen:{}},b(t[n],p,{obj:o,graph:t,opt:e}),o}};a.fn.is;var h,n=a.obj,d=n.is,g=n.del,v=n.has,m=n.empty,k=n.put,b=n.map,y=n.copy;t.exports=i})(G,"./graph"),G(function(t){G("./onto"),t.exports=function(t,n){if(this.on){if("function"!=typeof t){if(!t||!n)return;var e=t["#"]||t,o=(this.tag||"")[e];if(!o)return;return o=this.on(e,n),clearTimeout(o.err),!0}e=n&&n["#"]||Math.random().toString(36).slice(2);if(!t)return e;var i=this.on(e,t,n);return i.err=i.err||setTimeout(function(){i.next({err:"Error: No ACK yet.",lack:!0}),i.off()},(this.opt||{}).lack||9e3),e}}})(G,"./ask"),G(function(t){var u=G("./type");t.exports=function(i){var r={s:{}},a=r.s;i=i||{max:1e3,age:27e3},r.check=function(t){return!!a[t]&&n(t)};var n=r.track=function(t){var n=a[t]||(a[t]={});return n.was=+new Date,r.to||(r.to=setTimeout(r.drop,i.age+9)),n};return r.drop=function(e){var o=+new Date;u.obj.map(a,function(t,n){t&&(e||i.age)>o-t.was||delete a[n]}),r.to=null,console.STAT&&9<(e=new Date-o)&&console.STAT(o,e,"dup drop")},r}})(G,"./dup"),G(function(t){function _(t){return t instanceof _?(this._={$:this}).$:this instanceof _?_.create(this._={$:this,opt:t}):new _(t)}_.is=function(t){return t instanceof _||t&&t._&&t===t._.$||!1},_.version=.202,_.chain=_.prototype,_.chain.toJSON=function(){};var n=G("./type");function r(t,n,e,o){var i=this,r=_.state.is(e,n);if(!r)return i.err="Error: No state on '"+n+"' in node '"+o+"'!";var a=i.graph[o]||A,u=_.state.is(a,n,!0),s=a[n],f=_.HAM(i.machine,r,u,t,s);f.incoming?(i.put[o]=_.state.to(e,n,i.put[o]),(i.diff||(i.diff={}))[o]=_.state.to(e,n,i.diff[o]),i.souls[o]=!0):f.defer&&(i.defer=r<(i.defer||1/0)?r:i.defer)}function a(t,n){var e=this,o=e.$._,i=(o.next||A)[n];if(!i){if(!(o.opt||A).super)return void(e.souls[n]=!1);i=e.$.get(n)._}var r=e.map[n]={put:t,get:n,$:i.$},a={ctx:e,msg:r};e.async=!!o.tag.node,e.ack&&(r["@"]=e.ack),m(t,u,a),e.async&&(e.and||o.on("node",function(t){this.to.next(t),t===e.map[t.get]&&(e.souls[t.get]=!1,m(t.put,s,t),m(e.souls,function(t){if(t)return t})||e.c||(e.c=1,this.off(),m(e.map,f,e)))}),e.and=!0,o.on("node",r))}function u(t,n){var e=this.ctx,o=e.graph,i=this.msg,r=i.get,a=i.put,u=i.$._;o[r]=_.state.to(a,n,o[r]),e.async||(u.put=_.state.to(a,n,u.put))}function s(t,n){var e=this.put,o=this.$._;o.put=_.state.to(e,n,o.put)}function f(t,n){t.$&&(this.cat.stop=this.stop,t.$._.on("in",t),this.cat.stop=null)}function c(t,n){if(t!==this.graph[n])return!0}n.obj.to(n,_),_.HAM=G("./HAM"),_.val=G("./val"),_.node=G("./node"),_.state=G("./state"),_.graph=G("./graph"),_.on=G("./onto"),_.ask=G("./ask"),_.dup=G("./dup"),_.puff=G("./puff"),function(){function f(t){var n,e,o,i,r,a;t&&(t.out!==f?(o=(e=(n=this.as).at||n).$,i=e.dup,a=t.DBG,(r=t["#"])||(r=t["#"]=p(9)),i.check(r)||(i.track(r),r=t._,t._="function"==typeof r?r:function(){},t.$&&t.$===(t.$._||"").$||(t.$=o),e.ask(t["@"],t)||(a&&(a.u=+new Date),t.get&&_.on._get(t,o),!t.put)?(a&&(a.uc=+new Date),this.to.next(t),a&&(a.ua=+new Date),t.out=f,e.on("out",t),a&&(a.ue=+new Date)):u(t))):this.to.next(t))}function u(t){if(t){var n,e,o=t._||"",i=o.root=((o.$=t.$||"")._||"").root,r=t.put,a=t["#"],u=o.DBG=t.DBG;if(r["#"]&&r["."])i.on("put",t);else{o.out=t,o.lot={s:0,more:1};var s=+new Date;for(var f in u&&(u.p=s),r){var c,l=r[f];if(!l){n=v+m(f)+"no node.";break}if(!(e=l._)){n=v+m(f)+"no meta.";break}if(f!==e[j]){n=v+m(f)+"soul not same.";break}if(!(c=e[D])){n=v+m(f)+"no state.";break}for(var p in l)if(T!==p){var h=l[p],d=c[p];if(x===d){n=v+m(p)+"on"+m(f)+"no state.";break}if(!S(h)){n=v+m(p)+"on"+m(f)+"bad "+typeof h+m(h);break}!function t(n,e,o,i,r){var a,u=r._||"",s=u.root,f=s.graph;var c=f[o]||A,l=$(c,e,1),p=c[e];var h=y(),d=k(h,i,l,n,p);if(!d.incoming){if(d.defer){var g=i-h;return setTimeout(function(){t(n,e,o,i,r)},b<g?b:g),u.to||s.on("in",{"@":r["#"],warn:g}),u.to=1,g}if(!u.miss)return}(a=u.lot||"").s++;a.more++;(u.stun||(u.stun={}))[o+e]=1;var v=u.DBG;v&&(v.ph=v.ph||+new Date);s.on("put",{"#":r["#"],"@":r["@"],put:{"#":o,".":e,":":n,">":i},_:u})}(h,p,f,d,t)}if(n)break}u&&(u.pe=+new Date),console.STAT&&(console.STAT(s,new Date-s,"mix"),console.STAT(s,o.lot.s,"mix #")),(o.err=n)?i.on("in",{"@":a,err:_.log(n)}):(--o.lot.more||g(o),o.stun||t["@"]||i.on("in",{"@":a,ok:-1}))}}}function e(t){var n;(n=(t._||"").DBG)&&(n.pa=+new Date,n.pm=n.pm||+new Date);var e,o=this.as.graph,i=t._,r=t.put,a=r["#"],u=r["."],s=r[":"],f=r[">"];t["#"];o[a]=w(o[a],u,f,s,a),function(t,n,e,o,i){var r,a,u=t.root;if((u.opt||"").super&&u.$.get(n),!(u&&(a=u.next)&&(a=a[n])&&a.$))return;(r=t.put||(t.put={}))[n]=w(r[n],e,i,o,n),a.put=w(a.put,e,i,o,n)}(i,a,u,x!==(e=r["="])?e:s,f),(e=i.out)&&(e=e.put)&&(e[a]=w(e[a],u,f,s,a)),--i.lot.more||g(i),this.to.next(t)}function g(t){if(!t.err){var n,e={},o=((t.$||"")._||"").root,i=(o||"").next||"",r=t.put,a=+new Date;for(var u in r){var s=r[u];(n=i[u])&&n.$&&(o.stop=e,n.on("in",{$:n.$,get:u,put:s}),o.stop=null)}console.STAT&&console.STAT(a,new Date-a,"fire"),t.DBG&&(t.DBG.f=+new Date),(n=t.out)&&(n.out=f,o.on("out",n))}}_.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||_.on,t.ask=t.ask||_.ask,t.dup=t.dup||_.dup();var n=t.$.opt(t.opt);return t.once||(t.on("in",f,t),t.on("out",f,t),t.on("put",e,t),_.on("create",t),t.on("create",t)),t.once=1,n},_.on.put=u;var v="Error: Invalid graph!",m=function(t){return" '"+(""+t).slice(0,9)+"...' "},k=_.HAM,b=2147483647,y=_.state}(),_.on._put=function(t,n){var e,o=n._,i={$:n,graph:o.graph,put:{},map:{},souls:{},machine:_.state(),ack:t["@"],cat:o,stop:{}};if(_.obj.map(t.put,c,i)){if(_.graph.is(t.put,null,r,i)||(i.err="Error: Invalid graph!"),i.err)return o.on("in",{"@":t["#"],err:_.log(i.err)});m(i.put,a,i),i.async||m(i.map,f,i),x!==i.defer&&(e=i.defer-i.machine,setTimeout(function(){_.on._put(t,n)},e>MD?MD:e)),i.diff&&o.on("put",v(t,{put:i.diff}))}},_.on._get=function(t,n){var e=n._,o=t.get,i=o[j],r=e.graph[i],a=o[k],u=(e.next||(e.next={}))[i],s=(t._||"").DBG=t.DBG;if(s&&(s.g=+new Date),!r)return e.on("get",t);if(a){if("string"!=typeof a||!g(r,a))return e.on("get",t);r=_.state.to(r,a)}else r=_.window?_.obj.copy(r):r;r=_.graph.node(r),(u||A).ack;function f(){}f.ram=f.faith=!0,f.$=t.$,s&&(s.ga=+new Date),e.on("in",{"@":t["#"],put:r,ram:1,$:n,_:f}),s&&(s.gm=+new Date),e.on("get",t),s&&(s.gd=+new Date)},_.chain.opt=function(t){t=t||{};var n=this._,e=t.peers||t;return d(t)||(t={}),d(n.opt)||(n.opt=t),l(e)&&(e=[e]),o(e)&&(e=m(e,function(t,n,e){(n={}).id=n.url=t,e(t,n)}),d(n.opt.peers)||(n.opt.peers={}),n.opt.peers=v(e,n.opt.peers)),n.opt.peers=n.opt.peers||{},m(t,function t(n,e){!g(this,e)||i.is(n)||h.empty(n)?this[e]=n:n&&n.constructor!==Object&&!o(n)||m(n,t,this[e])},n.opt),_.on("opt",n),_.obj.native(),this},_.obj.native=function(){var t=Object.prototype;for(var n in t)console.log("Native Object.prototype polluted, reverting",n),delete t[n]};var x,e,o=_.list.is,i=_.text,l=i.is,p=i.random,h=_.obj,d=(h.empty,h.is),g=h.has,v=h.to,m=h.map,w=(h.copy,_.state.lex,_.state.ify),$=_.state.is,j=_.val.link._,k=".",T=_.node._,S=_.val.is,D=(_.val.link.is,_.state._),A={};_.log=function(){return _.log.off||e.log.apply(e,arguments),[].slice.call(arguments).join(" ")},_.log.once=function(t,n,e){return(e=_.log.once)[t]=e[t]||0,e[t]++||_.log(n)},"undefined"!=typeof window&&((window.GUN=window.Gun=_).window=window);try{void 0!==b&&(b.exports=_)}catch(t){}((t.exports=_).window||"").console=(_.window||"").console||{log:function(){}},(e=console).only=function(t,n){return e.only.i&&t===e.only.i&&e.only.i++&&(e.log.apply(e,arguments)||n)},_.log.once("welcome","Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!")})(G,"./root"),G(function(t){var u=G("./root");u.chain.back=function(t,n){if(-1===(t=t||1)||1/0===t)return this._.root.$;if(1===t)return(this._.back||this._).$;var e=this._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){for(var o=0,i=t.length,r=e;o<i;o++)r=(r||s)[t[o]];return void 0!==r?n?this:r:(r=e.back)?r.$.back(t,n):void 0}if("function"!=typeof t)return u.num.is(t)?(e.back||e).$.back(t-1):this;for(var a,r={back:e};(r=r.back)&&void 0===(a=t(r,n)););return a};var s={}})(G,"./back"),G(function(t){var s=G("./root");function r(t){var e,n,o=this.as,i=o.back,r=o.root;if(t.$||(t.$=o.$),this.to.next(t),e=t.get){if(o.lex&&(t.get=k(o.lex,t.get)),e["#"]||o.soul){if(e["#"]=e["#"]||o.soul,t["#"]||(t["#"]=y(9)),i=r.$.get(e["#"])._,e=e["."]){if(v(i.put,e)){if((n=(a=i.$.get(e)._).ack)||(a.ack=-1),i.on("in",{$:i.$,put:s.state.to(i.put,e),get:i.get}),n)return}else if("string"!=typeof e){var a={},u=(i.put||{})._;if(s.obj.map(i.put,function(t,n){s.text.match(n,e)&&(a[n]=t)}),s.obj.empty(a)||(a._=u,i.on("in",{$:i.$,put:a,get:i.get})),n=o.lex){if(n=n._||(n._=function(){}),i.ack<n.ask&&(n.ask=i.ack),n.ask)return;n.ask=1}}}else{if((n=i.ack)||(i.ack=-1),v(i,"put")&&i.on("in",i),n&&d!==i.put)return;t.$=i.$}return r.ask(h,t),r.on("in",t)}if(r.now&&(r.now[o.id]=r.now[o.id]||!0,o.pass={}),e["."])return o.get?(t={get:{".":o.get},$:o.$},i.ask||(i.ask={}),i.ask[o.get]=t.$._):t={get:{},$:o.$},i.on("out",t);if(o.ack=o.ack||-1,o.get)return t.$=o.$,e["."]=o.get,(i.ask||(i.ask={}))[o.get]=t.$._,i.on("out",t)}return i.on("out",t)}function a(t){var n,e=this,o=e.as,i=o.root,r=(t.$||g)._||g,a=t.put;if(o.get&&t.get!==o.get&&(t=k(t,{get:o.get})),o.has&&r!==o&&(t=k(t,{$:o.$}),r.ack&&(o.ack=r.ack)),d===a){if(u=r.put,e.to.next(t),o.soul)return;if(d===u&&d!==r.put)return;return c(o,t),o.has&&p(o,t),m(r.echo,o.id),void m(o.map,r.id)}if(o.soul)return e.to.next(t),c(o,t),void(o.next&&b(a,l,{msg:t,cat:o}));if(!(n=s.val.link.is(a)))return s.val.is(a)?(o.has||o.soul?p(o,t):(r.has||r.soul)&&((r.echo||(r.echo={}))[o.id]=r.echo[r.id]||o,(o.map||(o.map={}))[r.id]=o.map[r.id]||{at:r}),e.to.next(t),void c(o,t)):(o.has&&r!==o&&v(r,"put")&&(o.put=r.put),(n=s.node.soul(a))&&r.has&&(r.put=o.root.$.get(n)._.put),u=(i.stop||{})[r.id],e.to.next(t),f(o,t,r,n),c(o,t),void(o.next&&b(a,l,{msg:t,cat:o})));var u;i.stop;(u=(u=i.stop||{})[r.id]||(u[r.id]={})).is=u.is||r.put,u[o.id]=r.put||!0,e.to.next(t),f(o,t,r,n),c(o,t)}function f(t,n,e,o){var i;o&&u!==t.get&&(i=t.root.$.get(o)._,t.has?e=i:e.has&&f(e,n,e,o),e!==t&&(e.$||(e={}),(e.echo||(e.echo={}))[t.id]=e.echo[t.id]||t,t.has&&!(t.map||g)[e.id]&&p(t,n),(o!==(i=e.id?(t.map||(t.map={}))[e.id]=t.map[e.id]||{at:e}:{}).link||i.pass||t.pass)&&(t.pass&&(s.obj.map(t.map,function(t){t.pass=!0}),m(t,"pass")),i.pass&&m(i,"pass"),t.has&&(t.link=o),function(t,o){var n=t.root.$.get(o)._,e=t.lex;if((t.ack||e)&&((e=e||{})["#"]=o,n.on("out",{get:e}),!t.ask))return;n=t.ask,s.obj.del(t,"ask"),b(n||t.next,function(t,n){var e=t.lex||{};e["#"]=o,e["."]=e["."]||n,t.on("out",{get:e})}),s.obj.del(t,"ask")}(t,i.link=o))))}function c(t,n){t.echo&&b(t.echo,e,n)}function e(t){t&&t.on&&t.on("in",this)}function l(t,n){var e,o,i,r=this.cat.next||g,a=this.msg;(u!==n||r[n])&&(o=r[n])&&(o.has?(d!==o.put&&s.val.link.is(t)||(o.put=t),e=o.$):(i=a.$)&&(i=(e=a.$.get(n))._,d!==i.put&&s.val.link.is(t)||(i.put=t)),o.on("in",{put:t,get:n,$:e,via:a}))}function p(e,t){var o;(e.has||e.soul)&&(o=e.map,e.root,e.map=null,e.has&&(e.dub&&e.root.stop&&(e.dub=null),e.link=null),(e.pass||t["@"]||null!==o)&&(d===o&&s.val.link.is(e.put)||(b(o,function(t){(t=t.at)&&m(t.echo,e.id)}),o=e.put,b(e.next,function(t,n){return d===o&&d!==e.put||(t.put=d,t.ack&&(t.ack=-1),void t.on("in",{get:n,$:t.$,put:d}))}))))}function h(t,n){var e=this.as,o=e.get||"",i=e.$._,r=(t.put||"")[o["#"]];if(i.ack&&(i.ack=i.ack+1||1),t.put&&("string"!=typeof o["."]||v(r,i.get)))u!=o["."]?(i.$===(t._||"").$&&(t._.miss=i.put===d),s.on.put(t)):i.on("in",{get:i.get,put:s.val.link.ify(o["#"]),$:i.$,"@":t["@"]});else{if(i.put!==d)return;i.on("in",{get:i.get,put:i.put=d,$:i.$,"@":t["@"]})}}s.chain.chain=function(t){var n,e=this._,o=new(t||this).constructor(this),i=o._;return i.root=n=e.root,i.id=++n.once,i.back=this._,i.on=s.on,i.on("in",a,i),i.on("out",r,i),o};var d,g={},n=s.obj,v=n.has,m=(n.put,n.del),k=n.to,b=n.map,y=s.text.random,u=(s.val.link._,s.node._)})(G,"./chain"),G(function(t){var f=G("./root");function c(t){var n,e=this,o=e.as,i=o.at.root,r=(t.$||{})._||{},a=t.put||r.put;if((n=i.now)&&e!==n[o.now])return e.to.next(t);if(e.seen&&r.id&&e.seen[r.id])return e.to.next(t);if((n=a)&&n[g._]&&(n=g.is(n))&&(n=(t.$$=r.root.$.get(n))._,p!==n.put&&(t=s(t,{put:a=n.put}))),(n=i.mum)&&r.id){var u=r.id+(e.id||(e.id=f.text.random(9)));if(n[u])return;p===a||g.is(a)||(n[u]=!0)}o.use(t,e),e.stun?e.stun=null:e.to.next(t)}function l(t){var n=this.on;if(!t||n.soul||n.has)return this.off();if(t=(t=(t=t.$||t)._||t).id){var e;n.map;if((e=this.seen||(this.seen={}))[t])return!0;e[t]=!0}}f.chain.get=function(t,n,e){var o;if("string"!=typeof t){if("function"!=typeof t)return d(t)?this.get(""+t,n,e):(u=g.is(t))?this.get(u,n,e):h.is(t)?(o=this,(u=((u=t["#"])||m)["="]||u)&&(o=o.get(u)),o._.lex=t,o):((e=this.chain())._.err={err:f.log("Invalid get request!",t)},n&&n.call(e,e._.err),e);if(!0===n)return function(t,n,e){var a,u=t._,s=0;if(a=u.soul||u.link||u.dub)return n(a,e,u);if(u.jam)return u.jam.push([n,e]);u.jam=[[n,e]],t.get(function(t,n){if(!(p===t.put&&!u.root.opt.super&&(a=Object.keys(u.root.opt.peers).length)&&++s<=a)){n.rid(t);var e=(e=t.$)&&e._||{},o=0;for(a=u.jam,delete u.jam;r=a[o++];){var i=r[0],r=r[1];i&&i(e.link||e.soul||g.is(t.put)||v(t.put)||e.dub,r,t,n)}}},{out:{get:{".":!0}}})}(this,t,e),this;var i,r=(o=this)._,a=r.root,u=a.now;(e=n||{}).at=r,e.use=t,e.out=e.out||{},e.out.get=e.out.get||{},(i=r.on("in",c,e)).rid=l,(a.now={$:1})[e.now=r.id]=i;var s=a.mum;return a.mum={},r.on("out",e.out),a.mum=s,a.now=u,o}return(o=(this._.next||m)[t])||(o=function(t,n){var e=n._,o=e.next,i=n.chain()._;o=o||(e.next={});o[i.get=t]=i,n===e.root.$?i.soul=t:(e.soul||e.has)&&(i.has=t);return i}(t,this)),o=o.$,(u=this._.stun)&&(o._.stun=o._.stun||u),n&&"function"==typeof n&&o.get(n,e),o};var p,h=f.obj,s=(h.map,h.has,f.obj.to),d=f.num.is,g=f.val.link,v=f.node.soul,m=(f.node._,{})})(G,"./get"),G(function(t){var f=G("./root");function u(t){t&&t()}function s(){var r=this;r.graph&&n(r.stun)&&(r.res=r.res||function(t){t&&t()},r.res(function(){delete r.via._.stun;var n=r.$.back(-1)._,t=n.ask(function(t){n.root.on("ack",t),t.err&&f.log(t),++e>(r.acks||0)&&this.off(),r.ack&&r.ack(t,this)},r.opt),e=0,o=n.root.now;a.del(n.root,"now");var i=n.root.mum;n.root.mum={},r.ref._.on("out",{$:r.ref,put:r.out=r.env.graph,opt:r.opt,"#":t}),n.root.mum=i?a.to(i,n.root.mum):i,n.root.now=o,r.via._.on("res",{}),delete r.via._.tag.res},r),r.res&&r.res())}function c(r,t,n,a){var u=this,s=f.is(r);!t&&a.path.length&&(u.res||e)(function(){for(var t=a.path,n=u.ref,e=(u.opt,0),o=t.length;e<o;e++)n=n.get(t[e]);s&&(n=r);var i=n._.dub;if(i=i||f.node.soul(a.obj))return n.back(-1).get(i),void a.soul(i);(u.stun=u.stun||{})[t]=1,n.get(l,!0,{as:{at:a,as:u,p:t,ref:n}})},{as:u,at:a})}f.chain.put=function(t,o,i){var n,r=this,e=r._,a=e.root.$;a._;return(i=i||{}).data=t,i.via=i.$=i.via||i.$||r,"string"==typeof o?i.soul=o:i.ack=i.ack||o,e.soul&&(i.soul=e.soul),i.soul||a===r?d(i.data)?(i.soul=i.soul||(i.not=f.node.soul(i.data)||(i.via.back("opt.uuid")||f.text.random)()),i.via._.stun={},i.soul?(i.$=a.get(i.soul),i.ref=i.$,function(t){t.batch=s;var n=t.opt||{},e=t.env=f.state.map(c,n.state);if(e.soul=t.soul,t.graph=f.graph.ify(t.data,e,t),e.err)return(t.ack||m).call(t,t.out={err:f.log(e.err)}),t.res&&t.res();t.batch()}(i)):i.via.back("opt.uuid")(function(t,n){return t?f.log(t):void(i.ref||i.$).put(i.data,i.soul=n,i)}),r):((i.ack||m).call(i,i.out={err:f.log("Data saved to the root level of the graph must be a node (an object), not a",typeof i.data,'of "'+i.data+'"!')}),i.res&&i.res(),r):(i.via._.stun={},f.is(t)?(t.get(function(t,n,e){return t?void r.put(f.val.link.ify(t),o,i):(delete i.via._.stun,f.log("The reference you are saving is a",typeof e.put,'"'+e.put+'", not a node (object)!'))},!0),r):(e.has&&(n=f.val.link.is(t))&&(e.dub=n),i.ref=i.ref||a._===(n=e.back)?r:n.$,i.ref._.soul&&f.val.is(i.data)&&e.get?(i.data=g({},e.get,i.data),i.ref.put(i.data,i.soul,i)):(i.ref.get(h,!0,{as:i}),i.out||(i.res=i.res||u,i.$._.stun=i.ref._.stun)),r))};String.fromCharCode(31);function l(t,e,n,o){(e=e.as).p;var i,r=e.ref,a=e.at,u=[];e=e.as,r.back(function(t){return(i=t.soul||t.link||t.dub)?i:void u.push(t.has||t.get)}),u=[i||e.soul].concat(u.reverse());var s=((n||{}).$||{})._||{};t=s.dub=s.dub||t||f.node.soul(a.obj)||f.node.soul(n.put||s.put)||f.val.link.is(n.put||s.put)||u.join("/"),o&&(o.stun=!0),t?p(s,s.dub=t,a,e):e.via.back("opt.uuid")(function(t,n){return t?f.log(t):void p(s,s.dub=s.dub||n,a,e)})}function p(t,n,e,o){t.$.back(-1).get(n),e.soul(n),delete o.stun[e.path],o.batch()}function h(t,e,n,o){if(e=e.as,n.$&&n.$._)if(n.err)f.log("Please report this as an issue! Put.any.err");else{var i,r=n.$._,a=r.put,u=e.opt||{};if(!(i=e.ref)||!i._.now){if(o&&(o.stun=!0),e.ref!==e.$){if(!(i=e.$._.get||r.get))return delete e.via._.stun,void f.log("Please report this as an issue! Put.no.get");e.data=g({},i,e.data),i=null}if(void 0===a){if(!r.get)return void delete e.via._.stun;t||(i=r.$.back(function(t){return t.link||t.soul?t.link||t.soul:void(e.data=g({},t.get,e.data))}),e.not=!0),r=(i=i||r.soul||r.link||r.dub)?r.root.$.get(i)._:r,e.soul=i,a=e.data}e.not||(e.soul=e.soul||t)||(e.path&&d(e.data)?e.soul=(u.uuid||e.via.back("opt.uuid")||f.text.random)():(k==r.get&&(e.soul=(r.put||v)["#"]||r.dub),e.soul=e.soul||r.soul||r.link||(u.uuid||e.via.back("opt.uuid")||f.text.random)()),e.soul)?e.ref.put(e.data,e.soul,e):e.via.back("opt.uuid")(function(t,n){return t?(delete e.via._.stun,f.log(t)):void e.ref.put(e.data,e.soul=n,e)})}}}var a=f.obj,d=a.is,g=a.put,n=(a.map,a.empty),v={},m=function(){},e=function(t,n){t.call(n||v)},k=f.node._})(G,"./put"),G(function(t){var n=G("./root");G("./chain"),G("./back"),G("./put"),G("./get"),t.exports=n})(G,"./index"),G(function(t){var f=G("./index");function u(t,n){var e,o=this,i=t.$,r=((i||{})._||{}).put||t.put;o.at;if(l!==r){if(e=t.$$){if(e=t.$$._,l===e.put)return;r=e.put}o.change&&(r=t.put),o.as?o.ok.call(o.as,t,n):o.ok.call(i,r,t.get,t,n)}}function c(t,n,e){var o,i,r,a,u,s;t.$?((o=this.as).at,a=(r=(i=t.$)._).put||t.put,(s=t.$$)&&(u=s=t.$$._,l!==u.put&&(a=u.put)),(s=n.wait)&&(s=s[r.id])&&clearTimeout(s),n.ack=(n.ack||0)+1,!e&&l===a&&!r.root.opt.super&&n.ack<=(o.acks||Object.keys(r.root.opt.peers).length)||(!e&&(l===a||r.soul||r.link||u&&!(0<u.ack))||l===a&&!r.root.opt.super&&(s=Object.keys(r.root.opt.peers).length)&&!e&&(u||r).ack<s?s=(n.wait={})[r.id]=setTimeout(function(){c.call({as:o},t,n,s||1)},o.wait||99):(u&&l===u.put&&(s=p.is(a))&&(a=f.node.ify({},s)),n.rid?n.rid(t):n.off(),o.ok.call(i||o.$,a,t.get)))):n.off()}f.chain.on=function(t,n,e,o){var i,r=this._;if("string"==typeof t)return n?(i=r.on(t,n,e||r,o),e&&e.$&&(e.subs||(e.subs=[])).push(i),this):r.on(t);var a=n;return(a=!0===a?{change:!0}:a||{}).at=r,a.ok=t,this.get(u,a),this},f.chain.val=function(t,n){return f.log.once("onceval","Future Breaking API Change: .val -> .once, apologies unexpected."),this.once(t,n)},f.chain.once=function(t,n){var e=this,o=e._,i=o.put;if(0<o.ack&&l!==i)return(t||a).call(e,i,o.get),e;if(t)return(n=n||{}).ok=t,n.at=o,n.out={"#":f.text.random(9)},e.get(c,{as:n}),n.async=!0,e;f.log.once("valonce","Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");var r=e.chain();return r._.nix=e.once(function(){r._.on("in",e._)}),r},f.chain.off=function(){var t,n=this._,e=n.back;if(e)return n.ack=0,(t=e.next)&&t[n.get]&&i(t,n.get),(t=e.ask)&&i(t,n.get),(t=e.put)&&i(t,n.get),(t=n.soul)&&i(e.root.graph,t),(t=n.map)&&o(t,function(t){t.link&&e.root.$.get(t.link).off()}),(t=n.next)&&o(t,function(t){t.$.off()}),n.on("off",{}),this};var l,n=f.obj,o=n.map,i=(n.has,n.del),p=(n.to,f.val.link),a=function(){}})(G,"./on"),G(function(t){var u=G("./index");function i(t){if(!t.put||u.val.is(t.put))return this.to.next(t);this.as.nix&&this.off(),e(t.put,n,{at:this.as,msg:t}),this.to.next(t)}function n(t,n){var e,o,i,r;a!==n&&(o=(e=this.msg.$)._,i=this.at,(r=o.lex)&&!u.text.match(n,r["."]||r["#"]||r)||(((r=e.get(n)._).echo||(r.echo={}))[i.id]=r.echo[i.id]||i))}u.chain.map=function(r,t,n){var a,e=this,o=e._;return r?(u.log.once("mapfn","Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."),a=e.chain(),e.map().on(function(t,n,e,o){var i=(r||s).call(this,t,n,e,o);if(void 0!==i)return t===i?a._.on("in",e):u.is(i)?a._.on("in",i._):void a._.on("in",{get:n,put:i})})):(a=o.each)||(o.each=a=e.chain(),a._.nix=e.back("nix"),e.on("in",i,a._)),a};var e=u.obj.map,s=function(){},a=u.node._})(G,"./map"),G(function(t){var u=G("./index");function n(){return u.state.lex()+u.text.random(7)}u.chain.set=function(o,i,r){var t,a=this;return i=i||function(){},(r=r||{}).item=r.item||o,(t=u.node.soul(o))&&(o=u.obj.put({},t,u.val.link.ify(t))),u.is(o)?(o.get(function(t,n,e){return t||!o._.stun?t?void a.put(u.obj.put({},t,u.val.link.ify(t)),i,r):i.call(a,{err:u.log('Only a node can be linked! Not "'+e.put+'"!')}):void o._.on("res",function(){this.off(),a.set(o,i,r)})},!0),o):(u.obj.is(o)&&(t=t||u.node.soul(o)||n()),a.get(t||n()).put(o,i,r))}})(G,"./set"),G(function(t){if("undefined"!=typeof Gun){var h;try{h=(Gun.window||function(){}).localStorage}catch(t){}h||(Gun.log("Warning: No localStorage exists to persist data to!"),h={setItem:function(t,n){this[t]=n},removeItem:function(t){delete this[t]},getItem:function(t){return this[t]}}),Gun.on("create",function(t){var n=this.to,e=t.opt;if(t.once)return n.next(t);if(!1===e.localStorage)return n.next(t);e.prefix=e.file||"gun/";var i,o,r,a,u=Gun.obj.ify(h.getItem("gap/"+e.prefix))||{},s=Gun.obj.empty;function f(t){var o;!t.err&&t.ok&&(o=t["@"],setTimeout(function(){Gun.obj.map(u,function(e,t){Gun.obj.map(e,function(t,n){o===t&&delete e[n]}),s(e)&&delete u[t]}),l()},e.wait||1))}s(u)||(r=Gun.obj.ify(h.getItem(e.prefix))||{},a={},Gun.obj.map(u,function(t,e){Gun.obj.map(t,function(t,n){a[e]=Gun.state.to(r[e],n,a[e])})}),setTimeout(function(){t.on("out",{put:a,"#":t.ask(f)})},1)),t.on("out",function(t){t.lS||(Gun.is(t.$)&&t.put&&!t["@"]&&(i=t["#"],Gun.graph.is(t.put,null,c),o=o||setTimeout(l,e.wait||1)),this.to.next(t))}),t.on("ack",f),n.next(t);var c=function(t,n,e,o){(u[o]||(u[o]={}))[n]=i},l=function(){clearTimeout(o),o=!1;try{h.setItem("gap/"+e.prefix,JSON.stringify(u))}catch(t){Gun.log(err=t||"localStorage failure")}}}),Gun.on("create",function(a){this.to.next(a);var u,s,f,c,l,p=a.opt;a.once||!1!==p.localStorage&&(p.prefix=p.file||"gun/",a.graph,u={},s=0,c=Gun.obj.ify(h.getItem(p.prefix))||{},a.on("localStorage",c),a.on("put",function(t){this.to.next(t);var n,e=t.put,o=e["#"],i=e["."],r=e[":"],a=e[">"];if(c[o]=Gun.state.ify(c[o],i,a,r,o),t["@"]||((u[t["#"]]=n=(t._||"").lot||{}).lS=(n.lS||0)+1),(s+=1)>=(p.batch||1e3))return l();f=f||setTimeout(l,p.wait||1)}),a.on("get",function(n){this.to.next(n);var e,o,i,r=n.get;function t(){var t;r&&(e=r["#"])&&(t=r["."],(o=c[e]||i)&&t&&(o=Gun.state.to(o,t)),a.on("in",{"@":n["#"],put:Gun.graph.node(o),lS:1}))}Gun.debug?setTimeout(t,1):t()}),l=function(t){var e;s=0,clearTimeout(f),f=!1;var n=u;u={},t&&(c=t);try{h.setItem(p.prefix,JSON.stringify(c))}catch(t){Gun.log(e=(t||"localStorage failure")+" Consider using GUN's IndexedDB plugin for RAD for more storage space, https://gun.eco/docs/RAD#install"),a.on("localStorage:error",{err:e,file:p.prefix,flush:c,retry:l})}(e||Gun.obj.empty(p.peers))&&Gun.obj.map(n,function(t,n){if(t){if(t.more)return void(u[n]=t);t.s!==t.lS&&(e="localStorage batch not same.")}a.on("in",{"@":n,err:e,ok:0})})})})}})(G,"./adapters/localStorage"),G(function(t){var e,x=G("../type");function o(t,n){var e;if(!(n instanceof Object))return n;var o=+new Date;return x.obj.map(Object.keys(n).sort(),i,{to:e={},on:n}),console.STAT&&console.STAT(o,new Date-o,"sort"),e}function i(t){this.to[t]=this.on[t]}e=JSON.stringify,x.obj.hash=function(t,n){if(n||void 0!==(t=e(t,o)))return x.text.hash(n||t||"")},x.obj.hash.sort=o;var w=x.obj.is;x.obj.map;try{t.exports=function(f){var l=function(){},p=f.opt||{};p.log=p.log||console.log,p.gap=p.gap||p.wait||0,p.pack=p.pack||.3*(p.memory?1e3*p.memory*1e3:1399e6),p.puff=p.puff||9;var h,d,g,v=setTimeout.puff||setTimeout,m=f.dup,c=m.check,k=m.track,b=l.hear=function(t,i){if(t){if(p.pack<=t.length)return l.say({dam:"!",err:"Message too big!"},i);var r,n,e,o=t[0];if(l===this&&(b.d+=t.length||0,++b.c),"["!==o){if("{"===o||(t["#"]||w(t))&&(r=t)){try{r=r||JSON.parse(t)}catch(t){return p.log("DAM JSON parse error",t)}if(!r)return;if(r.DBG&&(r.DBG=e={DBG:r.DBG}),e&&(e.hp=+new Date),(n=r["#"])||(n=r["#"]=x.text.random(9)),o=c(n))return;if((r._=function(){}).via=l.leap=i,o=r.dam)return(o=l.hear[o])&&o(r,i,f),void k(n);var a,u=+new Date;e&&(e.is=u),f.on("in",r),e&&(e.hd=+new Date),console.STAT&&9<(a=new Date-u)&&console.STAT(u,a,"msg"),k(n).via=i,l.leap=null}}else{try{r=JSON.parse(t)}catch(t){p.log("DAM JSON parse error",t)}if(t="",!r)return;console.STAT&&console.STAT(+new Date,r.length,"# on hear batch");var s=p.puff;(function t(){for(var n,e=+new Date,o=0;o<s&&(n=r[o++]);)b(n,i);r=r.slice(o),console.STAT&&console.STAT(e,new Date-e,"hear loop"),y(i),r.length&&v(t,0)})()}}};function y(t){var n=t.batch,e="string"==typeof n;if(e&&(n+="]"),t.batch=t.tail=null,n&&(e?!(n.length<3):n.length)){if(!e)try{n=1===n.length?n[0]:JSON.stringify(n)}catch(t){return p.log("DAM JSON stringify error",t)}n&&_(n,t)}}function _(n,e){try{var t=e.wire;e.say?e.say(n):t.send&&t.send(n),l.say.d+=n.length||0,++l.say.c}catch(t){(e.queue=e.queue||[]).push(n)}}b.c=b.d=0,d=0,g=l.say=function(r,t){var n,e,a;if((n=this)&&(n=n.to)&&n.next&&n.next(r),!r)return!1;var o=r.DBG;t||(s=+new Date,o&&(o.y=s));var u=r._||(r._=function(){});if((e=r["#"])||(e=r["#"]=x.text.random(9)),(a=u.raw)||(a=l.raw(r)),s&&console.STAT&&console.STAT(s,new Date-s,"say prep"),h||k(e),!t&&(n=r["@"])&&(t=(n=m.s[n])&&(n.via||(n=n.it)&&(n=n._)&&n.via)||l.leap),!t&&r["@"])return console.STAT&&console.STAT(+new Date,++d,"total no peer to ack to"),!1;if(!t&&l.way)return l.way(r);if(t&&t.id){if(!t.wire&&l.wire&&l.wire(t),e!==t.last){if(t.last=e,t===u.via)return!1;if((n=u.to)&&(n[t.url]||n[t.pid]||n[t.id]))return!1;if(t.batch){if(t.tail=(n=t.tail||0)+a.length,t.tail<=p.pack)return void(t.batch+=(n?",":"")+a);y(t)}t.batch="[";var i,s=+new Date;setTimeout(function(){console.STAT&&9<(i=new Date-s)&&console.STAT(s,i,"0ms TO",e,t.id),y(t)},p.gap),_(a,t)}}else{if(!x.obj.is(t||p.peers))return!1;p.puff;var f=p.peers,c=Object.keys(t||p.peers||{});(function t(){var n=+new Date;h=1;var e=u.raw;u.raw=a;for(var o,i=0;i<9&&(o=(c||"")[i++]);)(o=f[o])&&g(r,o);u.raw=e,h=0,c=c.slice(i),console.STAT&&console.STAT(n,new Date-n,"say loop"),c.length&&(v(t,0),k(r["@"]))})()}},l.say.c=l.say.d=0,function(){l.raw=function(t){if(!t)return"";var n,e=t._||{};if(n=e.raw)return n;if("string"==typeof t)return t;var o=i(t);return e&&(o||"").length<1e5&&(e.raw=o),o};var i=JSON.stringify}(),l.hi=function(n){var t=n.wire||{};n.id?p.peers[n.url||n.id]=n:(t=n.id=n.id||x.text.random(9),l.say({dam:"?",pid:f.opt.pid},p.peers[t]=n),delete m.s[n.last]),n.met=n.met||+new Date,t.hied||f.on(t.hied="hi",n),t=n.queue,n.queue=[],x.obj.map(t,function(t){_(t,n)}),x.obj.native&&x.obj.native()},l.bye=function(t){f.on("bye",t);var n=+new Date;n-=t.met||n,l.bye.time=((l.bye.time||n)+n)/2},l.hear["!"]=function(t,n){p.log("Error:",t.err)},l.hear["?"]=function(t,n){t.pid&&(n.pid||(n.pid=t.pid),t["@"])||(l.say({dam:"?",pid:p.pid,"@":t["#"]},n),delete m.s[n.last])},f.on("create",function(t){t.opt.pid=t.opt.pid||x.text.random(9),this.to.next(t),t.on("out",l.say)}),f.on("bye",function(t,n){t=p.peers[t.id||t]||t,this.to.next(t),t.bye?t.bye():(n=t.wire)&&n.close&&n.close(),x.obj.del(p.peers,t.id),t.wire=null});var i={};return f.on("bye",function(t,n){this.to.next(t),(n=t.url)&&(i[n]=!0,setTimeout(function(){delete i[n]},p.lack||9e3))}),f.on("hi",function(e,o){this.to.next(e),(o=e.url)&&i[o]&&(delete i[o],p.super||x.obj.map(f.next,function(t,n){(o={})[n]=f.graph[n],l.say({"##":x.obj.hash(o),get:{"#":n}},e)}))}),l}}catch(t){}})(G,"./adapters/mesh"),G(function(t){var f=G("../index");f.Mesh=G("./mesh"),f.on("opt",function(t){this.to.next(t);var n,e,o,i,r,a=t.opt;function u(n){try{if(!n||!n.url)return e&&e(n);var t=n.url.replace(/^http/,"ws"),e=n.wire=new a.WebSocket(t);return e.onclose=function(){a.mesh.bye(n),s(n)},e.onerror=function(t){s(n)},e.onopen=function(){a.mesh.hi(n)},e.onmessage=function(t){t&&a.mesh.hear(t.data||t,n)},e}catch(t){}}function s(n){clearTimeout(n.defer),r&&n.retry<=0||(n.retry=(n.retry||a.retry||60)-1,n.defer=setTimeout(function t(){return r&&r.hidden?setTimeout(t,i):void u(n)},i))}t.once||!1!==a.WebSocket&&("undefined"!=typeof window&&(n=window),"undefined"!=typeof global&&(n=global),n=n||{},(e=a.WebSocket||n.WebSocket||n.webkitWebSocket||n.mozWebSocket)&&(a.WebSocket=e,(o=a.mesh=a.mesh||f.Mesh(t)).wire||a.wire,o.wire=a.wire=u,setTimeout(function(){t.on("out",{dam:"hi"})},1),i=2e3,r="undefined"!=typeof document&&document))})})(G,"./adapters/websocket")}();