opt.force

This commit is contained in:
Mark Nadal 2015-06-29 18:16:01 -07:00
parent bc5b992f90
commit 4212bc29d1
2 changed files with 62 additions and 0 deletions

1
gun.js
View File

@ -264,6 +264,7 @@
ctx.soul = Gun.is.soul(key); // if key is a soul, then the soul, else false.
cb = cb || function(){};
opt = opt || {};
if(opt.force){ load(key) } else
if(ctx.soul){
gun._.at('soul').emit({soul: ctx.soul, GET: 'SOUL'});
if(ctx.node = gun.__.graph[ctx.soul]){ // in memory

View File

@ -1481,5 +1481,66 @@ describe('Gun', function(){
},10);
},10);
});
it('get pseudo merge', function(done){
Gun.on('opt').event(function(gun, o){
gun.__.opt.hooks = {get: function(key, cb, opt){
var other = (o.alice? gun2 : gun1);
if(connect){
console.log('connect to peer and get', key);
other.get(key, cb);
} else {
cb();
}
}, put: function(nodes, cb, opt){
var other = (o.alice? gun2 : gun1);
if(connect){
Gun.union(other, nodes);
}
cb();
}, key: function(key, soul, cb, opt){
var other = (o.alice? gun2 : gun1);
if(connect){
other.key(key, null, soul);
}
cb();
}}
});
var connect, gun1 = Gun({alice: true}).get('pseudo/merge').not(function(){
return this.put({hello: "world!"}).key('pseudo/merge');
}), gun2;
gun1.val(function(val){
expect(val.hello).to.be('world!');
});
setTimeout(function(){
gun2 = Gun({bob: true}).get('pseudo/merge').not(function(){
return this.put({hi: "mars!"}).key('pseudo/merge');
});
gun2.val(function(val){
expect(val.hi).to.be('mars!');
});
setTimeout(function(){
// CONNECT THE TWO PEERS
connect = true;
Gun.log.verbose = true;
gun1.get('pseudo/merge', null, {force: true}); // fake a browser refersh, in real world we should auto-reconnect
gun2.get('pseudo/merge', null, {force: true}); // fake a browser refersh, in real world we should auto-reconnect
setTimeout(function(){
gun1.val(function(val){
expect(val.hello).to.be('world!');
expect(val.hi).to.be('mars!');
done.gun1 = true;
});
gun2.val(function(val){
expect(val.hello).to.be('world!');
expect(val.hi).to.be('mars!');
expect(done.gun1).to.be.ok();
done();
});
},10);
},10);
},10);
});
});
});