fix live replication bug depending on ordering

This commit is contained in:
Mark Nadal 2015-10-16 16:01:23 -07:00
parent ebe3f77693
commit 4b41d04ada
4 changed files with 41 additions and 18 deletions

View File

@ -8,7 +8,7 @@
}
, "dependencies": {
"express": "~>4.9.0",
"gun": "~>0.2.2"
"gun": "~>0.2.3"
}
, "scripts": {
"start": "node http.js",

9
gun.js
View File

@ -1021,11 +1021,12 @@
tab.put(p.graph = cb.graph, 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.
// TODO: What about syncing our keys up?
}
Gun.is.graph(reply.body, function(node, soul){ // make sure for each received node
if(!Gun.is.soul(key)){ tab.key(key, soul, function(){}, {local: true}) } // that the key points to it.
});
if(!Gun.is.soul(key)){
Gun.is.graph(reply.body || gun.__.key.s[key], function(node, soul){ // make sure for each received node or nodes of our key
tab.key(key, soul, function(){}); // that the key points to 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.
}), opt);
cb.peers = true;

View File

@ -127,15 +127,37 @@
return cb({headers: reply.headers, body: (err? (err.err? err : {err: err || "Unknown error."}) : null)});
}
if(Gun.obj.empty(graph)){ return cb({headers: reply.headers, body: graph}) } // we're out of stuff!
// TODO: chunk the graph even if it is already chunked. pseudo code below!
/*Gun.is.graph(graph, function(node, soul){
if(Object.keys(node).length > 100){
// split object into many objects that have a fixed size
// iterate over each object
// cb({headers: reply.headers, chunk: {object} );
}
});*/
return cb({headers: reply.headers, chunk: graph }); // keep streaming
/*
(function(chunks){// FEATURE! Stream chunks if the nodes are large!
var max = 10;
Gun.is.graph(graph, function(node, soul){
var chunk = {};
console.log("node big enough?", Object.keys(node).length);
if(Object.keys(node).length > max){
var count = 0, n = Gun.union.pseudo(soul);
Gun.obj.map(node, function(val, field){
if(!(++count % max)){
console.log("Sending chunk", chunk);
cb({headers: reply.headers, chunk: chunk});
n = Gun.union.pseudo(soul);
chunk = {};
}
chunk[soul] = n;
n[field] = val;
(n._[Gun._.HAM] = n._[Gun._.HAM] || {})[field] = ((node._||{})[Gun._.HAM]||{})[field];
});
if(count % max){ // finish off the last chunk
cb({headers: reply.headers, chunk: chunk});
}
} else {
chunk[soul] = node;
console.log("Send BLOB", chunk);
cb({headers: reply.headers, chunk: chunk});
}
});
}([]));
*/
cb({headers: reply.headers, chunk: graph }); // Use this if you don't want streaming chunks feature.
});
}
tran.put = function(req, cb){
@ -153,8 +175,8 @@
var ctx = ctx || {}; ctx.graph = {};
Gun.is.graph(req.body, function(node, soul){
ctx.graph[soul] = gun.__.graph[soul]; // TODO: BUG? Probably should be delta fields
})
gun.__.opt.hooks.put(ctx.graph, function(err, ok){
});
(gun.__.opt.hooks.put || function(g,cb){cb("No save.")})(ctx.graph, function(err, ok){
if(err){ return cb({headers: reply.headers, body: {err: err || "Failed."}}) }
cb({headers: reply.headers, body: {ok: ok || "Persisted."}});
});

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.2.2",
"version": "0.2.3",
"description": "Graph engine",
"main": "index.js",
"scripts": {
@ -43,7 +43,7 @@
"dependencies": {
"aws-sdk": "~>2.0.0",
"formidable": "~>1.0.15",
"ws": "~>0.4.32"
"ws": "~>0.8.0"
},
"devDependencies": {
"mocha": "~>1.9.0"