couple fixes

This commit is contained in:
Mark Nadal 2015-12-30 14:51:15 -08:00
parent 22f98065db
commit 59f20c2cbd
5 changed files with 40 additions and 35 deletions

View File

@ -24,6 +24,11 @@
<button>send</button>
</form>
<script>
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
$.sort = function(when, e){ return (when > ($('.sort', e)[$.text] || 0))? e : $.sort(when, e.previousSibling) }
$.text = document.body.textContent? 'textContent' : 'innerText'; // because browsers are stupid.
($.model = $('ul li').cloneNode(true)).removeAttribute('class');
var chat = Gun(location.origin + '/gun').get('example/chat/data').not(function(key){
return this.put({1: {who: 'Welcome', what: "to the chat app!", when: 1}}).key(key);
});
@ -38,12 +43,7 @@
window.scrollTo(0, document.body.scrollHeight);
}
});
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
$.sort = function(when, e){ return (when > ($('.sort', e)[$.text] || 0))? e : $.sort(when, e.previousSibling) }
$.text = document.body.textContent? 'textContent' : 'innerText'; // because browsers are stupid.
($.model = $('ul li').cloneNode(true)).removeAttribute('class');
$('.who', $('form')).value = (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'';
$('.what', $('form')).focus();
$('form').onsubmit = function(e){

View File

@ -22,10 +22,14 @@
</form>
</li></ul>
<script>
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
var ref = Gun(location.origin + '/gun').get('example/json/data');
ref.not(function(){
return this.put({hello: "world!"}).key('example/json/data');
}).on(function(data){
ref/*.not(function(key){
console.log("does NOT exist", key);
return this.put({hello: "world!"}).key(key);
})*/.on(function(data){
console.log("ugggh", data);
for(var field in data){
if(field === '_'){ continue } // skip meta data!
var val = (data[field = field || ''] || '').toString(), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe.
@ -34,8 +38,6 @@
$('span', elem).innerHTML = val.replace(/\</ig, '&lt;'); // escape and display value
}
});
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
document.onkeyup = function(e){
if(!e || !e.target){ return } // ignore if no element!
if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable!

38
gun.js
View File

@ -53,6 +53,7 @@
ctx.by = chain.__.by(ctx.soul);
if(!at.soul || (at.not && at.not === at.field) || cb[at.hash = at.hash || Gun.on.at.hash(at)]){ return } cb[at.hash] = true; // only put the data once on each soul we encounter.
if(!opt.key && Gun.is.node.soul(ctx.by.node, Gun._.key)){ return } // ignore key nodes unless we're trying to update one explicitly.
//if(Gun.is(val) && (val._||{}).get){ ctx.obj = val = Gun.is.rel.ify(val._.get) }
if(ctx.field){ Gun.obj.as(ctx.obj = {}, ctx.field, val) } // if there is a field, then data is actually getting put on the parent.
else if(!Gun.obj.is(val)){ return cb.call(chain, ctx.err = {err: Gun.log("No node exists to put " + (typeof val) + ' "' + val + '" in!')}), chain._.at('err').emit(ctx.err) } // if the data is a primitive and there is no context for it yet, then we have an error.
function soul(env, cb, map){ var eat;
@ -130,19 +131,20 @@
return chain;
}
Gun.chain.get = function(key, cb, opt){ // get opens up a reference to a node and loads it.
Gun.chain.get = function(soul, cb, opt){ // get opens up a reference to a node and loads it.
opt = opt || {};
opt.rel = opt.rel || Gun.is.rel(key);
if(!(key = Gun.is.rel(key) || key) || !Gun.text.is(key)){ return key = this.chain(), cb.call(key, {err: Gun.log('No key or relation to get!')}), key }
opt.rel = opt.rel || Gun.is.rel(soul);
if(!(soul = Gun.is.rel(soul) || soul) || !Gun.text.is(soul)){ return soul = this.chain(), cb.call(soul, {err: Gun.log('No soul or relation to get!')}), soul }
cb = cb || function(){}; // only gets called for wire stuff, not chain events.
var ctx = {by: this.__.by(key)};
var ctx = {by: this.__.by(soul)};
ctx.by.chain = ctx.by.chain || this.chain();
ctx.chain = opt.chain || ctx.by.chain; // TODO: BUG! opt.chain is asking for us to fire on this chain, not necessarily make it the this of callbacks.
//ctx.chain._.get = soul;
function chains(cb){ if(opt.chain){ cb(opt.chain) } cb(ctx.by.chain); }
function memory(at){
var cached = ctx.chain.__.by(at.soul).node;
if(!cached){ return false }
if(Gun.is.node.soul(cached, Gun._.key)){ // TODO: End node from wire might not have key indicator?
if(Gun.is.node.soul(cached, Gun._.key)){ // TODO: End node from wire might not have key indicator? // TODO: MARK! KEY! REVISE!
opt.key = opt.key || Gun.is.node.soul(cached);
Gun.is.node(at.change || cached, union);
}
@ -166,7 +168,7 @@
}
if(!data){
cb.call(ctx.chain, null, null);
return chains(function(chain){ chain._.at('null').emit({not: key}) });
return chains(function(chain){ chain._.at('null').emit({not: soul}) });
}
if(Gun.obj.empty(data)){ return }
if(err = Gun.union(ctx.chain, data).err){
@ -182,16 +184,16 @@
else if(at.field){ return }
if(memory(at)){ return }
if(Gun.fns.is(map.wire)){
return map.wire(key, get, opt);
return map.wire(soul, get, opt);
}
if(!Gun.log.count('no-wire-get')){ Gun.log("Warning! You have no persistence layer to get from!") }
cb.call(ctx.chain, null); // This is in memory success, hardly "success" at all.
chains(function(chain){ chain._.at('null').emit({not: key}) });
chains(function(chain){ chain._.at('null').emit({not: soul}) });
}
if(Gun.fns.is(map.wire = ctx.chain.__.opt.wire.get) && opt.force){ map.wire(key, get, opt) }
map.raw = (map.at = ctx.chain.__.at(key)).map(map);
if(Gun.fns.is(map.wire = ctx.chain.__.opt.wire.get) && opt.force){ map.wire(soul, get, opt) }
map.raw = (map.at = ctx.chain.__.at(soul)).map(map);
if(opt.raw){ opt.raw = map.raw }
if(!map.ran){ map.at.emit({soul: key}) } // TODO: BUG! Add a change set!
if(!map.ran){ map.at.emit({soul: soul}) } // TODO: BUG! Add a change set!
return ctx.chain;
}
@ -340,6 +342,7 @@
chain._.at('soul').emit(on);
});
};
//console.log("TON", at);
kick.c = -1
kick.chain = gun.chain();
kick.next = cb.call(kick.chain, opt.raw? at : (at.field || at.soul || at.not), kick);
@ -1015,17 +1018,18 @@
tab.store.get(tab.prefix + key, function(err, data){
if(!data){ return } // let the peers handle no data.
if(err){ return cb(err) }
cb(err, data); // node
cb(err, Gun.is.node.soul.ify({}, Gun.is.node.soul(data))); // end
cb(err, cb.node = data); // node
cb(err, Gun.is.node.soul.ify({_: data._}, Gun.is.node.soul(data))); // end
cb(err, {}); // terminate
});
}(key, cb));
if(!(cb.local = opt.local)){
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){ var p = {};
tab.request(url, null, tab.error(cb, "Error: Get failed through " + url, function(reply){
if(!p.graph && !Gun.obj.empty(cb.graph)){ // if we have local data
tab.put(p.graph = cb.graph, function(e,r){ // then sync it if we haven't already
Gun.log("Stateless handshake sync:", e, r);
if(!p.node && cb.node){ // if we have local data
//Gun.log("tab get <---", key);
tab.put(Gun.is.graph.ify(p.node = cb.node), function(e,r){ // then sync it if we haven't already
//Gun.log("Stateless handshake sync:", e, r);
}, {peers: tab.peers(url)}); // to the peer. // TODO: This forces local to flush again, not necessary.
}
setTimeout(function(){ tab.put(reply.body, function(){}, {local: true}) },1); // and flush the in memory nodes of this graph to localStorage after we've had a chance to union on it.
@ -1062,7 +1066,7 @@
if(Gun.text.is(cb)){ return (o = {})[cb] = {}, o }
if(cb && !cb.peers){ setTimeout(function(){
if(!cb.local){ console.log("Warning! You have no peers to connect to!") }
if(!(cb.graph || cb.node)){ cb() }
if(!(cb.graph || cb.node)){ cb(null, null) }
},1)}
}
tab.server = tab.server || function(req, res){

View File

@ -42,7 +42,7 @@ Gun.on('opt').event(function(gun, opts) {
node = all.nodes[soul];
if(!node){ return cb(null, null) }
cb(null, node);
node = Gun.is.node.ify({}, soul);
node = Gun.is.node.ify({_: node._}, soul);
cb(null, node); // end.
cb(null, {}); // done.
},

View File

@ -1146,7 +1146,6 @@ describe('Gun', function(){
}).get('yes/key', function(err, node){ // CHANGELOG: API 0.3 BREAKING CHANGE FROM err, graph
expect(err).to.not.be.ok();
expect(Gun.is.node.soul(node)).to.be('yes/key');
console.log("wait what?", done.keycb);
expect(done.keycb).to.be.ok();
expect(node.hello).to.be('key');
if(done.c){ return }
@ -1444,16 +1443,16 @@ describe('Gun', function(){
});
});
/* // TODO: Future feature!
it('put gun node', function(done){
it.skip('put gun node', function(done){
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){
expect(err).to.not.be.ok();
expect(false).to.be.ok(); // what whatttt???
});
mark.path('wife.name').val(function(val){
expect(val).to.be("Amber Nadal");
});
});
*/
it('put val', function(done){
gun.put({hello: "world"}).val(function(val){
@ -2250,7 +2249,7 @@ describe('Gun', function(){
});
it('init todo', function(done){
var gun = Gun(), todo = gun.get('init/todo');
var gun = Gun(), todo = gun.get('init/todo/defer');
todo.map().on(function(val, field){
expect(val).to.be('eat chocolate');
expect(field).to.be('random1');