mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
merge, update stun
This commit is contained in:
parent
fed500dd69
commit
d3dd54de0a
65
gun.js
65
gun.js
@ -62,7 +62,7 @@
|
||||
return l;
|
||||
}
|
||||
;(function(){ // max ~1ms or before stack overflow
|
||||
var u, sT = setTimeout, l = 0, c = 0, sI = (typeof setImmediate !== ''+u && setImmediate) || sT;
|
||||
var u, sT = setTimeout, l = 0, c = 0, sI = (typeof setImmediate !== ''+u && setImmediate) || sT; // queueMicrotask faster but blocks UI
|
||||
sT.poll = sT.poll || function(f){
|
||||
if((1 >= (+new Date - l)) && c++ < 3333){ f(); return }
|
||||
sI(function(){ l = +new Date; f() },c=0)
|
||||
@ -345,7 +345,7 @@
|
||||
++ni; kl = null; pop(o);
|
||||
}());
|
||||
} Gun.on.put = put;
|
||||
console.log("BEWARE: BETA VERSION OF NEW GUN! NOT ALL FEATURES FINISHED!"); // clock below, reconnect sync, SEA certify wire merge, // msg put, put, say ack, hear loop...
|
||||
console.log("BEWARE: BETA VERSION OF NEW GUN! NOT ALL FEATURES FINISHED!"); // clock below, reconnect sync, SEA certify wire merge, User.auth taking multiple times, // msg put, put, say ack, hear loop...
|
||||
function ham(val, key, soul, state, msg){
|
||||
var ctx = msg._||'', root = ctx.root, graph = root.graph, lot, tmp;
|
||||
var vertex = graph[soul] || empty, was = state_is(vertex, key, 1), known = vertex[key];
|
||||
@ -407,7 +407,7 @@
|
||||
|
||||
;(function(){
|
||||
Gun.on.get = function(msg, gun){
|
||||
var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'], tmp;
|
||||
var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'];
|
||||
var next = root.next || (root.next = {}), at = next[soul];
|
||||
// queue concurrent GETs?
|
||||
// TODO: consider tagging original message into dup for DAM.
|
||||
@ -823,7 +823,7 @@
|
||||
var wait = {}; // can we assign this to the at instead, like in once?
|
||||
function any(msg, eve, f){
|
||||
if(any.stun){ return }
|
||||
var at = msg.$._, data = at.put, aid, tmp;
|
||||
var at = msg.$._, data = at.put, aid, test, tmp;
|
||||
if((tmp = root.pass) && !tmp[id]){ return }
|
||||
if(!at.has && !at.soul){ data = (u !== (msg.put||'')['='])? msg.put['='] : msg.put } // handles non-core messages.
|
||||
if('string' == typeof (tmp = Gun.valid(data))){ data = root.$.get(tmp)._.put } // TODO: Can we delete this line of code, because the line below (which was inspired by @rogowski) handles it anyways?
|
||||
@ -832,14 +832,20 @@
|
||||
if(u === opt.stun){
|
||||
//if(tmp = root.stun){ tmp = tmp[at.id] || at.$.back(function(back){ return tmp[back.id] || u }); if(tmp && !tmp.end && any.id > (tmp._||'').id){ // this is more thorough, but below seems to work too?
|
||||
//if((tmp = root.stun) && (tmp = tmp[at.id] || tmp[at.back.id]) && !tmp.end && any.id > (tmp._||'').id){ // if we are in the middle of a write, don't read until it is done, unless our callback was earlier than the write.
|
||||
if((tmp = root.stun) && (tmp = tmp[aid = cat.id] || tmp[aid = at.id] || (msg.$$ && tmp[aid = msg.$$._.id]) /*|| tmp[aid = at.back.id]*/) && any.id > tmp.run){
|
||||
if(tmp.stun && !tmp.stun.end){
|
||||
tmp.stun[id] = function(){any(msg,eve,1)}; // add ourself to the stun callback list that is called at end of the write.
|
||||
return;
|
||||
if((tmp = root.stun) && tmp.on){
|
||||
tmp.on(''+(aid = cat.id), test = {});
|
||||
!test.run && tmp.on(''+(aid = at.id), test);
|
||||
!test.run && msg.$$ && tmp.on(''+(aid = msg.$$._.id), test);
|
||||
if(test.run && any.id > test.run){ // what if I'm less than the last item but more than an earlier item? Don't I need to check first item but add to last item?
|
||||
if(!test.stun || test.stun.end){
|
||||
test.stun = tmp.on('stun');
|
||||
test.stun = test.stun && test.stun.last;
|
||||
}
|
||||
if(test.stun && !test.stun.end){
|
||||
(test.stun.add || (test.stun.add = {}))[id] = function(){any(msg,eve,1)} // add ourself to the stun callback list that is called at end of the write.
|
||||
return;
|
||||
}
|
||||
}
|
||||
root.stun[aid] = tmp.next;
|
||||
any(msg,eve,f);
|
||||
return;
|
||||
}
|
||||
if((tmp = root.hatch) && !tmp.end && u === opt.hatch && !f){ // quick hack! // What's going on here? Because data is streamed, we get things one by one, but a lot of developers would rather get a callback after each batch instead, so this does that by creating a wait list per chain id that is then called at the end of the batch by the hatch code in the root put listener.
|
||||
if(wait[at.$._.id]){ return } wait[at.$._.id] = 1;
|
||||
@ -879,9 +885,6 @@
|
||||
if(cb){ cb.call(gun, gun._.err) }
|
||||
return gun;
|
||||
}
|
||||
if(tmp = this._.stun){ // TODO: Refactor?
|
||||
gun._.stun = gun._.stun || tmp;
|
||||
}
|
||||
if(cb && 'function' == typeof cb){
|
||||
gun.get(cb, as);
|
||||
}
|
||||
@ -945,7 +948,6 @@
|
||||
as = as || {};
|
||||
as.root = at.root;
|
||||
as.run || (as.run = root.once);
|
||||
(as.stun || (as.stun = function(){ return as.run })).back = (root.stun || (root.stun = {}))._; (as.ta = root.stun)._ = as.stun;
|
||||
stun(as, at.id); // set a flag for reads to check if this chain is writing.
|
||||
as.ack = as.ack || cb;
|
||||
as.via = as.via || gun;
|
||||
@ -1009,9 +1011,23 @@
|
||||
|
||||
function stun(as, id){
|
||||
if(!id){ return } id = (id._||'').id||id;
|
||||
var tmp = as.root.stun || (as.root.stun = as.ta);
|
||||
var it = {run: as.run, stun: as.stun};
|
||||
(tmp[id]? (tmp[id].last.next = it) : (tmp[id] = it)).last = it;
|
||||
var run = as.root.stun || (as.root.stun = {on: Gun.on}), test = {}, tmp;
|
||||
as.stun || (as.stun = run.on('stun', function(){ }));
|
||||
if(tmp = run.on(''+id)){ tmp.the.last.next(test) }
|
||||
if(test.run >= as.run){ return }
|
||||
run.on(''+id, function(test){
|
||||
if(as.stun.end){
|
||||
this.off();
|
||||
this.to.next(test);
|
||||
return;
|
||||
}
|
||||
test.run = test.run || as.run;
|
||||
if(this.to.to){
|
||||
this.the.last.next(test);
|
||||
return;
|
||||
}
|
||||
test.stun = as.stun;
|
||||
});
|
||||
}
|
||||
|
||||
function ran(as){
|
||||
@ -1026,14 +1042,9 @@
|
||||
}, as.opt), acks = 0, stun = as.stun, tmp;
|
||||
(tmp = function(){ // this is not official yet, but quick solution to hack in for now.
|
||||
if(!stun){ return } stun.end = noop; // like with the earlier id, cheaper to make this flag a function so below callbacks do not have to do an extra type check.
|
||||
if(root.stun){ delete root.stun['s'+as.run] }
|
||||
if((tmp = root.stun) && stun === tmp._){
|
||||
if(!(tmp = stun.back) || tmp.end){
|
||||
delete root.stun; // ABC, ACB, BAC, BCA, CBA, CAB;
|
||||
}
|
||||
(root.stun||{})._ = tmp;
|
||||
}
|
||||
setTimeout.each(Object.keys(stun), function(cb){ if(cb = stun[cb]){cb()} }); // resume the stunned reads // Any perf reasons to CPU schedule this .keys( ?
|
||||
if(stun.the.to === stun && stun === stun.the.last){ delete root.stun }
|
||||
stun.off();
|
||||
setTimeout.each(Object.keys(stun = stun.add||''), function(cb){ if(cb = stun[cb]){cb()} }); // resume the stunned reads // Any perf reasons to CPU schedule this .keys( ?
|
||||
}).hatch = tmp; // this is not official yet ^
|
||||
//console.log(1, "PUT", as.run, as.graph);
|
||||
(as.via._).on('out', {put: as.out = as.graph, opt: as.opt, '#': ask, _: tmp});
|
||||
@ -1703,4 +1714,4 @@
|
||||
});
|
||||
})(USE, './localStorage');
|
||||
|
||||
}());
|
||||
}());
|
Loading…
x
Reference in New Issue
Block a user