mirror of
https://github.com/amark/gun.git
synced 2025-06-08 07:06:44 +00:00
couple fixes
This commit is contained in:
parent
22f98065db
commit
59f20c2cbd
@ -24,6 +24,11 @@
|
|||||||
<button>send</button>
|
<button>send</button>
|
||||||
</form>
|
</form>
|
||||||
<script>
|
<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){
|
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);
|
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);
|
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]||'';
|
$('.who', $('form')).value = (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'';
|
||||||
$('.what', $('form')).focus();
|
$('.what', $('form')).focus();
|
||||||
$('form').onsubmit = function(e){
|
$('form').onsubmit = function(e){
|
||||||
|
@ -22,10 +22,14 @@
|
|||||||
</form>
|
</form>
|
||||||
</li></ul>
|
</li></ul>
|
||||||
<script>
|
<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');
|
var ref = Gun(location.origin + '/gun').get('example/json/data');
|
||||||
ref.not(function(){
|
ref/*.not(function(key){
|
||||||
return this.put({hello: "world!"}).key('example/json/data');
|
console.log("does NOT exist", key);
|
||||||
}).on(function(data){
|
return this.put({hello: "world!"}).key(key);
|
||||||
|
})*/.on(function(data){
|
||||||
|
console.log("ugggh", data);
|
||||||
for(var field in data){
|
for(var field in data){
|
||||||
if(field === '_'){ continue } // skip meta data!
|
if(field === '_'){ continue } // skip meta data!
|
||||||
var val = (data[field = field || ''] || '').toString(), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe.
|
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, '<'); // escape and display value
|
$('span', elem).innerHTML = val.replace(/\</ig, '<'); // escape and display value
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
|
|
||||||
document.onkeyup = function(e){
|
document.onkeyup = function(e){
|
||||||
if(!e || !e.target){ return } // ignore if no element!
|
if(!e || !e.target){ return } // ignore if no element!
|
||||||
if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable!
|
if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable!
|
||||||
|
38
gun.js
38
gun.js
@ -53,6 +53,7 @@
|
|||||||
ctx.by = chain.__.by(ctx.soul);
|
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(!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(!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.
|
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.
|
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;
|
function soul(env, cb, map){ var eat;
|
||||||
@ -130,19 +131,20 @@
|
|||||||
return chain;
|
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 = opt || {};
|
||||||
opt.rel = opt.rel || Gun.is.rel(key);
|
opt.rel = opt.rel || Gun.is.rel(soul);
|
||||||
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 }
|
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.
|
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.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 = 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 chains(cb){ if(opt.chain){ cb(opt.chain) } cb(ctx.by.chain); }
|
||||||
function memory(at){
|
function memory(at){
|
||||||
var cached = ctx.chain.__.by(at.soul).node;
|
var cached = ctx.chain.__.by(at.soul).node;
|
||||||
if(!cached){ return false }
|
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);
|
opt.key = opt.key || Gun.is.node.soul(cached);
|
||||||
Gun.is.node(at.change || cached, union);
|
Gun.is.node(at.change || cached, union);
|
||||||
}
|
}
|
||||||
@ -166,7 +168,7 @@
|
|||||||
}
|
}
|
||||||
if(!data){
|
if(!data){
|
||||||
cb.call(ctx.chain, null, null);
|
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(Gun.obj.empty(data)){ return }
|
||||||
if(err = Gun.union(ctx.chain, data).err){
|
if(err = Gun.union(ctx.chain, data).err){
|
||||||
@ -182,16 +184,16 @@
|
|||||||
else if(at.field){ return }
|
else if(at.field){ return }
|
||||||
if(memory(at)){ return }
|
if(memory(at)){ return }
|
||||||
if(Gun.fns.is(map.wire)){
|
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!") }
|
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.
|
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) }
|
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(key)).map(map);
|
map.raw = (map.at = ctx.chain.__.at(soul)).map(map);
|
||||||
if(opt.raw){ opt.raw = map.raw }
|
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;
|
return ctx.chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,6 +342,7 @@
|
|||||||
chain._.at('soul').emit(on);
|
chain._.at('soul').emit(on);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
//console.log("TON", at);
|
||||||
kick.c = -1
|
kick.c = -1
|
||||||
kick.chain = gun.chain();
|
kick.chain = gun.chain();
|
||||||
kick.next = cb.call(kick.chain, opt.raw? at : (at.field || at.soul || at.not), kick);
|
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){
|
tab.store.get(tab.prefix + key, function(err, data){
|
||||||
if(!data){ return } // let the peers handle no data.
|
if(!data){ return } // let the peers handle no data.
|
||||||
if(err){ return cb(err) }
|
if(err){ return cb(err) }
|
||||||
cb(err, data); // node
|
cb(err, cb.node = data); // node
|
||||||
cb(err, Gun.is.node.soul.ify({}, Gun.is.node.soul(data))); // end
|
cb(err, Gun.is.node.soul.ify({_: data._}, Gun.is.node.soul(data))); // end
|
||||||
cb(err, {}); // terminate
|
cb(err, {}); // terminate
|
||||||
});
|
});
|
||||||
}(key, cb));
|
}(key, cb));
|
||||||
if(!(cb.local = opt.local)){
|
if(!(cb.local = opt.local)){
|
||||||
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){ var p = {};
|
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){
|
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
|
if(!p.node && cb.node){ // if we have local data
|
||||||
tab.put(p.graph = cb.graph, function(e,r){ // then sync it if we haven't already
|
//Gun.log("tab get <---", key);
|
||||||
Gun.log("Stateless handshake sync:", e, r);
|
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.
|
}, {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.
|
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(Gun.text.is(cb)){ return (o = {})[cb] = {}, o }
|
||||||
if(cb && !cb.peers){ setTimeout(function(){
|
if(cb && !cb.peers){ setTimeout(function(){
|
||||||
if(!cb.local){ console.log("Warning! You have no peers to connect to!") }
|
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)}
|
},1)}
|
||||||
}
|
}
|
||||||
tab.server = tab.server || function(req, res){
|
tab.server = tab.server || function(req, res){
|
||||||
|
@ -42,7 +42,7 @@ Gun.on('opt').event(function(gun, opts) {
|
|||||||
node = all.nodes[soul];
|
node = all.nodes[soul];
|
||||||
if(!node){ return cb(null, null) }
|
if(!node){ return cb(null, null) }
|
||||||
cb(null, node);
|
cb(null, node);
|
||||||
node = Gun.is.node.ify({}, soul);
|
node = Gun.is.node.ify({_: node._}, soul);
|
||||||
cb(null, node); // end.
|
cb(null, node); // end.
|
||||||
cb(null, {}); // done.
|
cb(null, {}); // done.
|
||||||
},
|
},
|
||||||
|
@ -1146,7 +1146,6 @@ describe('Gun', function(){
|
|||||||
}).get('yes/key', function(err, node){ // CHANGELOG: API 0.3 BREAKING CHANGE FROM err, graph
|
}).get('yes/key', function(err, node){ // CHANGELOG: API 0.3 BREAKING CHANGE FROM err, graph
|
||||||
expect(err).to.not.be.ok();
|
expect(err).to.not.be.ok();
|
||||||
expect(Gun.is.node.soul(node)).to.be('yes/key');
|
expect(Gun.is.node.soul(node)).to.be('yes/key');
|
||||||
console.log("wait what?", done.keycb);
|
|
||||||
expect(done.keycb).to.be.ok();
|
expect(done.keycb).to.be.ok();
|
||||||
expect(node.hello).to.be('key');
|
expect(node.hello).to.be('key');
|
||||||
if(done.c){ return }
|
if(done.c){ return }
|
||||||
@ -1444,16 +1443,16 @@ describe('Gun', function(){
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/* // TODO: Future feature!
|
it.skip('put gun node', function(done){
|
||||||
it('put gun node', function(done){
|
|
||||||
var mark = gun.put({age: 23, name: "Mark Nadal"});
|
var mark = gun.put({age: 23, name: "Mark Nadal"});
|
||||||
var amber = gun.put({age: 23, name: "Amber Nadal"});
|
var amber = gun.put({age: 23, name: "Amber Nadal"});
|
||||||
mark.path('wife').put(amber, function(err){
|
mark.path('wife').put(amber, function(err){
|
||||||
expect(err).to.not.be.ok();
|
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){
|
it('put val', function(done){
|
||||||
gun.put({hello: "world"}).val(function(val){
|
gun.put({hello: "world"}).val(function(val){
|
||||||
@ -2250,7 +2249,7 @@ describe('Gun', function(){
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('init todo', function(done){
|
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){
|
todo.map().on(function(val, field){
|
||||||
expect(val).to.be('eat chocolate');
|
expect(val).to.be('eat chocolate');
|
||||||
expect(field).to.be('random1');
|
expect(field).to.be('random1');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user