From b7a335dd3bcd80439a3c0d84726c73cac80a521c Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Thu, 4 Feb 2016 11:11:53 -0800 Subject: [PATCH] link nodes like a boss! --- gun.js | 16 +++++++++++++--- package.json | 2 +- test/common.js | 24 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/gun.js b/gun.js index 973cb216..524f36cc 100644 --- a/gun.js +++ b/gun.js @@ -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? diff --git a/package.json b/package.json index f3b368fa..ca0dc823 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.3.2", + "version": "0.3.3", "description": "Graph engine", "main": "index.js", "scripts": { diff --git a/test/common.js b/test/common.js index 7498356d..7d089694 100644 --- a/test/common.js +++ b/test/common.js @@ -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(); }); });