From bf1b965b1fd2adb87f872493416f66efcbb30baf Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Sun, 2 Oct 2016 09:59:40 -0700 Subject: [PATCH] crazy maps work! --- gun.js | 40 ++--- test/common.js | 401 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 390 insertions(+), 51 deletions(-) diff --git a/gun.js b/gun.js index 68197c2f..45badd35 100644 --- a/gun.js +++ b/gun.js @@ -898,7 +898,6 @@ 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){ @@ -1357,8 +1356,18 @@ if(get){ cat.ask[get] = cat.ask._['.'] = 1 } tmp = false; cat.on('in', function(tac, ev){ - input.call(cat, tac, ev); tmp = true; + if(u !== tac.put){ + input.call(cat, tac, ev); + return; + } + // TODO: BUG! We want to query for it! So should still `input`. There should be no special case here. + // Nots require some special treatment. + at.gun.on('in', { + get: get, + gun: at.gun, + via: tac + }); return; var put = tac.put, rel = tac.put; if(!(rel = (Gun.node.soul(put) || Gun.val.rel.is(put)))){ @@ -1424,8 +1433,6 @@ Gun.on.ack(tmp, at); if(at.err){ return } }; - 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; } @@ -1436,19 +1443,22 @@ var cat = this, gun = at.gun, put = at.put, coat = gun._, rel, tmp; if(!(rel = Gun.val.rel.is(put))){ if(cat.proxy){ + if(cat.proxy.it === at){ return true } // TODO: PERF! Anyway to simplify this? if(cat.proxy.rel){ cat.change = coat.change; 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? + cat.on('in', obj_to(at, cat.proxy.it = {get: cat.get || at.get})); } - //not(); // ask? if(Gun.val.is(at)){ + //not(); // ask? + not(cat, at); return true; } // iterables? + not(cat, at); + ask(cat); return; } if(coat.proxy && rel === coat.proxy.rel){ @@ -1461,7 +1471,8 @@ 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()); + cat.TAP = 1; + coat.TAP = 2; gun.on('in', input, cat); // TODO: BUG! PERF! MEMORY LEAK! } return cat.put === coat.put? false : true; @@ -1476,7 +1487,7 @@ } //not() tmp = coat.proxy = {rel: rel, ref: coat.root.get(rel)}; - tmp.res = ev.stun(rel); tmp.as = coat; + tmp.ev = ev; tmp.res = ev.stun(rel); tmp.as = coat; tmp.ref.on('in', input, coat); return true; } @@ -1629,8 +1640,8 @@ }); } function not(cat, at){ - var ask = cat.ask, tmp = cat.link; - cat.link = null; + var ask = cat.ask, tmp = cat.proxy; + cat.proxy = null; if(null === tmp){ return } if(tmp){ if(tmp.sub){ @@ -2087,14 +2098,7 @@ var gun = this.gun, cat = this.cat, id = this.id; if(cat.list[id+f]){ return } // TODO: BUG! Ghosting! - 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){ - //Gun.chain.get.input.call(cat, {gun: cat.gun, get: f, put: v, MAP: true}, ev); - //cat.on('in', this); - cat.on('in',[id+f,this._,ev]); - }); } var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._; }()); diff --git a/test/common.js b/test/common.js index e37e1f84..d1b3b343 100644 --- a/test/common.js +++ b/test/common.js @@ -1707,12 +1707,11 @@ describe('Gun', function(){ },300); }); - it.only("in memory get before map map", function(done){ + it("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; - 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); @@ -1726,7 +1725,7 @@ describe('Gun', function(){ done(); } }); - console.debug.i=1;console.log("---------------------"); + gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m'}, users: { alice: { @@ -1757,11 +1756,10 @@ describe('Gun', function(){ } }); 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'}, @@ -1788,7 +1786,7 @@ describe('Gun', function(){ GUN: { name: "GUN" }, - ACME: { + ACME: {_:{'#':"CCINEMA4"}, name: "ACME" } } @@ -1796,7 +1794,7 @@ describe('Gun', function(){ var check = {}; gun.get('g/n/m/f/l/n/b/m/m').map().map().on(function(v,f){ check[f] = v; - if(check.alice && check.bob && check.GUN && check.ACME){ + 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(); @@ -1805,9 +1803,13 @@ 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(); } }); + setTimeout(function(){ + gun.get('CCINEMA4').put({corp: "C"}); + },300); }); it("in memory get before map map path", function(done){ @@ -1815,11 +1817,12 @@ describe('Gun', function(){ var check = {}; gun.get('g/n/m/f/l/n/b/m/m/p').map().map().path('name').on(function(v,f){ check[v] = f; - if(check.alice && check.bob && check.GUN && check.ACME){ + if(check.alice && check.bob && check.GUN && check.ACME && check.ACMEINC){ expect(check.alice).to.be('name'); expect(check.bob).to.be('name'); expect(check.GUN).to.be('name'); expect(check.ACME).to.be('name'); + expect(check.ACMEINC).to.be('name'); done(); } }); @@ -1847,11 +1850,14 @@ describe('Gun', function(){ GUN: { name: "GUN" }, - ACME: { + ACME: {_:{'#':"CCINEMA5"}, name: "ACME" } } }); + setTimeout(function(){ + gun.get('CCINEMA5').put({name: "ACMEINC"}); + },300); }); it("in memory get after map map path", function(done){ @@ -1881,21 +1887,25 @@ describe('Gun', function(){ GUN: { name: "GUN" }, - ACME: { + ACME: {_:{'#':'CCINEMA6'}, name: "ACME" } } }); gun.get('g/n/m/f/l/n/b/a/m/m/p').map().map().path('name').on(function(v,f){ check[v] = f; - if(check.alice && check.bob && check.GUN && check.ACME){ + if(check.alice && check.bob && check.GUN && check.ACME && check.ACMEINC){ expect(check.alice).to.be('name'); expect(check.bob).to.be('name'); expect(check.GUN).to.be('name'); expect(check.ACME).to.be('name'); + expect(check.ACMEINC).to.be('name'); done(); } }); + setTimeout(function(){ + gun.get('CCINEMA6').put({name: "ACMEINC"}); + },300); }); it("in memory get before map map path path", function(done){ @@ -1929,7 +1939,7 @@ describe('Gun', function(){ bob: { name: "bob", age: 26, - address: {_:{'#': 'DANDAO'}, + address: { state: "TX" }, spouse: { @@ -1965,7 +1975,7 @@ describe('Gun', function(){ alice: { name: "alice", age: 24, - address: {_:{'#':'QUANGO'}, + address: {_:{'#':'QUANGO1'}, state: "MA" }, spouse: { @@ -1977,7 +1987,7 @@ describe('Gun', function(){ bob: { name: "bob", age: 26, - address: {_:{'#': 'DANDAO'}, + address: { state: "TX" }, spouse: { @@ -2003,7 +2013,6 @@ describe('Gun', function(){ }); var check = {}; gun.get('g/n/m/f/l/n/b/a/m/m/p/p').map().map().path('address').path('state').on(function(v,f){ - console.log("**********", f,v); check[v] = f; if(check.QR && check.NY && check.CA && check.TX && check.MA){ expect(check.QR).to.be('state'); @@ -2015,25 +2024,23 @@ describe('Gun', function(){ } }); setTimeout(function(){ - gun.get('QUANGO').put({state: 'QR'}); + gun.get('QUANGO1').put({state: 'QR'}); },300); }); it("in memory get before map map path path path", function(done){ var gun = Gun(); var check = {}; - (window.STATE = gun.get('g/n/m/f/l/n/b/m/m/p/p/p').path('users').path('alice').path('address').path('state')).on(function(v,f){console.log("* * * * * * * *", f,v)}); gun.get('g/n/m/f/l/n/b/m/m/p/p/p').map().map().path('address').path('state') - //.path('code') + .path('code') .on(function(v,f){ - check[v.code] = f; - console.log("************", f,v);//return; + check[v] = f; if(check.QR && check.NY && check.CA && check.TX && check.MA){ - expect(check.QR).to.be('state'); - expect(check.NY).to.be('state'); - expect(check.CA).to.be('state'); - expect(check.TX).to.be('state'); - expect(check.MA).to.be('state'); + expect(check.QR).to.be('code'); + expect(check.NY).to.be('code'); + expect(check.CA).to.be('code'); + expect(check.TX).to.be('code'); + expect(check.MA).to.be('code'); done(); } }); @@ -2042,7 +2049,7 @@ describe('Gun', function(){ alice: { name: "alice", age: 24, - address: {_:{'#':'QUANGO'}, + address: { state: {_:{'#':'HIPPOM'}, code: "MA", county: { @@ -2059,7 +2066,7 @@ describe('Gun', function(){ bob: { name: "bob", age: 26, - address: {_:{'#': 'DANDAO'}, + address: { state: { code: "TX", county: { @@ -2099,14 +2106,342 @@ describe('Gun', function(){ } }); setTimeout(function(){ - console.debug.i=1;console.log("-------------------"); gun.get('HIPPOM').put({code: 'QR'}); },300); }); + it("in memory get before after map map path path path", function(done){ + var gun = Gun(); + var check = {}; + gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m/p/p/p'}, + users: { + alice: { + name: "alice", + age: 24, + address: { + state: {_:{'#':'HIPPOM1'}, + code: "MA", + county: { + MA1: "First" + } + } + }, + spouse: { + name: "carl", + age: 25 + }, + bout: {huh:1} + }, + bob: { + name: "bob", + age: 26, + address: { + state: { + code: "TX", + county: { + TX1: "First" + } + } + }, + spouse: { + name: "diana", + age: 27 + } + } + }, + companies: { + GUN: { + name: "GUN", + address: { + state: { + code: "CA", + county: { + CA1: "First" + } + } + } + }, + ACME: { + name: "ACME", + address: { + state: { + code: "NY", + county: { + NY1: "First" + } + } + } + } + } + }); + gun.get('g/n/m/f/l/n/b/a/m/m/p/p/p').map().map().path('address').path('state') + .path('code') + .on(function(v,f){ + check[v] = f; + if(check.QR && check.NY && check.CA && check.TX && check.MA){ + expect(check.QR).to.be('code'); + expect(check.NY).to.be('code'); + expect(check.CA).to.be('code'); + expect(check.TX).to.be('code'); + expect(check.MA).to.be('code'); + done(); + } + }); + setTimeout(function(){ + gun.get('HIPPOM1').put({code: 'QR'}); + },300); + }); + + it("in memory get before map map path path node", function(done){ + var gun = Gun(); + var check = {}; + gun.get('g/n/m/f/l/n/b/m/m/p/p/n').map().map().path('address').path('state').on(function(v,f){ + check[v.code] = f; + if(check.QR && check.NY && check.CA && check.TX && check.MA){ + expect(check.QR).to.be('state'); + expect(check.NY).to.be('state'); + expect(check.CA).to.be('state'); + expect(check.TX).to.be('state'); + expect(check.MA).to.be('state'); + done(); + } + }); + gun.put({_:{'#':'g/n/m/f/l/n/b/m/m/p/p/n'}, + users: { + alice: { + name: "alice", + age: 24, + address: { + state: {_:{'#':'HIPPOM3'}, + code: "MA", + county: { + MA1: "First" + } + } + }, + spouse: { + name: "carl", + age: 25 + }, + bout: {huh:1} + }, + bob: { + name: "bob", + age: 26, + address: { + state: { + code: "TX", + county: { + TX1: "First" + } + } + }, + spouse: { + name: "diana", + age: 27 + } + } + }, + companies: { + GUN: { + name: "GUN", + address: { + state: { + code: "CA", + county: { + CA1: "First" + } + } + } + }, + ACME: { + name: "ACME", + address: { + state: { + code: "NY", + county: { + NY1: "First" + } + } + } + } + } + }); + setTimeout(function(){ + gun.get('HIPPOM3').put({code: 'QR'}); + },300); + }); + + it("in memory get before after map map path path node", function(done){ + var gun = Gun(); + var check = {}; + gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m/p/p/n'}, + users: { + alice: { + name: "alice", + age: 24, + address: { + state: {_:{'#':'HIPPOM4'}, + code: "MA", + county: { + MA1: "First" + } + } + }, + spouse: { + name: "carl", + age: 25 + }, + bout: {huh:1} + }, + bob: { + name: "bob", + age: 26, + address: { + state: { + code: "TX", + county: { + TX1: "First" + } + } + }, + spouse: { + name: "diana", + age: 27 + } + } + }, + companies: { + GUN: { + name: "GUN", + address: { + state: { + code: "CA", + county: { + CA1: "First" + } + } + } + }, + ACME: { + name: "ACME", + address: { + state: { + code: "NY", + county: { + NY1: "First" + } + } + } + } + } + }); + gun.get('g/n/m/f/l/n/b/a/m/m/p/p/n').map().map().path('address').path('state').on(function(v,f){ + check[v.code] = f; + if(check.QR && check.NY && check.CA && check.TX && check.MA){ + expect(check.QR).to.be('state'); + expect(check.NY).to.be('state'); + expect(check.CA).to.be('state'); + expect(check.TX).to.be('state'); + expect(check.MA).to.be('state'); + done(); + } + }); + setTimeout(function(){ + gun.get('HIPPOM4').put({code: 'QR'}); + },300); + }); + + it("in memory get after map map path path path map", function(done){ + var gun = Gun(); + var check = {}; + gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m/p/p/p/n'}, + users: { + alice: { + name: "alice", + age: 24, + address: { + state: { + code: "MA", + county: { + MA1: "First" + ,MA2: "Second" + } + } + }, + spouse: { + name: "carl", + age: 25 + }, + bout: {huh:1} + }, + bob: { + name: "bob", + age: 26, + address: { + state: { + code: "TX", + county: { + TX1: "First" + ,TX2: "Second" + } + } + }, + spouse: { + name: "diana", + age: 27 + } + } + }, + companies: { + GUN: { + name: "GUN", + address: { + state: { + code: "CA", + county: { + CA1: "First" + ,CA2: "Second" + } + } + } + }, + ACME: { + name: "ACME", + address: { + state: { + code: "NY", + county: {_:{'#':'NYCOUNT'}, + NY1: "First" + ,NY2: "Second" + } + } + } + } + } + }); + gun.get('g/n/m/f/l/n/b/a/m/m/p/p/p/n').map().map().path('address').path('state').path('county').map().on(function(v,f){ + check[f] = v; + if(check.MA1 && check.MA2 && check.TX1 && check.TX2 && check.CA1 && check.CA2 && check.NY1 && check.NY2 && check.NY3){ + expect(check.MA1).to.be('First'); + expect(check.TX1).to.be('First'); + expect(check.CA1).to.be('First'); + expect(check.NY1).to.be('First'); + expect(check.MA2).to.be('Second'); + expect(check.TX2).to.be('Second'); + expect(check.CA2).to.be('Second'); + expect(check.NY2).to.be('Second'); + expect(check.NY3).to.be('Third'); + done(); + } + }); + setTimeout(function(){ + gun.get('NYCOUNT').put({NY3: "Third"}); + },300); + }); }); - return; - it('get get get any parallel', function(done){ + + it.only('get get get any parallel', function(done){ var s = Gun.state.map();s.soul = 'parallel'; Gun.on('put', {gun: gun, put: Gun.graph.ify({ bob: { @@ -2115,13 +2450,13 @@ describe('Gun', function(){ } }, s)}); gun.get('parallel').path('bob').path('age').any(function(err, data, field, at, ev){ - //console.log("***** age", data); + console.log("***** age", data); expect(data).to.be(29); expect(field).to.be('age'); done.age = true; }); gun.get('parallel').path('bob').path('name').any(function(err, data, field, at, ev){ - //console.log("*********** name", data); + console.log("*********** name", data); expect(data).to.be('Bob!'); expect(field).to.be('name'); done.name = true; @@ -2210,7 +2545,7 @@ describe('Gun', function(){ done(); }); }); - + return; it('get any any later', function(done){ var s = Gun.state.map();s.soul = 'full/later'; Gun.on('put', {gun: gun, put: Gun.graph.ify({