diff --git a/gun.js b/gun.js index d3a62df0..921539ab 100644 --- a/gun.js +++ b/gun.js @@ -898,6 +898,7 @@ function output(at){ var cat = this, gun = cat.gun, tmp; if(at.put){ + console.debug(3, 'OUT -> IN', at); cat.on('in', obj_to(at, {'#': 0, gun: cat.gun})); } if(!at.gun){ @@ -961,7 +962,7 @@ var _soul = Gun._.soul, _field = Gun._.field; var u; - console.debug = function(i, s){ return (console.debug.i && i === console.debug.i && console.debug.i++) && console.log.apply(console, arguments), s }; + console.debug = function(i, s){ return (console.debug.i && i === console.debug.i && console.debug.i++) && (console.log.apply(console, arguments) || s) }; Gun.log = function(){ return (!Gun.log.off && console.log.apply(console, arguments)), [].slice.call(arguments).join(' ') } Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) } @@ -1174,7 +1175,6 @@ at.soul(Gun.node.soul(at.obj) || (as.opt.uuid || as.gun.Back('opt.uuid') || Gun.text.random)()); return; } - console.log("LAAAME", at.path, v); (as.stun = as.stun || {})[path] = true; ref.any(soul, {as: {at: at, as: as}, '.': null}); }, {as: as, at: at}); @@ -1208,7 +1208,6 @@ }, ev); return; } - //console.log("HOLY BALONY", at, as); /* TODO: THIS WHOLE SECTION NEEDS TO BE CLEANED UP! Implicit behavior should be much cleaner. Right now it is hacky. @@ -1299,7 +1298,6 @@ if(!opt || !opt.path){ var back = this.Back(-1); } // TODO: CHANGING API! Remove this line! var gun, back = back || this, cat = back._; var next = cat.next || empty, tmp; - console.debug(2, 'get', lex); if(typeof lex === 'string'){ if(!(gun = next[lex])){ gun = cache(lex, back); @@ -1358,9 +1356,7 @@ cat.ask._ = cat.ask._ || {}; if(get){ cat.ask[get] = cat.ask._['.'] = 1 } tmp = false; - console.debug(6, 'out', cat.get, get, cat.ask); cat.on('in', function(tac, ev){ - console.debug(7, 'out', tac); input.call(cat, tac, ev); tmp = true; return; @@ -1407,7 +1403,6 @@ }); return; } - console.debug(5, 'any out', cat.get, cat.ask); cat.back.on('out', { get: obj_put({}, _field, cat.get), gun: gun @@ -1428,13 +1423,13 @@ if(at.via && (tmp = at.via['@'])){ Gun.on.ack(tmp, at); if(at.err){ return } - } - console.debug(8, 'in', cat.get, change, cat.ask); + }; + console.debug(2, 'in', cat.get, change, cat.on('in').s.slice()); + console.debug(4, 'in', cat.get, change, cat.on('in').s.slice()); if(value.call(cat, at, ev)){ return; } obj_map(change, map, {at: at, cat: cat}); // Important because `values` sometimes `ask`s for things which changes what the `changes` are. - //ask(cat, Gun.node.soul(change)); } Gun.chain.get.input = input; function value(at, ev){ @@ -1446,6 +1441,7 @@ cat.put = coat.put; } // TODO: BUG! This mutated `at` won't effect the original at that was sent via the poly-proxy approach. Meaning what is still cached in the poly-set is not this better/recent/fuller one. + //console.log('values...', at, cat); cat.proxy.res(obj_to(at, {get: cat.get || at.get})); // MUTATE AT? } //not(); // ask? @@ -1457,6 +1453,7 @@ } if(coat.proxy && rel === coat.proxy.rel){ coat.id = coat.id || Gun.text.random(6); + ask(cat, rel, coat); if(cat === coat){ cat.put = at.put = coat.proxy.ref._.put; // change? @@ -1464,11 +1461,10 @@ if(!cat.proxy || !cat.proxy[coat.id]){ (cat.proxy = cat.proxy || {})[coat.id] = coat; cat.proxy.res = ev.stun(rel); // TODO: BUG! Race? Or this all goes to the same thing so it doesn't matter? + console.log("VALUES?", cat.get, coat.get, Gun.obj.copy(coat.ask), cat.on('in').s.slice()); gun.on('in', input, cat); // TODO: BUG! PERF! MEMORY LEAK! } - console.debug(9, 'values', cat.ask, coat.ask, coat); - ask(cat, rel); - return; + return cat.put === coat.put? false : true; } if(cat !== coat){ coat.id = coat.id || Gun.text.random(6); // TODO: BUG! REPEATS ABOVE @@ -1662,30 +1658,29 @@ }); }); } - function ask(cat, soul){ - var ask = cat.ask, lex; - console.log("TODO: BUG! MARK COME BACK HERE!!!!! You can't have ASK shared by a single multi-observable cat. You need each ask to merge with the original? FIX THIS"); - console.log("TODO: BUG! MARK COME BACK HERE!!!!! You can't have ASK shared by a single multi-observable cat. You need each ask to merge with the original? FIX THIS"); - console.log("TODO: BUG! MARK COME BACK HERE!!!!! You can't have ASK shared by a single multi-observable cat. You need each ask to merge with the original? FIX THIS"); - console.log("TODO: BUG! MARK COME BACK HERE!!!!! You can't have ASK shared by a single multi-observable cat. You need each ask to merge with the original? FIX THIS"); - if(!ask || !soul){ return } - if(ask._['*']){ - if(0 <= ask._['*']){ - ask._['*'] = -1; - cat.gun.on('out', { + function ask(cat, soul, at){ + if(at && !at.ask){ at.ask = {} } // TODO: BUG! Second time through? Or first time through and *? + var ask = cat.ask, coat = at || cat, proxy = coat.ask || ask, lex, tmp; + if(!ask || !soul || !ask._){ return } + proxy._ = proxy._ || {}; + if(tmp = proxy._['*'] || ask._['*']){ // TODO! BUG! Should be proxy, right? + if(0 <= tmp){ + proxy._['*'] = -1; + coat.gun.on('out', { get: {'#': soul}, - gun: cat.gun, - '#': Gun.on.ask(ack, cat.gun) + gun: coat.gun, + '#': Gun.on.ask(ack, coat.gun) }); return; } } - if(0 >= ask._['.']){ return } - ask._['.'] = -1; + if(0 >= proxy._['.']){ return } + proxy._['.'] = -1; obj_map(ask, function(state, key){ + state = proxy[key] || (proxy[key] = state); if(1 > state){ return } if(!(state = (cat.next||empty)[key])){ return } - ask[key] = -1; + proxy[key] = -1; state.on('out', { get: {'#': soul, '.': key}, gun: state, @@ -1705,7 +1700,6 @@ var opt = opt || {}, gun = opt.gun = this; if(opt.change){ opt.change = 1 } opt.any = cb; - console.debug(4, 'any', gun._.get); return gun.on('in', any, opt).on('out', {get: opt}); } function any(at, ev){ var opt = this; @@ -1972,7 +1966,6 @@ opt = (true === opt)? {change: true} : opt || {}; opt.ok = tag; opt.last = {}; - console.debug(3, 'on'); gun.any(ok, {as: opt, change: opt.change}); // TODO: PERF! Event listener leak!!!???? return gun; } @@ -2094,7 +2087,7 @@ var gun = this.gun, cat = this.cat, id = this.id; if(cat.list[id+f]){ return } // TODO: BUG! Ghosting! - console.debug(1, 'EACH', f,v, gun); + f==='ACME'&&console.debug(1, 'EACH', f,v); return cat.on('in', [id+f, {gun: (cat.list[id+f] = gun.path(f)), get: f, put: v}]); return cat.on('in', [id+f, (cat.list[id+f] = gun.path(f))._]); (cat.list[id+f] = gun.path(f)).on(function(v,s,a,ev){ @@ -2169,7 +2162,7 @@ } Gun.on('put', put); Gun.on('get', get); - })(require, './src/localStorage'); + })(require, './adapters/localStorage'); ;require(function(module){ Gun.on('get', function(at){ @@ -2208,7 +2201,7 @@ }); Tab.peers(peers).send(msg, {headers: {'gun-sid': Tab.server.sid}}); }); - })(require, './src/WebSocket'); + })(require, './adapters/WebSocket'); }()); /* Gun.on('put', function(at){ diff --git a/test/common.js b/test/common.js index a0c1d5f4..e37e1f84 100644 --- a/test/common.js +++ b/test/common.js @@ -1463,11 +1463,12 @@ describe('Gun', function(){ if(check.alice && check.bob && check.alice.PhD){ expect(check.alice.age).to.be(24); expect(check.bob.age).to.be(26); + expect(check.alice.PhD).to.be(true); done(); } }); gun.put({_:{'#':'g/n/m/f/l/n/r'}, - alice: {_:{'#':'GALICE'}, + alice: {_:{'#':'GALICE1'}, name: "alice", age: 24, spouse: { @@ -1492,14 +1493,14 @@ describe('Gun', function(){ } }); setTimeout(function(){ - gun.get('GALICE').put({PhD: true}); + gun.get('GALICE1').put({PhD: true}); },300); }); it("in memory get after", function(done){ var gun = Gun(); gun.put({_:{'#':'g/n/m/f/l/n'}, - alice: { + alice: {_:{'#':'GALICE2'}, name: "alice", age: 24, spouse: { @@ -1526,12 +1527,16 @@ describe('Gun', function(){ var check = {}; gun.get('g/n/m/f/l/n').map().on(function(v,f){ check[f] = v; - if(check.alice && check.bob){ + if(check.alice && check.bob && check.alice.PhD){ expect(check.alice.age).to.be(24); expect(check.bob.age).to.be(26); + expect(check.alice.PhD).to.be(true); done(); } }); + setTimeout(function(){ + gun.get('GALICE2').put({PhD: true}); + },300); }); it("in memory get before map path", function(done){ @@ -1547,7 +1552,7 @@ describe('Gun', function(){ } }); gun.put({_:{'#':'g/n/m/f/l/n/b/p'}, - alice: {_:{'#':'GALICE'}, + alice: {_:{'#':'GALICE3'}, name: "alice", age: 24, spouse: { @@ -1572,14 +1577,14 @@ describe('Gun', function(){ } }); setTimeout(function(){ - gun.get('GALICE').put({name: 'Alice'}); + gun.get('GALICE3').put({name: 'Alice'}); },300); }); - it.only("in memory get after map path", function(done){ + it("in memory get after map path", function(done){ var gun = Gun(); gun.put({_:{'#':'g/n/m/f/l/n/m/p'}, - alice: {_:{'#':'GALICE'}, + alice: {_:{'#':'GALICE4'}, name: "alice", age: 24, spouse: { @@ -1604,10 +1609,8 @@ describe('Gun', function(){ } }); var check = {}; - console.debug.i=1;console.log("------------------"); gun.get('g/n/m/f/l/n/m/p').map().path('name').on(function(v,f){ check[v] = f; - console.log("*******", f,v); if(check.alice && check.bob && check.Alice){ expect(check.alice).to.be('name'); expect(check.bob).to.be('name'); @@ -1615,9 +1618,8 @@ describe('Gun', function(){ done(); } }); - return; setTimeout(function(){ - gun.get('GALICE').put({name: 'Alice'}); + gun.get('GALICE4').put({name: 'Alice'}); },300); }); @@ -1626,9 +1628,10 @@ describe('Gun', function(){ var check = {}; gun.get('g/n/m/f/l/n/b/p/p/p').map().path('spouse').path('work').on(function(v,f){ check[v.name] = f; - if(check['GUN INC'] && check['ACME INC']){ + if(check['GUN INC'] && check['ACME INC'] && check['ACME INC.']){ expect(check['GUN INC']).to.be('work'); expect(check['ACME INC']).to.be('work'); + expect(check['ACME INC.']).to.be('work'); done(); } }); @@ -1651,12 +1654,15 @@ describe('Gun', function(){ spouse: { name: "diana", age: 27, - work: { + work: {_:{'#':'CCINEMA1'}, name: "ACME INC" } } } }); + setTimeout(function(){ + gun.get('CCINEMA1').put({name: 'ACME INC.'}); + },300); }); it("in memory get after map path path", function(done){ @@ -1680,7 +1686,7 @@ describe('Gun', function(){ spouse: { name: "diana", age: 27, - work: { + work: {_:{'#':'CCINEMA2'}, name: "ACME INC" } } @@ -1689,20 +1695,25 @@ describe('Gun', function(){ var check = {}; gun.get('g/n/m/f/l/n/b/p/p/p/a').map().path('spouse').path('work').on(function(v,f){ check[v.name] = f; - if(check['GUN INC'] && check['ACME INC']){ + if(check['GUN INC'] && check['ACME INC'] && check['ACME INC.']){ expect(check['GUN INC']).to.be('work'); expect(check['ACME INC']).to.be('work'); + expect(check['ACME INC.']).to.be('work'); done(); } }); + setTimeout(function(){ + gun.get('CCINEMA2').put({name: 'ACME INC.'}); + },300); }); - it("in memory get before map map", function(done){ + it.only("in memory get before map map", function(done){ var gun = Gun(); var check = {}; gun.get('g/n/m/f/l/n/b/a/m/m').map().map().on(function(v,f){ check[f] = v; - if(check.alice && check.bob && check.GUN && check.ACME){ + console.log("*************", f,v); + if(check.alice && check.bob && check.GUN && check.ACME && check.ACME.corp){ expect(check.alice.name).to.be('alice'); expect(check.alice.age).to.be(24); expect(Gun.val.rel.is(check.alice.spouse)).to.be.ok(); @@ -1711,9 +1722,11 @@ describe('Gun', function(){ expect(Gun.val.rel.is(check.bob.spouse)).to.be.ok(); expect(check.GUN.name).to.be('GUN'); expect(check.ACME.name).to.be('ACME'); + expect(check.ACME.corp).to.be('C'); done(); } }); + console.debug.i=1;console.log("---------------------"); gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m'}, users: { alice: { @@ -1738,13 +1751,17 @@ describe('Gun', function(){ GUN: { name: "GUN" }, - ACME: { + ACME: {_:{'#':"CCINEMA3"}, name: "ACME" } } }); + setTimeout(function(){ + console.log("~~~~~~~~~~~~~~~~~"); + gun.get('CCINEMA3').put({corp: "C"}); + },300); }); - + return; it("in memory get after map map", function(done){ var gun = Gun(); gun.put({_:{'#':'g/n/m/f/l/n/b/m/m'},