From 736a530bccefe96ed728deb109f8faf3f1a9e042 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Tue, 26 Jun 2018 13:06:29 -0700 Subject: [PATCH] unbuild --- gun.min.js | 2 +- sea/create.js | 2 +- sea/https.js | 2 +- sea/index.js | 12 +-- sea/leave.js | 4 +- sea/login.js | 2 +- sea/user.js | 3 +- src/adapters/localStorage.js | 8 +- src/adapters/mesh.js | 3 - src/adapters/websocket.js | 1 - src/back.js | 10 +- src/chain.js | 188 +++++++++++++++++++---------------- src/get.js | 74 +++++++++++--- src/graph.js | 4 +- src/map.js | 19 ++-- src/node.js | 2 +- src/on.js | 78 ++++++--------- src/put.js | 119 ++++++++++------------ src/root.js | 69 ++++++------- src/set.js | 16 +-- src/type.js | 2 +- src/val.js | 4 +- 22 files changed, 315 insertions(+), 309 deletions(-) diff --git a/gun.min.js b/gun.min.js index 6cebffda..05bd4c2e 100644 --- a/gun.min.js +++ b/gun.min.js @@ -1 +1 @@ -!function(){var t;"undefined"!=typeof window&&(t=window),"undefined"!=typeof global&&(t=global);var _=(t=t||{}).console||{log:function(){}};function x(o){return o.slice?x[e(o)]:function(t,n){o(t={exports:{}}),x[e(n)]=t.exports};function e(t){return t.split("/").slice(-1).toString().replace(".js","")}}if("undefined"!=typeof module)var f=module;x(function(t){var p={};p.fns=p.fn={is:function(t){return!!t&&"function"==typeof t}},p.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},p.num={is:function(t){return!h(t)&&(0<=t-parseFloat(t)+1||1/0===t||-1/0===t)}},p.text={is:function(t){return"string"==typeof t}},p.text.ify=function(t){return p.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},p.text.random=function(t,n){var o="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";0")){if(!(n>t[">"]))return!1;o=!0}if(p.obj.has(t,"<")){if(!(n",s.drift=0,s.is=function(t,n,o){var e=n&&t&&t[b]&&t[b][s._]||o;if(e)return d(e=e[n])?e:-1/0},s.lex=function(){return s().toString(36).replace(".","")},s.ify=function(t,n,o,e,i){if(!t||!t[b]){if(!i)return;t=u.soul.ify(t,i)}var r=c(t[b],s._);return void 0!==n&&n!==b&&(d(o)&&(r[n]=o),void 0!==e&&(t[n]=e)),t},s.to=function(t,n,o){var e=t[n];return p(e)&&(e=h(e)),s.ify(o,n,s.is(t,n),e,u.soul(t))},function(){function a(t,n){b!==n&&s.ify(this.o,n,this.s)}s.map=function(i,r,u){var t=p(t=i||r)?t:null;return i=v(i=i||r)?i:null,t&&!i?(r=d(r)?r:s(),t[b]=t[b]||{},g(t,a,{o:t,s:r}),t):(u=u||p(r)?r:void 0,r=d(r)?r:s(),function(t,n,o,e){if(!i)return a.call({o:o,s:r},t,n),t;i.call(u||this||{},t,n,o,e),l(o,n)&&void 0===o[n]||a.call({o:o,s:r},t,n)})}}();var f=n.obj,c=f.as,l=f.has,p=f.is,g=f.map,h=f.copy,d=n.num.is,v=n.fn.is,b=u._;t.exports=s})(x,"./state"),x(function(t){var u=x("./type"),f=x("./val"),c=x("./node"),r={};!function(){function i(t,n){if(!t||n!==c.soul(t)||!c.is(t,this.fn,this.as))return!0;this.cb&&(o.n=t,o.as=this.as,this.cb.call(o.as,t,n,o))}function o(t){t&&c.is(o.n,t,o.as)}r.is=function(t,n,o,e){return!(!t||!l(t)||a(t))&&!s(t,i,{cb:n,fn:o,as:e})}}(),function(){function a(t,n){var o;return(o=function(t,n){var o,e=t.seen,i=e.length;for(;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}(t,n))?o:(n.env=t,n.soul=i,c.ify(n.obj,e,n)&&(t.graph[f.rel.is(n.rel)]=n.node),n)}function e(t,n,o){var e,i,r=this,u=r.env;if(c._===n&&g(t,f.rel._))return o._;if(e=s(t,n,o,r,u)){if(n||(r.node=r.node||o||{},g(t,c._)&&(r.node._=h(t._)),r.node=c.soul.ify(r.node,f.rel.is(r.rel)),r.rel=r.rel||f.rel.ify(c.soul(r.node))),(i=u.map)&&(i.call(u.as||{},t,n,o,r),g(o,n))){if(void 0===(t=o[n]))return void p(o,n);if(!(e=s(t,n,o,r,u)))return}if(!n)return r.node;if(!0===e)return t;if((i=a(u,{obj:t,path:r.path.concat(n)})).node)return i.rel}}function i(t){var n=this,o=f.rel.is(n.rel),e=n.env.graph;n.rel=n.rel||f.rel.ify(t),n.rel[f.rel._]=t,n.node&&n.node[c._]&&(n.node[c._][f.rel._]=t),g(e,o)&&(e[t]=e[o],p(e,o))}function s(t,n,o,e,i){var r;return!!f.is(t)||(l(t)?1:(r=i.invalid)?s(t=r.call(i.as||{},t,n,o),n,o,e,i):(i.err="Invalid value at '"+e.path.concat(n).join(".")+"'!",void(u.list.is(t)&&(i.err+=" Use `.set(item)` instead of an Array."))))}r.ify=function(t,n,o){var e={path:[],obj:t};return n?"string"==typeof n?n={soul:n}:n instanceof Function&&(n.map=n):n={},n.soul&&(e.rel=f.rel.ify(n.soul)),n.graph=n.graph||{},n.seen=n.seen||[],n.as=n.as||o,a(n,e),n.root=e.node,n.graph}}(),r.node=function(t){var n=c.soul(t);if(n)return o({},n,t)},function(){function i(t,n){var o,e;if(c._!==n)(o=f.rel.is(t))?(e=this.opt.seen[o])?this.obj[n]=e:this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt):this.obj[n]=t;else{if(a(t,f.rel._))return;this.obj[n]=h(t)}}r.to=function(t,n,o){if(t){var e={};return o=o||{seen:{}},s(t[n],i,{obj:e,graph:t,opt:o}),e}}}();u.fn.is;var n=u.obj,l=n.is,p=n.del,g=n.has,a=n.empty,o=n.put,s=n.map,h=n.copy;t.exports=r})(x,"./graph"),x(function(t){x("./onto"),t.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}}})(x,"./ask"),x(function(t){var r=x("./type");var u=r.time.is;t.exports=function(e){var i={s:{}};return e=e||{max:1e3,age:9e3},i.check=function(t){var n;return!!(n=i.s[t])&&(n.pass?n.pass=!1:i.track(t))},i.track=function(t,n){var o=i.s[t]||(i.s[t]={});return o.was=u(),n&&(o.pass=!0),i.to||(i.to=setTimeout(function(){var o=u();r.obj.map(i.s,function(t,n){e.age>o-t.was||r.obj.del(i.s,n)}),i.to=null},e.age+9)),o},i}})(x,"./dup"),x(function(t){function c(t){return t instanceof c?(this._={gun:this}).gun:this instanceof c?c.create(this._={gun:this,opt:t}):new c(t)}c.is=function(t){return t instanceof c||t&&t._&&t._.gun&&!0||!1},c.version=.9,(c.chain=c.prototype).toJSON=function(){};var n=x("./type");n.obj.to(n,c),c.HAM=x("./HAM"),c.val=x("./val"),c.node=x("./node"),c.state=x("./state"),c.graph=x("./graph"),c.on=x("./onto"),c.ask=x("./ask"),c.dup=x("./dup"),function(){function r(t){var n,o,e=this.as,i=e.gun;(o=t["#"])||(o=t["#"]=u(9)),(n=e.dup).check(o)?e.out===t.out&&(t.out=void 0,this.to.next(t)):(n.track(o),e.ask(t["@"],t)||(t.get&&c.on.get(t,i),t.put&&c.on.put(t,i)),this.to.next(t),e.out||(t.out=r,e.on("out",t)))}c.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||c.on,t.ask=t.ask||c.ask,t.dup=t.dup||c.dup();var n=t.gun.opt(t.opt);return t.once||(t.on("in",r,t),t.on("out",r,p(t,{out:r})),c.on("create",t),t.on("create",t)),t.once=1,n}}(),function(){function i(t,n,o,e){var i=this,r=c.state.is(o,n);if(!r)return i.err="Error: No state on '"+n+"' in node '"+e+"'!";var u=i.graph[e]||v,a=c.state.is(u,n,!0),s=u[n],f=c.HAM(i.machine,r,a,t,s);f.incoming?(i.put[e]=c.state.to(o,n,i.put[e]),(i.diff||(i.diff={}))[e]=c.state.to(o,n,i.diff[e]),i.souls[e]=!0):f.defer&&(i.defer=r<(i.defer||1/0)?r:i.defer)}function r(t,n){var o=this,e=o.gun._,i=(e.next||v)[n];if(!i){if(!(e.opt||v).super)return void(o.souls[n]=!1);i=o.gun.get(n)._}var r=o.map[n]={put:t,get:n,gun:i.gun},u={ctx:o,msg:r};o.async=!!e.tag.node,o.ack&&(r["@"]=o.ack),g(t,a,u),o.async&&(o.and||e.on("node",function(t){this.to.next(t),t===o.map[t.get]&&(o.souls[t.get]=!1,g(t.put,s,t),g(o.souls,function(t){if(t)return t})||o.c||(o.c=1,this.off(),e.stop={},g(o.map,f,o)))}),o.and=!0,e.on("node",r))}function a(t,n){var o=this.ctx,e=o.graph,i=this.msg,r=i.get,u=i.put,a=i.gun._;e[r]=c.state.to(u,n,e[r]),o.async||(a.put=c.state.to(u,n,a.put))}function s(t,n){var o=this.put,e=this.gun._;e.put=c.state.to(o,n,e.put)}function f(t,n){t.gun&&t.gun._.on("in",t)}c.on.put=function(t,n){var o=n._,e={gun:n,graph:o.graph,put:{},map:{},souls:{},machine:c.state(),ack:t["@"]};if(c.graph.is(t.put,null,i,e)||(e.err="Error: Invalid graph!"),e.err)return o.on("in",{"@":t["#"],err:c.log(e.err)});g(e.put,r,e),e.async||(o.stop={},g(e.map,f,e)),void 0!==e.defer&&setTimeout(function(){c.on.put(t,n)},e.defer-e.machine),e.diff&&o.on("put",p(t,{put:e.diff}))},c.on.get=function(t,n){var o=n._,e=t.get[h],i=o.graph[e],r=t.get[d],u=(o.next||(o.next={}))[e];if(!i||!u)return o.on("get",t);if(r){if(!l(i,r))return o.on("get",t);i=c.state.to(i,r)}else i=c.obj.copy(i);i=c.graph.node(i),o.on("in",{"@":t["#"],how:"mem",put:i,gun:n}),o.on("get",t)}}(),c.chain.opt=function(t){t=t||{};var n=this._,o=t.peers||t;return a(t)||(t={}),a(n.opt)||(n.opt=t),i(o)&&(o=[o]),e(o)&&(o=g(o,function(t,n,o){o(t,{url:t})}),a(n.opt.peers)||(n.opt.peers={}),n.opt.peers=p(o,n.opt.peers)),n.opt.peers=n.opt.peers||{},p(t,n.opt),c.on("opt",n),n.opt.uuid=n.opt.uuid||function(){return s()+u(12)},this};var e=c.list.is,o=c.text,i=o.is,u=o.random,r=c.obj,a=r.is,l=r.has,p=r.to,g=r.map,s=(r.copy,c.state.lex),h=c.val.rel._,d=".",v=(c.node._,c.val.rel.is,{});_.debug=function(t,n){return _.debug.i&&t===_.debug.i&&_.debug.i++&&(_.log.apply(_,arguments)||n)},(c.log=function(){return!c.log.off&&_.log.apply(_,arguments),[].slice.call(arguments).join(" ")}).once=function(t,n,o){return(o=c.log.once)[t]=o[t]||0,o[t]++||c.log(n)},c.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=c);try{void 0!==f&&(f.exports=c)}catch(t){}t.exports=c})(x,"./root"),x(function(t){var a=x("./root");a.chain.back=function(t,n){if(-1===(t=t||1)||1/0===t)return this._.root.gun;if(1===t)return(this._.back||this._).gun;var o=this._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){for(var e=0,i=t.length,r=o;e .once, apologies unexpected."),this.once(t,n)},s.chain.once=function(t,n){var o=this,e=o._,i=e.put;if(0=(u.batch||1e3))return f();e||(e=setTimeout(f,u.wait||1))}),r.on("get",function(t){this.to.next(t);var n,o,e=t.get;if(e&&(n=e["#"])){var i=e["."];(o=s[n]||void 0)&&i&&(o=Gun.state.to(o,i)),(o||Gun.obj.empty(u.peers))&&r.on("in",{"@":t["#"],put:Gun.graph.node(o),how:"lS",lS:t.I})}});var n=function(t,n,o,e){s[e]=Gun.state.to(o,n,s[e])},f=function(t){var o;a=0,clearTimeout(e),e=!1;var n=i;i={},t&&(s=t);try{c.setItem(u.file,JSON.stringify(s))}catch(t){Gun.log(o=t||"localStorage failure"),r.on("localStorage:error",{err:o,file:u.file,flush:s,retry:f})}(o||Gun.obj.empty(u.peers))&&Gun.obj.map(n,function(t,n){r.on("in",{"@":n,err:o,ok:0})})}}})}})(x,"./adapters/localStorage"),x(function(t){var h=x("../type");function o(p){var g=function(){};return g.out=function(t){var n;if(this.to&&this.to.next(t),(n=t["@"])&&(n=p.dup.s[n])&&(n=n.it)&&n.mesh)return g.say(t,n.mesh.via),void(n["##"]=t["##"]);g.say(t)},g.hear=function(t,n){if(t){var o,e,i,r=p.dup,u=t[0];try{i=JSON.parse(t)}catch(t){}if("{"===u){if(!i)return;if(r.check(o=i["#"]))return;if((u=(r.track(o,!0).it=i)["@"])&&i.put&&(u+=e=i["##"]||(i["##"]=g.hash(i)))!=o){if(r.check(u))return;(u=r.s)[e]=u[o]}return(i.mesh=function(){}).via=n,(u=i["><"])&&(i.mesh.to=h.obj.map(u.split(","),function(t,n,o){o(t,!0)})),void p.on("in",i)}if("["!==u);else{if(!i)return;for(var a,s=0;a=i[s++];)g.hear(a,n)}}},function(){function r(n,o){var t=o.wire;try{t.send?t.readyState===t.OPEN?t.send(n):(o.queue=o.queue||[]).push(n):o.say&&o.say(n)}catch(t){(o.queue=o.queue||[]).push(n)}}g.say=function(n,o){var t,e,i;o?(o.wire||p.opt.wire&&p.opt.wire(o))&&(e=n.mesh||u,o!==e.via&&((i=e.raw)||(i=g.raw(n)),(t=n["@"])&&(t=p.dup.s[t])&&(t=t.it)&&t.get&&t["##"]&&t["##"]===n["##"]||(t=e.to)&&(t[o.url]||t[o.id])||(o.batch?o.batch.push(i):(o.batch=[],setTimeout(function(){var t=o.batch;t&&(o.batch=null,t.length&&r(JSON.stringify(t),o))},p.opt.gap||p.opt.wait||1),r(i,o))))):h.obj.map(p.opt.peers,function(t){g.say(n,t)})}}(),function(){function f(t,n){var o;return n instanceof Object?(h.obj.map(Object.keys(n).sort(),e,{to:o={},on:n}),o):n}function e(t){this.to[t]=this.on[t]}g.raw=function(t){if(!t)return"";var n,o,e,i=p.dup,r=t.mesh||{};if(e=r.raw)return e;if("string"==typeof t)return t;t["@"]&&(e=t.put)&&((o=t["##"])||(n=c(e,f)||"",o=g.hash(t,n),t["##"]=o),(e=i.s)[o=t["@"]+o]=e[t["#"]],t["#"]=o||t["#"],n&&((t=h.obj.to(t)).put=l));var u=0,a=[];h.obj.map(p.opt.peers,function(t){if(a.push(t.url||t.id),9<++u)return!0}),t["><"]=a.join();var s=c(t);return d!==n&&(s=s.replace('"'+l+'"',n)),r&&(r.raw=s),s},g.hash=function(t,n){return o.hash(n||c(t.put,f)||"")||t["#"]||h.text.random(9)};var c=JSON.stringify,l=":])([:"}(),g.hi=function(n){p.on("hi",n);var t=n.queue;n.queue=[],h.obj.map(t,function(t){g.say(t,n)})},g}o.hash=function(t){if("string"!=typeof t)return{err:1};var n=0;if(!t.length)return n;for(var o=0,e=t.length;o")){if(!(n>t[">"]))return!1;o=!0}if(p.obj.has(t,"<")){if(!(n",s.drift=0,s.is=function(t,n,o){var e=n&&t&&t[m]&&t[m][s._]||o;if(e)return g(e=e[n])?e:-1/0},s.lex=function(){return s().toString(36).replace(".","")},s.ify=function(t,n,o,e,i){if(!t||!t[m]){if(!i)return;t=a.soul.ify(t,i)}var r=c(t[m],s._);return void 0!==n&&n!==m&&(g(o)&&(r[n]=o),void 0!==e&&(t[n]=e)),t},s.to=function(t,n,o){var e=t[n];return p(e)&&(e=d(e)),s.ify(o,n,s.is(t,n),e,a.soul(t))},function(){function u(t,n){m!==n&&s.ify(this.o,n,this.s)}s.map=function(i,r,a){var t=p(t=i||r)?t:null;return i=v(i=i||r)?i:null,t&&!i?(r=g(r)?r:s(),t[m]=t[m]||{},h(t,u,{o:t,s:r}),t):(a=a||p(r)?r:void 0,r=g(r)?r:s(),function(t,n,o,e){if(!i)return u.call({o:o,s:r},t,n),t;i.call(a||this||{},t,n,o,e),l(o,n)&&void 0===o[n]||u.call({o:o,s:r},t,n)})}}();var f=n.obj,c=f.as,l=f.has,p=f.is,h=f.map,d=f.copy,g=n.num.is,v=n.fn.is,m=a._;t.exports=s})(_,"./state"),_(function(t){var a=_("./type"),f=_("./val"),c=_("./node"),r={};!function(){function i(t,n){if(!t||n!==c.soul(t)||!c.is(t,this.fn,this.as))return!0;this.cb&&(o.n=t,o.as=this.as,this.cb.call(o.as,t,n,o))}function o(t){t&&c.is(o.n,t,o.as)}r.is=function(t,n,o,e){return!(!t||!l(t)||u(t))&&!s(t,i,{cb:n,fn:o,as:e})}}(),function(){function u(t,n){var o;return(o=function(t,n){var o,e=t.seen,i=e.length;for(;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}(t,n))?o:(n.env=t,n.soul=i,c.ify(n.obj,e,n)&&(t.graph[f.rel.is(n.rel)]=n.node),n)}function e(t,n,o){var e,i,r=this,a=r.env;if(c._===n&&h(t,f.rel._))return o._;if(e=s(t,n,o,r,a)){if(n||(r.node=r.node||o||{},h(t,c._)&&c.soul(t)&&(r.node._=d(t._)),r.node=c.soul.ify(r.node,f.rel.is(r.rel)),r.rel=r.rel||f.rel.ify(c.soul(r.node))),(i=a.map)&&(i.call(a.as||{},t,n,o,r),h(o,n))){if(void 0===(t=o[n]))return void p(o,n);if(!(e=s(t,n,o,r,a)))return}if(!n)return r.node;if(!0===e)return t;if((i=u(a,{obj:t,path:r.path.concat(n)})).node)return i.rel}}function i(t){var n=this,o=f.link.is(n.rel),e=n.env.graph;n.rel=n.rel||f.rel.ify(t),n.rel[f.rel._]=t,n.node&&n.node[c._]&&(n.node[c._][f.rel._]=t),h(e,o)&&(e[t]=e[o],p(e,o))}function s(t,n,o,e,i){var r;return!!f.is(t)||(l(t)?1:(r=i.invalid)?s(t=r.call(i.as||{},t,n,o),n,o,e,i):(i.err="Invalid value at '"+e.path.concat(n).join(".")+"'!",void(a.list.is(t)&&(i.err+=" Use `.set(item)` instead of an Array."))))}r.ify=function(t,n,o){var e={path:[],obj:t};return n?"string"==typeof n?n={soul:n}:n instanceof Function&&(n.map=n):n={},n.soul&&(e.rel=f.rel.ify(n.soul)),n.graph=n.graph||{},n.seen=n.seen||[],n.as=n.as||o,u(n,e),n.root=e.node,n.graph}}(),r.node=function(t){var n=c.soul(t);if(n)return o({},n,t)},function(){function i(t,n){var o,e;if(c._!==n)(o=f.rel.is(t))?(e=this.opt.seen[o])?this.obj[n]=e:this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt):this.obj[n]=t;else{if(u(t,f.rel._))return;this.obj[n]=d(t)}}r.to=function(t,n,o){if(t){var e={};return o=o||{seen:{}},s(t[n],i,{obj:e,graph:t,opt:o}),e}}}();a.fn.is;var n=a.obj,l=n.is,p=n.del,h=n.has,u=n.empty,o=n.put,s=n.map,d=n.copy;t.exports=r})(_,"./graph"),_(function(t){_("./onto"),t.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}}})(_,"./ask"),_(function(t){var r=_("./type");var a=r.time.is;t.exports=function(e){var i={s:{}};return e=e||{max:1e3,age:9e3},i.check=function(t){var n;return!!(n=i.s[t])&&(n.pass?n.pass=!1:i.track(t))},i.track=function(t,n){var o=i.s[t]||(i.s[t]={});return o.was=a(),n&&(o.pass=!0),i.to||(i.to=setTimeout(function(){var o=a();r.obj.map(i.s,function(t,n){e.age>o-t.was||r.obj.del(i.s,n)}),i.to=null},e.age+9)),o},i}})(_,"./dup"),_(function(t){function c(t){return t instanceof c?(this._={gun:this,$:this}).$:this instanceof c?c.create(this._={gun:this,$:this,opt:t}):new c(t)}c.is=function(t){return t instanceof c||t&&t._&&t===t._.$||!1},c.version=.9,(c.chain=c.prototype).toJSON=function(){};var n=_("./type");n.obj.to(n,c),c.HAM=_("./HAM"),c.val=_("./val"),c.node=_("./node"),c.state=_("./state"),c.graph=_("./graph"),c.on=_("./onto"),c.ask=_("./ask"),c.dup=_("./dup"),function(){function a(t){var n,o,e=this.as,i=e.at||e,r=i.$;(o=t["#"])||(o=t["#"]=u(9)),(n=i.dup).check(o)?e.out===t.out&&(t.out=void 0,this.to.next(t)):(n.track(o),i.ask(t["@"],t)||(t.get&&c.on.get(t,r),t.put&&c.on.put(t,r)),this.to.next(t),e.out||(t.out=a,i.on("out",t)))}c.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||c.on,t.ask=t.ask||c.ask,t.dup=t.dup||c.dup();var n=t.$.opt(t.opt);return t.once||(t.on("in",a,t),t.on("out",a,{at:t,out:a}),c.on("create",t),t.on("create",t)),t.once=1,n}}(),function(){function i(t,n,o,e){var i=this,r=c.state.is(o,n);if(!r)return i.err="Error: No state on '"+n+"' in node '"+e+"'!";var a=i.graph[e]||v,u=c.state.is(a,n,!0),s=a[n],f=c.HAM(i.machine,r,u,t,s);f.incoming?(i.put[e]=c.state.to(o,n,i.put[e]),(i.diff||(i.diff={}))[e]=c.state.to(o,n,i.diff[e]),i.souls[e]=!0):f.defer&&(i.defer=r<(i.defer||1/0)?r:i.defer)}function r(t,n){var o=this,e=o.$._,i=(e.next||v)[n];if(!i){if(!(e.opt||v).super)return void(o.souls[n]=!1);i=o.$.get(n)._}var r=o.map[n]={put:t,get:n,$:i.$},a={ctx:o,msg:r};o.async=!!e.tag.node,o.ack&&(r["@"]=o.ack),h(t,u,a),o.async&&(o.and||e.on("node",function(t){this.to.next(t),t===o.map[t.get]&&(o.souls[t.get]=!1,h(t.put,s,t),h(o.souls,function(t){if(t)return t})||o.c||(o.c=1,this.off(),h(o.map,f,o)))}),o.and=!0,e.on("node",r))}function u(t,n){var o=this.ctx,e=o.graph,i=this.msg,r=i.get,a=i.put,u=i.$._;e[r]=c.state.to(a,n,e[r]),o.async||(u.put=c.state.to(a,n,u.put))}function s(t,n){var o=this.put,e=this.$._;e.put=c.state.to(o,n,e.put)}function f(t,n){t.$&&(this.cat.stop=this.stop,t.$._.on("in",t),this.cat.stop=null)}c.on.put=function(t,n){var o=n._,e={$:n,graph:o.graph,put:{},map:{},souls:{},machine:c.state(),ack:t["@"],cat:o,stop:{}};if(c.graph.is(t.put,null,i,e)||(e.err="Error: Invalid graph!"),e.err)return o.on("in",{"@":t["#"],err:c.log(e.err)});h(e.put,r,e),e.async||h(e.map,f,e),void 0!==e.defer&&setTimeout(function(){c.on.put(t,n)},e.defer-e.machine),e.diff&&o.on("put",p(t,{put:e.diff}))},c.on.get=function(t,n){var o=n._,e=t.get[d],i=o.graph[e],r=t.get[g],a=(o.next||(o.next={}))[e];if(!i||!a)return o.on("get",t);if(r){if(!l(i,r))return o.on("get",t);i=c.state.to(i,r)}else i=c.obj.copy(i);i=c.graph.node(i),a.ack,o.on("in",{"@":t["#"],how:"mem",put:i,$:n}),o.on("get",t)}}(),c.chain.opt=function(t){t=t||{};var n=this._,o=t.peers||t;return a(t)||(t={}),a(n.opt)||(n.opt=t),i(o)&&(o=[o]),e(o)&&(o=h(o,function(t,n,o){o(t,{url:t})}),a(n.opt.peers)||(n.opt.peers={}),n.opt.peers=p(o,n.opt.peers)),n.opt.peers=n.opt.peers||{},p(t,n.opt),c.on("opt",n),n.opt.uuid=n.opt.uuid||function(){return s()+u(12)},this};var e=c.list.is,o=c.text,i=o.is,u=o.random,r=c.obj,a=r.is,l=r.has,p=r.to,h=r.map,s=(r.copy,c.state.lex),d=c.val.rel._,g=".",v=(c.node._,c.val.link.is,{});b.debug=function(t,n){return b.debug.i&&t===b.debug.i&&b.debug.i++&&(b.log.apply(b,arguments)||n)},(c.log=function(){return!c.log.off&&b.log.apply(b,arguments),[].slice.call(arguments).join(" ")}).once=function(t,n,o){return(o=c.log.once)[t]=o[t]||0,o[t]++||c.log(n)},c.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=c);try{void 0!==f&&(f.exports=c)}catch(t){}t.exports=c})(_,"./root"),_(function(t){var u=_("./root");u.chain.back=function(t,n){if(-1===(t=t||1)||1/0===t)return this._.root.$;if(1===t)return(this._.back||this._).$;var o=this._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){for(var e=0,i=t.length,r=o;e .once, apologies unexpected."),this.once(t,n)},a.chain.once=function(t,n){var o=this,e=o._,i=e.put;if(0=(a.batch||1e3))return f();e||(e=setTimeout(f,a.wait||1))}),r.on("get",function(n){this.to.next(n);var o,e,i=n.get;function t(){if(i&&(o=i["#"])){var t=i["."];(e=s[o]||void 0)&&t&&(e=Gun.state.to(e,t)),(e||Gun.obj.empty(a.peers))&&r.on("in",{"@":n["#"],put:Gun.graph.node(e),how:"lS",lS:n.I})}}Gun.debug?setTimeout(t,1):t()});var n=function(t,n,o,e){s[e]=Gun.state.to(o,n,s[e])},f=function(t){var o;u=0,clearTimeout(e),e=!1;var n=i;i={},t&&(s=t);try{c.setItem(a.file,JSON.stringify(s))}catch(t){Gun.log(o=t||"localStorage failure"),r.on("localStorage:error",{err:o,file:a.file,flush:s,retry:f})}(o||Gun.obj.empty(a.peers))&&Gun.obj.map(n,function(t,n){r.on("in",{"@":n,err:o,ok:0})})}}})}})(_,"./adapters/localStorage"),_(function(t){var d=_("../type");function o(p){var h=function(){};return h.out=function(t){var n;if(this.to&&this.to.next(t),(n=t["@"])&&(n=p.dup.s[n])&&(n=n.it)&&n.mesh)return h.say(t,n.mesh.via),void(n["##"]=t["##"]);h.say(t)},h.hear=function(t,n){if(t){var o,e,i,r=p.dup,a=t[0];try{i=JSON.parse(t)}catch(t){}if("{"===a){if(!i)return;if(r.check(o=i["#"]))return;if((a=(r.track(o,!0).it=i)["@"])&&i.put&&(a+=e=i["##"]||(i["##"]=h.hash(i)))!=o){if(r.check(a))return;(a=r.s)[e]=a[o]}return(i.mesh=function(){}).via=n,(a=i["><"])&&(i.mesh.to=d.obj.map(a.split(","),function(t,n,o){o(t,!0)})),void p.on("in",i)}if("["!==a);else{if(!i)return;for(var u,s=0;u=i[s++];)h.hear(u,n)}}},function(){function r(n,o){var t=o.wire;try{t.send?t.readyState===t.OPEN?t.send(n):(o.queue=o.queue||[]).push(n):o.say&&o.say(n)}catch(t){(o.queue=o.queue||[]).push(n)}}h.say=function(n,o){var t,e,i;o?(o.wire||p.opt.wire&&p.opt.wire(o))&&(e=n.mesh||a,o!==e.via&&((i=e.raw)||(i=h.raw(n)),(t=n["@"])&&(t=p.dup.s[t])&&(t=t.it)&&t.get&&t["##"]&&t["##"]===n["##"]||(t=e.to)&&(t[o.url]||t[o.id])||(o.batch?o.batch.push(i):(o.batch=[],setTimeout(function(){var t=o.batch;t&&(o.batch=null,t.length&&r(JSON.stringify(t),o))},p.opt.gap||p.opt.wait||1),r(i,o))))):d.obj.map(p.opt.peers,function(t){h.say(n,t)})}}(),function(){function f(t,n){var o;return n instanceof Object?(d.obj.map(Object.keys(n).sort(),e,{to:o={},on:n}),o):n}function e(t){this.to[t]=this.on[t]}h.raw=function(t){if(!t)return"";var n,o,e,i=p.dup,r=t.mesh||{};if(e=r.raw)return e;if("string"==typeof t)return t;t["@"]&&(e=t.put)&&((o=t["##"])||(n=c(e,f)||"",o=h.hash(t,n),t["##"]=o),(e=i.s)[o=t["@"]+o]=e[t["#"]],t["#"]=o||t["#"],n&&((t=d.obj.to(t)).put=l));var a=0,u=[];d.obj.map(p.opt.peers,function(t){if(u.push(t.url||t.id),9<++a)return!0}),t["><"]=u.join();var s=c(t);return g!==n&&(s=s.replace('"'+l+'"',n)),r&&(r.raw=s),s},h.hash=function(t,n){return o.hash(n||c(t.put,f)||"")||t["#"]||d.text.random(9)};var c=JSON.stringify,l=":])([:"}(),h.hi=function(n){p.on("hi",n);var t=n.queue;n.queue=[],d.obj.map(t,function(t){h.say(t,n)})},h}o.hash=function(t){if("string"!=typeof t)return{err:1};var n=0;if(!t.length)return n;for(var o=0,e=t.length;o { cat.ing = false; resolve({ ok: 0, pub: pairs.pub}) }, 10) // TODO: BUG! If `.auth` happens synchronously after `create` finishes, auth won't work. This setTimeout is a temporary hack until we can properly fix it. } catch (e) { diff --git a/sea/https.js b/sea/https.js index 86d83150..1c7fddf9 100644 --- a/sea/https.js +++ b/sea/https.js @@ -4,7 +4,7 @@ if(location.protocol.indexOf('s') < 0 && location.host.indexOf('localhost') < 0 && location.protocol.indexOf('file:') < 0){ - location.protocol = 'https:'; + location.protocol = 'https:'; // WebCrypto does NOT work without HTTPS! } } \ No newline at end of file diff --git a/sea/index.js b/sea/index.js index f6c9cddd..c11292e4 100644 --- a/sea/index.js +++ b/sea/index.js @@ -30,7 +30,7 @@ function each(msg){ // TODO: Warning: Need to switch to `gun.on('node')`! Do not use `Gun.on('node'` in your apps! // NOTE: THE SECURITY FUNCTION HAS ALREADY VERIFIED THE DATA!!! // WE DO NOT NEED TO RE-VERIFY AGAIN, JUST TRANSFORM IT TO PLAINTEXT. - var to = this.to, vertex = (msg.gun._).put, c = 0, d; + var to = this.to, vertex = (msg.$._).put, c = 0, d; Gun.node.is(msg.put, function(val, key, node){ c++; // for each property on the node // TODO: consider async/await use here... SEA.verify(val, false, function(data){ c--; // false just extracts the plain data. @@ -96,12 +96,12 @@ }; each.alias = function(val, key, node, soul){ // Example: {_:#~@, ~@alice: {#~@alice}} if(!val){ return each.end({err: "Data must exist!"}) } // data MUST exist - if('~@'+key === Gun.val.rel.is(val)){ return check['alias'+key] = 0 } // in fact, it must be EXACTLY equal to itself + if('~@'+key === Gun.val.link.is(val)){ return check['alias'+key] = 0 } // in fact, it must be EXACTLY equal to itself each.end({err: "Mismatching alias."}); // if it isn't, reject. }; each.pubs = function(val, key, node, soul){ // Example: {_:#~@alice, ~asdf: {#~asdf}} if(!val){ return each.end({err: "Alias must exist!"}) } // data MUST exist - if(key === Gun.val.rel.is(val)){ return check['pubs'+soul+key] = 0 } // and the ID must be EXACTLY equal to its property + if(key === Gun.val.link.is(val)){ return check['pubs'+soul+key] = 0 } // and the ID must be EXACTLY equal to its property each.end({err: "Alias must match!"}); // that way nobody can tamper with the list of public keys. }; each.pub = function(val, key, node, soul, pub, user){ // Example: {_:#~asdf, hello:SEA{'world',fdsa}} @@ -114,7 +114,7 @@ //var id = Gun.text.random(3); SEA.sign(val, user.sea, function(data){ var rel; if(u === data){ return each.end({err: SEA.err || 'Pub signature fail.'}) } - if(rel = Gun.val.rel.is(val)){ + if(rel = Gun.val.link.is(val)){ (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true; } node[key] = data; @@ -128,7 +128,7 @@ if(u === data){ // make sure the signature matches the account it claims to be on. return each.end({err: "Unverified data."}); // reject any updates that are signed with a mismatched account. } - if((rel = Gun.val.rel.is(data)) && pub === relpub(rel)){ + if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){ (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true; } check['user'+soul+key] = 0; @@ -150,7 +150,7 @@ check['any'+soul+key] = 1; SEA.verify(val, pub = tmp, function(data){ var rel; if(!data){ return each.end({err: "Mismatched owner on '" + key + "'."}) } - if((rel = Gun.val.rel.is(data)) && pub === relpub(rel)){ + if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){ (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true; } check['any'+soul+key] = 0; diff --git a/sea/leave.js b/sea/leave.js index db7fcf43..df497b8f 100644 --- a/sea/leave.js +++ b/sea/leave.js @@ -6,8 +6,8 @@ const authLeave = async (gunRoot, alias = gunRoot._.user._.alias) => { var user = gunRoot._.user._ || {}; [ 'get', 'soul', 'ack', 'put', 'is', 'alias', 'pub', 'epub', 'sea' ].map((key) => delete user[key]) - if(user.gun){ - delete user.gun.is; + if(user.$){ + delete user.$.is; } // Let's use default gunRoot.user(); diff --git a/sea/login.js b/sea/login.js index 457d7299..6d9fa0c4 100644 --- a/sea/login.js +++ b/sea/login.js @@ -6,7 +6,7 @@ // add our credentials in-memory only to our root gun instance //var tmp = user._.tag; var opt = user._.opt; - user._ = key.at.gun._; + user._ = key.at.$._; user._.opt = opt; //user._.tag = tmp || user._.tag; // so that way we can use the credentials to encrypt/decrypt data diff --git a/sea/user.js b/sea/user.js index 8f24188b..05a91f1c 100644 --- a/sea/user.js +++ b/sea/user.js @@ -4,7 +4,7 @@ var then = require('./then'); function User(){ - this._ = {gun: this} + this._ = {$: this} Gun.call() } User.prototype = (function(){ function F(){}; F.prototype = Gun.chain; return new F() }()) // Object.create polyfill @@ -27,5 +27,6 @@ } return user; } + Gun.User = User; module.exports = User; \ No newline at end of file diff --git a/src/adapters/localStorage.js b/src/adapters/localStorage.js index 8ddbda82..c3672909 100644 --- a/src/adapters/localStorage.js +++ b/src/adapters/localStorage.js @@ -30,7 +30,7 @@ Gun.on('create', function(root){ }); }); setTimeout(function(){ - root.on('out', {put: send, '#': root.ask(ack), I: root.gun}); + root.on('out', {put: send, '#': root.ask(ack), I: root.$}); },10); }); } @@ -101,7 +101,7 @@ Gun.on('create', function(root){ root.on('get', function(msg){ this.to.next(msg); var lex = msg.get, soul, data, u; - //setTimeout(function(){ + function to(){ if(!lex || !(soul = lex['#'])){ return } //if(0 >= msg.cap){ return } var has = lex['.']; @@ -112,8 +112,10 @@ Gun.on('create', function(root){ 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? } + //console.log("lS get", lex, data); root.on('in', {'@': msg['#'], put: Gun.graph.node(data), how: 'lS', lS: msg.I}); - //},1); + }; + Gun.debug? setTimeout(to,1) : to(); }); var map = function(val, key, node, soul){ diff --git a/src/adapters/mesh.js b/src/adapters/mesh.js index 042b8e59..8c5d4fb8 100644 --- a/src/adapters/mesh.js +++ b/src/adapters/mesh.js @@ -5,7 +5,6 @@ function Mesh(ctx){ var mesh = function(){}; mesh.out = function(msg){ var tmp; - //console.log("count:", msg['#'], msg); if(this.to){ this.to.next(msg) } //if(mesh.last != msg['#']){ return mesh.last = msg['#'], this.to.next(msg) } if((tmp = msg['@']) @@ -81,7 +80,6 @@ function Mesh(ctx){ } } if((tmp = msh.to) && (tmp[peer.url] || tmp[peer.id])){ return } // TODO: still needs to be tested - //console.log('out', JSON.parse(raw)); if(peer.batch){ peer.batch.push(raw); return; @@ -102,7 +100,6 @@ function Mesh(ctx){ try{ if(wire.send){ if(wire.readyState === wire.OPEN){ - //console.log("send:", raw); wire.send(raw); } else { (peer.queue = peer.queue || []).push(raw); diff --git a/src/adapters/websocket.js b/src/adapters/websocket.js index d08f7c99..9464bb7b 100644 --- a/src/adapters/websocket.js +++ b/src/adapters/websocket.js @@ -43,7 +43,6 @@ Gun.on('opt', function(root){ mesh.hi(peer); } wire.onmessage = function(msg){ - //console.log('in', JSON.parse(msg.data || msg)); if(!msg){ return } env.inLength = (env.inLength || 0) + (msg.data || msg).length; // TEMPORARY, NON-STANDARD, FOR DEBUG mesh.hear(msg.data || msg, peer); diff --git a/src/back.js b/src/back.js index 8990a46d..32325b6f 100644 --- a/src/back.js +++ b/src/back.js @@ -3,10 +3,10 @@ var Gun = require('./root'); Gun.chain.back = function(n, opt){ var tmp; n = n || 1; if(-1 === n || Infinity === n){ - return this._.root.gun; + return this._.root.$; } else if(1 === n){ - return (this._.back || this._).gun; + return (this._.back || this._).$; } var gun = this, at = gun._; if(typeof n === 'string'){ @@ -21,18 +21,18 @@ Gun.chain.back = function(n, opt){ var tmp; return opt? gun : tmp; } else if((tmp = at.back)){ - return tmp.gun.back(n, opt); + return tmp.$.back(n, opt); } return; } if(n instanceof Function){ var yes, tmp = {back: at}; while((tmp = tmp.back) - && !(yes = n(tmp, opt))){} + && u === (yes = n(tmp, opt))){} return yes; } if(Gun.num.is(n)){ - return (at.back || at).gun.back(n - 1); + return (at.back || at).$.back(n - 1); } return this; } diff --git a/src/chain.js b/src/chain.js index f76b0823..f2c5d8b0 100644 --- a/src/chain.js +++ b/src/chain.js @@ -15,56 +15,59 @@ Gun.chain.chain = function(sub){ } function output(msg){ - var put, get, at = this.as, back = at.back, root = at.root; - if(!msg.I){ msg.I = at.gun } - if(!msg.gun){ msg.gun = at.gun } + var put, get, at = this.as, back = at.back, root = at.root, tmp; + if(!msg.I){ msg.I = at.$ } + if(!msg.$){ msg.$ = at.$ } this.to.next(msg); if(get = msg.get){ /*if(u !== at.put){ at.on('in', at); return; }*/ + //console.log("out!", at.get, get); if(get['#'] || at.soul){ get['#'] = get['#'] || at.soul; msg['#'] || (msg['#'] = text_rand(9)); - back = (root.gun.get(get['#'])._); + back = (root.$.get(get['#'])._); if(!(get = get['.'])){ + tmp = back.ack; + if(!tmp){ back.ack = -1 } if(obj_has(back, 'put')){ - //if(u !== back.put){ back.on('in', back); } - if(back.ack){ return } - msg.gun = back.gun; - back.ack = -1; + if(tmp){ return } + msg.$ = back.$; } else if(obj_has(back.put, get)){ + put = (back.$.get(get)._); + if(!(tmp = put.ack)){ put.ack = -1 } back.on('in', { - gun: back.gun, + $: back.$, put: Gun.state.to(back.put, get), get: back.get }); - return; + if(tmp){ return } } root.ask(ack, msg); return root.on('in', msg); } - if(root.now){ - root.now[at.id] = root.now[at.id] || true; - } + if(root.now){ root.now[at.id] = root.now[at.id] || true; at.pass = {} } if(get['.']){ if(at.get){ - msg = {get: {'.': at.get}, gun: at.gun}; - (back.ask || (back.ask = {}))[at.get] = msg.gun._; // TODO: PERFORMANCE? More elegant way? + msg = {get: {'.': at.get}, $: at.$}; + //if(back.ask || (back.ask = {})[at.get]){ return } + (back.ask || (back.ask = {})); + back.ask[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way? return back.on('out', msg); } - msg = {get: {}, gun: at.gun}; + msg = {get: {}, $: at.$}; return back.on('out', msg); } at.ack = at.ack || -1; if(at.get){ - msg.gun = at.gun; + msg.$ = at.$; get['.'] = at.get; - (back.ask || (back.ask = {}))[at.get] = msg.gun._; // TODO: PERFORMANCE? More elegant way? + (back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way? return back.on('out', msg); } } @@ -72,25 +75,23 @@ function output(msg){ } function input(msg){ - var ev = this, cat = this.as, gun = msg.gun, at = gun._, change = msg.put, rel, tmp; + var eve = this, cat = eve.as, root = cat.root, gun = msg.$, at = (gun||empty)._ || empty, change = msg.put, rel, tmp; if(cat.get && msg.get !== cat.get){ msg = obj_to(msg, {get: cat.get}); } if(cat.has && at !== cat){ - msg = obj_to(msg, {gun: cat.gun}); + msg = obj_to(msg, {$: cat.$}); 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.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(msg); - if(cat.soul){ return } // TODO: BUG, I believe the fresh input refactor caught an edge case that a `gun.get('soul').get('key')` that points to a soul that doesn't exist will not trigger val/get etc. - echo(cat, msg, ev); + tmp = at.put; + eve.to.next(msg); + if(cat.soul){ return } // TODO: BUG, I believee the fresh input refactor caught an edge case that a `gun.get('soul').get('key')` that points to a soul that doesn't exist will not trigger val/get etc. + if(u === tmp && u !== at.put){ return } + echo(cat, msg, eve); if(cat.has){ not(cat, msg); } @@ -99,45 +100,57 @@ function input(msg){ return; } if(cat.soul){ - ev.to.next(msg); - echo(cat, msg, ev); - obj_map(change, map, {at: msg, cat: cat}); + eve.to.next(msg); + echo(cat, msg, eve); + if(cat.next){ obj_map(change, map, {msg: msg, cat: cat}) } return; } - if(!(rel = Gun.val.rel.is(change))){ + if(!(rel = Gun.val.link.is(change))){ if(Gun.val.is(change)){ if(cat.has || cat.soul){ not(cat, msg); } else if(at.has || at.soul){ - (at.echo || (at.echo = {}))[cat.id] = cat; + (at.echo || (at.echo = {}))[cat.id] = at.echo[at.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(msg); - echo(cat, msg, ev); + eve.to.next(msg); + echo(cat, msg, eve); return; } if(cat.has && at !== cat && obj_has(at, 'put')){ cat.put = at.put; }; if((rel = Gun.node.soul(change)) && at.has){ - at.put = (cat.root.gun.get(rel)._).put; + at.put = (cat.root.$.get(rel)._).put; } - ev.to.next(msg); - echo(cat, msg, ev); + tmp = (root.stop || {})[at.id]; + //if(tmp && tmp[cat.id]){ } else { + eve.to.next(msg); + //} relate(cat, msg, at, rel); - obj_map(change, map, {at: msg, cat: cat}); + echo(cat, msg, eve); + if(cat.next){ obj_map(change, map, {msg: msg, cat: cat}) } return; } + var was = root.stop; + tmp = root.stop || {}; + tmp = tmp[at.id] || (tmp[at.id] = {}); + //if(tmp[cat.id]){ return } + tmp.is = tmp.is || at.put; + tmp[cat.id] = at.put || true; + //if(root.stop){ + eve.to.next(msg) + //} relate(cat, msg, at, rel); - ev.to.next(msg); - echo(cat, msg, ev); + echo(cat, msg, eve); } +var C = 0; function relate(at, msg, from, rel){ if(!rel || node_ === at.get){ return } - var tmp = (at.root.gun.get(rel)._); + var tmp = (at.root.$.get(rel)._); if(at.has){ from = tmp; } else @@ -145,61 +158,61 @@ function relate(at, msg, from, rel){ relate(from, msg, from, rel); } if(from === at){ return } - (from.echo || (from.echo = {}))[at.id] = at; + if(!from.$){ from = {} } + (from.echo || (from.echo = {}))[at.id] = from.echo[at.id] || at; if(at.has && !(at.map||empty)[from.id]){ // if we haven't seen this before. not(at, msg); } - tmp = (at.map || (at.map = {}))[from.id] = at.map[from.id] || {at: from}; - var now = at.root.now; - //now = now || at.root.stop; - if(rel === tmp.rel){ - // NOW is a hack to get synchronous replies to correctly call. - // and STOP is a hack to get async behavior to correctly call. - // neither of these are ideal, need to be fixed without hacks, - // but for now, this works for current tests. :/ - if(!now){ + tmp = from.id? ((at.map || (at.map = {}))[from.id] = at.map[from.id] || {at: from}) : {}; + //console.log("REL?", at.id, at.get, rel === tmp.link, tmp.pass || at.pass); + if(rel === tmp.link){ + if(!(tmp.pass || at.pass)){ return; - /*var stop = at.root.stop; - if(!stop){ return } - if(stop[at.id] === rel){ return } - stop[at.id] = rel;*/ - } else { - if(u === now[at.id]){ return } - if((now._ || (now._ = {}))[at.id] === rel){ return } - now._[at.id] = rel; } } - ask(at, tmp.rel = rel); + if(at.pass){ + Gun.obj.map(at.map, function(tmp){ tmp.pass = true }) + obj_del(at, 'pass'); + } + if(tmp.pass){ obj_del(tmp, 'pass') } + if(at.has){ at.link = rel } + ask(at, tmp.link = rel); } function echo(at, msg, ev){ if(!at.echo){ return } // || node_ === at.get ? - if(at.has){ msg = obj_to(msg, {event: ev}) } + //if(at.has){ msg = obj_to(msg, {event: ev}) } obj_map(at.echo, reverb, msg); } function reverb(to){ + if(!to || !to.on){ return } to.on('in', this); } function map(data, key){ // Map over only the changes on every update. - var cat = this.cat, next = cat.next || empty, via = this.at, chain, at, tmp; + var cat = this.cat, next = cat.next || empty, via = this.msg, chain, at, tmp; if(node_ === key && !next[key]){ return } if(!(at = next[key])){ return; } - //if(data && data[_soul] && (tmp = Gun.val.rel.is(data)) && (tmp = (cat.root.gun.get(tmp)._)) && obj_has(tmp, 'put')){ + //if(data && data[_soul] && (tmp = Gun.val.rel.is(data)) && (tmp = (cat.root.$.get(tmp)._)) && obj_has(tmp, 'put')){ // data = tmp.put; //} if(at.has){ - if(!(data && data[_soul] && Gun.val.rel.is(data) === Gun.node.soul(at.put))){ + //if(!(data && data[_soul] && Gun.val.rel.is(data) === Gun.node.soul(at.put))){ + if(u === at.put || !Gun.val.link.is(data)){ at.put = data; } - chain = at.gun; - } else { - chain = via.gun.get(key); + chain = at.$; + } else + if(tmp = via.$){ + tmp = (chain = via.$.get(key))._; + if(u === tmp.put || !Gun.val.link.is(data)){ + tmp.put = data; + } } at.on('in', { put: data, get: key, - gun: chain, + $: chain, via: via }); } @@ -207,63 +220,62 @@ function not(at, msg){ if(!(at.has || at.soul)){ return } var tmp = at.map, root = at.root; at.map = null; - if(!root.now || !root.now[at.id]){ + if(at.has){ at.link = null } + //if(!root.now || !root.now[at.id]){ + if(!at.pass){ if((!msg['@']) && null === tmp){ return } + //obj_del(at, 'pass'); } - if(u === tmp && Gun.val.rel.is(at.put)){ return } // TODO: Bug? Threw second condition in for a particular test, not sure if a counter example is tested though. + if(u === tmp && Gun.val.link.is(at.put)){ return } // This prevents the very first call of a thing from triggering a "clean up" call. // TODO: link.is(at.put) || !val.is(at.put) ? obj_map(tmp, function(proxy){ if(!(proxy = proxy.at)){ return } obj_del(proxy.echo, at.id); }); + tmp = at.put; obj_map(at.next, function(neat, key){ + if(u === tmp && u !== at.put){ return true } neat.put = u; if(neat.ack){ neat.ack = -1; } neat.on('in', { get: key, - gun: neat.gun, + $: neat.$, put: u }); }); } function ask(at, soul){ - var tmp = (at.root.gun.get(soul)._); + var tmp = (at.root.$.get(soul)._); if(at.ack){ tmp.on('out', {get: {'#': soul}}); if(!at.ask){ return } // TODO: PERFORMANCE? More elegant way? } - obj_map(at.ask || at.next, function(neat, key){ - //(tmp.gun.get(key)._).on('out', {get: {'#': soul, '.': key}}); - //tmp.on('out', {get: {'#': soul, '.': key}}); + tmp = at.ask; Gun.obj.del(at, 'ask'); + obj_map(tmp || at.next, function(neat, key){ neat.on('out', {get: {'#': soul, '.': key}}); - //at.on('out', {get: {'#': soul, '.': key}}); }); Gun.obj.del(at, 'ask'); // TODO: PERFORMANCE? More elegant way? } function ack(msg, ev){ - var as = this.as, get = as.get || empty, at = as.gun._, tmp = (msg.put||empty)[get['#']]; - if(at.ack){ at.ack = (at.ack + 1) || 1 } - if(!msg.put /*|| node_ == get['.']*/ || (get['.'] && !obj_has(tmp, at.get))){ + var as = this.as, get = as.get || empty, at = as.$._, tmp = (msg.put||empty)[get['#']]; + if(at.ack){ at.ack = (at.ack + 1) || 1; } + if(!msg.put || (get['.'] && !obj_has(tmp, at.get))){ if(at.put !== u){ return } - //at.ack = 0; at.on('in', { get: at.get, put: at.put = u, - gun: at.gun, + $: at.$, '@': msg['@'] - }) + }); return; } if(node_ == get['.']){ // is this a security concern? - at.on('in', {get: at.get, put: tmp[at.get], gun: at.gun, '@': msg['@']}); + at.on('in', {get: at.get, put: Gun.val.link.ify(get['#']), $: at.$, '@': msg['@']}); return; } - //if(/*!msg.gun &&*/ !get['.'] && get['#']){ at.ack = (at.ack + 1) || 1 } - //msg = obj_to(msg); - msg.gun = at.root.gun; - //Gun.on('put', at); - Gun.on.put(msg, at.root.gun); + msg.$ = at.root.$; + Gun.on.put(msg, at.root.$); } 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; diff --git a/src/get.js b/src/get.js index 9a199ad4..54d2e9e1 100644 --- a/src/get.js +++ b/src/get.js @@ -8,18 +8,22 @@ Gun.chain.get = function(key, cb, as){ if(!(gun = next[key])){ gun = cache(key, back); } - gun = gun.gun; + gun = gun.$; } else if(key instanceof Function){ + if(true === cb){ return soul(this, key, cb, as) } gun = this; var at = gun._, root = at.root, tmp = root.now, ev; as = cb || {}; + as.at = at; as.use = key; as.out = as.out || {}; as.out.get = as.out.get || {}; - ev = at.on('in', use, as); + (ev = at.on('in', use, as)).rid = rid; (root.now = {$:1})[as.now = at.id] = ev; + var mum = root.mum; root.mum = {}; at.on('out', as.out); + root.mum = mum; root.now = tmp; return gun; } else @@ -45,7 +49,7 @@ function cache(key, back){ var cat = back._, next = cat.next, gun = back.chain(), at = gun._; if(!next){ next = cat.next = {} } next[at.get = key] = at; - if(back === cat.root.gun){ + if(back === cat.root.$){ at.soul = key; } else if(cat.soul || cat.has){ @@ -56,25 +60,65 @@ function cache(key, back){ } return at; } +function soul(gun, cb, opt, as){ + var cat = gun._, tmp; + if(tmp = cat.soul){ return cb(tmp, as, cat), gun } + if(tmp = cat.link){ return cb(tmp, as, cat), gun } + gun.get(function(msg, ev){ + ev.rid(msg); + var at = ((at = msg.$) && at._) || {}; + tmp = at.link || at.soul || rel.is(msg.put) || node_soul(msg.put); + cb(tmp, as, msg, ev); + }, {out: {get: {'.':true}}}); + return gun; +} function use(msg){ - var ev = this, as = ev.as, gun = msg.gun, at = gun._, root = at.root, data = msg.put, tmp; - if((tmp = root.now) && ev !== tmp[as.now]){ - return ev.to.next(msg); - } - if(u === data){ - data = at.put; - } + var eve = this, as = eve.as, cat = as.at, root = cat.root, gun = msg.$, at = (gun||{})._ || {}, data = msg.put || at.put, tmp; + if((tmp = root.now) && eve !== tmp[as.now]){ return eve.to.next(msg) } + //console.log("USE:", cat.id, cat.soul, cat.has, cat.get, msg, root.mum); + //if(at.async && msg.root){ return } + //if(at.async === 1 && cat.async !== true){ return } + //if(root.stop && root.stop[at.id]){ return } root.stop && (root.stop[at.id] = true); + //if(!at.async && !cat.async && at.put && msg.put === at.put){ return } + //else if(!cat.async && msg.put !== at.put && root.stop && root.stop[at.id]){ return } root.stop && (root.stop[at.id] = true); + + + //root.stop && (root.stop.ID = root.stop.ID || Gun.text.random(2)); + //if((tmp = root.stop) && (tmp = tmp[at.id] || (tmp[at.id] = {})) && tmp[cat.id]){ return } tmp && (tmp[cat.id] = true); + if(eve.seen && at.id && eve.seen[at.id]){ return eve.to.next(msg) } + //if((tmp = root.stop)){ if(tmp[at.id]){ return } tmp[at.id] = msg.root; } // temporary fix till a better solution? if((tmp = data) && tmp[rel._] && (tmp = rel.is(tmp))){ - tmp = (at.root.gun.get(tmp)._); + tmp = ((msg.$$ = at.root.gun.get(tmp))._); if(u !== tmp.put){ - msg = obj_to(msg, {put: tmp.put}); + msg = obj_to(msg, {put: data = tmp.put}); } } - as.use(msg, msg.event || ev); - ev.to.next(msg); + if((tmp = root.mum) && at.id){ + if(tmp[at.id]){ return } + if(u !== data && !rel.is(data)){ tmp[at.id] = true; } + } + as.use(msg, eve); + if(eve.stun){ + eve.stun = null; + return; + } + eve.to.next(msg); +} +function rid(at){ + var cat = this.on; + if(!at || cat.soul || cat.has){ return this.off() } + if(!(at = (at = (at = at.$ || at)._ || at).id)){ return } + var map = cat.map, tmp, seen; + //if(!map || !(tmp = map[at]) || !(tmp = tmp.at)){ return } + if(tmp = (seen = this.seen || (this.seen = {}))[at]){ return true } + seen[at] = true; + return; + //tmp.echo[cat.id] = {}; // TODO: Warning: This unsubscribes ALL of this chain's listeners from this link, not just the one callback event. + //obj.del(map, at); // TODO: Warning: This unsubscribes ALL of this chain's listeners from this link, not just the one callback event. + return; } var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to; var num_is = Gun.num.is; -var rel = Gun.val.rel, node_ = Gun.node._; +var rel = Gun.val.link, node_soul = Gun.node.soul, node_ = Gun.node._; var empty = {}, u; \ No newline at end of file diff --git a/src/graph.js b/src/graph.js index 828a292f..49b305ed 100644 --- a/src/graph.js +++ b/src/graph.js @@ -58,7 +58,7 @@ var Graph = {}; if(!(is = valid(v,k,n, at,env))){ return } if(!k){ at.node = at.node || n || {}; - if(obj_has(v, Node._)){ // && Node.soul(v) ? for safety ? + if(obj_has(v, Node._) && Node.soul(v)){ // ? for safety ? at.node._ = obj_copy(v._); } at.node = Node.soul.ify(at.node, Val.rel.is(at.rel)); @@ -84,7 +84,7 @@ var Graph = {}; return tmp.rel; //{'#': Node.soul(tmp.node)}; } function soul(id){ var at = this; - var prev = Val.rel.is(at.rel), graph = at.env.graph; + var prev = Val.link.is(at.rel), graph = at.env.graph; at.rel = at.rel || Val.rel.ify(id); at.rel[Val.rel._] = id; if(at.node && at.node[Node._]){ diff --git a/src/map.js b/src/map.js index 741a4b91..04cc4518 100644 --- a/src/map.js +++ b/src/map.js @@ -3,10 +3,9 @@ var Gun = require('./index'); Gun.chain.map = function(cb, opt, t){ var gun = this, cat = gun._, chain; if(!cb){ - if(chain = cat.fields){ return chain } - chain = cat.fields = gun.chain(); - chain._.val = gun.back('val'); - chain._.MAPOF = cat.soul; + if(chain = cat.each){ return chain } + cat.each = chain = gun.chain(); + chain._.nix = gun.back('nix'); gun.on('in', map, chain._); return chain; } @@ -15,24 +14,24 @@ Gun.chain.map = function(cb, opt, t){ gun.map().on(function(data, key, at, ev){ var next = (cb||noop).call(this, data, key, at, ev); if(u === next){ return } - if(Gun.is(next)){ + if(data === next || Gun.is(next)){ chain._.on('in', next._); return; } - chain._.on('in', {get: key, put: next, gun: chain}); + chain._.on('in', {get: key, put: next}); }); return chain; } function map(msg){ - if(!msg.put || Gun.val.is(msg.put)){ return } - if(this.as.val){ this.off() } // TODO: Ugly hack! + if(!msg.put || Gun.val.is(msg.put)){ return this.to.next(msg) } + if(this.as.nix){ this.off() } // TODO: Ugly hack! obj_map(msg.put, each, {at: this.as, msg: msg}); this.to.next(msg); } 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 msg = this.msg, gun = msg.$, at = this.at, tmp = (gun.get(k)._); + (tmp.echo || (tmp.echo = {}))[at.id] = tmp.echo[at.id] || at; } var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u; \ No newline at end of file diff --git a/src/node.js b/src/node.js index 0a016d26..66472d1f 100644 --- a/src/node.js +++ b/src/node.js @@ -10,7 +10,7 @@ Node.soul.ify = function(n, o){ // put a soul on an object. n._[soul_] = o.soul || n._[soul_] || text_random(); // put the soul on it. return n; } -Node.soul._ = Val.rel._; +Node.soul._ = Val.link._; ;(function(){ Node.is = function(n, cb, as){ var s; // checks to see if an object is a valid node. if(!obj_is(n)){ return false } // must be an object. diff --git a/src/on.js b/src/on.js index d153bfb5..9979c8e0 100644 --- a/src/on.js +++ b/src/on.js @@ -5,50 +5,45 @@ Gun.chain.on = function(tag, arg, eas, as){ if(typeof tag === 'string'){ if(!arg){ return at.on(tag) } act = at.on(tag, arg, eas || at, as); - if(eas && eas.gun){ + if(eas && eas.$){ (eas.subs || (eas.subs = [])).push(act); } - off = function() { - if (act && act.off) act.off(); - off.off(); - }; - off.off = gun.off.bind(gun) || noop; - gun.off = off; return gun; } var opt = arg; opt = (true === opt)? {change: true} : opt || {}; + opt.at = at; opt.ok = tag; - opt.last = {}; + //opt.last = {}; gun.get(ok, opt); // TODO: PERF! Event listener leak!!!? return gun; } -function ok(at, ev){ var opt = this; - var gun = at.gun, cat = gun._, data = cat.put || at.put, tmp = opt.last, id = cat.id+at.get, tmp; +function ok(msg, ev){ var opt = this; + var gun = msg.$, at = (gun||{})._ || {}, data = at.put || msg.put, cat = opt.at, tmp; if(u === data){ return; } - if(data && data[rel._] && (tmp = rel.is(data))){ - tmp = (cat.root.gun.get(tmp)._); + if(tmp = msg.$$){ + tmp = (msg.$$._); if(u === tmp.put){ return; } data = tmp.put; } if(opt.change){ // TODO: BUG? Move above the undef checks? - data = at.put; + data = msg.put; } // DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE - if(tmp.put === data && tmp.get === id && !Gun.node.soul(data)){ return } - tmp.put = data; - tmp.get = id; + //if(tmp.put === data && tmp.get === id && !Gun.node.soul(data)){ return } + //tmp.put = data; + //tmp.get = id; // DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE - cat.last = data; + //at.last = data; if(opt.as){ - opt.ok.call(opt.as, at, ev); + opt.ok.call(opt.as, msg, ev); } else { - opt.ok.call(gun, data, at.get, at, ev); + opt.ok.call(gun, data, msg.get, msg, ev); } } @@ -64,14 +59,14 @@ Gun.chain.once = function(cb, opt){ } if(cb){ (opt = opt || {}).ok = cb; - opt.cat = at; + opt.at = at; opt.out = {'#': Gun.text.random(9)}; gun.get(val, {as: opt}); 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(); - chain._.val = gun.once(function(){ + chain._.nix = gun.once(function(){ chain._.on('in', gun._); }); return chain; @@ -79,35 +74,24 @@ Gun.chain.once = function(cb, opt){ return gun; } -function val(msg, ev, to){ - var opt = this.as, cat = opt.cat, gun = msg.gun, coat = gun._, data = coat.put || msg.put, tmp; - if(u === data){ - //return; - } - //if(coat.soul && !(0 < coat.ack)){ return } - if(tmp = Gun.node.soul(data) || rel.is(data)){ - //if(data && data[rel._] && (tmp = rel.is(data))){ - tmp = (cat.root.gun.get(tmp)._); - if(u === tmp.put){//} || !(0 < tmp.ack)){ +function val(msg, eve, to){ + var opt = this.as, cat = opt.at, gun = msg.$, at = gun._, data = at.put || msg.put, link, tmp; + if(tmp = msg.$$){ + link = tmp = (msg.$$._); + if(u === tmp.put){ return; } data = tmp.put; } - if(ev.wait){ clearTimeout(ev.wait) } - //if(!to && (!(0 < coat.ack) || ((true === opt.async) && 0 !== opt.wait))){ - if(!to){ - ev.wait = setTimeout(function(){ - val.call({as:opt}, msg, ev, ev.wait || 1); + if((tmp = eve.wait) && (tmp = tmp[at.id])){ clearTimeout(tmp) } + if(!to && (at.soul || at.link || (link && !(0 < link.ack)))){ + tmp = (eve.wait = {})[at.id] = setTimeout(function(){ + val.call({as:opt}, msg, eve, tmp || 1); }, opt.wait || 99); return; } - 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 } - opt.seen[coat.id] = true; - } - opt.ok.call(msg.gun || opt.gun, data, msg.get); + eve.rid(msg); + opt.ok.call(gun || opt.$, data, msg.get); } Gun.chain.off = function(){ @@ -133,20 +117,20 @@ Gun.chain.off = function(){ } if(tmp = at.map){ obj_map(tmp, function(at){ - if(at.rel){ - cat.root.gun.get(at.rel).off(); + if(at.link){ + cat.root.$.get(at.link).off(); } }); } if(tmp = at.next){ obj_map(tmp, function(neat){ - neat.gun.off(); + neat.$.off(); }); } at.on('off', {}); return gun; } var obj = Gun.obj, obj_map = obj.map, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to; -var rel = Gun.val.rel; +var rel = Gun.val.link; var empty = {}, noop = function(){}, u; \ No newline at end of file diff --git a/src/put.js b/src/put.js index 98ca9c5c..eea9e437 100644 --- a/src/put.js +++ b/src/put.js @@ -4,10 +4,10 @@ Gun.chain.put = function(data, cb, as){ // #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.gun, tmp; + var gun = this, at = (gun._), root = at.root.$, tmp; as = as || {}; as.data = data; - as.via = as.gun = as.via || as.gun || gun; + as.via = as.$ = as.via || as.$ || gun; if(typeof cb === 'string'){ as.soul = cb; } else { @@ -26,35 +26,35 @@ Gun.chain.put = function(data, cb, as){ if(!as.soul){ // polyfill async uuid for SEA as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback if(err){ return Gun.log(err) } // TODO: Handle error! - (as.ref||as.gun).put(as.data, as.soul = soul, as); + (as.ref||as.$).put(as.data, as.soul = soul, as); }); return gun; } - as.gun = gun = root.get(as.soul); - as.ref = as.gun; + as.$ = gun = root.get(as.soul); + as.ref = as.$; ify(as); return gun; } if(Gun.is(data)){ - data.get('_').get(function(at, ev, tmp){ ev.off(); - if(!(tmp = at.gun) || !(tmp = tmp._.back) || !tmp.soul){ - return Gun.log("The reference you are saving is a", typeof at.put, '"'+ as.put +'", not a node (object)!'); + data.get(function(soul, o, msg){ + if(!soul && Gun.val.is(msg.put)){ + return Gun.log("The reference you are saving is a", typeof msg.put, '"'+ msg.put +'", not a node (object)!'); } - gun.put(Gun.val.rel.ify(tmp.soul), cb, as); - }); + gun.put(Gun.val.rel.ify(soul), cb, as); + }, true); return gun; } - as.ref = as.ref || (root._ === (tmp = at.back))? gun : tmp.gun; + 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}); + as.ref.get(any, true, {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. as.res = as.res || stun; // Gun.on.stun(as.ref); // TODO: BUG! Deal with locking? - as.gun._.stun = as.ref._.stun; + as.$._.stun = as.ref._.stun; } return gun; }; @@ -92,7 +92,7 @@ function batch(){ var as = this; if(!as.graph || obj_map(as.stun, no)){ return } as.res = as.res || function(cb){ if(cb){ cb() } }; as.res(function(){ - var cat = (as.gun.back(-1)._), ask = cat.ask(function(ack){ + var cat = (as.$.back(-1)._), ask = cat.ask(function(ack){ cat.root.on('ack', ack); this.off(); // One response is good enough for us currently. Later we may want to adjust this. if(!as.ack){ return } @@ -102,22 +102,19 @@ function batch(){ var as = this; // and STOP is a hack to get async behavior to correctly call. // neither of these are ideal, need to be fixed without hacks, // but for now, this works for current tests. :/ - var tmp = cat.root.now; obj.del(cat.root, 'now'); cat.root.PUT = true; - var tmp2 = cat.root.stop; - (as.ref._).now = true; + var tmp = cat.root.now; obj.del(cat.root, 'now'); + var mum = cat.root.mum; cat.root.mum = {}; (as.ref._).on('out', { - gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask + $: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask }); - obj.del((as.ref._), 'now'); - obj.del((cat.root), 'PUT'); + cat.root.mum = mum? obj.to(mum, cat.root.mum) : mum; cat.root.now = tmp; - cat.root.stop = tmp2; }, as); if(as.res){ as.res() } } function no(v,k){ if(v){ return true } } function map(v,k,n, at){ var as = this; - //if(Gun.is(v)){} // TODO: HANDLE! + var is = Gun.is(v); if(k || !at.path.length){ return } (as.res||iife)(function(){ var path = at.path, ref = as.ref, opt = as.opt; @@ -125,65 +122,53 @@ function map(v,k,n, at){ var as = this; for(i; i < l; i++){ ref = ref.get(path[i]); } - if(Gun.node.soul(at.obj)){ - var id = Gun.node.soul(at.obj) || (as.via.back('opt.uuid') || Gun.text.random)(); - if(!id){ // polyfill async uuid for SEA - (as.stun = as.stun || {})[path] = true; // make DRY - as.via.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback - if(err){ return Gun.log(err) } // TODO: Handle error. - ref.back(-1).get(id); - at.soul(id); - as.stun[path] = false; - as.batch(); - }); - return; - } + if(is){ ref = v } + var id = (ref._).dub; + if(id || (id = Gun.node.soul(at.obj))){ ref.back(-1).get(id); at.soul(id); return; } (as.stun = as.stun || {})[path] = true; - ref.get('_').get(soul, {as: {at: at, as: as}}); + ref.get(soul, true, {as: {at: at, as: as, p:path}}); }, {as: as, at: at}); + //if(is){ return {} } } -function soul(msg, ev){ var as = this.as, cat = as.at; as = as.as; - //ev.stun(); // TODO: BUG!? - if(!msg.gun || !msg.gun._.back){ return } // TODO: Handle - var at = msg.gun._, at_ = at; - var _id = (msg.put||empty)['#']; - ev.off(); - at = (msg.gun._.back); // go up 1! - var id = id || Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || _id || at_._id || (as.via.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? +function soul(id, as, msg, eve){ + var as = as.as, cat = as.at; as = as.as; + var at = ((msg || {}).$ || {})._ || {}; + id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.rel.is(msg.put || at.put) || (as.via.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? + if(eve){ eve.stun = true } if(!id){ // polyfill async uuid for SEA at.via.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback if(err){ return Gun.log(err) } // TODO: Handle error. - solve(at, at_._id = at_._id || id, cat, as); + solve(at, at.dub = at.dub || id, cat, as); }); return; } - solve(at, at_._id = at_._id || id, cat, as); + solve(at, at.dub = id, cat, as); } function solve(at, id, cat, as){ - at.gun.back(-1).get(id); + at.$.back(-1).get(id); cat.soul(id); as.stun[cat.path] = false; as.batch(); } -function any(at, ev){ - var as = this.as; - if(!at.gun || !at.gun._){ return } // TODO: Handle - if(at.err){ // TODO: Handle +function any(soul, as, msg, eve){ + as = as.as; + if(!msg.$ || !msg.$._){ return } // TODO: Handle + if(msg.err){ // TODO: Handle console.log("Please report this as an issue! Put.any.err"); return; } - var cat = (at.gun._.back), data = cat.put, opt = as.opt||{}, root, tmp; + var at = (msg.$._), data = at.put, opt = as.opt||{}, root, tmp; if((tmp = as.ref) && tmp._.now){ return } - ev.off(); - if(as.ref !== as.gun){ - tmp = (as.gun._).get || cat.get; + if(eve){ eve.stun = true } + if(as.ref !== as.$){ + tmp = (as.$._).get || at.get; if(!tmp){ // TODO: Handle console.log("Please report this as an issue! Put.no.get"); // TODO: BUG!?? return; @@ -192,27 +177,27 @@ function any(at, ev){ tmp = null; } if(u === data){ - if(!cat.get){ return } // TODO: Handle - if(!cat.soul){ - tmp = cat.gun.back(function(at){ - if(at.soul){ return at.soul } + if(!at.get){ return } // TODO: Handle + if(!soul){ + tmp = at.$.back(function(at){ + if(at.link || at.soul){ return at.link || at.soul } as.data = obj_put({}, at.get, as.data); }); } - tmp = tmp || cat.get; - cat = (cat.root.gun.get(tmp)._); - as.not = as.soul = tmp; + tmp = tmp || at.get; + at = (at.root.$.get(tmp)._); + as.soul = tmp; data = as.data; } - if(!as.not && !(as.soul = Gun.node.soul(data))){ - if(as.path && obj_is(as.data)){ // Apparently necessary + if(!as.not && !(as.soul = as.soul || soul)){ + if(as.path && obj_is(as.data)){ as.soul = (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)(); } else { - //as.data = obj_put({}, as.gun._.get, as.data); + //as.data = obj_put({}, as.$._.get, as.data); if(node_ == at.get){ - as.soul = (at.put||empty)['#'] || at._id; + as.soul = (at.put||empty)['#'] || at.dub; } - as.soul = as.soul || at.soul || cat.soul || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)(); + as.soul = as.soul || at.soul || at.soul || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)(); } if(!as.soul){ // polyfill async uuid for SEA as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback diff --git a/src/root.js b/src/root.js index f2a06c2a..e362dd34 100644 --- a/src/root.js +++ b/src/root.js @@ -1,12 +1,12 @@ function Gun(o){ - if(o instanceof Gun){ return (this._ = {gun: this}).gun } + if(o instanceof Gun){ return (this._ = {gun: this, $: this}).$ } if(!(this instanceof Gun)){ return new Gun(o) } - return Gun.create(this._ = {gun: this, opt: o}); + return Gun.create(this._ = {gun: this, $: this, opt: o}); } -Gun.is = function(gun){ return (gun instanceof Gun) || (gun && gun._ && gun._.gun && true) || false } +Gun.is = function($){ return ($ instanceof Gun) || ($ && $._ && ($ === $._.$)) || false } Gun.version = 0.9; @@ -31,10 +31,10 @@ Gun.dup = require('./dup'); at.on = at.on || Gun.on; at.ask = at.ask || Gun.ask; at.dup = at.dup || Gun.dup(); - var gun = at.gun.opt(at.opt); + var gun = at.$.opt(at.opt); if(!at.once){ at.on('in', root, at); - at.on('out', root, obj_to(at, {out: root})); + at.on('out', root, {at: at, out: root}); Gun.on('create', at); at.on('create', at); } @@ -42,31 +42,27 @@ Gun.dup = require('./dup'); return gun; } function root(msg){ - //console.log("add to.next(at)"); // TODO: MISSING FEATURE!!! - var ev = this, at = ev.as, gun = at.gun, dup, tmp; - //if(!msg.gun){ msg.gun = at.gun } + //add to.next(at); // TODO: MISSING FEATURE!!! + var ev = this, as = ev.as, at = as.at || as, gun = at.$, dup, tmp; if(!(tmp = msg['#'])){ tmp = msg['#'] = text_rand(9) } if((dup = at.dup).check(tmp)){ - if(at.out === msg.out){ + if(as.out === msg.out){ msg.out = u; ev.to.next(msg); } return; } dup.track(tmp); - //msg = obj_to(msg);//, {gun: at.gun}); // can we delete this now? if(!at.ask(msg['@'], msg)){ if(msg.get){ - Gun.on.get(msg, gun); - //at.on('get', get(msg)); + Gun.on.get(msg, gun); //at.on('get', get(msg)); } if(msg.put){ - Gun.on.put(msg, gun); - //at.on('put', put(msg)); + Gun.on.put(msg, gun); //at.on('put', put(msg)); } } ev.to.next(msg); - if(!at.out){ + if(!as.out){ msg.out = root; at.on('out', msg); } @@ -75,14 +71,11 @@ Gun.dup = require('./dup'); ;(function(){ Gun.on.put = function(msg, gun){ - var at = gun._, ctx = {gun: gun, graph: at.graph, put: {}, map: {}, souls: {}, machine: Gun.state(), ack: msg['@']}; + var at = gun._, ctx = {$: gun, graph: at.graph, put: {}, map: {}, souls: {}, machine: Gun.state(), ack: msg['@'], cat: at, stop: {}}; if(!Gun.graph.is(msg.put, null, verify, ctx)){ ctx.err = "Error: Invalid graph!" } if(ctx.err){ return at.on('in', {'@': msg['#'], err: Gun.log(ctx.err) }) } obj_map(ctx.put, merge, ctx); - if(!ctx.async){ - at.stop = {}; // temporary fix till a better solution? - obj_map(ctx.map, map, ctx) - } + if(!ctx.async){ obj_map(ctx.map, map, ctx) } if(u !== ctx.defer){ setTimeout(function(){ Gun.on.put(msg, gun); @@ -107,34 +100,33 @@ Gun.dup = require('./dup'); ctx.souls[soul] = true; } function merge(node, soul){ - var ctx = this, cat = ctx.gun._, at = (cat.next || empty)[soul]; + var ctx = this, cat = ctx.$._, at = (cat.next || empty)[soul]; if(!at){ if(!(cat.opt||empty).super){ ctx.souls[soul] = false; return; } - at = (ctx.gun.get(soul)._); + at = (ctx.$.get(soul)._); } var msg = ctx.map[soul] = { put: node, get: soul, - gun: at.gun + $: at.$ }, as = {ctx: ctx, msg: msg}; ctx.async = !!cat.tag.node; if(ctx.ack){ msg['@'] = ctx.ack } 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) + // If it is async, we only need to setup one listener per context (ctx) cat.on('node', function(m){ 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 + obj_map(m.put, patch, 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(); - cat.stop = {}; // temporary fix till a better solution? obj_map(ctx.map, map, ctx); // all done, trigger chains. }); } @@ -142,19 +134,20 @@ Gun.dup = require('./dup'); 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; + var ctx = this.ctx, graph = ctx.graph, msg = this.msg, soul = msg.get, node = msg.put, at = (msg.$._), tmp; graph[soul] = Gun.state.to(node, key, graph[soul]); if(ctx.async){ return } at.put = Gun.state.to(node, key, at.put); } - function aeach(val, key){ - var msg = this, node = msg.put, at = (msg.gun._); + function patch(val, key){ + var msg = this, node = msg.put, at = (msg.$._); at.put = Gun.state.to(node, key, at.put); } function map(msg, soul){ - if(!msg.gun){ return } - //console.log('map ->', soul, msg.put); - (msg.gun._).on('in', msg); + if(!msg.$){ return } + this.cat.stop = this.stop; // temporary fix till a better solution? + (msg.$._).on('in', msg); + this.cat.stop = null; // temporary fix till a better solution? } Gun.on.get = function(msg, gun){ @@ -171,16 +164,14 @@ Gun.dup = require('./dup'); node = Gun.obj.copy(node); } node = Gun.graph.node(node); - //tmp = at.ack; + tmp = at.ack; root.on('in', { '@': msg['#'], how: 'mem', put: node, - gun: gun + $: gun }); - //if(0 < tmp){ - // return; - //} + //if(0 < tmp){ return } root.on('get', msg); } }()); @@ -210,7 +201,7 @@ Gun.dup = require('./dup'); 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.val.rel._, _has = '.', 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.link.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) }; @@ -231,10 +222,8 @@ module.exports = Gun; if(ctx.once){ return } 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); },1); }); diff --git a/src/set.js b/src/set.js index f856c96f..1e1c6072 100644 --- a/src/set.js +++ b/src/set.js @@ -8,20 +8,14 @@ Gun.chain.set = function(item, cb, opt){ if(!Gun.is(item)){ var id = gun.back('opt.uuid')(); if(id && Gun.obj.is(item)){ - return gun.set(gun._.root.gun.put(item, id), cb, opt); + return gun.set(gun._.root.$.put(item, id), cb, opt); } return gun.get((Gun.state.lex() + Gun.text.random(7))).put(item, cb, opt); } - item.get('_').get(function(at, ev){ - if(!at.gun || !at.gun._.back){ return } - ev.off(); - var soul = (at.put||{})['#']; - at = (at.gun._.back); - var put = {}, node = at.put; - soul = at.soul || Gun.node.soul(node) || soul; - if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + node + '"!')}) } - gun.put(Gun.obj.put(put, soul, Gun.val.rel.ify(soul)), cb, opt); - },{wait:0}); + item.get(function(soul, o, msg){ + if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}) } + gun.put(Gun.obj.put({}, soul, Gun.val.link.ify(soul)), cb, opt); + },true); return item; } \ No newline at end of file diff --git a/src/type.js b/src/type.js index 0ef9cf0c..bab8fd65 100644 --- a/src/type.js +++ b/src/type.js @@ -2,7 +2,7 @@ // Generic javascript utilities. var Type = {}; //Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }} -Type.fns = Type.fn = {is: function(fn){ return (!!fn && 'function' == typeof fn) }} +Type.fn = {is: function(fn){ return (!!fn && 'function' == typeof fn) }} Type.bi = {is: function(b){ return (b instanceof Boolean || typeof b == 'boolean') }} Type.num = {is: function(n){ return !list_is(n) && ((n - parseFloat(n) + 1) >= 0 || Infinity === n || -Infinity === n) }} Type.text = {is: function(t){ return (typeof t == 'string') }} diff --git a/src/val.js b/src/val.js index f9dd6456..fa4ac8f7 100644 --- a/src/val.js +++ b/src/val.js @@ -12,7 +12,7 @@ Val.is = function(v){ // Valid values are a subset of JSON: null, binary, number } return Val.rel.is(v) || false; // is the value a soul relation? Then it is valid and return it. If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types. } -Val.rel = {_: '#'}; +Val.link = Val.rel = {_: '#'}; ;(function(){ Val.rel.is = function(v){ // this defines whether an object is a soul relation or not, they look like this: {'#': 'UUID'} if(v && v[rel_] && !v._ && obj_is(v)){ // must be an object. @@ -35,7 +35,7 @@ Val.rel = {_: '#'}; }()); Val.rel.ify = function(t){ return obj_put({}, rel_, t) } // convert a soul into a relation and return it. Type.obj.has._ = '.'; -var rel_ = Val.rel._, u; +var rel_ = Val.link._, u; var bi_is = Type.bi.is; var num_is = Type.num.is; var text_is = Type.text.is;