From 4b41d04adab7d521f545a6b0f640bc2b6d23e6ca Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Fri, 16 Oct 2015 16:01:23 -0700 Subject: [PATCH] fix live replication bug depending on ordering --- examples/package.json | 2 +- gun.js | 9 +++++---- lib/wsp.js | 44 ++++++++++++++++++++++++++++++++----------- package.json | 4 ++-- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/examples/package.json b/examples/package.json index fd4bb1ee..21708c13 100644 --- a/examples/package.json +++ b/examples/package.json @@ -8,7 +8,7 @@ } , "dependencies": { "express": "~>4.9.0", - "gun": "~>0.2.2" + "gun": "~>0.2.3" } , "scripts": { "start": "node http.js", diff --git a/gun.js b/gun.js index a6842af6..8bf1058d 100644 --- a/gun.js +++ b/gun.js @@ -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; diff --git a/lib/wsp.js b/lib/wsp.js index feb9787a..59420a0f 100644 --- a/lib/wsp.js +++ b/lib/wsp.js @@ -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."}}); }); diff --git a/package.json b/package.json index 094a891c..790679bd 100644 --- a/package.json +++ b/package.json @@ -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"