diff --git a/gun.min.js b/gun.min.js index 8a5406fa..0f48f01c 100644 --- a/gun.min.js +++ b/gun.min.js @@ -1 +1 @@ -!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);var o=(n=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){var e=!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;e=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;e=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){if(!(t.indexOf(n)>=0))return!0;e=!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){if(!(t.indexOf(n)<0))return!0;e=!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;e=!0}if(n.obj.has(o,"<")){if(!(to?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])}},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){if(t)return t[n]=null,delete t[n],t},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||void 0}n.obj.empty=function(n,o){return!n||!a(n,t,{n:o})}}(),function(){function t(n,o){if(2===arguments.length)return t.r=t.r||{},void(t.r[n]=o);t.r=t.r||[],t.r.push(n)}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,g,d,h=0,v=o(s);if(t.r=null,i&&r(a)&&(g=i(a),d=!0),e(a)||g)for(l=(g||a).length;h",o.drift=0,o.is=function(t,n,e){var i=n&&t&&t[k]&&t[k][o._]||e;if(i)return b(i=i[n])?i:-1/0},o.lex=function(){return o().toString(36).replace(".","")},o.ify=function(t,n,e,r,u){if(!t||!t[k]){if(!u)return;t=i.soul.ify(t,u)}var a=g(t[k],o._);return l!==n&&n!==k&&(b(e)&&(a[n]=e),l!==r&&(t[n]=r)),t},o.to=function(t,n,e){var r=t[n];return h(r)&&(r=_(r)),o.ify(e,n,o.is(t,n),r,i.soul(t))},function(){function t(t,n){k!==n&&o.ify(this.o,n,this.s)}o.map=function(n,e,i){var r,u=h(u=n||e)?u:null;return n=m(n=n||e)?n:null,u&&!n?(e=b(e)?e:o(),u[k]=u[k]||{},v(u,t,{o:u,s:e}),u):(i=i||h(e)?e:r,e=b(e)?e:o(),function(o,u,a,s){if(!n)return t.call({o:a,s:e},o,u),o;n.call(i||this||{},o,u,a,s),d(a,u)&&r===a[u]||t.call({o:a,s:e},o,u)})}}();var l,p=e.obj,g=p.as,d=p.has,h=p.is,v=p.map,_=p.copy,b=e.num.is,m=e.fn.is,k=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){if(!t||o!==i.soul(t)||!i.is(t,this.fn,this.as))return!0;this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n))}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return!(!n||!s(n)||l(n))&&!g(n,t,{cb:o,fn:e,as:i})}}(),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,g=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,g)){if(o||(p.node=p.node||r||{},c(n,i._)&&(p.node._=d(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=g.map)&&(l.call(g.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,g)))return}if(!o)return p.node;if(!0===s)return n;if((l=t(g,{obj:n,path:p.path.concat(o)})).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)||(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)(o=e.rel.is(t))?(u=this.opt.seen[o])?this.obj[n]=u:this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt):this.obj[n]=t;else{if(l(t,e.rel._))return;this.obj[n]=d(t)}}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},g(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,g=a.map,d=a.copy;n.exports=r})(t,"./graph"),t(function(n){t("./onto"),n.exports=function(t,n){if(this.on){if(!(t instanceof Function)){if(!t||!n)return;var o=t["#"]||t,e=(this.tag||empty)[o];if(!e)return;return e=this.on(o,n),clearTimeout(e.err),!0}o=n&&n["#"]||Math.random().toString(36).slice(2);if(!t)return o;var i=this.on(o,t,n);return i.err=i.err||setTimeout(function(){i.next({err:"Error: No ACK received yet."}),i.off()},(this.opt||{}).lack||9e3),o}}})(t,"./ask"),t(function(n){var o=t("./type"),e=o.time.is;n.exports=function(t){var n={s:{}};return t=t||{max:1e3,age:9e3},n.check=function(t){return!!n.s[t]&&n.track(t)},n.track=function(i){return n.s[i]=e(),n.to||(n.to=setTimeout(function(){o.obj.map(n.s,function(i,r){t.age>e()-i||o.obj.del(n.s,r)}),n.to=null},t.age)),i},n}})(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=.9,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.on=t("./onto"),i.ask=t("./ask"),i.dup=t("./dup"),i._={node:i.node._,soul:i.val.rel._,state:i.state._,field:".",value:"="},function(){function t(t){var n,o=this.as,e=o.gun;(n=t["#"])||(n=t["#"]=c(9)),o.dup.check(n)||(o.dup.track(n),t=d(t),o.ask(t["@"],t)||(t.get&&i.on.get(t,e),t.put&&i.on.put(t,e)),o.on("out",t))}i.create=function(n){n.root=n.root||n.gun,n.graph=n.graph||{},n.on=n.on||i.on,n.ask=n.ask||i.ask,n.dup=n.dup||i.dup();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(){function t(t,n,o,e){var r=this,u=i.state.is(o,n);if(!u)return r.err="Error: No state on '"+n+"' in node '"+e+"'!";var a=r.graph[e]||m,s=i.state.is(a,n,!0),f=a[n],c=i.HAM(r.machine,u,s,t,f);c.incoming?(r.put[e]=i.state.to(o,n,r.put[e]),(r.diff||(r.diff={}))[e]=i.state.to(o,n,r.diff[e]),r.souls[e]=!0):c.defer&&(r.defer=u<(r.defer||1/0)?u:r.defer)}function n(t,n){var i=this,u=i.gun._,a=(u.next||m)[n];if(a){var s=i.map[n]={put:t,get:n,gun:a},f={ctx:i,msg:s};i.async=!!u.tag.node,i.ack&&(s["@"]=i.ack),h(t,o,f),i.async&&(i.and||u.on("node",function(t){this.to.next(t),t===i.map[t.get]&&(i.souls[t.get]=!1,h(t.put,e,t),h(i.souls,function(t){if(t)return t})||i.c||(i.c=1,this.off(),h(i.map,r,i)))}),i.and=!0,u.on("node",s))}else i.souls[n]=!1}function o(t,n){var o=this.ctx,e=o.graph,r=this.msg,u=r.get,a=r.put,s=r.gun._;e[u]=i.state.to(a,n,e[u]),o.async||(s.put=i.state.to(a,n,s.put))}function e(t,n){var o=this,e=o.put,r=o.gun._;r.put=i.state.to(e,n,r.put)}function r(t,n){t.gun&&(t.gun._.root._.stop={},t.gun._.on("in",t),t.gun._.root._.stop={})}i.on.put=function(o,e){var a=e._,s={gun:e,graph:a.graph,put:{},map:{},souls:{},machine:i.state(),ack:o["@"]};if(i.graph.is(o.put,null,t,s)||(s.err="Error: Invalid graph!"),s.err)return a.on("in",{"@":o["#"],err:i.log(s.err)});h(s.put,n,s),s.async||h(s.map,r,s),u!==s.defer&&setTimeout(function(){i.on.put(o,e)},s.defer-s.machine),s.diff&&a.on("put",d(o,{put:s.diff}))},i.on.get=function(t,n){var o=n._,e=t.get[_],r=o.graph[e],u=t.get[b],a=((o.next||(o.next={}))[e]||m)._;if(!r||!a)return o.on("get",t);if(u){if(!g(r,u))return o.on("get",t);r=i.state.to(r,u)}else r=i.obj.copy(r);r=i.graph.node(r),o.on("in",{"@":t["#"],put:r,gun:n}),o.on("get",t)}}(),i.chain.opt=function(t){t=t||{};var n=this,o=n._,e=t.peers||t;return p(t)||(t={}),p(o.opt)||(o.opt=t),f(e)&&(e=[e]),a(e)&&(e=h(e,function(t,n,o){o(t,{url:t})}),p(o.opt.peers)||(o.opt.peers={}),o.opt.peers=d(e,o.opt.peers)),o.opt.peers=o.opt.peers||{},d(t,o.opt),i.on("opt",o),o.opt.uuid=o.opt.uuid||function(){return v()+c(12)},n};var u,a=i.list.is,s=i.text,f=s.is,c=s.random,l=i.obj,p=l.is,g=l.has,d=l.to,h=l.map,v=(l.copy,i.state.lex),_=i._.soul,b=i._.field,m=(i._.node,i.val.rel.is,{});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),void 0!==e&&(e.exports=i),n.exports=i})(t,"./root"),t(function(n){var o=t("./root");o.chain.back=function(t,n){if(-1===(t=t||1)||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)){if(t instanceof Function){for(var a,s={back:r};(s=s.back)&&(s=s._)&&!(a=t(s,n)););return a}return o.num.is(t)?u.back.back(t-1):this}var f=0,c=t.length,s=u;for(f;f=(n.batch||1e3))return s();o||(o=setTimeout(s,n.wait||1))}),t.on("get",function(o){this.to.next(o);var e,i,r=o.get;if(r&&(e=r[Gun._.soul])){var a=r["."];(i=u[e]||void 0)&&a&&(i=Gun.state.to(i,a)),(i||Gun.obj.empty(n.peers))&&t.on("in",{"@":o["#"],put:Gun.graph.node(i),how:"lS"})}});var a=function(t,n,o,e){u[e]=Gun.state.to(o,n,u[e])},s=function(){var a;r=0,clearTimeout(o),o=!1;var s=i;i={};try{e.setItem(n.file,JSON.stringify(u))}catch(t){Gun.log(a=t||"localStorage failure")}(a||Gun.obj.empty(n.peers))&&Gun.obj.map(s,function(n,o){t.on("in",{"@":o,err:a,ok:0})})}}})}})(t,"./adapters/localStorage"),t(function(n){var o,e=t("./index");"undefined"!=typeof WebSocket?o=WebSocket:("undefined"!=typeof webkitWebSocket&&(o=webkitWebSocket),"undefined"!=typeof mozWebSocket&&(o=mozWebSocket)),e.on("opt",function(t){function n(t){var n=this,o=c,e=t.wire||u(t,n);e&&(e.readyState!==e.OPEN?(t.queue=t.queue||[]).push(o):e.send(o))}function r(t,n,o){if(o&&t){try{t=JSON.parse(t.data||t)}catch(t){}if(t instanceof Array)for(var e,u=0;e=t[u++];)r(e,n,o);else 1==f.who&&(t.ws=i),o.on("in",t)}}function u(i,u){if(i&&i.url){var s=i.url.replace("http","ws"),f=i.wire=new o(s);return f.onclose=function(){t.on("bye",i),a(i,u)},f.onerror=function(t){a(i,u),t&&t.code},f.onopen=function(){t.on("hi",i);var o=i.queue;i.queue=[],e.obj.map(o,function(t){c=t,n.call(u,i)})},f.onmessage=function(t){r(t,i,u)},f}}function a(t,n){clearTimeout(t.defer),t.defer=setTimeout(function(){u(t,n)},2e3)}this.to.next(t);var s=t.opt;if(!t.once&&o&&!1!==s.WebSocket){var f=s.ws||(s.ws={});if(f.who=0,e.obj.map(s.peers,function(){++f.who}),!t.once){var c;t.on("out",function(o){this.to.next(o),o.ws&&1==f.who||(c=JSON.stringify(o),f.drain?f.drain.push(c):(f.drain=[],setTimeout(function(){if(f.drain){var o=f.drain;f.drain=null,o.length&&(c=JSON.stringify(o),e.obj.map(s.peers,n,t))}},s.wait||1),e.obj.map(s.peers,n,t)))})}}});var i=function(){}})(t,"./adapters/websocket")}(); \ 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);var o=(n=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){var e=!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;e=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;e=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){if(!(t.indexOf(n)>=0))return!0;e=!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){if(!(t.indexOf(n)<0))return!0;e=!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;e=!0}if(n.obj.has(o,"<")){if(!(to?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])}},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){if(t)return t[n]=null,delete t[n],t},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||void 0}n.obj.empty=function(n,o){return!n||!a(n,t,{n:o})}}(),function(){function t(n,o){if(2===arguments.length)return t.r=t.r||{},void(t.r[n]=o);t.r=t.r||[],t.r.push(n)}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,g,h,d=0,v=o(s);if(t.r=null,i&&r(a)&&(g=i(a),h=!0),e(a)||g)for(l=(g||a).length;d",o.drift=0,o.is=function(t,n,e){var i=n&&t&&t[k]&&t[k][o._]||e;if(i)return b(i=i[n])?i:-1/0},o.lex=function(){return o().toString(36).replace(".","")},o.ify=function(t,n,e,r,u){if(!t||!t[k]){if(!u)return;t=i.soul.ify(t,u)}var a=g(t[k],o._);return l!==n&&n!==k&&(b(e)&&(a[n]=e),l!==r&&(t[n]=r)),t},o.to=function(t,n,e){var r=t[n];return d(r)&&(r=_(r)),o.ify(e,n,o.is(t,n),r,i.soul(t))},function(){function t(t,n){k!==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=m(n=n||e)?n:null,u&&!n?(e=b(e)?e:o(),u[k]=u[k]||{},v(u,t,{o:u,s:e}),u):(i=i||d(e)?e:r,e=b(e)?e:o(),function(o,u,a,s){if(!n)return t.call({o:a,s:e},o,u),o;n.call(i||this||{},o,u,a,s),h(a,u)&&r===a[u]||t.call({o:a,s:e},o,u)})}}();var l,p=e.obj,g=p.as,h=p.has,d=p.is,v=p.map,_=p.copy,b=e.num.is,m=e.fn.is,k=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){if(!t||o!==i.soul(t)||!i.is(t,this.fn,this.as))return!0;this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n))}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return!(!n||!s(n)||l(n))&&!g(n,t,{cb:o,fn:e,as:i})}}(),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,g=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,g)){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=g.map)&&(l.call(g.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,g)))return}if(!o)return p.node;if(!0===s)return n;if((l=t(g,{obj:n,path:p.path.concat(o)})).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)||(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)(o=e.rel.is(t))?(u=this.opt.seen[o])?this.obj[n]=u:this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt):this.obj[n]=t;else{if(l(t,e.rel._))return;this.obj[n]=h(t)}}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},g(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,g=a.map,h=a.copy;n.exports=r})(t,"./graph"),t(function(n){t("./onto"),n.exports=function(t,n){if(this.on){if(!(t instanceof Function)){if(!t||!n)return;var o=t["#"]||t,e=(this.tag||empty)[o];if(!e)return;return e=this.on(o,n),clearTimeout(e.err),!0}o=n&&n["#"]||Math.random().toString(36).slice(2);if(!t)return o;var i=this.on(o,t,n);return i.err=i.err||setTimeout(function(){i.next({err:"Error: No ACK received yet."}),i.off()},(this.opt||{}).lack||9e3),o}}})(t,"./ask"),t(function(n){var o=t("./type"),e=o.time.is;n.exports=function(t){var n={s:{}};return t=t||{max:1e3,age:9e3},n.check=function(t){return!!n.s[t]&&n.track(t)},n.track=function(i){return n.s[i]=e(),n.to||(n.to=setTimeout(function(){o.obj.map(n.s,function(i,r){t.age>e()-i||o.obj.del(n.s,r)}),n.to=null},t.age)),i},n}})(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=.9,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.on=t("./onto"),i.ask=t("./ask"),i.dup=t("./dup"),function(){function t(t){var n,o=this.as,e=o.gun;(n=t["#"])||(n=t["#"]=c(9)),o.dup.check(n)||(o.dup.track(n),t=h(t),o.ask(t["@"],t)||(t.get&&i.on.get(t,e),t.put&&i.on.put(t,e)),o.on("out",t))}i.create=function(n){n.root=n.root||n.gun,n.graph=n.graph||{},n.on=n.on||i.on,n.ask=n.ask||i.ask,n.dup=n.dup||i.dup();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(){function t(t,n,o,e){var r=this,u=i.state.is(o,n);if(!u)return r.err="Error: No state on '"+n+"' in node '"+e+"'!";var a=r.graph[e]||m,s=i.state.is(a,n,!0),f=a[n],c=i.HAM(r.machine,u,s,t,f);c.incoming?(r.put[e]=i.state.to(o,n,r.put[e]),(r.diff||(r.diff={}))[e]=i.state.to(o,n,r.diff[e]),r.souls[e]=!0):c.defer&&(r.defer=u<(r.defer||1/0)?u:r.defer)}function n(t,n){var i=this,u=i.gun._,a=(u.next||m)[n];if(a){var s=i.map[n]={put:t,get:n,gun:a},f={ctx:i,msg:s};i.async=!!u.tag.node,i.ack&&(s["@"]=i.ack),d(t,o,f),i.async&&(i.and||u.on("node",function(t){this.to.next(t),t===i.map[t.get]&&(i.souls[t.get]=!1,d(t.put,e,t),d(i.souls,function(t){if(t)return t})||i.c||(i.c=1,this.off(),d(i.map,r,i)))}),i.and=!0,u.on("node",s))}else i.souls[n]=!1}function o(t,n){var o=this.ctx,e=o.graph,r=this.msg,u=r.get,a=r.put,s=r.gun._;e[u]=i.state.to(a,n,e[u]),o.async||(s.put=i.state.to(a,n,s.put))}function e(t,n){var o=this,e=o.put,r=o.gun._;r.put=i.state.to(e,n,r.put)}function r(t,n){t.gun&&(t.gun._.root._.stop={},t.gun._.on("in",t),t.gun._.root._.stop={})}i.on.put=function(o,e){var a=e._,s={gun:e,graph:a.graph,put:{},map:{},souls:{},machine:i.state(),ack:o["@"]};if(i.graph.is(o.put,null,t,s)||(s.err="Error: Invalid graph!"),s.err)return a.on("in",{"@":o["#"],err:i.log(s.err)});d(s.put,n,s),s.async||d(s.map,r,s),u!==s.defer&&setTimeout(function(){i.on.put(o,e)},s.defer-s.machine),s.diff&&a.on("put",h(o,{put:s.diff}))},i.on.get=function(t,n){var o=n._,e=t.get[_],r=o.graph[e],u=t.get[b],a=((o.next||(o.next={}))[e]||m)._;if(!r||!a)return o.on("get",t);if(u){if(!g(r,u))return o.on("get",t);r=i.state.to(r,u)}else r=i.obj.copy(r);r=i.graph.node(r),o.on("in",{"@":t["#"],put:r,gun:n}),o.on("get",t)}}(),i.chain.opt=function(t){t=t||{};var n=this,o=n._,e=t.peers||t;return p(t)||(t={}),p(o.opt)||(o.opt=t),f(e)&&(e=[e]),a(e)&&(e=d(e,function(t,n,o){o(t,{url:t})}),p(o.opt.peers)||(o.opt.peers={}),o.opt.peers=h(e,o.opt.peers)),o.opt.peers=o.opt.peers||{},h(t,o.opt),i.on("opt",o),o.opt.uuid=o.opt.uuid||function(){return v()+c(12)},n};var u,a=i.list.is,s=i.text,f=s.is,c=s.random,l=i.obj,p=l.is,g=l.has,h=l.to,d=l.map,v=(l.copy,i.state.lex),_=i.val.rel._,b=".",m=(i.node._,i.val.rel.is,{});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);try{void 0!==e&&(e.exports=i)}catch(t){}n.exports=i})(t,"./root"),t(function(n){var o=t("./root");o.chain.back=function(t,n){if(-1===(t=t||1)||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)){if(t instanceof Function){for(var a,s={back:r};(s=s.back)&&(s=s._)&&!(a=t(s,n)););return a}return o.num.is(t)?u.back.back(t-1):this}var f=0,c=t.length,s=u;for(f;f=(n.batch||1e3))return s();o||(o=setTimeout(s,n.wait||1))}),t.on("get",function(o){this.to.next(o);var e,i,r=o.get;if(r&&(e=r["#"])){var a=r["."];(i=u[e]||void 0)&&a&&(i=Gun.state.to(i,a)),(i||Gun.obj.empty(n.peers))&&t.on("in",{"@":o["#"],put:Gun.graph.node(i),how:"lS"})}});var a=function(t,n,o,e){u[e]=Gun.state.to(o,n,u[e])},s=function(){var a;r=0,clearTimeout(o),o=!1;var s=i;i={};try{e.setItem(n.file,JSON.stringify(u))}catch(t){Gun.log(a=t||"localStorage failure")}(a||Gun.obj.empty(n.peers))&&Gun.obj.map(s,function(n,o){t.on("in",{"@":o,err:a,ok:0})})}}})}})(t,"./adapters/localStorage"),t(function(n){var o,e=t("./index");"undefined"!=typeof WebSocket?o=WebSocket:("undefined"!=typeof webkitWebSocket&&(o=webkitWebSocket),"undefined"!=typeof mozWebSocket&&(o=mozWebSocket)),e.on("opt",function(t){function n(t){var n=this,o=c,e=t.wire||u(t,n);e&&(e.readyState!==e.OPEN?(t.queue=t.queue||[]).push(o):e.send(o))}function r(t,n,o){if(o&&t){try{t=JSON.parse(t.data||t)}catch(t){}if(t instanceof Array)for(var e,u=0;e=t[u++];)r(e,n,o);else 1==f.who&&(t.ws=i),o.on("in",t)}}function u(i,u){if(i&&i.url){var s=i.url.replace("http","ws"),f=i.wire=new o(s);return f.onclose=function(){t.on("bye",i),a(i,u)},f.onerror=function(t){a(i,u),t&&t.code},f.onopen=function(){t.on("hi",i);var o=i.queue;i.queue=[],e.obj.map(o,function(t){c=t,n.call(u,i)})},f.onmessage=function(t){r(t,i,u)},f}}function a(t,n){clearTimeout(t.defer),t.defer=setTimeout(function(){u(t,n)},2e3)}this.to.next(t);var s=t.opt;if(!t.once&&o&&!1!==s.WebSocket){var f=s.ws||(s.ws={});if(f.who=0,e.obj.map(s.peers,function(){++f.who}),!t.once){var c;t.on("out",function(o){this.to.next(o),o.ws&&1==f.who||(c=JSON.stringify(o),f.drain?f.drain.push(c):(f.drain=[],setTimeout(function(){if(f.drain){var o=f.drain;f.drain=null,o.length&&(c=JSON.stringify(o),e.obj.map(s.peers,n,t))}},s.wait||1),e.obj.map(s.peers,n,t)))})}}});var i=function(){}})(t,"./adapters/websocket")}(); \ No newline at end of file diff --git a/src/adapters/localStorage.js b/src/adapters/localStorage.js index bb2f0eca..0be45cc9 100644 --- a/src/adapters/localStorage.js +++ b/src/adapters/localStorage.js @@ -35,12 +35,12 @@ Gun.on('opt', function(root){ this.to.next(at); var lex = at.get, soul, data, u; //setTimeout(function(){ - if(!lex || !(soul = lex[Gun._.soul])){ return } + if(!lex || !(soul = lex['#'])){ return } //if(0 >= at.cap){ return } - var field = lex['.']; + var has = lex['.']; data = disk[soul] || u; - if(data && field){ - data = Gun.state.to(data, field); + if(data && has){ + data = Gun.state.to(data, has); } if(!data && !Gun.obj.empty(opt.peers)){ // if data not found, don't ack if there are peers. return; // Hmm, what if we have peers but we are disconnected? diff --git a/src/chain.js b/src/chain.js index 887de025..984bf7be 100644 --- a/src/chain.js +++ b/src/chain.js @@ -72,39 +72,37 @@ function output(msg){ return back.on('out', msg); } -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(cat.get && at.get !== cat.get){ - at = obj_to(at, {get: cat.get}); +function input(msg){ + var ev = this, cat = this.as, gun = msg.gun, at = gun._, change = msg.put, rel, tmp; + if(cat.get && msg.get !== cat.get){ + msg = obj_to(msg, {get: cat.get}); } - if(cat.field && coat !== cat){ - at = obj_to(at, {gun: cat.gun}); - if(coat.ack){ - cat.ack = coat.ack; - //cat.ack = cat.ack || coat.ack; + if(cat.has && at !== cat){ + msg = obj_to(msg, {gun: cat.gun}); + if(at.ack){ + cat.ack = at.ack; + //cat.ack = cat.ack || at.ack; } } if(node_ === cat.get && change && change['#']){ - // TODO: Potential bug? What if (soul.field = pointer) gets changed to (soul.field = primitive), we still need to clear out / wipe /reset (soul.field._) to have _id = nothing, or puts might have false positives (revert back to old soul). + // TODO: Potential bug? What if (soul.has = pointer) gets changed to (soul.has = primitive), we still need to clear out / wipe /reset (soul.has._) to have _id = nothing, or puts might have false positives (revert back to old soul). cat._id = change['#']; } if(u === change){ - ev.to.next(at); + ev.to.next(msg); if(cat.soul){ return } - echo(cat, at, ev); - if(cat.field){ - not(cat, at); + echo(cat, msg, ev); + if(cat.has){ + not(cat, msg); } - obj_del(coat.echo, cat.id); - obj_del(cat.map, coat.id); + obj_del(at.echo, cat.id); + obj_del(cat.map, at.id); 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}); + ev.to.next(msg); + echo(cat, msg, ev); + obj_map(change, map, {at: msg, cat: cat}); return; } /*if(rel = Gun.val.rel.is(change)){ @@ -116,35 +114,35 @@ function input(at){ if(!rel || tmp){*/ if(!(rel = Gun.val.rel.is(change))){ if(Gun.val.is(change)){ - if(cat.field || cat.soul){ - not(cat, at); + if(cat.has || cat.soul){ + not(cat, msg); } else - if(coat.field || coat.soul){ - (coat.echo || (coat.echo = {}))[cat.id] = cat; - (cat.map || (cat.map = {}))[coat.id] = cat.map[coat.id] || {at: coat}; - //if(u === coat.put){ return } // Not necessary but improves performance. If we have it but coat does not, that means we got things out of order and coat will get it. Once coat gets it, it will tell us again. + if(at.has || at.soul){ + (at.echo || (at.echo = {}))[cat.id] = cat; + (cat.map || (cat.map = {}))[at.id] = cat.map[at.id] || {at: at}; + //if(u === at.put){ return } // Not necessary but improves performance. If we have it but at does not, that means we got things out of order and at will get it. Once at gets it, it will tell us again. } - ev.to.next(at); - echo(cat, at, ev); + ev.to.next(msg); + echo(cat, msg, ev); return; } - if(cat.field && coat !== cat && obj_has(coat, 'put')){ - cat.put = coat.put; + if(cat.has && at !== cat && obj_has(at, 'put')){ + cat.put = at.put; }; - if((rel = Gun.node.soul(change)) && coat.field){ - coat.put = (cat.root.get(rel)._).put; + if((rel = Gun.node.soul(change)) && at.has){ + at.put = (cat.root.get(rel)._).put; } - ev.to.next(at); - echo(cat, at, ev); - relate(cat, at, coat, rel); - obj_map(change, map, {at: at, cat: cat}); + ev.to.next(msg); + echo(cat, msg, ev); + relate(cat, msg, at, rel); + obj_map(change, map, {at: msg, cat: cat}); return; } - relate(cat, at, coat, rel); - ev.to.next(at); - echo(cat, at, ev); + relate(cat, msg, at, rel); + ev.to.next(msg); + echo(cat, msg, ev); } -Gun.chain.chain.input = input; + function relate(at, msg, from, rel){ if(!rel || node_ === at.get){ return } var tmp = (at.root.get(rel)._); @@ -182,7 +180,7 @@ function relate(at, msg, from, rel){ } function echo(at, msg, ev){ if(!at.echo){ return } // || node_ === at.get ? - if(at.has || at.field){ msg = obj_to(msg, {event: ev}) } + if(at.has){ msg = obj_to(msg, {event: ev}) } obj_map(at.echo, reverb, msg); } function reverb(to){ @@ -198,7 +196,7 @@ function map(data, key){ // Map over only the changes on every update. //if(data && data[_soul] && (tmp = Gun.val.rel.is(data)) && (tmp = (cat.root.get(tmp)._)) && obj_has(tmp, 'put')){ // data = tmp.put; //} - if(at.field){ + if(at.has){ if(!(data && data[_soul] && Gun.val.rel.is(data) === Gun.node.soul(at.put))){ at.put = data; } @@ -280,5 +278,5 @@ function ack(msg, ev){ var empty = {}, u; var obj = Gun.obj, obj_has = obj.has, obj_put = obj.put, obj_del = obj.del, obj_to = obj.to, obj_map = obj.map; var text_rand = Gun.text.random; -var _soul = Gun._.soul, _field = Gun._.field, node_ = Gun.node._; +var _soul = Gun.val.rel._, node_ = Gun.node._; \ No newline at end of file diff --git a/src/get.js b/src/get.js index 0921ffb6..69c73a91 100644 --- a/src/get.js +++ b/src/get.js @@ -42,8 +42,8 @@ function cache(key, back){ if(cat.root === back){ at.soul = key; } else - if(cat.soul || cat.field || cat.has){ // TODO: Convert field to has! - at.field = at.has = key; + if(cat.soul || cat.has){ + at.has = key; //if(obj_has(cat.put, key)){ //at.put = cat.put[key]; //} diff --git a/src/graph.js b/src/graph.js index 94e8db0c..cdbb8fb2 100644 --- a/src/graph.js +++ b/src/graph.js @@ -15,7 +15,7 @@ var Graph = {}; this.cb.call(nf.as, n, s, nf); } function nf(fn){ // optional callback for each node. - if(fn){ Node.is(nf.n, fn, nf.as) } // where we then have an optional callback for each field/value. + if(fn){ Node.is(nf.n, fn, nf.as) } // where we then have an optional callback for each key/value. } }()); ;(function(){ @@ -50,13 +50,13 @@ var Graph = {}; } return at; } - function map(v,f,n){ + function map(v,k,n){ var at = this, env = at.env, is, tmp; - if(Node._ === f && obj_has(v,Val.rel._)){ + if(Node._ === k && obj_has(v,Val.rel._)){ return n._; // TODO: Bug? } - if(!(is = valid(v,f,n, at,env))){ return } - if(!f){ + if(!(is = valid(v,k,n, at,env))){ return } + if(!k){ at.node = at.node || n || {}; if(obj_has(v, Node._)){ at.node._ = obj_copy(v._); @@ -65,21 +65,21 @@ var Graph = {}; at.rel = at.rel || Val.rel.ify(Node.soul(at.node)); } if(tmp = env.map){ - tmp.call(env.as || {}, v,f,n, at); - if(obj_has(n,f)){ - v = n[f]; + tmp.call(env.as || {}, v,k,n, at); + if(obj_has(n,k)){ + v = n[k]; if(u === v){ - obj_del(n, f); + obj_del(n, k); return; } - if(!(is = valid(v,f,n, at,env))){ return } + if(!(is = valid(v,k,n, at,env))){ return } } } - if(!f){ return at.node } + if(!k){ return at.node } if(true === is){ return v; } - tmp = node(env, {obj: v, path: at.path.concat(f)}); + tmp = node(env, {obj: v, path: at.path.concat(k)}); if(!tmp.node){ return } return tmp.rel; //{'#': Node.soul(tmp.node)}; } @@ -95,14 +95,14 @@ var Graph = {}; obj_del(graph, prev); } } - function valid(v,f,n, at,env){ var tmp; + function valid(v,k,n, at,env){ var tmp; if(Val.is(v)){ return true } if(obj_is(v)){ return 1 } if(tmp = env.invalid){ - v = tmp.call(env.as || {}, v,f,n); - return valid(v,f,n, at,env); + v = tmp.call(env.as || {}, v,k,n); + return valid(v,k,n, at,env); } - env.err = "Invalid value at '" + at.path.concat(f).join('.') + "'!"; + env.err = "Invalid value at '" + at.path.concat(k).join('.') + "'!"; } function seen(env, at){ var arr = env.seen, i = arr.length, has; @@ -125,23 +125,23 @@ Graph.node = function(node){ obj_map(graph[root], map, {obj:obj, graph: graph, opt: opt}); return obj; } - function map(v,f){ var tmp, obj; - if(Node._ === f){ + function map(v,k){ var tmp, obj; + if(Node._ === k){ if(obj_empty(v, Val.rel._)){ return; } - this.obj[f] = obj_copy(v); + this.obj[k] = obj_copy(v); return; } if(!(tmp = Val.rel.is(v))){ - this.obj[f] = v; + this.obj[k] = v; return; } if(obj = this.opt.seen[tmp]){ - this.obj[f] = obj; + this.obj[k] = obj; return; } - this.obj[f] = this.opt.seen[tmp] = Graph.to(this.graph, tmp, this.opt); + this.obj[k] = this.opt.seen[tmp] = Graph.to(this.graph, tmp, this.opt); } }()); var fn_is = Type.fn.is; diff --git a/src/map.js b/src/map.js index 52f049b4..741a4b91 100644 --- a/src/map.js +++ b/src/map.js @@ -29,9 +29,9 @@ function map(msg){ obj_map(msg.put, each, {at: this.as, msg: msg}); this.to.next(msg); } -function each(v,f){ - if(n_ === f){ return } - var msg = this.msg, gun = msg.gun, at = this.at, tmp = (gun.get(f)._); +function each(v,k){ + if(n_ === k){ return } + var msg = this.msg, gun = msg.gun, at = this.at, tmp = (gun.get(k)._); (tmp.echo || (tmp.echo = {}))[at.id] = at; } var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u; diff --git a/src/node.js b/src/node.js index 36febdfe..0a016d26 100644 --- a/src/node.js +++ b/src/node.js @@ -19,10 +19,10 @@ Node.soul._ = Val.rel._; } return false; // nope! This was not a valid node. } - function map(v, f){ // we invert this because the way we check for this is via a negation. - if(f === Node._){ return } // skip over the metadata. + function map(v, k){ // we invert this because the way we check for this is via a negation. + if(k === Node._){ return } // skip over the metadata. if(!Val.is(v)){ return true } // it is true that this is an invalid node. - if(this.cb){ this.cb.call(this.as, v, f, this.n, this.s) } // optionally callback each field/value. + if(this.cb){ this.cb.call(this.as, v, k, this.n, this.s) } // optionally callback each key/value. } }()); ;(function(){ @@ -36,17 +36,17 @@ Node.soul._ = Val.rel._; } return o.node; // This will only be a valid node if the object wasn't already deep! } - function map(v, f){ var o = this.o, tmp, u; // iterate over each field/value. + function map(v, k){ var o = this.o, tmp, u; // iterate over each key/value. if(o.map){ - tmp = o.map.call(this.as, v, ''+f, o.node); + tmp = o.map.call(this.as, v, ''+k, o.node); if(u === tmp){ - obj_del(o.node, f); + obj_del(o.node, k); } else - if(o.node){ o.node[f] = tmp } + if(o.node){ o.node[k] = tmp } return; } if(Val.is(v)){ - o.node[f] = v; + o.node[k] = v; } } }()); diff --git a/src/on.js b/src/on.js index 1bc4bd12..b63eb80a 100644 --- a/src/on.js +++ b/src/on.js @@ -97,7 +97,7 @@ function val(msg, ev, to){ }, opt.wait || 99); return; } - if(cat.field || cat.soul){ + if(cat.has || cat.soul){ if(ev.off()){ return } // if it is already off, don't call again! } else { if((opt.seen = opt.seen || {})[coat.id]){ return } diff --git a/src/put.js b/src/put.js index 1659d394..5f26a1a0 100644 --- a/src/put.js +++ b/src/put.js @@ -1,7 +1,7 @@ var Gun = require('./root'); Gun.chain.put = function(data, cb, as){ - // #soul.field=value>state + // #soul.has=value>state // ~who#where.where=what>when@was // TODO: BUG! Put probably cannot handle plural chains! var gun = this, at = (gun._), root = at.root, tmp; @@ -104,7 +104,6 @@ function batch(){ var as = this; var tmp = cat.root._.now; obj.del(cat.root._, 'now'); var tmp2 = cat.root._.stop; (as.ref._).now = true; - //console.log("PUT!", as.env.graph); (as.ref._).on('out', { gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask }); @@ -113,10 +112,10 @@ function batch(){ var as = this; cat.root._.stop = tmp2; }, as); if(as.res){ as.res() } -} function no(v,f){ if(v){ return true } } +} function no(v,k){ if(v){ return true } } -function map(v,f,n, at){ var as = this; - if(f || !at.path.length){ return } +function map(v,k,n, at){ var as = this; + if(k || !at.path.length){ return } (as.res||iife)(function(){ var path = at.path, ref = as.ref, opt = as.opt; var i = 0, l = path.length; diff --git a/src/root.js b/src/root.js index 5c140573..db41531a 100644 --- a/src/root.js +++ b/src/root.js @@ -24,14 +24,6 @@ Gun.on = require('./onto'); Gun.ask = require('./ask'); Gun.dup = require('./dup'); -Gun._ = { // some reserved key words, these are not the only ones. - node: Gun.node._ // all metadata of a node is stored in the meta property on the node. - ,soul: Gun.val.rel._ // a soul is a UUID of a node but it always points to the "latest" data known. - ,state: Gun.state._ // other than the soul, we store HAM metadata. - ,field: '.' // a field is a property on a node which points to a value. - ,value: '=' // the primitive value. -} - ;(function(){ Gun.create = function(at){ at.root = at.root || at.gun; @@ -115,19 +107,20 @@ Gun._ = { // some reserved key words, these are not the only ones. obj_map(node, each, as); if(!ctx.async){ return } if(!ctx.and){ + // If it is async, we only need to setup on listener per context (ctx) cat.on('node', function(m){ - this.to.next(m); - if(m !== ctx.map[m.get]){ return } - ctx.souls[m.get] = false; - obj_map(m.put, aeach, m); - if(obj_map(ctx.souls, function(v){ if(v){ return v } })){ return } - if(ctx.c){ return } ctx.c = 1; + this.to.next(m); // make sure to call other context's listeners. + if(m !== ctx.map[m.get]){ return } // filter out events not from this context! + ctx.souls[m.get] = false; // set our many-async flag + obj_map(m.put, aeach, m); // merge into view + if(obj_map(ctx.souls, function(v){ if(v){ return v } })){ return } // if flag still outstanding, keep waiting. + if(ctx.c){ return } ctx.c = 1; // failsafe for only being called once per context. this.off(); - obj_map(ctx.map, map, ctx); + obj_map(ctx.map, map, ctx); // all done, trigger chains. }); } ctx.and = true; - cat.on('node', msg); + cat.on('node', msg); // each node on the current context's graph needs to be emitted though. } function each(val, key){ var ctx = this.ctx, graph = ctx.graph, msg = this.msg, soul = msg.get, node = msg.put, at = (msg.gun._), tmp; @@ -142,17 +135,21 @@ Gun._ = { // some reserved key words, these are not the only ones. function map(msg, soul){ if(!msg.gun){ return } msg.gun._.root._.stop = {}; + //console.log('map ->', soul, msg.put); (msg.gun._).on('in', msg); msg.gun._.root._.stop = {}; } Gun.on.get = function(msg, gun){ - var root = gun._, soul = msg.get[_soul], node = root.graph[soul], field = msg.get[_field], tmp; + var root = gun._, soul = msg.get[_soul], node = root.graph[soul], has = msg.get[_has], tmp; var next = root.next || (root.next = {}), at = ((next[soul] || empty)._); if(!node || !at){ return root.on('get', msg) } - if(field){ - if(!obj_has(node, field)){ return root.on('get', msg) } - node = Gun.state.to(node, field); + if(has){ + if(!obj_has(node, has)){ return root.on('get', msg) } + node = Gun.state.to(node, has); + // If we have a key in-memory, do we really need to fetch? + // Maybe... in case the in-memory key we have is a local write + // we still need to trigger a pull/merge from peers. } else { node = Gun.obj.copy(node); } @@ -196,7 +193,7 @@ Gun._ = { // some reserved key words, these are not the only ones. var list_is = Gun.list.is; var text = Gun.text, text_is = text.is, text_rand = text.random; var obj = Gun.obj, obj_is = obj.is, obj_has = obj.has, obj_to = obj.to, obj_map = obj.map, obj_copy = obj.copy; -var state_lex = Gun.state.lex, _soul = Gun._.soul, _field = Gun._.field, node_ = Gun._.node, rel_is = Gun.val.rel.is; +var state_lex = Gun.state.lex, _soul = Gun.val.rel._, _has = '.', node_ = Gun.node._, rel_is = Gun.val.rel.is; var empty = {}, u; console.debug = function(i, s){ return (console.debug.i && i === console.debug.i && console.debug.i++) && (console.log.apply(console, arguments) || s) }; @@ -209,7 +206,7 @@ Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, feel f ;"Please do not remove these messages unless you are paying for a monthly sponsorship, thanks!"; if(typeof window !== "undefined"){ window.Gun = Gun } -if(typeof common !== "undefined"){ common.exports = Gun } +try{ if(typeof common !== "undefined"){ common.exports = Gun } }catch(e){} module.exports = Gun; /*Gun.on('opt', function(ctx){ // FOR TESTING PURPOSES @@ -218,6 +215,7 @@ module.exports = Gun; ctx.on('node', function(msg){ var to = this.to; //console.log(">>>", msg.put); + //Gun.node.is(msg.put, function(v,k){ msg.put[k] = v + v }); setTimeout(function(){ //console.log("<<<<<", msg.put); to.next(msg); diff --git a/src/state.js b/src/state.js index 43690786..c77a37b4 100644 --- a/src/state.js +++ b/src/state.js @@ -17,13 +17,13 @@ var time = Type.time.is, last = -Infinity, N = 0, D = 1000; // WARNING! In the f var perf = (typeof performance !== 'undefined')? (performance.timing && performance) : false, start = (perf && perf.timing && perf.timing.navigationStart) || (perf = false); State._ = '>'; State.drift = 0; -State.is = function(n, f, o){ // convenience function to get the state on a field on a node and return it. - var tmp = (f && n && n[N_] && n[N_][State._]) || o; +State.is = function(n, k, o){ // convenience function to get the state on a key on a node and return it. + var tmp = (k && n && n[N_] && n[N_][State._]) || o; if(!tmp){ return } - return num_is(tmp = tmp[f])? tmp : -Infinity; + return num_is(tmp = tmp[k])? tmp : -Infinity; } State.lex = function(){ return State().toString(36).replace('.','') } -State.ify = function(n, f, s, v, soul){ // put a field's state on a node. +State.ify = function(n, k, s, v, soul){ // put a key's state on a node. if(!n || !n[N_]){ // reject if it is not node-like. if(!soul){ // unless they passed a soul return; @@ -31,22 +31,22 @@ State.ify = function(n, f, s, v, soul){ // put a field's state on a node. n = Node.soul.ify(n, soul); // then make it so! } var tmp = obj_as(n[N_], State._); // grab the states data. - if(u !== f && f !== N_){ + if(u !== k && k !== N_){ if(num_is(s)){ - tmp[f] = s; // add the valid state. + tmp[k] = s; // add the valid state. } if(u !== v){ // Note: Not its job to check for valid values! - n[f] = v; + n[k] = v; } } return n; } -State.to = function(from, f, to){ - var val = from[f]; +State.to = function(from, k, to){ + var val = from[k]; if(obj_is(val)){ val = obj_copy(val); } - return State.ify(to, f, State.is(from, f), val, Node.soul(from)); + return State.ify(to, k, State.is(from, k), val, Node.soul(from)); } ;(function(){ State.map = function(cb, s, as){ var u; // for use with Node.ify @@ -60,19 +60,19 @@ State.to = function(from, f, to){ } as = as || obj_is(s)? s : u; s = num_is(s)? s : State(); - return function(v, f, o, opt){ + return function(v, k, o, opt){ if(!cb){ - map.call({o: o, s: s}, v,f); + map.call({o: o, s: s}, v,k); return v; } - cb.call(as || this || {}, v, f, o, opt); - if(obj_has(o,f) && u === o[f]){ return } - map.call({o: o, s: s}, v,f); + cb.call(as || this || {}, v, k, o, opt); + if(obj_has(o,k) && u === o[k]){ return } + map.call({o: o, s: s}, v,k); } } - function map(v,f){ - if(N_ === f){ return } - State.ify(this.o, f, this.s) ; + function map(v,k){ + if(N_ === k){ return } + State.ify(this.o, k, this.s) ; } }()); var obj = Type.obj, obj_as = obj.as, obj_has = obj.has, obj_is = obj.is, obj_map = obj.map, obj_copy = obj.copy; diff --git a/src/type.js b/src/type.js index bb684f97..0ef9cf0c 100644 --- a/src/type.js +++ b/src/type.js @@ -43,7 +43,7 @@ Type.text.match = function(t, o){ var r = false; } Type.list = {is: function(l){ return (l instanceof Array) }} Type.list.slit = Array.prototype.slice; -Type.list.sort = function(k){ // creates a new sort function based off some field +Type.list.sort = function(k){ // creates a new sort function based off some key return function(A,B){ if(!A || !B){ return 0 } A = A[k]; B = B[k]; if(A < B){ return -1 }else if(A > B){ return 1 } @@ -53,15 +53,15 @@ Type.list.sort = function(k){ // creates a new sort function based off some fiel Type.list.map = function(l, c, _){ return obj_map(l, c, _) } Type.list.index = 1; // change this to 0 if you want non-logical, non-mathematical, non-matrix, non-convenient array notation Type.obj = {is: function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }} -Type.obj.put = function(o, f, v){ return (o||{})[f] = v, o } -Type.obj.has = function(o, f){ return o && Object.prototype.hasOwnProperty.call(o, f) } +Type.obj.put = function(o, k, v){ return (o||{})[k] = v, o } +Type.obj.has = function(o, k){ return o && Object.prototype.hasOwnProperty.call(o, k) } Type.obj.del = function(o, k){ if(!o){ return } o[k] = null; delete o[k]; return o; } -Type.obj.as = function(o, f, v, u){ return o[f] = o[f] || (u === v? {} : v) } +Type.obj.as = function(o, k, v, u){ return o[k] = o[k] || (u === v? {} : v) } Type.obj.ify = function(o){ if(obj_is(o)){ return o } try{o = JSON.parse(o); @@ -69,9 +69,9 @@ Type.obj.ify = function(o){ return o; } ;(function(){ var u; - function map(v,f){ - if(obj_has(this,f) && u !== this[f]){ return } - this[f] = v; + function map(v,k){ + if(obj_has(this,k) && u !== this[k]){ return } + this[k] = v; } Type.obj.to = function(from, to){ to = to || {}; diff --git a/src/val.js b/src/val.js index a935f8e2..28905996 100644 --- a/src/val.js +++ b/src/val.js @@ -3,7 +3,7 @@ var Type = require('./type'); var Val = {}; Val.is = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first. if(v === u){ return false } - if(v === null){ return true } // "deletes", nulling out fields. + if(v === null){ return true } // "deletes", nulling out keys. if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face. if(text_is(v) // by "text" we mean strings. || bi_is(v) // by "binary" we mean boolean. @@ -24,9 +24,9 @@ Val.rel = {_: '#'}; } return false; // the value was not a valid soul relation. } - function map(s, f){ var o = this; // map over the object... + function map(s, k){ var o = this; // map over the object... if(o.id){ return o.id = false } // if ID is already defined AND we're still looping through the object, it is considered invalid. - if(f == rel_ && text_is(s)){ // the field should be '#' and have a text value. + if(k == rel_ && text_is(s)){ // the key should be '#' and have a text value. o.id = s; // we found the soul! } else { return o.id = false; // if there exists anything else on the object that isn't the soul, then it is considered invalid.