This commit is contained in:
Mark Nadal 2016-10-01 15:37:56 -07:00
parent aaf34a3dfe
commit 4d34c69e2f
2 changed files with 64 additions and 54 deletions

61
gun.js
View File

@ -898,6 +898,7 @@
function output(at){ function output(at){
var cat = this, gun = cat.gun, tmp; var cat = this, gun = cat.gun, tmp;
if(at.put){ if(at.put){
console.debug(3, 'OUT -> IN', at);
cat.on('in', obj_to(at, {'#': 0, gun: cat.gun})); cat.on('in', obj_to(at, {'#': 0, gun: cat.gun}));
} }
if(!at.gun){ if(!at.gun){
@ -961,7 +962,7 @@
var _soul = Gun._.soul, _field = Gun._.field; var _soul = Gun._.soul, _field = Gun._.field;
var u; 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 = 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) } 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)()); at.soul(Gun.node.soul(at.obj) || (as.opt.uuid || as.gun.Back('opt.uuid') || Gun.text.random)());
return; return;
} }
console.log("LAAAME", at.path, v);
(as.stun = as.stun || {})[path] = true; (as.stun = as.stun || {})[path] = true;
ref.any(soul, {as: {at: at, as: as}, '.': null}); ref.any(soul, {as: {at: at, as: as}, '.': null});
}, {as: as, at: at}); }, {as: as, at: at});
@ -1208,7 +1208,6 @@
}, ev); }, ev);
return; return;
} }
//console.log("HOLY BALONY", at, as);
/* /*
TODO: THIS WHOLE SECTION NEEDS TO BE CLEANED UP! TODO: THIS WHOLE SECTION NEEDS TO BE CLEANED UP!
Implicit behavior should be much cleaner. Right now it is hacky. 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! if(!opt || !opt.path){ var back = this.Back(-1); } // TODO: CHANGING API! Remove this line!
var gun, back = back || this, cat = back._; var gun, back = back || this, cat = back._;
var next = cat.next || empty, tmp; var next = cat.next || empty, tmp;
console.debug(2, 'get', lex);
if(typeof lex === 'string'){ if(typeof lex === 'string'){
if(!(gun = next[lex])){ if(!(gun = next[lex])){
gun = cache(lex, back); gun = cache(lex, back);
@ -1358,9 +1356,7 @@
cat.ask._ = cat.ask._ || {}; cat.ask._ = cat.ask._ || {};
if(get){ cat.ask[get] = cat.ask._['.'] = 1 } if(get){ cat.ask[get] = cat.ask._['.'] = 1 }
tmp = false; tmp = false;
console.debug(6, 'out', cat.get, get, cat.ask);
cat.on('in', function(tac, ev){ cat.on('in', function(tac, ev){
console.debug(7, 'out', tac);
input.call(cat, tac, ev); input.call(cat, tac, ev);
tmp = true; tmp = true;
return; return;
@ -1407,7 +1403,6 @@
}); });
return; return;
} }
console.debug(5, 'any out', cat.get, cat.ask);
cat.back.on('out', { cat.back.on('out', {
get: obj_put({}, _field, cat.get), get: obj_put({}, _field, cat.get),
gun: gun gun: gun
@ -1428,13 +1423,13 @@
if(at.via && (tmp = at.via['@'])){ if(at.via && (tmp = at.via['@'])){
Gun.on.ack(tmp, at); Gun.on.ack(tmp, at);
if(at.err){ return } 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)){ if(value.call(cat, at, ev)){
return; return;
} }
obj_map(change, map, {at: at, cat: cat}); // Important because `values` sometimes `ask`s for things which changes what the `changes` are. 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; Gun.chain.get.input = input;
function value(at, ev){ function value(at, ev){
@ -1446,6 +1441,7 @@
cat.put = coat.put; 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. // 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.proxy.res(obj_to(at, {get: cat.get || at.get})); // MUTATE AT?
} }
//not(); // ask? //not(); // ask?
@ -1457,6 +1453,7 @@
} }
if(coat.proxy && rel === coat.proxy.rel){ if(coat.proxy && rel === coat.proxy.rel){
coat.id = coat.id || Gun.text.random(6); coat.id = coat.id || Gun.text.random(6);
ask(cat, rel, coat);
if(cat === coat){ if(cat === coat){
cat.put = at.put = coat.proxy.ref._.put; cat.put = at.put = coat.proxy.ref._.put;
// change? // change?
@ -1464,11 +1461,10 @@
if(!cat.proxy || !cat.proxy[coat.id]){ if(!cat.proxy || !cat.proxy[coat.id]){
(cat.proxy = cat.proxy || {})[coat.id] = coat; (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? 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! gun.on('in', input, cat); // TODO: BUG! PERF! MEMORY LEAK!
} }
console.debug(9, 'values', cat.ask, coat.ask, coat); return cat.put === coat.put? false : true;
ask(cat, rel);
return;
} }
if(cat !== coat){ if(cat !== coat){
coat.id = coat.id || Gun.text.random(6); // TODO: BUG! REPEATS ABOVE coat.id = coat.id || Gun.text.random(6); // TODO: BUG! REPEATS ABOVE
@ -1662,30 +1658,29 @@
}); });
}); });
} }
function ask(cat, soul){ function ask(cat, soul, at){
var ask = cat.ask, lex; if(at && !at.ask){ at.ask = {} } // TODO: BUG! Second time through? Or first time through and *?
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"); var ask = cat.ask, coat = at || cat, proxy = coat.ask || ask, lex, tmp;
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 || !ask._){ return }
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"); proxy._ = proxy._ || {};
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(tmp = proxy._['*'] || ask._['*']){ // TODO! BUG! Should be proxy, right?
if(!ask || !soul){ return } if(0 <= tmp){
if(ask._['*']){ proxy._['*'] = -1;
if(0 <= ask._['*']){ coat.gun.on('out', {
ask._['*'] = -1;
cat.gun.on('out', {
get: {'#': soul}, get: {'#': soul},
gun: cat.gun, gun: coat.gun,
'#': Gun.on.ask(ack, cat.gun) '#': Gun.on.ask(ack, coat.gun)
}); });
return; return;
} }
} }
if(0 >= ask._['.']){ return } if(0 >= proxy._['.']){ return }
ask._['.'] = -1; proxy._['.'] = -1;
obj_map(ask, function(state, key){ obj_map(ask, function(state, key){
state = proxy[key] || (proxy[key] = state);
if(1 > state){ return } if(1 > state){ return }
if(!(state = (cat.next||empty)[key])){ return } if(!(state = (cat.next||empty)[key])){ return }
ask[key] = -1; proxy[key] = -1;
state.on('out', { state.on('out', {
get: {'#': soul, '.': key}, get: {'#': soul, '.': key},
gun: state, gun: state,
@ -1705,7 +1700,6 @@
var opt = opt || {}, gun = opt.gun = this; var opt = opt || {}, gun = opt.gun = this;
if(opt.change){ opt.change = 1 } if(opt.change){ opt.change = 1 }
opt.any = cb; opt.any = cb;
console.debug(4, 'any', gun._.get);
return gun.on('in', any, opt).on('out', {get: opt}); return gun.on('in', any, opt).on('out', {get: opt});
} }
function any(at, ev){ var opt = this; function any(at, ev){ var opt = this;
@ -1972,7 +1966,6 @@
opt = (true === opt)? {change: true} : opt || {}; opt = (true === opt)? {change: true} : opt || {};
opt.ok = tag; opt.ok = tag;
opt.last = {}; opt.last = {};
console.debug(3, 'on');
gun.any(ok, {as: opt, change: opt.change}); // TODO: PERF! Event listener leak!!!???? gun.any(ok, {as: opt, change: opt.change}); // TODO: PERF! Event listener leak!!!????
return gun; return gun;
} }
@ -2094,7 +2087,7 @@
var gun = this.gun, cat = this.cat, id = this.id; var gun = this.gun, cat = this.cat, id = this.id;
if(cat.list[id+f]){ return } if(cat.list[id+f]){ return }
// TODO: BUG! Ghosting! // 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, {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))._]); 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){ (cat.list[id+f] = gun.path(f)).on(function(v,s,a,ev){
@ -2169,7 +2162,7 @@
} }
Gun.on('put', put); Gun.on('put', put);
Gun.on('get', get); Gun.on('get', get);
})(require, './src/localStorage'); })(require, './adapters/localStorage');
;require(function(module){ ;require(function(module){
Gun.on('get', function(at){ Gun.on('get', function(at){
@ -2208,7 +2201,7 @@
}); });
Tab.peers(peers).send(msg, {headers: {'gun-sid': Tab.server.sid}}); Tab.peers(peers).send(msg, {headers: {'gun-sid': Tab.server.sid}});
}); });
})(require, './src/WebSocket'); })(require, './adapters/WebSocket');
}()); }());
/* /*
Gun.on('put', function(at){ Gun.on('put', function(at){

View File

@ -1463,11 +1463,12 @@ describe('Gun', function(){
if(check.alice && check.bob && check.alice.PhD){ if(check.alice && check.bob && check.alice.PhD){
expect(check.alice.age).to.be(24); expect(check.alice.age).to.be(24);
expect(check.bob.age).to.be(26); expect(check.bob.age).to.be(26);
expect(check.alice.PhD).to.be(true);
done(); done();
} }
}); });
gun.put({_:{'#':'g/n/m/f/l/n/r'}, gun.put({_:{'#':'g/n/m/f/l/n/r'},
alice: {_:{'#':'GALICE'}, alice: {_:{'#':'GALICE1'},
name: "alice", name: "alice",
age: 24, age: 24,
spouse: { spouse: {
@ -1492,14 +1493,14 @@ describe('Gun', function(){
} }
}); });
setTimeout(function(){ setTimeout(function(){
gun.get('GALICE').put({PhD: true}); gun.get('GALICE1').put({PhD: true});
},300); },300);
}); });
it("in memory get after", function(done){ it("in memory get after", function(done){
var gun = Gun(); var gun = Gun();
gun.put({_:{'#':'g/n/m/f/l/n'}, gun.put({_:{'#':'g/n/m/f/l/n'},
alice: { alice: {_:{'#':'GALICE2'},
name: "alice", name: "alice",
age: 24, age: 24,
spouse: { spouse: {
@ -1526,12 +1527,16 @@ describe('Gun', function(){
var check = {}; var check = {};
gun.get('g/n/m/f/l/n').map().on(function(v,f){ gun.get('g/n/m/f/l/n').map().on(function(v,f){
check[f] = v; 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.alice.age).to.be(24);
expect(check.bob.age).to.be(26); expect(check.bob.age).to.be(26);
expect(check.alice.PhD).to.be(true);
done(); done();
} }
}); });
setTimeout(function(){
gun.get('GALICE2').put({PhD: true});
},300);
}); });
it("in memory get before map path", function(done){ 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'}, gun.put({_:{'#':'g/n/m/f/l/n/b/p'},
alice: {_:{'#':'GALICE'}, alice: {_:{'#':'GALICE3'},
name: "alice", name: "alice",
age: 24, age: 24,
spouse: { spouse: {
@ -1572,14 +1577,14 @@ describe('Gun', function(){
} }
}); });
setTimeout(function(){ setTimeout(function(){
gun.get('GALICE').put({name: 'Alice'}); gun.get('GALICE3').put({name: 'Alice'});
},300); },300);
}); });
it.only("in memory get after map path", function(done){ it("in memory get after map path", function(done){
var gun = Gun(); var gun = Gun();
gun.put({_:{'#':'g/n/m/f/l/n/m/p'}, gun.put({_:{'#':'g/n/m/f/l/n/m/p'},
alice: {_:{'#':'GALICE'}, alice: {_:{'#':'GALICE4'},
name: "alice", name: "alice",
age: 24, age: 24,
spouse: { spouse: {
@ -1604,10 +1609,8 @@ describe('Gun', function(){
} }
}); });
var check = {}; 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){ gun.get('g/n/m/f/l/n/m/p').map().path('name').on(function(v,f){
check[v] = f; check[v] = f;
console.log("*******", f,v);
if(check.alice && check.bob && check.Alice){ if(check.alice && check.bob && check.Alice){
expect(check.alice).to.be('name'); expect(check.alice).to.be('name');
expect(check.bob).to.be('name'); expect(check.bob).to.be('name');
@ -1615,9 +1618,8 @@ describe('Gun', function(){
done(); done();
} }
}); });
return;
setTimeout(function(){ setTimeout(function(){
gun.get('GALICE').put({name: 'Alice'}); gun.get('GALICE4').put({name: 'Alice'});
},300); },300);
}); });
@ -1626,9 +1628,10 @@ describe('Gun', function(){
var check = {}; var check = {};
gun.get('g/n/m/f/l/n/b/p/p/p').map().path('spouse').path('work').on(function(v,f){ 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; 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['GUN INC']).to.be('work');
expect(check['ACME INC']).to.be('work'); expect(check['ACME INC']).to.be('work');
expect(check['ACME INC.']).to.be('work');
done(); done();
} }
}); });
@ -1651,12 +1654,15 @@ describe('Gun', function(){
spouse: { spouse: {
name: "diana", name: "diana",
age: 27, age: 27,
work: { work: {_:{'#':'CCINEMA1'},
name: "ACME INC" name: "ACME INC"
} }
} }
} }
}); });
setTimeout(function(){
gun.get('CCINEMA1').put({name: 'ACME INC.'});
},300);
}); });
it("in memory get after map path path", function(done){ it("in memory get after map path path", function(done){
@ -1680,7 +1686,7 @@ describe('Gun', function(){
spouse: { spouse: {
name: "diana", name: "diana",
age: 27, age: 27,
work: { work: {_:{'#':'CCINEMA2'},
name: "ACME INC" name: "ACME INC"
} }
} }
@ -1689,20 +1695,25 @@ describe('Gun', function(){
var check = {}; var check = {};
gun.get('g/n/m/f/l/n/b/p/p/p/a').map().path('spouse').path('work').on(function(v,f){ 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; 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['GUN INC']).to.be('work');
expect(check['ACME INC']).to.be('work'); expect(check['ACME INC']).to.be('work');
expect(check['ACME INC.']).to.be('work');
done(); 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 gun = Gun();
var check = {}; var check = {};
gun.get('g/n/m/f/l/n/b/a/m/m').map().map().on(function(v,f){ gun.get('g/n/m/f/l/n/b/a/m/m').map().map().on(function(v,f){
check[f] = v; 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.name).to.be('alice');
expect(check.alice.age).to.be(24); expect(check.alice.age).to.be(24);
expect(Gun.val.rel.is(check.alice.spouse)).to.be.ok(); 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(Gun.val.rel.is(check.bob.spouse)).to.be.ok();
expect(check.GUN.name).to.be('GUN'); expect(check.GUN.name).to.be('GUN');
expect(check.ACME.name).to.be('ACME'); expect(check.ACME.name).to.be('ACME');
expect(check.ACME.corp).to.be('C');
done(); done();
} }
}); });
console.debug.i=1;console.log("---------------------");
gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m'}, gun.put({_:{'#':'g/n/m/f/l/n/b/a/m/m'},
users: { users: {
alice: { alice: {
@ -1738,13 +1751,17 @@ describe('Gun', function(){
GUN: { GUN: {
name: "GUN" name: "GUN"
}, },
ACME: { ACME: {_:{'#':"CCINEMA3"},
name: "ACME" name: "ACME"
} }
} }
}); });
setTimeout(function(){
console.log("~~~~~~~~~~~~~~~~~");
gun.get('CCINEMA3').put({corp: "C"});
},300);
}); });
return;
it("in memory get after map map", function(done){ it("in memory get after map map", function(done){
var gun = Gun(); var gun = Gun();
gun.put({_:{'#':'g/n/m/f/l/n/b/m/m'}, gun.put({_:{'#':'g/n/m/f/l/n/b/m/m'},