call sub chains on clear/empty WIP

This commit is contained in:
Mark Nadal 2021-01-30 03:04:48 -08:00
parent dcd6fc55fa
commit e16f982ee8
2 changed files with 29 additions and 12 deletions

24
gun.js
View File

@ -687,19 +687,33 @@
sat.on('in', {get: get, put: u, $: sat.$}); // TODO: BUG? Add recursive seen check? sat.on('in', {get: get, put: u, $: sat.$}); // TODO: BUG? Add recursive seen check?
},0,99); },0,99);
return; return;
} if(cat.ask && at.has && !msg.$$ && (('string' != typeof (tmp = valid(change))) || (cat.link && cat.link != tmp))){ // whenever we are not a link, nor the same link as before, (for our layer) we need to clear out subchains, and... }
if(cat.link !== null || (root.pass||'')[cat.id]){ if(!cat.soul && !msg.$$){
cat.link = null; // ideally only once, don't forget pass logic, see linking logic for why. if(
(cat.link !== null || (root.pass||'')[cat.id]) &&
//cat.link !== u && // TODO: BUG? actually this should be if link as a property exists or not.
(('string' != typeof (tmp = valid(change))) || (cat.link && tmp != cat.link)) // any time there is a change in value that is different from the previous link in any way, we need to fire a clear/empty event on chains below. // However! Do this only when unique, and make sure to do it with performance in mind.
){
cat.link = null;
cat.next && setTimeout.each(Object.keys(cat.ask||''), function(get, sat){ // TODO: Bug? If we're a map do we want to clear out everything, wouldn't it just be the one item's subchains, not all? cat.next && setTimeout.each(Object.keys(cat.ask||''), function(get, sat){ // TODO: Bug? If we're a map do we want to clear out everything, wouldn't it just be the one item's subchains, not all?
if(!(sat = cat.next[get])){ return } // only if next, even if asked. (right?) if(!(sat = cat.next[get])){ return } // only if next, even if asked. (right?)
sat.on('in', {get: get, put: u, $: sat.$}); sat.on('in', {get: get, put: u, $: sat.$});
},0,99); },0,99);
} }
} // this was an absurd amount of code to handle a tiny edge case :/ maybe don't support it in future? }
/*if(cat.ask && ((at.has && !msg.$$) || (!at.has && msg.$$)) && (('string' != typeof (tmp = valid(change))) || (cat.link && cat.link != tmp))){ // whenever we are not a link, nor the same link as before, (for our layer) we need to clear out subchains, and... // this code is so ugly & heavy, is there something easier?
if(cat.link !== null || (root.pass||'')[cat.id]){ cat.link = null; // ideally only once, don't forget pass logic, see linking logic for why. // TODO: What about async loaded?
console.log("ugggggh", msg, cat);
cat.next && setTimeout.each(Object.keys(cat.ask||''), function(get, sat){ // TODO: Bug? If we're a map do we want to clear out everything, wouldn't it just be the one item's subchains, not all?
if(!(sat = cat.next[get])){ return } // only if next, even if asked. (right?)
sat.on('in', {get: get, put: u, $: sat.$});
},0,99);
}
} // this was an absurd amount of code to handle a tiny edge case :/ maybe don't support it in future? */
if(((msg.$$||'')._||at).soul){ // comments are linear, but this line of code is non-linear, so if I were to comment what it does, you'd have to read 42 other comments first... but you can't read any of those comments until you first read this comment. What!? // shouldn't this match link's check? if(((msg.$$||'')._||at).soul){ // comments are linear, but this line of code is non-linear, so if I were to comment what it does, you'd have to read 42 other comments first... but you can't read any of those comments until you first read this comment. What!? // shouldn't this match link's check?
// is there cases where it is a $$ that we do NOT want to do the following? // is there cases where it is a $$ that we do NOT want to do the following?
if((sat = cat.next) && (sat = sat[key])){ // TODO: possible trick? Maybe have `ionmap` code set a sat? if((sat = cat.next) && (sat = sat[key])){ // TODO: possible trick? Maybe have `ionmap` code set a sat? // TODO: Maybe we should do `cat.ask` instead? I guess does not matter.
tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] }); tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] });
tmp.$ = (msg.$$||msg.$).get(tmp.get = key); delete tmp.$$; delete tmp.$$$; tmp.$ = (msg.$$||msg.$).get(tmp.get = key); delete tmp.$$; delete tmp.$$$;
sat.on('in', tmp); sat.on('in', tmp);

View File

@ -2964,7 +2964,7 @@ describe('Gun', function(){
}, 'parallel/later', function(){ }, 'parallel/later', function(){
gun.get('parallel/later').get('bob').get('age').get(function(at, ev){ gun.get('parallel/later').get('bob').get('age').get(function(at, ev){
var err = at.err, data = at.put, field = at.get; var err = at.err, data = at.put, field = at.get;
//console.log("***** age", data, field, at); return; //console.log("*****", field, data); return;
expect(data).to.be(29); expect(data).to.be(29);
expect(field).to.be('age'); expect(field).to.be('age');
done.age = true; done.age = true;
@ -2972,7 +2972,7 @@ describe('Gun', function(){
setTimeout(function(){ setTimeout(function(){
gun.get('parallel/later').get('bob').get('name').get(function(at, ev){ gun.get('parallel/later').get('bob').get('name').get(function(at, ev){
var err = at.err, data = at.put, field = at.get; var err = at.err, data = at.put, field = at.get;
//console.log("*********** name", data, field); return; //console.log("***********", field, data); return;
expect(data).to.be('Bob!'); expect(data).to.be('Bob!');
expect(field).to.be('name'); expect(field).to.be('name');
done.name = true; done.name = true;
@ -3511,7 +3511,7 @@ describe('Gun', function(){
}, {v2020:1}); }, {v2020:1});
setTimeout(function(){ // adding more rigorous test! setTimeout(function(){ // adding more rigorous test!
//console.log("------------");
app.get('a').get('b').get(function(d){ app.get('a').get('b').get(function(d){
d = (d.$$||d.$)._.put; d = (d.$$||d.$)._.put;
//console.log('****::::', d); //console.log('****::::', d);
@ -3536,25 +3536,28 @@ describe('Gun', function(){
var app = gun.get('nl/app').get('bar'); var app = gun.get('nl/app').get('bar');
app.get(function(d){ app.get(function(d){
//console.log("!!", d.put); //d = (d.$$||d.$)._.put;
if(!d || !d.put || !d.put.wat){ return } if(!d || !d.put || !d.put.wat){ return }
console.log('should be called: {wat:1}=', d.put);
expect(d.put.wat).to.be(1); expect(d.put.wat).to.be(1);
done.a = 1; done.a = 1;
if(!done.u){ return } if(!done.u){ return }
expect(done.u).to.be.ok(); expect(done.u).to.be.ok();
if(done.c){ return } done.c = 1; if(done.c){ return } done.c = 1;
done(); done();
}); });//, {v2020:1});
console.log("----------");
app.get('a').get('b').get(function(d){ app.get('a').get('b').get(function(d){
//console.log("????", d.put); console.log("empty/clear: undefined=", d.put);
//d = (d.$$||d.$)._.put;
expect(d.put).to.be(u); expect(d.put).to.be(u);
done.u = true; done.u = true;
if(!done.a){ return } if(!done.a){ return }
expect(done.a).to.be.ok(); expect(done.a).to.be.ok();
if(done.c){ return } done.c = 1; if(done.c){ return } done.c = 1;
done(); done();
}); }, {v2020:1});
}, 1000); }, 1000);
}); });