link nodes like a boss!

This commit is contained in:
Mark Nadal 2016-02-04 11:11:53 -08:00
parent d1828e9871
commit b7a335dd3b
3 changed files with 37 additions and 5 deletions

16
gun.js
View File

@ -548,13 +548,23 @@
Gun.chain.put = function(val, cb, opt){
opt = opt || {};
cb = cb || function(){}; cb.hash = {};
var gun = this, chain = gun.chain(), drift = Gun.time.now();
var gun = this, chain = gun.chain(), tmp = {val: val}, drift = Gun.time.now();
function put(at){
if(cb.hash[at.hash = at.hash || Gun.on.at.hash(at)]){ return } // if we have already seen this hash...
cb.hash[at.hash] = true; // else mark that we're processing the data (failure to write could still occur).
var val = tmp.val;
var ctx = {obj: val}; // prep the value for serialization
ctx.soul = at.field? at.soul : (at.at && at.at.soul) || at.soul; // figure out where we are
ctx.field = at.field? at.field : (at.at && at.at.field) || at.field; // did we come from some where?
if(Gun.is(val)){
if(!ctx.field){ return cb.call(chain, {err: ctx.err = Gun.log('No field to link node to!')}), chain._.at('err').emit(ctx.err) }
return val.val(function(node){
var soul = Gun.is.node.soul(node);
if(!soul){ return cb.call(chain, {err: ctx.err = Gun.log('Only a node can be linked! Not "' + node + '"!')}), chain._.at('err').emit(ctx.err) }
tmp.val = Gun.is.rel.ify(soul);
put(at);
});
}
if(cb.hash[at.hash = at.hash || Gun.on.at.hash(at)]){ return } // if we have already seen this hash...
cb.hash[at.hash] = true; // else mark that we're processing the data (failure to write could still occur).
ctx.by = chain.__.by(ctx.soul);
ctx.not = at.not || (at.at && at.at.not);
Gun.obj.del(at, 'not'); Gun.obj.del(at.at || at, 'not'); // the data is no longer not known! // TODO: BUG! It could have been asynchronous by the time we now delete these properties. Don't other parts of the code assume their deletion is synchronous?

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.3.2",
"version": "0.3.3",
"description": "Graph engine",
"main": "index.js",
"scripts": {

View File

@ -1714,7 +1714,8 @@ describe('Gun', function(){
});
});
it.skip('put gun node', function(done){
it('put gun node', function(done){
var gun = Gun();
var mark = gun.put({age: 23, name: "Mark Nadal"});
var amber = gun.put({age: 23, name: "Amber Nadal"});
mark.path('wife').put(amber, function(err){
@ -1722,6 +1723,27 @@ describe('Gun', function(){
});
mark.path('wife.name').val(function(val){
expect(val).to.be("Amber Nadal");
done();
});
});
it('put gun node on node', function(done){
var gun = Gun();
var mark = gun.put({age: 23, name: "Mark Nadal"});
var amber = gun.put({age: 23, name: "Amber Nadal"});
mark.put(amber, function(err){
expect(err).to.be.ok();
done();
});
});
it('put gun path on path', function(done){
var gun = Gun();
var mark = gun.put({age: 23, name: "Mark Nadal"});
var amber = gun.put({age: 23, name: "Amber Nadal"});
mark.path('wife').put(amber.path('age'), function(err){
expect(err).to.be.ok();
done();
});
});