diff --git a/gun.js b/gun.js index da6299c8..5bb3097a 100644 --- a/gun.js +++ b/gun.js @@ -1117,10 +1117,10 @@ }); setTimeout(function(){ - var c = new Chain(), u; - c.get('app').map().map(x => x.bat? {baz: x.bat} : u).get('baz').val(function(data, key, env){ - console.log("envelope", env); - }); + //var c = new Chain(), u; + //c.get('app').map().map(x => x.bat? {baz: x.bat} : u).get('baz').val(function(data, key, env){ + // console.log("envelope", env); + //}); },1000); diff --git a/gun.min.js b/gun.min.js index a02f3237..2c2051b1 100644 --- a/gun.min.js +++ b/gun.min.js @@ -1,2 +1 @@ -(function(){function t(n){function o(t){return(""+t.split("/").slice(-1)).replace(".js","")}return n.slice?t[o(n)]:function(e,i){n(e={exports:{}}),t[o(i)]=e.exports}}var n;"undefined"!=typeof window&&(n=window),"undefined"!=typeof global&&(n=global),n=n||{};var o=n.console||{log:function(){}};if("undefined"!=typeof module)var e=module;t(function(t){var n={};n.fns=n.fn={is:function(t){return!!t&&"function"==typeof t}},n.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},n.num={is:function(t){return!e(t)&&(t-parseFloat(t)+1>=0||1/0===t||-1/0===t)}},n.text={is:function(t){return"string"==typeof t}},n.text.ify=function(t){return n.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?""+t:t},n.text.random=function(t,n){var o="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";t>0;)o+=n.charAt(Math.floor(Math.random()*n.length)),t--;return o},n.text.match=function(t,o){function e(t,n){for(var o,e=-1,i=0;o=n[i++];)if(!~(e=t.indexOf(o,e+1)))return!1;return!0}var i=!1;if(t=t||"",o=n.text.is(o)?{"=":o}:o||{},n.obj.has(o,"~")&&(t=t.toLowerCase(),o["="]=(o["="]||o["~"]).toLowerCase()),n.obj.has(o,"="))return t===o["="];if(n.obj.has(o,"*")){if(t.slice(0,o["*"].length)!==o["*"])return!1;i=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;i=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){return t.indexOf(n)>=0?(i=!0,void 0):!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){return 0>t.indexOf(n)?(i=!0,void 0):!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;i=!0}if(n.obj.has(o,"<")){if(!(o["<"]>t))return!1;i=!0}if(n.obj.has(o,"?")){if(!e(t,o["?"]))return!1;i=!0}return i},n.list={is:function(t){return t instanceof Array}},n.list.slit=Array.prototype.slice,n.list.sort=function(t){return function(n,o){return n&&o?(n=n[t],o=o[t],o>n?-1:n>o?1:0):0}},n.list.map=function(t,n,o){return a(t,n,o)},n.list.index=1,n.obj={is:function(t){return t?t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1]:!1}},n.obj.put=function(t,n,o){return(t||{})[n]=o,t},n.obj.has=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n.obj.del=function(t,n){return t?(t[n]=null,delete t[n],t):void 0},n.obj.as=function(t,n,o,e){return t[n]=t[n]||(e===o?{}:o)},n.obj.ify=function(t){if(r(t))return t;try{t=JSON.parse(t)}catch(n){t={}}return t},function(){function t(t,n){u(this,n)&&o!==this[n]||(this[n]=t)}var o;n.obj.to=function(n,o){return o=o||{},a(n,t,o),o}}(),n.obj.copy=function(t){return t?JSON.parse(JSON.stringify(t)):t},function(){function t(t,n){var o=this.n;if(!o||!(n===o||r(o)&&u(o,n)))return n?!0:void 0}n.obj.empty=function(n,o){return n?a(n,t,{n:o})?!1:!0:!0}}(),function(){function t(n,o){return 2===arguments.length?(t.r=t.r||{},t.r[n]=o,void 0):(t.r=t.r||[],t.r.push(n),void 0)}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,d,h,g=0,v=o(s);if(t.r=null,i&&r(a)&&(d=Object.keys(a),h=!0),e(a)||d)for(l=(d||a).length;l>g;g++){var b=g+n.list.index;if(v){if(p=h?s.call(f||this,a[d[g]],d[g],t):s.call(f||this,a[g],b,t),p!==c)return p}else if(s===a[h?d[g]:g])return d?d[g]:b}else for(g in a)if(v){if(u(a,g)&&(p=f?s.call(f,a[g],g,t):s(a[g],g,t),p!==c))return p}else if(s===a[g])return g;return v?t.r:n.list.index?0:-1}}(),n.time={},n.time.is=function(t){return t?t instanceof Date:+(new Date).getTime()};var o=n.fn.is,e=n.list.is,i=n.obj,r=i.is,u=i.has,a=i.map;t.exports=n})(t,"./type"),t(function(t){t.exports=function n(t,o,e){if(!t)return{to:n};var t=(this.tag||(this.tag={}))[t]||(this.tag[t]={tag:t,to:n._={next:function(){}}});if(o instanceof Function){var i={off:n.off||(n.off=function(){return this.next===n._.next?!0:(this===this.the.last&&(this.the.last=this.back),this.to.back=this.back,this.next=n._.next,this.back.to=this.to,void 0)}),to:n._,next:o,the:t,on:this,as:e};return(i.back=t.last||t).to=i,t.last=i}return(t=t.to).next(o),t}})(t,"./onto"),t(function(n){function o(t,n){n=n||{},n.id=n.id||"#",n.rid=n.rid||"@",n.uuid=n.uuid||function(){return+new Date+Math.random()};var o=e;return o.stun=function(t){var n=function(t){return n.off&&n===this.stun?(this.stun=null,!1):o.stun.skip?!1:(t&&(t.cb=t.fn,t.off(),e.queue.push(t)),!0)},e=n.res=function(t,r){if(!n.off){if(t instanceof Function)return o.stun.skip=!0,t.call(r),o.stun.skip=!1,void 0;n.off=!0;var u,a=0,s=e.queue,f=s.length;for(e.queue=[],n===i.stun&&(i.stun=null),a;f>a;a++)u=s[a],u.fn=u.cb,u.cb=null,o.stun.skip=!0,u.ctx.on(u.tag,u.fn,u),o.stun.skip=!1}},i=t._;return e.back=i.stun||(i.back||{_:{}})._.stun,e.back&&(e.back.next=n),e.queue=[],i.stun=n,e},o}var e=t("./onto");n.exports=o})(t,"./onify"),t(function(n){function o(t,n,e){o.time=e||Gun.time.is,o.waiting.push({when:t,event:n||function(){}}),t>o.soonest||o.set(t)}var e=t("./type");o.waiting=[],o.soonest=1/0,o.sort=e.list.sort("when"),o.set=function(t){if(!((o.soonest=t)>=1/0)){var n=o.time();t=n>=t?0:t-n,clearTimeout(o.id),o.id=setTimeout(o.check,t)}},o.each=function(t,n,o){var e=this;t&&(t.when<=e.now?t.event instanceof Function&&setTimeout(function(){t.event()},0):(e.soonest=e.soonestt)return{defer:!0};if(e>n)return{historical:!0};if(n>e)return{converge:!0,incoming:!0};if(n===e){if(o(i)===o(r))return{state:!0};if(o(i)u?(a=0,u=t):u=t+(a+=1)/s}var e=t("./type"),i=t("./node"),r=e.time.is,u=-1/0,a=0,s=1e3;o._=">",o.ify=function(t,n,e,r,u){if(!t||!t[m]){if(!u)return;t=i.soul.ify(t,u)}var a=l(t[m],o._);return f!==n&&n!==m&&(v(e)&&(a[n]=e),f!==r&&(t[n]=r)),t},o.is=function(t,n,e){var i=n&&t&&t[m]&&t[m][o._]||e;if(i)return v(i[n])?i[n]:-1/0},function(){function t(t,n){m!==n&&o.ify(this.o,n,this.s)}o.map=function(n,e,i){var r,u=d(u=n||e)?u:null;return n=y(n=n||e)?n:null,u&&!n?(e=v(e)?e:o(),u[m]=u[m]||{},h(u,t,{o:u,s:e}),u):(i=i||d(e)?e:r,e=v(e)?e:o(),function(o,u,a,s){return n?(n.call(i||this||{},o,u,a,s),p(a,u)&&r===a[u]||t.call({o:a,s:e},o,u),void 0):(t.call({o:a,s:e},o,u),o)})}}();var f,c=e.obj,l=c.as,p=c.has,d=c.is,h=c.map,g=e.num,v=g.is,b=e.fn,y=b.is,m=i._;n.exports=o})(t,"./state"),t(function(n){var o=t("./type"),e=t("./val"),i=t("./node"),r={};(function(){function t(t,o){return t&&o===i.soul(t)&&i.is(t,this.fn)?(this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n)),void 0):!0}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return n&&s(n)&&!l(n)?!d(n,t,{cb:o,fn:e,as:i}):!1}})(),function(){function t(t,r){var u;return(u=l(t,r))?u:(r.env=t,r.soul=o,i.ify(r.obj,n,r)&&(t.graph[e.rel.is(r.rel)]=r.node),r)}function n(n,o,r){var s,l,p=this,d=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,d)){if(o||(p.node=p.node||r||{},c(n,i._)&&(p.node._=h(n._)),p.node=i.soul.ify(p.node,e.rel.is(p.rel)),p.rel=p.rel||e.rel.ify(i.soul(p.node))),(l=d.map)&&(l.call(d.as||{},n,o,r,p),c(r,o))){if(n=r[o],u===n)return f(r,o),void 0;if(!(s=a(n,o,r,p,d)))return}if(!o)return p.node;if(!0===s)return n;if(l=t(d,{obj:n,path:p.path.concat(o)}),l.node)return l.rel}}function o(t){var n=this,o=e.rel.is(n.rel),r=n.env.graph;n.rel=n.rel||e.rel.ify(t),n.rel[e.rel._]=t,n.node&&n.node[i._]&&(n.node[i._][e.rel._]=t),c(r,o)&&(r[t]=r[o],f(r,o))}function a(t,n,o,i,r){var u;return e.is(t)?!0:s(t)?1:(u=r.invalid)?(t=u.call(r.as||{},t,n,o),a(t,n,o,i,r)):(r.err="Invalid value at '"+i.path.concat(n).join(".")+"'!",void 0)}function l(t,n){for(var o,e=t.seen,i=e.length;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}r.ify=function(n,o,i){var r={path:[],obj:n};return o?"string"==typeof o?o={soul:o}:o instanceof Function&&(o.map=o):o={},o.soul&&(r.rel=e.rel.ify(o.soul)),o.graph=o.graph||{},o.seen=o.seen||[],o.as=o.as||i,t(o,r),o.root=r.node,o.graph}}(),r.node=function(t){var n=i.soul(t);if(n)return p({},n,t)},function(){function t(t,n){var o,u;if(i._===n){if(l(t,e.rel._))return;return this.obj[n]=h(t),void 0}return(o=e.rel.is(t))?(u=this.opt.seen[o])?(this.obj[n]=u,void 0):(this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt),void 0):(this.obj[n]=t,void 0)}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},d(n[o],t,{obj:i,graph:n,opt:e}),i}}}(),o.fn.is;var u,a=o.obj,s=a.is,f=a.del,c=a.has,l=a.empty,p=a.put,d=a.map,h=a.copy;n.exports=r})(t,"./graph"),t(function(n){function o(){this.cache={}}var e=t("./type");o.prototype.track=function(t){return this.cache[t]=e.time.is(),this.to||this.gc(),t},o.prototype.check=function(t){return e.obj.has(this.cache,t)?this.track(t):!1},o.prototype.gc=function(){var t=this,n=e.time.is(),o=n,i=3e5;e.obj.map(t.cache,function(r,u){o=Math.min(n,r),i>n-r||e.obj.del(t.cache,u)});var r=e.obj.empty(t.cache);if(r)return t.to=null,void 0;var u=n-o,a=i-u;t.to=setTimeout(function(){t.gc()},a)},n.exports=o})(t,"./dup"),t(function(n){function i(t){return t instanceof i?(this._={gun:this}).gun:this instanceof i?i.create(this._={gun:this,opt:t}):new i(t)}i.is=function(t){return t instanceof i},i.version=.6,i.chain=i.prototype,i.chain.toJSON=function(){};var r=t("./type");r.obj.to(r,i),i.HAM=t("./HAM"),i.val=t("./val"),i.node=t("./node"),i.state=t("./state"),i.graph=t("./graph"),i.dup=t("./dup"),i.on=t("./onify")(),i._={node:i.node._,soul:i.val.rel._,state:i.state._,field:".",value:"="},function(){function t(t){var o=this.as,e=o.gun;t.get&&n(t,o)||(o.on("in",l(t,{gun:o.gun})),t["#"]&&o.dup.track(t["#"]),t.gun||(t=l(t,{gun:e})),i.on("out",t))}function n(t,n){var o,e=t.get[d],r=n.graph[e],u=t.get[h],a=n.next||(n.next={}),s=(a[e]||(a[e]=n.gun.get(e)))._;if(r){if(u){if(!c(r,u))return;o=i.obj.put(i.node.soul.ify({},e),u,r[u]),r=i.state.ify(o,u,i.state.is(r,u))}return s.on("in",{put:r,get:s.soul,gun:s.gun}),s.ack>0?!0:void 0}}function o(t){var n=this,o=n.as;if(t.gun||(t.gun=o.gun),!t["#"]&&t["@"]){if(t["#"]=i.text.random(),i.on.ack(t["@"],t))return;return o.dup.track(t["#"]),o.on("out",t),void 0}t["#"]&&o.dup.check(t["#"])||(o.dup.track(t["#"]),i.on.ack(t["@"],t)||(t.put&&(i.HAM.synth(t,n,o.gun),i.on("put",t)),t.get&&i.on("get",t),i.on("out",t)))}i.create=function(n){n.on=n.on||i.on,n.root=n.root||n.gun,n.graph=n.graph||{},n.dup=n.dup||new i.dup;var e=n.gun.opt(n.opt);return n.once||(n.on("in",o,n),n.on("out",t,n)),n.once=1,e}}(),function(){var t=i.on.ask=function(n,o){var e=i.text.random();return n&&t.on(e,n,o),e};t.on=i.on,i.on.ack=function(n,o){if(n&&o&&t.on){var e=n["#"]||n;if(t.tag&&t.tag[e])return t.on(e,o),!0}}}(),function(){i.chain.opt=function(t){t=t||{};var n=this,o=n._,e=t.peers||t;return f(t)||(t={}),f(o.opt)||(o.opt=t),u(e)&&(e=[e]),a(e)&&(e=p(e,function(t,n,o){o(t,{url:t})}),f(o.opt.peers)||(o.opt.peers={}),o.opt.peers=l(e,o.opt.peers)),o.opt.peers=o.opt.peers||{},l(t,o.opt),i.on("opt",o),n}}();var u=i.text.is,a=i.list.is,s=i.obj,f=s.is,c=s.has,l=s.to,p=s.map,d=i._.soul,h=i._.field;o.debug=function(t,n){return o.debug.i&&t===o.debug.i&&o.debug.i++&&(o.log.apply(o,arguments)||n)},i.log=function(){return!i.log.off&&o.log.apply(o,arguments),[].slice.call(arguments).join(" ")},i.log.once=function(t,n,o){return(o=i.log.once)[t]=o[t]||0,o[t]++||i.log(n)},i.log.once("welcome","Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!"),"undefined"!=typeof window&&(window.Gun=i),e!==void 0&&(e.exports=i),n.exports=i})(t,"./root"),t(function(){var n=t("./root");n.chain.back=function(t,n){var i;if(-1===t||1/0===t)return this._.root;if(1===t)return this._.back||this;var r=this,u=r._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){var a=0,s=t.length,i=u;for(a;s>a;a++)i=(i||e)[t[a]];if(o!==i)return n?r:i;if(i=u.back)return i.back(t,n)}else if(t instanceof Function){for(var f,i={back:r};(i=i.back)&&(i=i._)&&!(f=t(i,n)););return f}};var o,e={}})(t,"./back"),t(function(){function o(t){var n,o=this.as,e=o.gun;if(e.back(-1),t.gun||(t.gun=e),(n=t.get)&&!n[m])if(h(n,_)){n=n[_];var i=n?e.get(n)._:o;if(h(i,"put"))return i.on("in",i),void 0;if(h(o,"put")){var r,u=o.put;if((r=c.node.soul(u))&&(u=c.val.rel.ify(r)),r=c.val.rel.is(u)){if(!t.gun._)return;return t.gun._.on("out",{get:{"#":r,".":n},"#":c.on.ask(c.HAM.synth,t.gun),gun:t.gun}),void 0}if(l===u||c.val.is(u)){if(!t.gun._)return;return t.gun._.on("in",{get:n,gun:t.gun}),void 0}}else o.map&&y(o.map,function(t){t.at.on("in",t.at)});if(o.soul){if(!t.gun._)return;return t.gun._.on("out",{get:{"#":o.soul,".":n},"#":c.on.ask(c.HAM.synth,t.gun),gun:t.gun}),void 0}if(o.get){if(!o.back._)return;return o.back._.on("out",{get:g({},_,o.get),gun:e}),void 0}t=b(t,{get:{}})}else{if(h(o,"put")?o.on("in",o):o.map&&y(o.map,function(t){t.at.on("in",t.at)}),o.ack&&!h(o,"put"))return;if(o.ack=-1,o.soul)return o.on("out",{get:{"#":o.soul},"#":c.on.ask(c.HAM.synth,o.gun)}),void 0;if(o.get){if(!o.back._)return;return o.back._.on("out",{get:g({},_,o.get),gun:o.gun}),void 0}}o.back._.on("out",t)}function e(t){t=t._||t;var n,o=this,e=this.as,u=t.gun,f=u._,d=t.put;if(e.back._||p,0>e.ack&&t.via&&!c.val.rel.is(d)&&(e.ack=1),e.get&&t.get!==e.get&&(t=b(t,{get:e.get})),e.field&&f!==e&&(t=b(t,{gun:e.gun}),f.ack&&(e.ack=e.ack||f.ack)),l===d){if(o.to.next(t),e.soul)return;return r(e,t,o),e.field&&s(e,t),v(f.echo,e.id),v(e.map,f.id),void 0}return e.soul?(o.to.next(t),r(e,t,o),y(d,a,{at:t,cat:e}),void 0):(n=c.val.rel.is(d))?(i(e,t,f,n),o.to.next(t),r(e,t,o),void 0):c.val.is(d)?(e.field||e.soul?s(e,t):(f.field||f.soul)&&((f.echo||(f.echo={}))[e.id]=e,(e.map||(e.map={}))[f.id]=e.map[f.id]||{at:f}),o.to.next(t),r(e,t,o),void 0):(e.field&&f!==e&&h(f,"put")&&(e.put=f.put),(n=c.node.soul(d))&&f.field&&(f.put=e.root.get(n)._.put),o.to.next(t),r(e,t,o),i(e,t,f,n),y(d,a,{at:t,cat:e}),void 0)}function i(t,n,o,e){if(e&&k!==t.get){var r=t.root.get(e)._;t.field?o=r:o.field&&i(o,n,o,e),o!==t&&((o.echo||(o.echo={}))[t.id]=t,t.field&&!(t.map||p)[o.id]&&s(t,n),r=(t.map||(t.map={}))[o.id]=t.map[o.id]||{at:o},e!==r.rel&&f(t,r.rel=e))}}function r(t,n,o){t.echo&&(t.field&&(n=b(n,{event:o})),y(t.echo,u,n))}function u(t){t.on("in",this)}function a(t,n){var o,e,i,r=this.cat,u=r.next||p,a=this.at;(k!==n||u[n])&&(o=u[n])&&(i=o._,i.field?(t&&t[m]&&c.val.rel.is(t)===c.node.soul(i.put)||(i.put=t),e=o):e=a.gun.get(n),i.on("in",{put:t,get:n,gun:e,via:a}))}function s(t){if(t.field||t.soul){var n=t.map;t.map=null,null!==n&&(l!==n||t.put===l)&&(y(n,function(n){(n=n.at)&&v(n.echo,t.id)}),y(t.next,function(t,n){var o=t._;o.put=l,o.ack&&(o.ack=-1),o.on("in",{get:n,gun:t,put:l})}))}}function f(t,n){var o=t.root.get(n)._;return t.ack?(o.ack=o.ack||-1,o.on("out",{get:{"#":n},"#":c.on.ask(c.HAM.synth,o.gun),gun:o.gun}),void 0):(y(t.next,function(t,e){t._.on("out",{get:{"#":n,".":e},"#":c.on.ask(c.HAM.synth,o.gun),gun:t})}),void 0)}var c=t("./root");c.chain.chain=function(){var t=this._,i=new this.constructor(this),r=i._;return r.root=n=t.root,r.id=++n._.once,r.back=this,r.on=c.on,c.on("chain",r),r.on("in",e,r),r.on("out",o,r),i},c.chain.chain.input=e;var l,p={},d=c.obj,h=d.has,g=d.put,v=d.del,b=d.to,y=d.map,m=c._.soul,_=c._.field,k=c.node._})(t,"./chain"),t(function(){function n(t,n){var o=n._,e=o.next,i=n.chain(),r=i._;return e||(e=o.next={}),e[r.get=t]=i,o.root===n?r.soul=t:(o.soul||o.field)&&(r.field=t),i}function o(t){var n,o=this,e=o.as,i=t.gun,u=i._,f=t.put;s===f&&(f=u.put),(n=f)&&n[a._]&&(n=a.is(n))&&(n=u.root.get(n)._,s!==n.put&&(t=r(t,{put:n.put}))),e.use(t,t.event||o),o.to.next(t)}var e=t("./root");e.chain.get=function(t,i,r){if("string"!=typeof t){if(t instanceof Function){var a=this,s=a._;return r=i||{},r.use=t,r.out=r.out||{},r.out.get=r.out.get||{},s.root._.now=!0,s.on("in",o,r),s.on("out",r.out),s.root._.now=!1,a}return u(t)?this.get(""+t,i,r):((r=this.chain())._.err={err:e.log("Invalid get request!",t)},i&&i.call(r,r._.err),r)}var a,c,l=l||this,p=l._,d=p.next||f;return(a=d[t])||(a=n(t,l)),(c=p.stun)&&(a._.stun=a._.stun||c),i&&i instanceof Function&&a.get(i,r),a};var i=e.obj,r=(i.has,e.obj.to),u=e.num.is,a=e.val.rel;e.node._;var s,f={}})(t,"./get"),t(function(){function n(t){t.batch=e;var n=t.opt||{},o=t.env=s.state.map(r,n.state);return o.soul=t.soul,t.graph=s.graph.ify(t.data,o,t),o.err?((t.ack||g).call(n.as||t.gun,t.out={err:s.log(o.err)}),t.res&&t.res(),void 0):(t.batch(),void 0)}function e(){var t=this;t.graph&&!d(t.stun,i)&&((t.res||v)(function(){t.ref._.on("out",{gun:t.ref,put:t.out=t.env.graph,opt:t.opt,"#":s.on.ask(function(n){this.off(),t.ack&&t.ack(n,this)},t.opt)})},t),t.res&&t.res())}function i(t){return t?!0:void 0}function r(t,n,o,e){var i=this;!n&&e.path.length&&(i.res||v)(function(){var t=e.path,n=i.ref;i.opt;var o=0,r=t.length;for(o;r>o;o++)n=n.get(t[o]);return i.not||s.node.soul(e.obj)?(e.soul(s.node.soul(e.obj)||((i.opt||{}).uuid||i.gun.back("opt.uuid")||s.text.random)()),void 0):((i.stun=i.stun||{})[t]=!0,n.get("_").get(u,{as:{at:e,as:i}}),void 0)},{as:i,at:e})}function u(t,n){var o=this.as,e=o.at;o=o.as,t.gun&&t.gun._.back&&(n.off(),t=t.gun._.back._,e.soul(s.node.soul(e.obj)||s.node.soul(t.put)||s.val.rel.is(t.put)||((o.opt||{}).uuid||o.gun.back("opt.uuid")||s.text.random)()),o.stun[e.path]=!1,o.batch())}function a(t,n){var e=this.as;if(t.gun&&t.gun._){if(t.err)return o.log("Please report this as an issue! Put.any.err"),void 0;var i,r=t.gun._.back._,u=r.put,a=e.opt||{};if(n.off(),e.ref!==e.gun){if(i=e.gun._.get,!i)return o.log("Please report this as an issue! Put.no.get"),void 0;e.data=p({},i,e.data),i=null}if(f===u){if(!r.get)return;r.soul||(i=r.gun.back(function(t){return t.soul?t.soul:(e.data=p({},t.get,e.data),void 0)})),i=i||r.get,r=r.root.get(i)._,e.not=e.soul=i,u=e.data}e.not||(e.soul=s.node.soul(u))||(e.soul=e.path&&l(e.data)?(a.uuid||e.gun.back("opt.uuid")||s.text.random)():t.soul),e.ref.put(e.data,e.soul,e)}}var s=t("./root");s.chain.put=function(t,o,e){var i,r=this,u=r._.root;return e=e||{},e.data=t,e.gun=e.gun||r,"string"==typeof o?e.soul=o:e.ack=o,e.soul||u===r?l(e.data)?(e.gun=r=u.get(e.soul=e.soul||(e.not=s.node.soul(e.data)||(u._.opt.uuid||s.text.random)())),e.ref=e.gun,n(e),r):((opt.any||g).call(opt.as||r,e.out={err:s.log("No field to put",typeof e.data,'"'+e.data+'" on!')}),e.res&&e.res(),r):s.is(t)?(t.get(function(t,n){n.off();var i=s.node.soul(t.put);return i?(r.put(s.val.rel.ify(i),o,e),void 0):(s.log("Can only save a node, not a property."),void 0)}),r):(e.ref=e.ref||u===(i=r._.back)?r:i,e.ref.get("_").get(a,{as:e}),e.out||(e.res=e.res||s.on.stun(e.ref),e.gun._.stun=e.ref._.stun),r)};var f,c=s.obj,l=c.is,p=c.put,d=c.map,h={},g=function(){},v=function(t,n){t.call(n||h)}})(t,"./put"),t(function(n){var e=t("./root");n.exports=e,function(){function t(t,n){if(e._.node!==n){var i=this.node,r=this.vertex,u=this.union,a=this.machine,s=d(i,n),f=d(r,n);if(b===s||b===f)return!0;var c=t,l=r[n];if(!v(c)&&b!==c)return!0;if(!v(l)&&b!==l)return!0;var p=e.HAM(a,s,f,c,l);if(p.err)return o.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.",p.err),void 0;if(!(p.state||p.historical||p.current))return p.incoming?(u[n]=t,h(u,n,s),void 0):p.defer?(u[n]=t,h(u,n,s),void 0):void 0}}function n(t,n){var o=this;if(e._.node!==n&&v(t)){var i=o.node,r=o.vertex,u=d(i,n,!0),a=d(r,n,!0),s=o.delta,f=e.HAM(o.machine,u,a,t,r[n]);f.incoming&&(s[n]=t,h(s,n,u))}}e.HAM.union=function(n,o,i){return o&&o._&&(n=n||e.node.soul.ify({_:{">":{}}},e.node.soul(o)),n&&n._&&(i=u(i)?{machine:i}:{machine:e.state()},i.union=n||e.obj.copy(n),i.vertex=n,i.node=o,!c(o,t,i)))?i.union:void 0},e.HAM.delta=function(t,o,i){return i=u(i)?{machine:i}:{machine:e.state()},t?(i.soul=e.node.soul(i.vertex=t),i.soul?(i.delta=e.node.soul.ify({},i.soul),c(i.node=o,n,i),i.delta):void 0):e.obj.copy(o)},e.HAM.synth=function(t,n,o){var i=this.as||o,r=i._,u=r.root._,a={};if(!t.put){if(r.put!==b)return;return r.on("in",{get:r.get,put:r.put=b,gun:i,via:t}),void 0}c(t.put,function(t,n){var o=this.graph;a[n]=e.HAM.delta(o[n],t,{graph:o}),o[n]=e.HAM.union(o[n],t)||o[n]},u),c(a,function(o,i){var u=this,a=u.next||(u.next={}),c=a[i]||(a[i]=u.gun.get(i)),l=c._;return l.put=u.graph[i],r.field&&!s(o,r.field)?((t=f(t,{})).put=b,e.HAM.synth(t,n,r.gun),void 0):(l.on("in",{put:o,get:i,gun:c,via:t}),void 0)},u)}}();var i=e,r=i.num,u=r.is,a=i.obj,s=a.has,f=(a.put,a.to),c=a.map,l=e.node;l.soul,l.is,l.ify;var p=e.state,d=p.is,h=p.ify,g=e.val,v=g.is;g.rel.is;var b})(t,"./index"),t(function(n){var o=t("./root");t("./index"),t("./opt"),t("./chain"),t("./back"),t("./put"),t("./get"),n.exports=o})(t,"./core"),t(function(){var n=t("./core"),e=n.obj,i=(e.is,e.put);e.map,e.empty;var r=n.num;r.is,n.val.rel._,function(){function t(t,o){var u=this;return o.off(),u.soul=n.node.soul(t.put),u.soul&&u.key!==u.soul?(u.data=i({},r._,n.node.ify(i({},u.soul,n.val.rel.ify(u.soul)),"#"+u.key+"#")),(u.res||e)(function(){u.ref.put(u.data,u.any,{soul:u.key,key:u.key})},u),u.res&&u.res(),void 0):u.data={}}function e(t,n){t.call(n||{})}function r(t){if(t&&"#"===t[0]&&"#"===t[t.length-1]){var n=t.slice(1,-1);if(n)return n}}function u(t){var o=this;if(!t.put)return t.get&&a.call(t.gun?t.gun._:o,t),void 0;if(!t.opt||!t.opt.key){var e=t.put,i=o.gun.back(-1)._.graph;n.graph.is(e,function(t,o){n.node.is(i["#"+o+"#"],function r(o,u){return u===n.val.rel.is(o)?(o=i["#"+u+"#"])?(n.node.is(o,r),void 0):(n.node.soul.ify(o=e[u]=n.obj.copy(t),u),void 0):void 0})&&n.obj.del(e,o)})}}function a(t){function o(u,a){var s=(u.put,t.get);if(!i.pseudo||u.via)return n.on.ack(e,u);if(u.put){if(!s["."])return a.off(),n.on.ack(e,u);if(c(u.put[s["#"]],s["."]))return a.off(),n.on.ack(e,u)}n.obj.map(i.seen,function(i,a){return r[a]?n.on.ack(e,u):(r[a]=!0,i.on("out",{gun:i,get:a={"#":a,".":t.get["."]},"#":n.on.ask(o)}),void 0)})}var e,i=this;if(n.obj.is(e=t.get)&&n.obj.has(e,"#")){if((e=t.get)&&null===e["."])return e["."]="##",void 0;(e=t.get)&&n.obj.has(e,".")&&(e["#"]&&(i=i.root.gun.get(e["#"])._),e=t["#"],t["#"]=n.on.ask(o));var r={}}}function s(t,o){function e(t){n.node.is(t,i)}function i(t,o){o===n.val.rel.is(t)&&(p[o]||(p[o]=l.get(o).on(u,!0)))}function u(t){t&&(a.pseudo=n.HAM.union(a.pseudo,t)||a.pseudo,a.change=a.changed=t,a.put=a.pseudo,c({gun:a.gun,put:a.pseudo,get:f}))}var a=this;if(a.pseudo){if(a.pseudo===t.put)return;return o.stun(),a.change=a.changed||a.pseudo,a.on("in",n.obj.to(t,{put:a.put=a.pseudo})),void 0}if(t.put){var s=n.val.rel.is(t.put[r._]);if(s){var f=n.node.soul(t.put),c=o.stun(c),l=a.gun.back(-1),p=a.seen={};a.pseudo=a.put=n.state.ify(n.node.ify({},f)),l.get(s).on(e,{change:!0})}}}n.chain.key=function(e,i,r){if(!e)return i&&i.call(this,{err:n.log("No key!")}),this;var u=this;return"string"==typeof r?(o.log("Please report this as an issue! key.opt.string"),u):u===u._.root?(i&&i({err:n.log("Can't do that on root instance.")}),u):(r=r||{},r.key=e,r.any=i||function(){},r.ref=u.back(-1).get(r.key),r.gun=r.gun||u,u.on(t,{as:r}),r.data||(r.res=n.on.stun(r.ref)),u)},r._="##",n.on("next",function(t){var n=t.gun;n.back(-1)===t.back&&(n.on("in",s,n._),n.on("out",u,n._))});var f=n.obj,c=f.has}()})(t,"./key"),t(function(){var n=t("./core");n.chain.path=function(t,o,e){var i,r=this,u=r;if(e=e||{},e.path=!0,u===u._.root)return o&&o({err:n.log("Can't do that on root instance.")}),u;if("string"==typeof t){if(i=t.split(e.split||"."),1===i.length)return u=r.get(t,o,e),u._.opt=e,u;t=i}if(t instanceof Array){if(t.length>1){u=r;var a=0,s=t.length;for(a;s>a;a++)u=u.get(t[a],a+1===s?o:null,e)}else u=r.get(t[0],o,e);return u._.opt=e,u}return t||0==t?(u=r.get(""+t,o,e),u._.opt=e,u):r}})(t,"./path"),t(function(){function n(t,n){var o,i=this,r=t.gun,s=r._,f=s.put||t.put,o=i.last,c=s.id+t.get;if(u!==f){if(f&&f[a._]&&(o=a.is(f))){if(o=s.root.get(o)._,u===o.put)return;f=o.put}i.change&&(f=t.put),(o.put!==f||o.get!==c||e.node.soul(f))&&(o.put=f,o.get=c,s.last=f,i.as?i.ok.call(i.as,t,n):i.ok.call(r,f,t.get,t,n))}}function o(t,n,e){var i,r=this.as,s=r.cat,f=t.gun,c=f._,l=c.put||t.put;if(u!==l){if(l&&l[a._]&&(i=a.is(l))){if(i=s.root.get(i)._,u===i.put)return;l=i.put}if(n.wait&&clearTimeout(n.wait),!(e||c.ack>0&&(!0!==r.async||0===r.wait)))return n.wait=setTimeout(function(){o.call({as:r},t,n,n.wait||1)},r.wait||99),void 0;if(s.field||s.soul){if(n.off())return}else{if((r.seen=r.seen||{})[c.id])return;r.seen[c.id]=!0}r.ok.call(t.gun||r.gun,l,t.get)}}var e=t("./core");e.chain.on=function(t,o,e,i){var r,u,a=this,s=a._;if("string"==typeof t)return o?(r=s.on(t,o,e||s,i),e&&e.gun&&(e.subs||(e.subs=[])).push(r),u=function(){r&&r.off&&r.off(),u.off()},u.off=a.off.bind(a)||noop,a.off=u,a):s.on(t);var f=o;return f=!0===f?{change:!0}:f||{},f.ok=t,f.last={},a.get(n,f),a},e.chain.val=function(t,n){var i=this,r=i._,a=r.put;if(r.ack>0&&u!==a&&t)return t.call(i,a,r.get),i;if(!t){e.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 s=i.chain();return s._.val=i.val(function(){s._.on("in",i._)}),s}return(n=n||{}).ok=t,n.cat=r,i.get(o,{as:n}),n.async=r.stun?1:!0,i},e.chain.off=function(){var t,n=this,o=n._,e=o.back||{},i=e._;return i?((t=i.next)&&(t[o.get]?r(t,o.get):obj_map(t,function(o,e){n===o&&r(t,e)})),(t=n.back(-1))===e&&r(t.graph,o.get),o.ons&&(t=o.ons["@$"])&&obj_map(t.s,function(t){t.off()}),n):void 0};var i=e.obj,r=(i.has,i.del);i.to;var u,a=e.val.rel})(t,"./on"),t(function(){function n(t,n){n.off(),t.err||e!==t.put||this.not&&this.not.call(t.gun,t.get,function(){o.log("Please report this bug on https://gitter.im/amark/gun and in the issues."),need.to.implement})}var e,i=t("./core");i.chain.not=function(t){return this.get(n,{not:t})}})(t,"./not"),t(function(){function n(t){t.put&&!e.val.is(t.put)&&(this.as.val&&this.off(),r(t.put,o,{cat:this.as,gun:t.gun}),this.to.next(t))}function o(t,n){if(a!==n){var o=this.cat,e=this.gun.get(n),i=e._;(i.echo||(i.echo={}))[o.id]=o}}var e=t("./core");e.chain.map=function(t){var o,r=this,a=r._;return t?(e.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."),o=r.chain(),r.map().on(function(n,r,a,s){var f=(t||u).call(this,n,r,a,s);if(i!==f)return e.is(f)?(o._.on("in",f._),void 0):(o._.on("in",{get:r,put:f,gun:o}),void 0)}),o):(o=a.fields)?o:(o=a.fields=r.chain(),o._.val=r.back("val"),r.on("in",n,o._),o)};var i,r=e.obj.map,u=function(){},a=e.node._})(t,"./map"),t(function(){var n=t("./core");n.chain.init=function(){return(this._.opt=this._.opt||{}).init=!0,this.back(-1).put(n.node.ify({},this._.get),null,this._.get)}})(t,"./init"),t(function(){var n=t("./core");n.chain.set=function(t,o,e){var i,r=this;return o=o||function(){},(i=n.node.soul(t))?r.set(r.back(-1).get(i),o,e):n.obj.is(t)&&!n.is(t)?r.set(r._.root.put(t),o,e):t.get("_").get(function(t,i){!t.gun||!t.gun._.back,i.off(),t=t.gun._.back._;var u={},a=t.put,s=n.node.soul(a);return s?(r.put(n.obj.put(u,s,n.val.rel.ify(s)),o,e),void 0):o.call(r,{err:n.log('Only a node can be linked! Not "'+a+'"!')})},{wait:0})}})(t,"./set"),t(function(){if("undefined"!=typeof Gun){var t,n=function(){};"undefined"!=typeof window&&(t=window);var o=t.localStorage||{setItem:n,removeItem:n,getItem:n};Gun.on("put",function(t){var n,e;t.gun._.root,this.to.next(t),(e={}).prefix=(t.opt||e).prefix||t.gun.back("opt.prefix")||"gun/",Gun.graph.is(t.put,function(t,i,r){var u=Gun.obj.ify(o.getItem(e.prefix+i+"_")||{});r(function(r,a){u[a]=1;var s=Gun.state.is(t,a);try{o.setItem(e.prefix+i+a,JSON.stringify([r,s]))}catch(f){n=f||"localStorage failure"}});try{o.setItem(e.prefix+i+"_",JSON.stringify(u))}catch(a){n=a||"localStorage failure"}}),Gun.obj.empty(t.gun.back("opt.peers"))&&Gun.on.ack(t,{err:n,ok:0})}),Gun.on("get",function(t){this.to.next(t);var n,e,i,r,u=t.gun,a=t.get;if((i=t.opt||{}).prefix=i.prefix||t.gun.back("opt.prefix")||"gun/",a&&(n=a[Gun._.soul])){var s=a["."];s?(e=Gun.obj.ify(o.getItem(i.prefix+n+s)||null)||r)&&(e=Gun.state.ify(r,s,e[1],e[0],n)):Gun.obj.map(Gun.obj.ify(o.getItem(i.prefix+n+"_")),function(t,r){t=Gun.obj.ify(o.getItem(i.prefix+n+r)||{}),e=Gun.state.ify(e,r,t[1],t[0],n)}),u.back(-1).on("in",{"@":t["#"],put:Gun.graph.node(e)})}})}})(t,"./adapters/localStorage"),t(function(n){function o(t,n,e){return this instanceof o?(this.url=o.formatURL(t),this.socket=null,this.queue=[],this.sid=i.text.random(10),this.on=i.on,this.options=n||{},this.options.wsc=e,this.resetBackoff(),void 0):new o(t,n,e)}function e(t,n){o.isSupported&&o.broadcast(t,n)}var i=t("./core");if("undefined"==typeof JSON)throw Error("Gun depends on JSON. Please load it first:\najax.cdnjs.com/ajax/libs/json2/20110223/json2.js");o.prototype={constructor:o,drainQueue:function(){var t=this.queue,n=this;return this.queue=[],t.forEach(function(t){n.send(t)}),t.length},connect:function(){var t=this,n=new o.WebSocket(this.url,this.options.wsc.protocols,this.options.wsc);return this.socket=n,n.addEventListener("message",function(n){t.on("message",n)}),n.addEventListener("close",function(){t.scheduleReconnect()}),this.ready(function(){t.drainQueue()}),n},resetBackoff:function(){var t=this.options;return this.backoff={time:t.time||100,max:t.max||2e3,factor:t.factor||2},this.backoff},nextBackoff:function(){var t=this.backoff,n=t.time*t.factor,o=t.max;return n>o&&(n=o),t.time=n},scheduleReconnect:function(){var t=this,n=this.backoff.time;this.nextBackoff(),setTimeout(function(){t.connect(),t.ready(function(){t.resetBackoff()})},n)},isClosed:function(){var t=this.socket;if(!t)return!0;var n=t.readyState;return n===t.CLOSING||n===t.CLOSED?!0:!1},ready:function(t){var n=this.socket,o=n.readyState;return o===n.OPEN?(t(),void 0):(o===n.CONNECTING&&n.addEventListener("open",t),void 0)},send:function(t){if(this.isClosed())return this.queue.push(t),this.connect(),!1;var n=this.socket;return this.ready(function(){n.send(t)}),!0}},"undefined"!=typeof window&&(o.WebSocket=window.WebSocket||window.webkitWebSocket||window.mozWebSocket||null),o.isSupported=!!o.WebSocket,o.isSupported&&(o.formatURL=function(t){return t.replace(/^http/,"ws")},o.broadcast=function(t,n){var e=o.pool;n.headers=n.headers||{},i.obj.map(t,function(t,r){var u=o.formatURL(r),a=e[u],s={headers:i.obj.to(n.headers,{"gun-sid":a.sid}),body:n.body},f=i.text.ify(s);a.send(f)})},o.pool={},"undefined"!=typeof window&&window.addEventListener("unload",function(){i.obj.map(o.pool,function(t){t.isClosed()||t.socket.close() -})}),i.on("opt",function(t){this.to.next(t);var n=t.gun,e=n.back("opt.peers")||{};i.obj.map(e,function(t,e){var r=o.formatURL(e);if(!o.pool.hasOwnProperty(r)){var u=new o(r,t.backoff,n.back("opt.wsc")||{protocols:null});o.pool[r]=u,u.on("message",function(t){var o;try{o=i.obj.ify(t.data)}catch(e){return}o&&o.body&&n.on("in",o.body)})}})}),i.on("out",function(t){this.to.next(t);var n=t.gun,o=n.back("opt.peers")||{},r=n.back("opt.headers")||{};i.obj.empty(o)||e(o,{body:t,headers:r})}),e.jsonp=function(t,n){e.jsonp.ify(t,function(o){o&&e.jsonp.send(o,function(o,i){n(o,i),e.jsonp.poll(t,i)},t.jsonp)})},e.jsonp.send=function(t,n,o){var e=document.createElement("script");return e.src=t,e.onerror=function(){(window[e.id]||function(){})(null,{err:"JSONP failed!"})},window[e.id=o]=function(t,o){n(o,t),n.id=e.id,e.parentNode.removeChild(e),delete window[n.id]},e.async=!0,document.getElementsByTagName("head")[0].appendChild(e),e},e.jsonp.poll=function(t,n){if(t&&t.base&&n&&n.headers&&n.headers.poll){var o=e.jsonp.poll.s=e.jsonp.poll.s||{};o[t.base]=o[t.base]||setTimeout(function(){var n={base:t.base,headers:{pull:1}};e.each(t.headers,function(t,o){n.headers[o]=t}),e.jsonp(n,function(n,i){delete o[t.base];for(var r=i.body||[];r.length&&r.shift;){var u=i.body.shift();u&&u.body&&e.createServer.ing(u,function(){e(t.base,null,null,u)})}})},n.headers.poll)}},e.jsonp.ify=function(t,n){var o=encodeURIComponent,i="?";if(t.url&&t.url.pathname&&(i=t.url.pathname+i),i=t.base+i,e.each((t.url||{}).query,function(t,n){i+=o(n)+"="+o(t)+"&"}),t.headers&&(i+=o("`")+"="+o(JSON.stringify(t.headers))+"&"),e.jsonp.maxc)return n();for(;u;)n(i+a(s,s+=c,f)+u.slice(0,s)),u=u.slice(s)}else n(i)},e.jsonp.max=2e3,e.each=function(t,n,o){if(t&&n)for(var e in t)t.hasOwnProperty(e)&&n.call(o,t[e],e)},n.exports=o)})(t,"./polyfill/request")})(); \ No newline at end of file +!function(){function t(n){function o(t){return t.split("/").slice(-1).toString().replace(".js","")}return n.slice?t[o(n)]:function(e,i){n(e={exports:{}}),t[o(i)]=e.exports}}var n;"undefined"!=typeof window&&(n=window),"undefined"!=typeof global&&(n=global),n=n||{};var o=n.console||{log:function(){}};if("undefined"!=typeof module)var e=module;t(function(t){var n={};n.fns=n.fn={is:function(t){return!!t&&"function"==typeof t}},n.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},n.num={is:function(t){return!e(t)&&(t-parseFloat(t)+1>=0||1/0===t||-(1/0)===t)}},n.text={is:function(t){return"string"==typeof t}},n.text.ify=function(t){return n.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},n.text.random=function(t,n){var o="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";t>0;)o+=n.charAt(Math.floor(Math.random()*n.length)),t--;return o},n.text.match=function(t,o){function e(t,n){for(var o,e=-1,i=0;o=n[i++];)if(!~(e=t.indexOf(o,e+1)))return!1;return!0}var i=!1;if(t=t||"",o=n.text.is(o)?{"=":o}:o||{},n.obj.has(o,"~")&&(t=t.toLowerCase(),o["="]=(o["="]||o["~"]).toLowerCase()),n.obj.has(o,"="))return t===o["="];if(n.obj.has(o,"*")){if(t.slice(0,o["*"].length)!==o["*"])return!1;i=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;i=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){return t.indexOf(n)>=0?void(i=!0):!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){return t.indexOf(n)<0?void(i=!0):!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;i=!0}if(n.obj.has(o,"<")){if(!(tn?-1:n>o?1:0):0}},n.list.map=function(t,n,o){return a(t,n,o)},n.list.index=1,n.obj={is:function(t){return t?t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1]:!1}},n.obj.put=function(t,n,o){return(t||{})[n]=o,t},n.obj.has=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n.obj.del=function(t,n){return t?(t[n]=null,delete t[n],t):void 0},n.obj.as=function(t,n,o,e){return t[n]=t[n]||(e===o?{}:o)},n.obj.ify=function(t){if(r(t))return t;try{t=JSON.parse(t)}catch(n){t={}}return t},function(){function t(t,n){u(this,n)&&o!==this[n]||(this[n]=t)}var o;n.obj.to=function(n,o){return o=o||{},a(n,t,o),o}}(),n.obj.copy=function(t){return t?JSON.parse(JSON.stringify(t)):t},function(){function t(t,n){var o=this.n;if(!o||!(n===o||r(o)&&u(o,n)))return n?!0:void 0}n.obj.empty=function(n,o){return n&&a(n,t,{n:o})?!1:!0}}(),function(){function t(n,o){return 2===arguments.length?(t.r=t.r||{},void(t.r[n]=o)):(t.r=t.r||[],void t.r.push(n))}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,d,g,h=0,v=o(s);if(t.r=null,i&&r(a)&&(d=Object.keys(a),g=!0),e(a)||d)for(l=(d||a).length;l>h;h++){var b=h+n.list.index;if(v){if(p=g?s.call(f||this,a[d[h]],d[h],t):s.call(f||this,a[h],b,t),p!==c)return p}else if(s===a[g?d[h]:h])return d?d[h]:b}else for(h in a)if(v){if(u(a,h)&&(p=f?s.call(f,a[h],h,t):s(a[h],h,t),p!==c))return p}else if(s===a[h])return h;return v?t.r:n.list.index?0:-1}}(),n.time={},n.time.is=function(t){return t?t instanceof Date:+(new Date).getTime()};var o=n.fn.is,e=n.list.is,i=n.obj,r=i.is,u=i.has,a=i.map;t.exports=n})(t,"./type"),t(function(t){t.exports=function n(t,o,e){if(!t)return{to:n};var t=(this.tag||(this.tag={}))[t]||(this.tag[t]={tag:t,to:n._={next:function(){}}});if(o instanceof Function){var i={off:n.off||(n.off=function(){return this.next===n._.next?!0:(this===this.the.last&&(this.the.last=this.back),this.to.back=this.back,this.next=n._.next,void(this.back.to=this.to))}),to:n._,next:o,the:t,on:this,as:e};return(i.back=t.last||t).to=i,t.last=i}return(t=t.to).next(o),t}})(t,"./onto"),t(function(n){function o(t,n){n=n||{},n.id=n.id||"#",n.rid=n.rid||"@",n.uuid=n.uuid||function(){return+new Date+Math.random()};var o=e;return o.stun=function(t){var n=function(t){return n.off&&n===this.stun?(this.stun=null,!1):o.stun.skip?!1:(t&&(t.cb=t.fn,t.off(),e.queue.push(t)),!0)},e=n.res=function(t,r){if(!n.off){if(t instanceof Function)return o.stun.skip=!0,t.call(r),void(o.stun.skip=!1);n.off=!0;var u,a=0,s=e.queue,f=s.length;for(e.queue=[],n===i.stun&&(i.stun=null),a;f>a;a++)u=s[a],u.fn=u.cb,u.cb=null,o.stun.skip=!0,u.ctx.on(u.tag,u.fn,u),o.stun.skip=!1}},i=t._;return e.back=i.stun||(i.back||{_:{}})._.stun,e.back&&(e.back.next=n),e.queue=[],i.stun=n,e},o}var e=t("./onto");n.exports=o})(t,"./onify"),t(function(n){function o(t,n,e){o.time=e||Gun.time.is,o.waiting.push({when:t,event:n||function(){}}),o.soonest=t?0:t-n,clearTimeout(o.id),o.id=setTimeout(o.check,t)}},o.each=function(t,n,o){var e=this;t&&(t.when<=e.now?t.event instanceof Function&&setTimeout(function(){t.event()},0):(e.soonest=e.soonestt)return{defer:!0};if(e>n)return{historical:!0};if(n>e)return{converge:!0,incoming:!0};if(n===e){if(o(i)===o(r))return{state:!0};if(o(i)u?(a=0,u=t):u=t+(a+=1)/s}var e=t("./type"),i=t("./node"),r=e.time.is,u=-(1/0),a=0,s=1e3;o._=">",o.ify=function(t,n,e,r,u){if(!t||!t[m]){if(!u)return;t=i.soul.ify(t,u)}var a=l(t[m],o._);return f!==n&&n!==m&&(v(e)&&(a[n]=e),f!==r&&(t[n]=r)),t},o.is=function(t,n,e){var i=n&&t&&t[m]&&t[m][o._]||e;if(i)return v(i[n])?i[n]:-(1/0)},function(){function t(t,n){m!==n&&o.ify(this.o,n,this.s)}o.map=function(n,e,i){var r,u=d(u=n||e)?u:null;return n=_(n=n||e)?n:null,u&&!n?(e=v(e)?e:o(),u[m]=u[m]||{},g(u,t,{o:u,s:e}),u):(i=i||d(e)?e:r,e=v(e)?e:o(),function(o,u,a,s){return n?(n.call(i||this||{},o,u,a,s),void(p(a,u)&&r===a[u]||t.call({o:a,s:e},o,u))):(t.call({o:a,s:e},o,u),o)})}}();var f,c=e.obj,l=c.as,p=c.has,d=c.is,g=c.map,h=e.num,v=h.is,b=e.fn,_=b.is,m=i._;n.exports=o})(t,"./state"),t(function(n){var o=t("./type"),e=t("./val"),i=t("./node"),r={};!function(){function t(t,o){return t&&o===i.soul(t)&&i.is(t,this.fn)?void(this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n))):!0}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return n&&s(n)&&!l(n)?!d(n,t,{cb:o,fn:e,as:i}):!1}}(),function(){function t(t,r){var u;return(u=l(t,r))?u:(r.env=t,r.soul=o,i.ify(r.obj,n,r)&&(t.graph[e.rel.is(r.rel)]=r.node),r)}function n(n,o,r){var s,l,p=this,d=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,d)){if(o||(p.node=p.node||r||{},c(n,i._)&&(p.node._=g(n._)),p.node=i.soul.ify(p.node,e.rel.is(p.rel)),p.rel=p.rel||e.rel.ify(i.soul(p.node))),(l=d.map)&&(l.call(d.as||{},n,o,r,p),c(r,o))){if(n=r[o],u===n)return void f(r,o);if(!(s=a(n,o,r,p,d)))return}if(!o)return p.node;if(!0===s)return n;if(l=t(d,{obj:n,path:p.path.concat(o)}),l.node)return l.rel}}function o(t){var n=this,o=e.rel.is(n.rel),r=n.env.graph;n.rel=n.rel||e.rel.ify(t),n.rel[e.rel._]=t,n.node&&n.node[i._]&&(n.node[i._][e.rel._]=t),c(r,o)&&(r[t]=r[o],f(r,o))}function a(t,n,o,i,r){var u;return e.is(t)?!0:s(t)?1:(u=r.invalid)?(t=u.call(r.as||{},t,n,o),a(t,n,o,i,r)):void(r.err="Invalid value at '"+i.path.concat(n).join(".")+"'!")}function l(t,n){for(var o,e=t.seen,i=e.length;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}r.ify=function(n,o,i){var r={path:[],obj:n};return o?"string"==typeof o?o={soul:o}:o instanceof Function&&(o.map=o):o={},o.soul&&(r.rel=e.rel.ify(o.soul)),o.graph=o.graph||{},o.seen=o.seen||[],o.as=o.as||i,t(o,r),o.root=r.node,o.graph}}(),r.node=function(t){var n=i.soul(t);if(n)return p({},n,t)},function(){function t(t,n){var o,u;if(i._===n){if(l(t,e.rel._))return;return void(this.obj[n]=g(t))}return(o=e.rel.is(t))?(u=this.opt.seen[o])?void(this.obj[n]=u):void(this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt)):void(this.obj[n]=t)}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},d(n[o],t,{obj:i,graph:n,opt:e}),i}}}();var u,a=(o.fn.is,o.obj),s=a.is,f=a.del,c=a.has,l=a.empty,p=a.put,d=a.map,g=a.copy;n.exports=r})(t,"./graph"),t(function(n){function o(){this.cache={}}var e=t("./type");o.prototype.track=function(t){return this.cache[t]=e.time.is(),this.to||this.gc(),t},o.prototype.check=function(t){return e.obj.has(this.cache,t)?this.track(t):!1},o.prototype.gc=function(){var t=this,n=e.time.is(),o=n,i=3e5;e.obj.map(t.cache,function(r,u){o=Math.min(n,r),i>n-r||e.obj.del(t.cache,u)});var r=e.obj.empty(t.cache);if(r)return void(t.to=null);var u=n-o,a=i-u;t.to=setTimeout(function(){t.gc()},a)},n.exports=o})(t,"./dup"),t(function(n){function i(t){return t instanceof i?(this._={gun:this}).gun:this instanceof i?i.create(this._={gun:this,opt:t}):new i(t)}i.is=function(t){return t instanceof i},i.version=.6,i.chain=i.prototype,i.chain.toJSON=function(){};var r=t("./type");r.obj.to(r,i),i.HAM=t("./HAM"),i.val=t("./val"),i.node=t("./node"),i.state=t("./state"),i.graph=t("./graph"),i.dup=t("./dup"),i.on=t("./onify")(),i._={node:i.node._,soul:i.val.rel._,state:i.state._,field:".",value:"="},function(){function t(t){var o,e=this,r=e.as;if(t.gun||(t.gun=r.gun),!t["#"]&&t["@"]){if(t["#"]=i.text.random(),r.ack(t["@"],t))return;return r.dup.track(t["#"]),void i.on("out",l(t,{gun:r.gun}))}t["#"]&&r.dup.check(t["#"])||(r.dup.track(t["#"]),r.ack(t["@"],t)||(o=l(t,{gun:r.gun}),t.get&&(n(t,r)||i.on("get",o)),t.put&&(i.HAM.synth(t,e,r.gun),i.on("put",o)),i.on("out",o)))}function n(t,n){var o,e=t.get[d],r=n.graph[e],u=t.get[g],a=n.next||(n.next={}),s=(a[e]||(a[e]=n.gun.get(e)))._;if(r){if(u){if(!c(r,u))return;o=i.obj.put(i.node.soul.ify({},e),u,r[u]),r=i.state.ify(o,u,i.state.is(r,u))}return r=i.graph.node(r),o=s.ack,n.on("in",{"@":t["#"],how:"mem",put:r,gun:s.gun}),o>0?!0:void 0}}i.create=function(n){n.on=n.on||i.on,n.root=n.root||n.gun,n.graph=n.graph||{},n.dup=n.dup||new i.dup,n.ask=i.on.ask,n.ack=i.on.ack;var o=n.gun.opt(n.opt);return n.once||(n.on("in",t,n),n.on("out",t,n)),n.once=1,o}}(),function(){i.on.ask=function(t,n){if(this.on){var o=i.text.random();return t&&this.on(o,t,n),o}},i.on.ack=function(t,n){if(t&&n&&this.on){var o=t["#"]||t;if(this.tag&&this.tag[o])return this.on(o,n),!0}}}(),function(){i.chain.opt=function(t){t=t||{};var n=this,o=n._,e=t.peers||t;return f(t)||(t={}),f(o.opt)||(o.opt=t),u(e)&&(e=[e]),a(e)&&(e=p(e,function(t,n,o){o(t,{url:t})}),f(o.opt.peers)||(o.opt.peers={}),o.opt.peers=l(e,o.opt.peers)),o.opt.peers=o.opt.peers||{},l(t,o.opt),i.on("opt",o),n}}();var u=i.text.is,a=i.list.is,s=i.obj,f=s.is,c=s.has,l=s.to,p=s.map,d=i._.soul,g=i._.field;o.debug=function(t,n){return o.debug.i&&t===o.debug.i&&o.debug.i++&&(o.log.apply(o,arguments)||n)},i.log=function(){return!i.log.off&&o.log.apply(o,arguments),[].slice.call(arguments).join(" ")},i.log.once=function(t,n,o){return(o=i.log.once)[t]=o[t]||0,o[t]++||i.log(n)},i.log.once("welcome","Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!"),"undefined"!=typeof window&&(window.Gun=i),"undefined"!=typeof e&&(e.exports=i),n.exports=i})(t,"./root"),t(function(){return})(t,"./experiment"),t(function(){var n=t("./root");n.chain.back=function(t,n){var i;if(-1===t||1/0===t)return this._.root;if(1===t)return this._.back||this;var r=this,u=r._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){var a=0,s=t.length,i=u;for(a;s>a;a++)i=(i||e)[t[a]];if(o!==i)return n?r:i;if(i=u.back)return i.back(t,n)}else if(t instanceof Function){for(var f,i={back:r};(i=i.back)&&(i=i._)&&!(f=t(i,n)););return f}};var o,e={}})(t,"./back"),t(function(){function o(t){var n,o=this.as,e=o.gun,i=e.back(-1);if(t.gun||(t.gun=e),(n=t.get)&&!n[m])if(g(n,y)){n=n[y];var r=n?e.get(n)._:o;if(g(r,"put"))return void r.on("in",r);if(g(o,"put")){var u,a=o.put;if((u=c.node.soul(a))&&(a=c.val.rel.ify(u)),u=c.val.rel.is(a)){if(!t.gun._)return;return void t.gun._.on("out",{get:{"#":u,".":n},"#":i._.ask(c.HAM.synth,t.gun),gun:t.gun})}if(l===a||c.val.is(a)){if(!t.gun._)return;return void t.gun._.on("in",{get:n,gun:t.gun})}}else o.map&&_(o.map,function(t){t.at.on("in",t.at)});if(o.soul){if(!t.gun._)return;return void t.gun._.on("out",{get:{"#":o.soul,".":n},"#":i._.ask(c.HAM.synth,t.gun),gun:t.gun})}if(o.get){if(!o.back._)return;return void o.back._.on("out",{get:h({},y,o.get),gun:e})}t=b(t,{get:{}})}else{if(g(o,"put")?o.on("in",o):o.map&&_(o.map,function(t){t.at.on("in",t.at)}),o.ack&&!g(o,"put"))return;if(o.ack=-1,o.soul)return void o.on("out",{get:{"#":o.soul},"#":i._.ask(c.HAM.synth,o.gun),gun:o.gun});if(o.get){if(!o.back._)return;return void o.back._.on("out",{get:h({},y,o.get),gun:o.gun})}}o.back._.on("out",t)}function e(t){t=t._||t;{var n,o=this,e=this.as,u=t.gun,f=u._,d=t.put;e.back._||p}if(0>e.ack&&!c.val.rel.is(d)&&(e.ack=1),e.get&&t.get!==e.get&&(t=b(t,{get:e.get})),e.field&&f!==e&&(t=b(t,{gun:e.gun}),f.ack&&(e.ack=e.ack||f.ack)),l===d){if(o.to.next(t),e.soul)return;return r(e,t,o),e.field&&s(e,t),v(f.echo,e.id),void v(e.map,f.id)}return e.soul?(e.root._.now&&(t=b(t,{put:d=f.put})),o.to.next(t),r(e,t,o),void _(d,a,{at:t,cat:e})):(n=c.val.rel.is(d))?(i(e,t,f,n),o.to.next(t),void r(e,t,o)):c.val.is(d)?(e.field||e.soul?s(e,t):(f.field||f.soul)&&((f.echo||(f.echo={}))[e.id]=e,(e.map||(e.map={}))[f.id]=e.map[f.id]||{at:f}),o.to.next(t),void r(e,t,o)):(e.field&&f!==e&&g(f,"put")&&(e.put=f.put),(n=c.node.soul(d))&&f.field&&(f.put=e.root.get(n)._.put),o.to.next(t),r(e,t,o),i(e,t,f,n),void _(d,a,{at:t,cat:e}))}function i(t,n,o,e){if(e&&k!==t.get){var r=t.root.get(e)._;t.field?o=r:o.field&&i(o,n,o,e),o!==t&&((o.echo||(o.echo={}))[t.id]=t,t.field&&!(t.map||p)[o.id]&&s(t,n),r=(t.map||(t.map={}))[o.id]=t.map[o.id]||{at:o},e!==r.rel&&f(t,r.rel=e))}}function r(t,n,o){t.echo&&(t.field&&(n=b(n,{event:o})),_(t.echo,u,n))}function u(t){t.on("in",this)}function a(t,n){var o,e,i,r=this.cat,u=r.next||p,a=this.at;(k!==n||u[n])&&(o=u[n])&&(i=o._,i.field?(t&&t[m]&&c.val.rel.is(t)===c.node.soul(i.put)||(i.put=t),e=o):e=a.gun.get(n),i.on("in",{put:t,get:n,gun:e,via:a}))}function s(t){if(t.field||t.soul){var n=t.map;t.map=null,null!==n&&(l!==n||t.put===l)&&(_(n,function(n){(n=n.at)&&v(n.echo,t.id)}),_(t.next,function(t,n){var o=t._;o.put=l,o.ack&&(o.ack=-1),o.on("in",{get:n,gun:t,put:l})}))}}function f(t,n){var o=t.root.get(n)._;return t.ack?(o.ack=o.ack||-1,void o.on("out",{get:{"#":n},"#":t.root._.ask(c.HAM.synth,o.gun),gun:o.gun})):void _(t.next,function(e,i){e._.on("out",{get:{"#":n,".":i},"#":t.root._.ask(c.HAM.synth,o.gun),gun:e})})}var c=t("./root");c.chain.chain=function(){var t=this._,i=new this.constructor(this),r=i._;return r.root=n=t.root,r.id=++n._.once,r.back=this,r.on=c.on,c.on("chain",r),r.on("in",e,r),r.on("out",o,r),i},c.chain.chain.input=e;var l,p={},d=c.obj,g=d.has,h=d.put,v=d.del,b=d.to,_=d.map,m=c._.soul,y=c._.field,k=c.node._})(t,"./chain"),t(function(){function n(t,n){var o=n._,e=o.next,i=n.chain(),r=i._;return e||(e=o.next={}),e[r.get=t]=i,o.root===n?r.soul=t:(o.soul||o.field)&&(r.field=t),i}function o(t){var n,o=this,e=o.as,r=t.gun,a=r._,f=t.put;i===f&&(f=a.put),(n=f)&&n[s._]&&(n=s.is(n))&&(n=a.root.get(n)._,i!==n.put&&(t=u(t,{put:n.put}))),e.use(t,t.event||o),o.to.next(t)}var e=t("./root");e.chain.get=function(t,i,r){if("string"!=typeof t){if(t instanceof Function){var u=this,s=u._;return r=i||{},r.use=t,r.out=r.out||{cap:1},r.out.get=r.out.get||{},"_"!=s.get&&(s.root._.now=!0),s.on("in",o,r),s.on("out",r.out),s.root._.now=!1,u}return a(t)?this.get(""+t,i,r):((r=this.chain())._.err={err:e.log("Invalid get request!",t)},i&&i.call(r,r._.err),r)}var u,c,l=l||this,p=l._,d=p.next||f;return(u=d[t])||(u=n(t,l)),(c=p.stun)&&(u._.stun=u._.stun||c),i&&i instanceof Function&&u.get(i,r),u};var i,r=e.obj,u=(r.has,e.obj.to),a=e.num.is,s=e.val.rel,f=(e.node._,{})})(t,"./get"),t(function(){function n(t){t.batch=e;var n=t.opt||{},o=t.env=s.state.map(r,n.state);return o.soul=t.soul,t.graph=s.graph.ify(t.data,o,t),o.err?((t.ack||h).call(t,t.out={err:s.log(o.err)}),void(t.res&&t.res())):void t.batch()}function e(){var t=this;t.graph&&!d(t.stun,i)&&((t.res||v)(function(){t.ref._.on("out",{cap:3,gun:t.ref,put:t.out=t.env.graph,opt:t.opt,"#":t.gun.back(-1)._.ask(function(n){this.off(),t.ack&&t.ack(n,this)},t.opt)})},t),t.res&&t.res())}function i(t){return t?!0:void 0}function r(t,n,o,e){var i=this;!n&&e.path.length&&(i.res||v)(function(){var t=e.path,n=i.ref,o=(i.opt,0),r=t.length;for(o;r>o;o++)n=n.get(t[o]);return i.not||s.node.soul(e.obj)?void e.soul(s.node.soul(e.obj)||((i.opt||{}).uuid||i.gun.back("opt.uuid")||s.text.random)()):((i.stun=i.stun||{})[t]=!0,void n.get("_").get(u,{as:{at:e,as:i}}))},{as:i,at:e})}function u(t,n){var o=this.as,e=o.at;o=o.as,t.gun&&t.gun._.back&&(n.off(),t=t.gun._.back._,e.soul(s.node.soul(e.obj)||s.node.soul(t.put)||s.val.rel.is(t.put)||((o.opt||{}).uuid||o.gun.back("opt.uuid")||s.text.random)()),o.stun[e.path]=!1,o.batch())}function a(t,n){var e=this.as;if(t.gun&&t.gun._){if(t.err)return void o.log("Please report this as an issue! Put.any.err");var i,r=t.gun._.back._,u=r.put,a=e.opt||{};if(n.off(),e.ref!==e.gun){if(i=e.gun._.get||r.get,!i)return void o.log("Please report this as an issue! Put.no.get");e.data=p({},i,e.data),i=null}if(f===u){if(!r.get)return;r.soul||(i=r.gun.back(function(t){return t.soul?t.soul:void(e.data=p({},t.get,e.data))})),i=i||r.get,r=r.root.get(i)._,e.not=e.soul=i,u=e.data}e.not||(e.soul=s.node.soul(u))||(e.soul=e.path&&l(e.data)?(a.uuid||r.root._.opt.uuid||s.text.random)():t.soul||r.soul||(a.uuid||r.root._.opt.uuid||s.text.random)()),e.ref.put(e.data,e.soul,e)}}var s=t("./root");s.chain.put=function(t,o,e){var i,r=this,u=r._,f=u.root;return e=e||{},e.data=t,e.gun=e.gun||r,"string"==typeof o?e.soul=o:e.ack=o,u.soul&&(e.soul=u.soul),e.soul||f===r?l(e.data)?(e.gun=r=f.get(e.soul=e.soul||(e.not=s.node.soul(e.data)||(f._.opt.uuid||s.text.random)())),e.ref=e.gun,n(e),r):((e.ack||h).call(e,e.out={err:s.log("Data saved to the root level of the graph must be a node (an object), not a",typeof e.data,'of "'+e.data+'"!')}),e.res&&e.res(),r):s.is(t)?(t.get(function(t,n){n.off();var i=s.node.soul(t.put);return i?void r.put(s.val.rel.ify(i),o,e):void s.log("The reference you are saving is a",typeof t.put,'"'+e.put+'", not a node (object)!')}),r):(e.ref=e.ref||f===(i=u.back)?r:i,e.ref._.soul&&s.val.is(e.data)&&u.get?(e.data=p({},u.get,e.data),e.ref.put(e.data,e.soul,e),r):(e.ref.get("_").get(a,{as:e}),e.out||(e.res=e.res||s.on.stun(e.ref),e.gun._.stun=e.ref._.stun),r))};var f,c=s.obj,l=c.is,p=c.put,d=c.map,g={},h=function(){},v=function(t,n){t.call(n||g)}})(t,"./put"),t(function(n){var e=t("./root");n.exports=e,function(){function t(t,n){if(e._.node!==n){var r=this.node,u=this.vertex,a=this.union,s=this.machine,f=g(r,n),c=g(u,n);if(i===f||i===c)return!0;var l=t,p=u[n];if(!b(l)&&i!==l)return!0;if(!b(p)&&i!==p)return!0;var d=e.HAM(s,f,c,l,p);if(d.err)return void o.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.",n,d.err);if(!(d.state||d.historical||d.current))return d.incoming?(a[n]=t,void h(a,n,f)):d.defer?(a[n]=t,void h(a,n,f)):void 0}}function n(t,n){var o=this;if(e._.node!==n&&b(t)){var i=o.node,r=o.vertex,u=g(i,n,!0),a=g(r,n,!0),s=o.delta,f=e.HAM(o.machine,u,a,t,r[n]);f.incoming&&(s[n]=t,h(s,n,u))}}e.HAM.union=function(n,o,i){return o&&o._&&(n=n||e.node.soul.ify({_:{">":{}}},e.node.soul(o)),n&&n._&&(i=a(i)?{machine:i}:{machine:e.state()},i.union=n||e.obj.copy(n),i.vertex=n,i.node=o,!l(o,t,i)))?i.union:void 0},e.HAM.delta=function(t,o,i){return i=a(i)?{machine:i}:{machine:e.state()},t?(i.soul=e.node.soul(i.vertex=t),i.soul?(i.delta=e.node.soul.ify({},i.soul),l(i.node=o,n,i),i.delta):void 0):e.obj.copy(o)},e.HAM.synth=function(t,n,o){var r=this.as||o,u=r._,a=u.root._,s={};if(!t.put){if(u.put!==i)return;return void u.on("in",{get:u.get,put:u.put=i,gun:r,via:t})}l(t.put,function(t,n){var o=this.graph;s[n]=e.HAM.delta(o[n],t,{graph:o}),o[n]=e.HAM.union(o[n],t)||o[n]},a),t.gun!==a.gun&&(s=t.put),l(s,function(o,r){var a=this,s=a.next||(a.next={}),l=s[r]||(s[r]=a.gun.get(r)),p=l._;return p.put=a.graph[r],u.field&&!f(o,u.field)?((t=c(t,{})).put=i,void e.HAM.synth(t,n,u.gun)):void p.on("in",{put:o,get:r,gun:l,via:t})},a)}}();{var i,r=e,u=r.num,a=u.is,s=r.obj,f=s.has,c=(s.put,s.to),l=s.map,p=e.node,d=(p.soul,p.is,p.ify,e.state),g=d.is,h=d.ify,v=e.val,b=v.is;v.rel.is}})(t,"./index"),t(function(n){var o=t("./root");t("./index"),t("./opt"),t("./chain"),t("./back"),t("./put"),t("./get"),n.exports=o})(t,"./core"),t(function(){{var n=t("./core"),e=n.obj,i=(e.is,e.put),r=(e.map,e.empty,n.num);r.is,n.val.rel._}!function(){function t(t,o){var u=this;return o.off(),u.soul=n.node.soul(t.put),u.soul&&u.key!==u.soul?(u.data=i({},r._,n.node.ify(i({},u.soul,n.val.rel.ify(u.soul)),"#"+u.key+"#")),(u.res||e)(function(){u.ref.put(u.data,u.any,{soul:u.key,key:u.key})},u),void(u.res&&u.res())):u.data={}}function e(t,n){t.call(n||{})}function r(t){if(t&&"#"===t[0]&&"#"===t[t.length-1]){var n=t.slice(1,-1);if(n)return n}}function u(t){var o=this;if(!t.put)return void(t.get&&a.call(t.gun?t.gun._:o,t));if(!t.opt||!t.opt.key){var e=t.put,i=o.gun.back(-1)._.graph;n.graph.is(e,function(t,o){n.node.is(i["#"+o+"#"],function r(o,u){return u===n.val.rel.is(o)?(o=i["#"+u+"#"])?void n.node.is(o,r):void n.node.soul.ify(o=e[u]=n.obj.copy(t),u):void 0})&&n.obj.del(e,o)})}}function a(t){function o(u,a){var s=(u.put,t.get);if(!i.pseudo||u.via)return n.on.ack(e,u);if(u.put){if(!s["."])return a.off(),n.on.ack(e,u);if(c(u.put[s["#"]],s["."]))return a.off(),n.on.ack(e,u)}n.obj.map(i.seen,function(i,a){return r[a]?n.on.ack(e,u):(r[a]=!0,void i.on("out",{gun:i,get:a={"#":a,".":t.get["."]},"#":n.on.ask(o)}))})}var e,i=this;if(n.obj.is(e=t.get)&&n.obj.has(e,"#")){if((e=t.get)&&null===e["."])return void(e["."]="##");(e=t.get)&&n.obj.has(e,".")&&(e["#"]&&(i=i.root.gun.get(e["#"])._),e=t["#"],t["#"]=n.on.ask(o));var r={}}}function s(t,o){function e(t){n.node.is(t,i)}function i(t,o){o===n.val.rel.is(t)&&(p[o]||(p[o]=l.get(o).on(u,!0)))}function u(t){t&&(a.pseudo=n.HAM.union(a.pseudo,t)||a.pseudo,a.change=a.changed=t,a.put=a.pseudo,c({gun:a.gun,put:a.pseudo,get:f}))}var a=this;if(a.pseudo){if(a.pseudo===t.put)return;return o.stun(),a.change=a.changed||a.pseudo,void a.on("in",n.obj.to(t,{put:a.put=a.pseudo}))}if(t.put){var s=n.val.rel.is(t.put[r._]);if(s){var f=n.node.soul(t.put),c=o.stun(c),l=a.gun.back(-1),p=a.seen={};a.pseudo=a.put=n.state.ify(n.node.ify({},f)),l.get(s).on(e,{change:!0})}}}n.chain.key=function(e,i,r){if(!e)return i&&i.call(this,{err:n.log("No key!")}),this;var u=this;return"string"==typeof r?(o.log("Please report this as an issue! key.opt.string"),u):u===u._.root?(i&&i({err:n.log("Can't do that on root instance.")}),u):(r=r||{},r.key=e,r.any=i||function(){},r.ref=u.back(-1).get(r.key),r.gun=r.gun||u,u.on(t,{as:r}),r.data||(r.res=n.on.stun(r.ref)),u)},r._="##",n.on("next",function(t){var n=t.gun;n.back(-1)===t.back&&(n.on("in",s,n._),n.on("out",u,n._))});var f=n.obj,c=f.has}()})(t,"./key"),t(function(){var n=t("./core");n.chain.path=function(t,o,e){var i,r=this,u=r;if(e=e||{},e.path=!0,u===u._.root)return o&&o({err:n.log("Can't do that on root instance.")}),u;if("string"==typeof t){if(i=t.split(e.split||"."),1===i.length)return u=r.get(t,o,e),u._.opt=e,u;t=i}if(t instanceof Array){if(t.length>1){u=r;var a=0,s=t.length;for(a;s>a;a++)u=u.get(t[a],a+1===s?o:null,e)}else u=r.get(t[0],o,e);return u._.opt=e,u}return t||0==t?(u=r.get(""+t,o,e),u._.opt=e,u):r}})(t,"./path"),t(function(){function n(t,n){var o,r=this,u=t.gun,s=u._,f=s.put||t.put,o=r.last,c=s.id+t.get;if(i!==f){if(f&&f[a._]&&(o=a.is(f))){if(o=s.root.get(o)._,i===o.put)return;f=o.put}r.change&&(f=t.put),(o.put!==f||o.get!==c||e.node.soul(f))&&(o.put=f,o.get=c,s.last=f,r.as?r.ok.call(r.as,t,n):r.ok.call(u,f,t.get,t,n))}}function o(t,n){var e,r=this.as,u=r.cat,s=t.gun,f=s._,c=f.put||t.put;if(i!==c){if(c&&c[a._]&&(e=a.is(c))){if(e=u.root.get(e)._,i===e.put)return;c=e.put}if(n.wait&&clearTimeout(n.wait),!r.async)return void(n.wait=setTimeout(function(){o.call({as:r},t,n,n.wait||1)},r.wait||99));if(u.field||u.soul){if(n.off())return}else{if((r.seen=r.seen||{})[f.id])return;r.seen[f.id]=!0}r.ok.call(t.gun||r.gun,c,t.get)}}var e=t("./core");e.chain.on=function(t,o,e,i){var r,u,a=this,f=a._;if("string"==typeof t)return o?(r=f.on(t,o,e||f,i),e&&e.gun&&(e.subs||(e.subs=[])).push(r),u=function(){r&&r.off&&r.off(),u.off()},u.off=a.off.bind(a)||s,a.off=u,a):f.on(t);var c=o;return c=!0===c?{change:!0}:c||{},c.ok=t,c.last={},a.get(n,c),a},e.chain.val=function(t,n){var r=this,u=r._,a=u.put;if(0=a?n():void(o||(clearTimeout(o),o=setTimeout(n,1e3)))}),Gun.on("get",function(t){this.to.next(t);var n,o,i,u,a=t.gun,s=t.get;if((i=t.opt||{}).prefix=i.prefix||t.gun.back("opt.prefix")||"gun/",s&&(n=s[Gun._.soul])){var f=s["."];o=Gun.obj.ify(e.getItem(i.prefix+n)||null)||r[n]||u,o&&f&&(o=Gun.state.ify(u,f,Gun.state.is(o,f),o[f],n)),(o||Gun.obj.empty(a.back("opt.peers")))&&a.on("in",{"@":t["#"],put:Gun.graph.node(o),how:"lS"})}})}})(t,"./adapters/localStorage"),t(function(){function n(t){var n=a,o=this,i=t.wire||e(t,o);return o.wsp&&o.wsp.count++,i?i.readyState===i.OPEN?void i.send(n):void(t.queue=t.queue||[]).push(n):void 0}function o(t,n,e){if(e&&t){try{t=JSON.parse(t.data||t)}catch(i){}if(t instanceof Array)for(var r,u=0;r=t[u++];)o(r,n,e);else e.wsp&&1===e.wsp.count&&((t.body||t).wsp=f),e.gun.on("in",t.body||t)}}function e(t,e){if(t&&t.url){var s=t.url.replace("http","ws"),f=t.wire=new u(s);return f.onclose=function(){i(t,e)},f.onerror=function(n){i(t,e),n&&"ECONNREFUSED"===n.code},f.onopen=function(){var o=t.queue;t.queue=[],r.obj.map(o,function(o){a=o,n.call(e,t)})},f.onmessage=function(n){o(n,t,e)},f}}function i(t,n){clearTimeout(t.defer),t.defer=setTimeout(function(){e(t,n)},2e3)}var r=t("./core");if("undefined"==typeof JSON)throw new Error("Gun depends on JSON. Please load it first:\najax.cdnjs.com/ajax/libs/json2/20110223/json2.js");var u;if("undefined"!=typeof window){u=window.WebSocket||window.webkitWebSocket||window.mozWebSocket;var a,s,f=function(){};r.on("out",function(t){this.to.next(t);var o=t.gun._.root._,e=o.wsp||(o.wsp={});if(!t.wsp||1!==e.count){if(a=JSON.stringify(t),o.udrain)return void o.udrain.push(a);o.udrain=[],clearTimeout(s),s=setTimeout(function(){if(o.udrain){var t=o.udrain;o.udrain=null,a=JSON.stringify(t),r.obj.map(o.opt.peers,n,o)}},1),e.count=0,r.obj.map(o.opt.peers,n,o)}})}})(t,"./polyfill/request")}(); \ No newline at end of file diff --git a/src/HAM.js b/src/HAM.js index fc936501..90aa82c9 100644 --- a/src/HAM.js +++ b/src/HAM.js @@ -31,7 +31,7 @@ function HAM(machineState, incomingState, currentState, incomingValue, currentVa return {converge: true, incoming: true}; } } - return {err: "you have not properly handled recursion through your data or filtered it as JSON"}; + return {err: "Invalid CRDT Data: "+ incomingValue +" to "+ currentValue +" at "+ incomingState +" to "+ currentState +"!"}; } if(typeof JSON === 'undefined'){ throw new Error( diff --git a/src/adapters/localStorage.js b/src/adapters/localStorage.js index 191d2e7f..ad3e0b74 100644 --- a/src/adapters/localStorage.js +++ b/src/adapters/localStorage.js @@ -5,24 +5,48 @@ var root, noop = function(){}, u; if(typeof window !== 'undefined'){ root = window } var store = root.localStorage || {setItem: noop, removeItem: noop, getItem: noop}; +var check = {}, dirty = {}, async = {}, count = 0, max = 10000, wait; + Gun.on('put', function(at){ var err, id, opt, root = at.gun._.root; this.to.next(at); (opt = {}).prefix = (at.opt || opt).prefix || at.gun.back('opt.prefix') || 'gun/'; - Gun.graph.is(at.put, function(node, soul, map){ - var keys = Gun.obj.ify(store.getItem(opt.prefix + soul+'_')||{}); - map(function(val, key){ - keys[key] = 1; - var state = Gun.state.is(node, key); - // #soul.field=val>state - try{store.setItem(opt.prefix + soul+key, JSON.stringify([val,state])); + var graph = root._.graph; + + Gun.obj.map(at.put, function(node, soul){ + async[soul] = graph[soul] || node; + }); + count += 1; + check[at['#']] = root; + function save(){ + clearTimeout(wait); + var ack = check; + var all = async; + count = 0; + wait = false; + check = {}; + async = {}; + Gun.obj.map(all, function(node, soul){ + // Since localStorage only has 5MB, it is better that we keep only + // the data that the user is currently interested in. + node = graph[soul] || all[soul]; + try{store.setItem(opt.prefix + soul, JSON.stringify(node)); }catch(e){ err = e || "localStorage failure" } }); - try{store.setItem(opt.prefix + soul+'_', JSON.stringify(keys)); - }catch(e){ err = e || "localStorage failure" } - }); - if(Gun.obj.empty(at.gun.back('opt.peers'))){ - Gun.on.ack(at, {err: err, ok: 0}); // only ack if there are no peers. + if(!Gun.obj.empty(at.gun.back('opt.peers'))){ return } // only ack if there are no peers. + Gun.obj.map(ack, function(root, id){ + root.on('in', { + '@': id, + err: err, + ok: 0 // localStorage isn't reliable, so make its `ok` code be a low number. + }); + }); } + if(count >= max){ // goal is to do 10K inserts/second. + return save(); + } + if(wait){ return } + clearTimeout(wait); + wait = setTimeout(save, 1000); }); Gun.on('get', function(at){ this.to.next(at); @@ -30,18 +54,17 @@ Gun.on('get', function(at){ //setTimeout(function(){ (opt = at.opt || {}).prefix = opt.prefix || at.gun.back('opt.prefix') || 'gun/'; if(!lex || !(soul = lex[Gun._.soul])){ return } + //if(0 >= at.cap){ return } var field = lex['.']; - if(field){ - if(data = Gun.obj.ify(store.getItem(opt.prefix + soul+field)||null)||u){ - data = Gun.state.ify(u, field, data[1], data[0], soul); - } - } else { - Gun.obj.map(Gun.obj.ify(store.getItem(opt.prefix + soul+'_')), function(v,field){ - v = Gun.obj.ify(store.getItem(opt.prefix + soul+field)||{}); - data = Gun.state.ify(data, field, v[1], v[0], soul); - }); + + data = Gun.obj.ify(store.getItem(opt.prefix + soul) || null) || async[soul] || u; + if(data && field){ + data = Gun.state.ify(u, field, Gun.state.is(data, field), data[field], soul); } - gun.back(-1).on('in', {'@': at['#'], put: Gun.graph.node(data)}); + if(!data && !Gun.obj.empty(gun.back('opt.peers'))){ // if data not found, don't ack if there are peers. + return; // Hmm, what if we have peers but we are disconnected? + } + gun.on('in', {'@': at['#'], put: Gun.graph.node(data), how: 'lS'}); //},11); }); \ No newline at end of file diff --git a/src/chain.js b/src/chain.js index 8e640e67..46a28ba2 100644 --- a/src/chain.js +++ b/src/chain.js @@ -38,7 +38,7 @@ function output(at){ if(!at.gun._){ return } (at.gun._).on('out', { get: {'#': rel, '.': get}, - '#': Gun.on.ask(Gun.HAM.synth, at.gun), + '#': root._.ask(Gun.HAM.synth, at.gun), gun: at.gun }); return; @@ -61,7 +61,7 @@ function output(at){ if(!at.gun._){ return } (at.gun._).on('out', { get: {'#': cat.soul, '.': get}, - '#': Gun.on.ask(Gun.HAM.synth, at.gun), + '#': root._.ask(Gun.HAM.synth, at.gun), gun: at.gun }); return; @@ -86,7 +86,7 @@ function output(at){ }); } if(cat.ack){ - if(!obj_has(cat, 'put')){ + if(!obj_has(cat, 'put')){ // u !== cat.put instead? return; } } @@ -94,7 +94,8 @@ function output(at){ if(cat.soul){ cat.on('out', { get: {'#': cat.soul}, - '#': Gun.on.ask(Gun.HAM.synth, cat.gun), + '#': root._.ask(Gun.HAM.synth, cat.gun), + gun: cat.gun }); return; } @@ -114,7 +115,7 @@ function output(at){ function input(at){ at = at._ || at; var ev = this, cat = this.as, gun = at.gun, coat = gun._, change = at.put, back = cat.back._ || empty, rel, tmp; - if(0 > cat.ack && at.via && !Gun.val.rel.is(change)){ // for better behavior? + if(0 > cat.ack && !Gun.val.rel.is(change)){ // for better behavior? cat.ack = 1; } if(cat.get && at.get !== cat.get){ @@ -138,6 +139,7 @@ function input(at){ return; } if(cat.soul){ + if(cat.root._.now){ at = obj_to(at, {put: change = coat.put}) } // TODO: Ugly hack for uncached synchronous maps. ev.to.next(at); echo(cat, at, ev); obj_map(change, map, {at: at, cat: cat}); @@ -255,7 +257,7 @@ function ask(cat, soul){ tmp.ack = tmp.ack || -1; tmp.on('out', { get: {'#': soul}, - '#': Gun.on.ask(Gun.HAM.synth, tmp.gun), + '#': cat.root._.ask(Gun.HAM.synth, tmp.gun), gun: tmp.gun }); return; @@ -263,7 +265,7 @@ function ask(cat, soul){ obj_map(cat.next, function(gun, key){ (gun._).on('out', { get: {'#': soul, '.': key}, - '#': Gun.on.ask(Gun.HAM.synth, tmp.gun), + '#': cat.root._.ask(Gun.HAM.synth, tmp.gun), gun: gun }); }); diff --git a/src/experiment.js b/src/experiment.js new file mode 100644 index 00000000..af3b0be7 --- /dev/null +++ b/src/experiment.js @@ -0,0 +1,169 @@ + +return; +var Gun = require('./root'); +var onto = require('./onto'); +function Chain(back){ + var at = this._ = {back: back, on: onto, $: this, next: {}}; + at.root = back? back.root : at; + at.on('in', input, at); + at.on('out', output, at); +} +var chain = Chain.prototype; +chain.back = function(arg){ var tmp; + if(tmp = this._.back){ + return tmp.$; + } +} +chain.next = function(arg){ + var at = this._, cat; + if(cat = at.next[arg]){ + return cat.$; + } + cat = (new Chain(at)._); + at.next[arg] = cat; + cat.key = arg; + return cat.$; +} +chain.get = function(arg){ + if(typeof arg == 'string'){ + var at = this._, cat; + if(cat = at.next[arg]){ + return cat.$; + } + cat = (this.next(arg)._); + if(at.get || at === at.root){ + cat.get = arg; + } + return cat.$; + } else { + var at = this._; + var out = {'#': Gun.text.random(), get: {}, cap: 1}; + var to = at.root.on(out['#'], get, {next: arg}) + at.on('in', get, to); + at.on('out', out); + } + return this; +} +function get(env){ + var as = this.as; + if(as.next){ + as.next(env, this); + } +} +chain.map = function(cb){ + var at = this._; + var chain = new Chain(at); + var cat = chain._; + var u; + at.on('in', function(env){ var tmp; + if(!env){ return } + var cat = this.as; + var to = this.to; + if(tmp = env.put){ + to.next(env); + Gun.obj.map(tmp, function(data, key){ + if('_' == key){ return } + if(cb){ + data = cb(data, key); + if(u === data){ return } + } + cat.on('in', Gun.obj.to(env, {put: data})); + }); + } + }, cat); + return chain; +} +function input(env){ var tmp; + if(!env){ return } + var cat = this.as; + var to = this.to; + if(tmp = env.put){ + if(tmp && tmp['#'] && (tmp = Gun.val.rel.is(tmp))){ + //input.call(this, Gun.obj.to(env, {put: cat.root.put[tmp]})); + return; + } + cat.put = tmp; + to.next(env); + var next = cat.next; + Gun.obj.map(tmp, function(data, key){ + if(!(key = next[key])){ return } + key.on('in', Gun.obj.to(env, {put: data})) + }); + } +} +function output(env){ var tmp; + var u; + if(!env){ return } + var cat = this.as; + var to = this; + if(!cat.back){ + env.test = true; + env.gun = cat.root.$; + Gun.on('out', env); + return; + } + if(tmp = env.get){ + if(cat.get){ + env = Gun.obj.to(env, {get: {'#': cat.get, '.': tmp}}); + } else + if(cat.key){ + env = Gun.obj.to(env, {get: Gun.obj.put({}, cat.key, tmp)}); + } else { + env = Gun.obj.to(env, {get: {'*': tmp}}) + } + } + cat.back.on('out', env); +} +chain.val = function(cb, opt){ + var at = this._; + if(cb){ + if(opt){ + } else { + if(at.val){ + cb(at.put, at.get, at); + } + } + this.get(function(env, ev){ + cb(env.put, env.get, env); + }); + } +} + + + + +var graph = { + app: {_:{'#':'app'}, + foo: {_:{'#':'foo'}, + bar: {'#': 'asdf'}, + rab: {'#': 'fdsa'} + }/*, + oof: {_:{'#':'oof'}, + bar: {bat: "really"}, + rab: {bat: "nice!"} + }*/ + }, + asdf: {_:{'#': 'asdf'}, baz: "hello world!"}, + fdsa: {_:{'#': 'fdsa'}, baz: "world hello!"} + } +Gun.on('out', function(env){ + if(!env.test){ return } + setTimeout(function(){ + console.log("reply", env.get); + env.gun._.on('in', {'@': env['#'], + put: Gun.graph.node(graph[env.get['#']]) + }); + return; + env.gun._.on('in', {put: graph, '@': env['#']}); + },100); +}); +setTimeout(function(){ + + //var c = new Chain(), u; + //c.get('app').map().map(x => x.bat? {baz: x.bat} : u).get('baz').val(function(data, key, env){ + // console.log("envelope", env); + //}); + +},1000); + + \ No newline at end of file diff --git a/src/get.js b/src/get.js index 27d96718..d5713dc1 100644 --- a/src/get.js +++ b/src/get.js @@ -13,9 +13,9 @@ Gun.chain.get = function(key, cb, as){ var gun = this, at = gun._; as = cb || {}; as.use = key; - as.out = as.out || {}; + as.out = as.out || {cap: 1}; as.out.get = as.out.get || {}; - (at.root._).now = true; + '_' != at.get && ((at.root._).now = true); // ugly hack for now. at.on('in', use, as); at.on('out', as.out); (at.root._).now = false; diff --git a/src/index.js b/src/index.js index 2090adc9..bac20429 100644 --- a/src/index.js +++ b/src/index.js @@ -35,7 +35,7 @@ module.exports = Gun; if(!val_is(cv) && u !== cv){ return true } // Undefined is okay since a value might not exist on both nodes. // it is true that this is an invalid HAM comparison. var HAM = Gun.HAM(machine, is, cs, iv, cv); if(HAM.err){ - console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", HAM.err); // this error should never happen. + console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", field, HAM.err); // this error should never happen. return; } if(HAM.state || HAM.historical || HAM.current){ // TODO: BUG! Not implemented. @@ -103,13 +103,11 @@ module.exports = Gun; } Gun.HAM.synth = function(at, ev, as){ var gun = this.as || as; var cat = gun._, root = cat.root._, put = {}, tmp; - //if(cat.ack){ - // cat.ack = 1; - //} if(!at.put){ //if(obj_has(cat, 'put')){ return } if(cat.put !== u){ return } cat.on('in', { + //root.ack(at['@'], { get: cat.get, put: cat.put = u, gun: gun, @@ -122,6 +120,9 @@ module.exports = Gun; put[soul] = Gun.HAM.delta(graph[soul], node, {graph: graph}); // TODO: PERF! SEE IF WE CAN OPTIMIZE THIS BY MERGING UNION INTO DELTA! graph[soul] = Gun.HAM.union(graph[soul], node) || graph[soul]; }, root); + if(at.gun !== root.gun){ + put = at.put; + } // TODO: PERF! Have options to determine if this data should even be in memory on this peer! obj_map(put, function(node, soul){ var root = this, next = root.next || (root.next = {}), gun = next[soul] || (next[soul] = root.gun.get(soul)), coat = (gun._); diff --git a/src/init.js b/src/init.js deleted file mode 100644 index 9d0714b9..00000000 --- a/src/init.js +++ /dev/null @@ -1,7 +0,0 @@ - -var Gun = require('./core'); -Gun.chain.init = function(){ // TODO: DEPRECATE? - (this._.opt = this._.opt || {}).init = true; - return this.back(-1).put(Gun.node.ify({}, this._.get), null, this._.get); -} - \ No newline at end of file diff --git a/src/map.js b/src/map.js index 85282150..949e04ff 100644 --- a/src/map.js +++ b/src/map.js @@ -24,7 +24,7 @@ Gun.chain.map = function(cb, opt, t){ } function map(at){ if(!at.put || Gun.val.is(at.put)){ return } - if(this.as.val){ this.off() } + if(this.as.val){ this.off() } // TODO: Ugly hack! obj_map(at.put, each, {cat: this.as, gun: at.gun}); this.to.next(at); } diff --git a/src/on.js b/src/on.js index 6e9cfda5..c7012e3e 100644 --- a/src/on.js +++ b/src/on.js @@ -54,15 +54,15 @@ function ok(at, ev){ var opt = this; Gun.chain.val = function(cb, opt){ var gun = this, at = gun._, data = at.put; - if(0 < at.ack && u !== data && cb){ - cb.call(gun, data, at.get); + if(0 < at.ack && u !== data){ + (cb || noop).call(gun, data, at.get); return gun; } if(cb){ (opt = opt || {}).ok = cb; opt.cat = at; gun.get(val, {as: opt}); - opt.async = at.stun? 1 : true; + opt.async = true; //opt.async = at.stun? 1 : true; } else { Gun.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 chain = gun.chain(); @@ -87,7 +87,8 @@ function val(at, ev, to){ data = tmp.put; } if(ev.wait){ clearTimeout(ev.wait) } - if(!to && (!(0 < coat.ack) || ((true === opt.async) && 0 !== opt.wait))){ + //if(!to && (!(0 < coat.ack) || ((true === opt.async) && 0 !== opt.wait))){ + if(!opt.async){ ev.wait = setTimeout(function(){ val.call({as:opt}, at, ev, ev.wait || 1) }, opt.wait || 99); @@ -128,5 +129,5 @@ Gun.chain.off = function(){ } var obj = Gun.obj, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to; var rel = Gun.val.rel; -var empty = {}, u; +var empty = {}, noop = function(){}, u; \ No newline at end of file diff --git a/src/polyfill/request.js b/src/polyfill/request.js index d37998dc..874dc72a 100644 --- a/src/polyfill/request.js +++ b/src/polyfill/request.js @@ -1,7 +1,6 @@ var Gun = require('./core'); -// Check for stone-age browsers. if (typeof JSON === 'undefined') { throw new Error( 'Gun depends on JSON. Please load it first:\n' + @@ -9,382 +8,97 @@ if (typeof JSON === 'undefined') { ); } -function Client (url, options, wscOptions ) { - if (!(this instanceof Client)) { - return new Client(url, options, wscOptions); +var WebSocket; +if(typeof window !== 'undefined'){ + WebSocket = window.WebSocket || window.webkitWebSocket || window.mozWebSocket; +} else { + return; +} +var message, count = 0, noop = function(){}, wait; + +Gun.on('out', function(at){ + this.to.next(at); + var cat = at.gun._.root._, wsp = cat.wsp || (cat.wsp = {}); + if(at.wsp && 1 === wsp.count){ return } // if the message came FROM the only peer we are connected to, don't echo it back. + message = JSON.stringify(at); + //if(++count){ console.log("msg OUT:", count, Gun.obj.ify(message)) } + if(cat.udrain){ + cat.udrain.push(message); + return; } - - this.url = Client.formatURL(url); - this.socket = null; - this.queue = []; - this.sid = Gun.text.random(10); - - this.on = Gun.on; - - this.options = options || {}; - this.options.wsc = wscOptions; - this.resetBackoff(); -} - -Client.prototype = { - constructor: Client, - - drainQueue: function () { - var queue = this.queue; - var client = this; - - // Reset the queue. - this.queue = []; - - // Send each message. - queue.forEach(function (msg) { - client.send(msg); - }); - - return queue.length; - }, - - connect: function () { - var client = this; - var socket = new Client.WebSocket(this.url, this.options.wsc.protocols, this.options.wsc ); - this.socket = socket; - - // Forward messages into the emitter. - socket.addEventListener('message', function (msg) { - client.on('message', msg); - }); - - // Reconnect on close events. - socket.addEventListener('close', function () { - client.scheduleReconnect(); - }); - - // Send the messages in the queue. - this.ready(function () { - client.drainQueue(); - }); - - return socket; - }, - - resetBackoff: function () { - var backoff = this.options; - - this.backoff = { - time: backoff.time || 100, - max: backoff.max || 2000, - factor: backoff.factor || 2 - }; - - return this.backoff; - }, - - nextBackoff: function () { - var backoff = this.backoff; - var next = backoff.time * backoff.factor; - var max = backoff.max; - - if (next > max) { - next = max; - } - - return (backoff.time = next); - }, - - // Try to efficiently reconnect. - scheduleReconnect: function () { - var client = this; - var time = this.backoff.time; - this.nextBackoff(); - - setTimeout(function () { - client.connect(); - - client.ready(function () { - client.resetBackoff(); - }); - }, time); - }, - - isClosed: function () { - var socket = this.socket; - - if (!socket) { - return true; - } - - var state = socket.readyState; - - if (state === socket.CLOSING || state === socket.CLOSED) { - return true; - } - - return false; - }, - - ready: function (callback) { - var socket = this.socket; - var state = socket.readyState; - - if (state === socket.OPEN) { - callback(); - return; - } - - if (state === socket.CONNECTING) { - socket.addEventListener('open', callback); - } - }, - - send: function (msg) { - if (this.isClosed()) { - this.queue.push(msg); - - // Will send once connected. - this.connect(); - return false; - } - - var socket = this.socket; - - // Make sure the socket is open. - this.ready(function () { - socket.send(msg); - }); - - return true; - } -}; - -if (typeof window !== 'undefined') { - Client.WebSocket = window.WebSocket || - window.webkitWebSocket || - window.mozWebSocket || - null; -} - -Client.isSupported = !!Client.WebSocket; - -if(!Client.isSupported){ return } // TODO: For now, don't do anything in browsers/servers that don't work. Later, use JSONP fallback and merge with server code? - -// Ensure the protocol is correct. -Client.formatURL = function (url) { - return url.replace(/^http/, 'ws'); -}; - -// Send a message to a group of peers. -Client.broadcast = function (urls, msg) { - var pool = Client.pool; - msg.headers = msg.headers || {}; - - Gun.obj.map(urls, function (options, addr) { - - var url = Client.formatURL(addr); - - var peer = pool[url]; - - var envelope = { - headers: Gun.obj.to(msg.headers, { - 'gun-sid': peer.sid - }), - body: msg.body - }; - - var serialized = Gun.text.ify(envelope); - - peer.send(serialized); - }); - -}; - -// A map of URLs to client instances. -Client.pool = {}; - -// Close all WebSockets when the window closes. -if (typeof window !== 'undefined') { - window.addEventListener('unload', function () { - Gun.obj.map(Client.pool, function (client) { - if (client.isClosed()) { - return; - } - - client.socket.close(); - }); - }); -} - -// Define client instances as gun needs them. -// Sockets will not be opened until absolutely necessary. -Gun.on('opt', function (ctx) { - this.to.next(ctx); - - var gun = ctx.gun; - var peers = gun.back('opt.peers') || {}; - - Gun.obj.map(peers, function (options, addr) { - var url = Client.formatURL(addr); - - // Ignore clients we've seen before. - if (Client.pool.hasOwnProperty(url)) { - return; - } - - var client = new Client(url, options.backoff, gun.back('opt.wsc') || {protocols:null}); - - // Add it to the pool. - Client.pool[url] = client; - - // Listen to incoming messages. - client.on('message', function (msg) { - var data; - - try { - data = Gun.obj.ify(msg.data); - } catch (err) { - // Invalid message, discard it. - return; - } - - if (!data || !data.body) { - return; - } - - gun.on('in', data.body); - }); - }); + cat.udrain = []; + clearTimeout(wait); + wait = setTimeout(function(){ + if(!cat.udrain){ return } + var tmp = cat.udrain; + cat.udrain = null; + message = JSON.stringify(tmp); + Gun.obj.map(cat.opt.peers, send, cat); + },1); + wsp.count = 0; + Gun.obj.map(cat.opt.peers, send, cat); }); -function request (peers, ctx) { - if (Client.isSupported) { - Client.broadcast(peers, ctx); +function send(peer){ + var msg = message, cat = this; + var wire = peer.wire || open(peer, cat); + if(cat.wsp){ cat.wsp.count++ } + if(!wire){ return } + if(wire.readyState === wire.OPEN){ + wire.send(msg); + return; } + (peer.queue = peer.queue || []).push(msg); } -// Broadcast the messages. -Gun.on('out', function (ctx) { - this.to.next(ctx); - var gun = ctx.gun; - var peers = gun.back('opt.peers') || {}; - var headers = gun.back('opt.headers') || {}; - // Validate. - if (Gun.obj.empty(peers)) { +function receive(msg, peer, cat){ + if(!cat || !msg){ return } + try{msg = JSON.parse(msg.data || msg); + }catch(e){} + if(msg instanceof Array){ + var i = 0, m; + while(m = msg[i++]){ + receive(m, peer, cat); + } return; } + //if(++count){ console.log("msg in:", count, msg.body || msg) } + if(cat.wsp && 1 === cat.wsp.count){ (msg.body || msg).wsp = noop } // If there is only 1 client, just use noop since it doesn't matter. + cat.gun.on('in', msg.body || msg); +} - request(peers, {body: ctx, headers: headers}); -}); - -request.jsonp = function (opt, cb) { - request.jsonp.ify(opt, function (url) { - if (!url) { - return; - } - request.jsonp.send(url, function (err, reply) { - cb(err, reply); - request.jsonp.poll(opt, reply); - }, opt.jsonp); - }); -}; -request.jsonp.send = function (url, cb, id) { - var js = document.createElement('script'); - js.src = url; - js.onerror = function () { - (window[js.id] || function () {})(null, { - err: 'JSONP failed!' - }); +function open(peer, as){ + if(!peer || !peer.url){ return } + var url = peer.url.replace('http', 'ws'); + var wire = peer.wire = new WebSocket(url); + wire.onclose = function(){ + reconnect(peer, as); }; - window[js.id = id] = function (res, err) { - cb(err, res); - cb.id = js.id; - js.parentNode.removeChild(js); - delete window[cb.id]; + wire.onerror = function(error){ + reconnect(peer, as); + if(!error){ return } + if(error.code === 'ECONNREFUSED'){ + //reconnect(peer, as); + } }; - js.async = true; - document.getElementsByTagName('head')[0].appendChild(js); - return js; -}; -request.jsonp.poll = function (opt, res) { - if (!opt || !opt.base || !res || !res.headers || !res.headers.poll) { - return; - } - var polls = request.jsonp.poll.s = request.jsonp.poll.s || {}; - polls[opt.base] = polls[opt.base] || setTimeout(function () { - var msg = { - base: opt.base, - headers: { pull: 1 } - }; - - request.each(opt.headers, function (header, name) { - msg.headers[name] = header; + wire.onopen = function(){ + var queue = peer.queue; + peer.queue = []; + Gun.obj.map(queue, function(msg){ + message = msg; + send.call(as, peer); }); + } + wire.onmessage = function(msg){ + receive(msg, peer, as); + }; + return wire; +} - request.jsonp(msg, function (err, reply) { - delete polls[opt.base]; - - var body = reply.body || []; - while (body.length && body.shift) { - var res = reply.body.shift(); - if (res && res.body) { - request.createServer.ing(res, function () { - request(opt.base, null, null, res); - }); - } - } - }); - }, res.headers.poll); -}; -request.jsonp.ify = function (opt, cb) { - var uri = encodeURIComponent, query = '?'; - if (opt.url && opt.url.pathname) { - query = opt.url.pathname + query; - } - query = opt.base + query; - request.each((opt.url || {}).query, function (value, key) { - query += (uri(key) + '=' + uri(value) + '&'); - }); - if (opt.headers) { - query += uri('`') + '=' + uri( - JSON.stringify(opt.headers) - ) + '&'; - } - if (request.jsonp.max < query.length) { - return cb(); - } - var random = Math.floor(Math.random() * (0xffff + 1)); - query += (uri('jsonp') + '=' + uri(opt.jsonp = 'P' + random)); - if (opt.body) { - query += '&'; - var w = opt.body, wls = function (w, l, s) { - return uri('%') + '=' + uri(w+'-'+(l||w)+'/'+(s||w)) + '&' + uri('$') + '='; - } - if (typeof w != 'string') { - w = JSON.stringify(w); - query += uri('^') + '=' + uri('json') + '&'; - } - w = uri(w); - var i = 0, l = w.length - , s = request.jsonp.max - (query.length + wls(l.toString()).length); - if (s < 0){ - return cb(); - } - while (w) { - cb(query + wls(i, (i += s), l) + w.slice(0, i)); - w = w.slice(i); - } - } else { - cb(query); - } -}; -request.jsonp.max = 2000; -request.each = function (obj, cb, as) { - if (!obj || !cb) { - return; - } - for (var key in obj) { - if (obj.hasOwnProperty(key)) { - cb.call(as, obj[key], key); - } - } -}; -module.exports = Client; +function reconnect(peer, as){ + clearTimeout(peer.defer); + peer.defer = setTimeout(function(){ + open(peer, as); + }, 2 * 1000); +} \ No newline at end of file diff --git a/src/put.js b/src/put.js index 9debaa3a..4187105c 100644 --- a/src/put.js +++ b/src/put.js @@ -4,7 +4,7 @@ Gun.chain.put = function(data, cb, as){ // #soul.field=value>state // ~who#where.where=what>when@was // TODO: BUG! Put probably cannot handle plural chains! - var gun = this, root = (gun._).root, tmp; + var gun = this, at = (gun._), root = at.root, tmp; as = as || {}; as.data = data; as.gun = as.gun || gun; @@ -13,9 +13,12 @@ Gun.chain.put = function(data, cb, as){ } else { as.ack = cb; } + if(at.soul){ + as.soul = at.soul; + } if(as.soul || root === gun){ if(!obj_is(as.data)){ - (opt.any||noop).call(opt.as || gun, as.out = {err: Gun.log("No field to put", (typeof as.data), '"' + as.data + '" on!')}); + (as.ack||noop).call(as, as.out = {err: Gun.log("Data saved to the root level of the graph must be a node (an object), not a", (typeof as.data), 'of "' + as.data + '"!')}); if(as.res){ as.res() } return gun; } @@ -27,12 +30,17 @@ Gun.chain.put = function(data, cb, as){ if(Gun.is(data)){ data.get(function(at,ev){ev.off(); var s = Gun.node.soul(at.put); - if(!s){Gun.log("Can only save a node, not a property.");return} + if(!s){Gun.log("The reference you are saving is a", typeof at.put, '"'+ as.put +'", not a node (object)!');return} gun.put(Gun.val.rel.ify(s), cb, as); }); return gun; } - as.ref = as.ref || (root === (tmp = (gun._).back))? gun : tmp; + as.ref = as.ref || (root === (tmp = at.back))? gun : tmp; + if(as.ref._.soul && Gun.val.is(as.data) && at.get){ + as.data = obj_put({}, at.get, as.data); + as.ref.put(as.data, as.soul, as); + return gun; + } as.ref.get('_').get(any, {as: as}); if(!as.out){ // TODO: Perf idea! Make a global lock, that blocks everything while it is on, but if it is on the lock it does the expensive lookup to see if it is a dependent write or not and if not then it proceeds full speed. Meh? For write heavy async apps that would be terrible. @@ -48,7 +56,7 @@ function ify(as){ env.soul = as.soul; as.graph = Gun.graph.ify(as.data, env, as); if(env.err){ - (as.ack||noop).call(opt.as || as.gun, as.out = {err: Gun.log(env.err)}); + (as.ack||noop).call(as, as.out = {err: Gun.log(env.err)}); if(as.res){ as.res() } return; } @@ -59,8 +67,9 @@ function batch(){ var as = this; if(!as.graph || obj_map(as.stun, no)){ return } (as.res||iife)(function(){ (as.ref._).on('out', { + cap: 3, gun: as.ref, put: as.out = as.env.graph, opt: as.opt, - '#': Gun.on.ask(function(ack){ this.off(); // One response is good enough for us currently. Later we may want to adjust this. + '#': as.gun.back(-1)._.ask(function(ack){ this.off(); // One response is good enough for us currently. Later we may want to adjust this. if(!as.ack){ return } as.ack(ack, this); }, as.opt) @@ -75,7 +84,7 @@ function map(v,f,n, at){ var as = this; var path = at.path, ref = as.ref, opt = as.opt; var i = 0, l = path.length; for(i; i < l; i++){ - ref = ref.get(path[i]); // TODO: API change! We won't need 'path: true' anymore. + ref = ref.get(path[i]); } if(as.not || Gun.node.soul(at.obj)){ at.soul(Gun.node.soul(at.obj) || ((as.opt||{}).uuid || as.gun.back('opt.uuid') || Gun.text.random)()); @@ -106,7 +115,7 @@ function any(at, ev){ var cat = (at.gun._.back._), data = cat.put, opt = as.opt||{}, root, tmp; ev.off(); if(as.ref !== as.gun){ - tmp = (as.gun._).get; + tmp = (as.gun._).get || cat.get; if(!tmp){ // TODO: Handle console.log("Please report this as an issue! Put.no.get"); // TODO: BUG!?? return; @@ -129,10 +138,10 @@ function any(at, ev){ } if(!as.not && !(as.soul = Gun.node.soul(data))){ if(as.path && obj_is(as.data)){ // Apparently necessary - as.soul = (opt.uuid || as.gun.back('opt.uuid') || Gun.text.random)(); + as.soul = (opt.uuid || cat.root._.opt.uuid || Gun.text.random)(); } else { //as.data = obj_put({}, as.gun._.get, as.data); - as.soul = at.soul; + as.soul = at.soul || cat.soul || (opt.uuid || cat.root._.opt.uuid || Gun.text.random)(); } } as.ref.put(as.data, as.soul, as); diff --git a/src/root.js b/src/root.js index e14ca36c..cfa622c3 100644 --- a/src/root.js +++ b/src/root.js @@ -37,27 +37,43 @@ Gun._ = { // some reserved key words, these are not the only ones. at.root = at.root || at.gun; at.graph = at.graph || {}; at.dup = at.dup || new Gun.dup; + at.ask = Gun.on.ask; + at.ack = Gun.on.ack; var gun = at.gun.opt(at.opt); if(!at.once){ - at.on('in', input, at); - at.on('out', output, at); + at.on('in', root, at); + at.on('out', root, at); } at.once = 1; return gun; } - function output(at){ - //console.log("add to.next(at)!"); // TODO: BUG!!!! - var cat = this.as, gun = cat.gun, tmp; - // TODO: BUG! Outgoing `get` to read from in memory!!! - if(at.get && get(at, cat)){ return } - cat.on('in', obj_to(at, {gun: cat.gun})); // TODO: PERF! input now goes to output so it would be nice to reduce the circularity here for perf purposes. - if(at['#']){ + function root(at){ + //console.log("add to.next(at)"); // TODO: BUG!!! + var ev = this, cat = ev.as, coat; + if(!at.gun){ at.gun = cat.gun } + if(!at['#'] && at['@']){ + at['#'] = Gun.text.random(); // TODO: Use what is used other places instead. + // TODO: BUG! For multi-instances, the "ack" system is globally shared, but it shouldn't be. + if(cat.ack(at['@'], at)){ return } // TODO: Consider not returning here, maybe, where this would let the "handshake" on sync occur for Holy Grail? cat.dup.track(at['#']); + Gun.on('out', obj_to(at, {gun: cat.gun})); + return; } - if(!at.gun){ - at = obj_to(at, {gun: gun}); + if(at['#'] && cat.dup.check(at['#'])){ return } + cat.dup.track(at['#']); + if(cat.ack(at['@'], at)){ return } + //cat.ack(at['@'], at); + coat = obj_to(at, {gun: cat.gun}); + if(at.get){ + if(!get(at, cat)){ + Gun.on('get', coat); + } } - Gun.on('out', at); // TODO: BUG! PERF? WARNING!!! A in-memory `put` triggers an out with an existing ID which reflows into IN which at the end also goes Gun OUT, and then this scope/function resumes and it triggers OUT again! + if(at.put){ + Gun.HAM.synth(at, ev, cat.gun); // TODO: Clean up, just make it part of on('put')! + Gun.on('put', coat); + } + Gun.on('out', coat); } function get(at, cat){ var soul = at.get[_soul], node = cat.graph[soul], field = at.get[_field], tmp; @@ -68,50 +84,36 @@ Gun._ = { // some reserved key words, these are not the only ones. tmp = Gun.obj.put(Gun.node.soul.ify({}, soul), field, node[field]); node = Gun.state.ify(tmp, field, Gun.state.is(node, field)); } - as.on('in', { - put: node, // TODO: BUG! Clone node! - get: as.soul, + //if(at.gun === cat.gun){ + node = Gun.graph.node(node); // TODO: BUG! Clone node? + //} else { + // cat = (at.gun._); + //} + tmp = as.ack; + cat.on('in', { + '@': at['#'], + how: 'mem', + put: node, gun: as.gun }); - if(0 < as.ack){ + if(0 < tmp){ return true; } } - function input(at){ - //console.log("add to.next(at)"); // TODO: BUG!!! - var ev = this, cat = ev.as; - if(!at.gun){ at.gun = cat.gun } - if(!at['#'] && at['@']){ - at['#'] = Gun.text.random(); // TODO: Use what is used other places instead. - if(Gun.on.ack(at['@'], at)){ return } // TODO: Consider not returning here, maybe, where this would let the "handshake" on sync occur for Holy Grail? - cat.dup.track(at['#']); - cat.on('out', at); - return; - } - if(at['#'] && cat.dup.check(at['#'])){ return } - cat.dup.track(at['#']); - if(Gun.on.ack(at['@'], at)){ return } - if(at.put){ - Gun.HAM.synth(at, ev, cat.gun); // TODO: Clean up, just make it part of on('put')! - Gun.on('put', at); - } - if(at.get){ Gun.on('get', at) } - Gun.on('out', at); - } }()); ;(function(){ - var ask = Gun.on.ask = function(cb, as){ + Gun.on.ask = function(cb, as){ + if(!this.on){ return } var id = Gun.text.random(); - if(cb){ ask.on(id, cb, as) } + if(cb){ this.on(id, cb, as) } return id; } - ask.on = Gun.on; Gun.on.ack = function(at, reply){ - if(!at || !reply || !ask.on){ return } + if(!at || !reply || !this.on){ return } var id = at['#'] || at; - if(!ask.tag || !ask.tag[id]){ return } - ask.on(id, reply); + if(!this.tag || !this.tag[id]){ return } + this.on(id, reply); return true; } }());