From 43e4b59d9300c3ff1551387f659f8fe1a6a22f43 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Wed, 23 May 2018 23:02:36 -0700 Subject: [PATCH] don't double send --- gun.js | 33 ++++++++++++++++++++++----------- lib/radisk.js | 4 ++-- lib/server.js | 1 + 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/gun.js b/gun.js index ba520d9b..32f1180b 100644 --- a/gun.js +++ b/gun.js @@ -169,7 +169,7 @@ // On event emitter generic javascript utility. module.exports = function onto(tag, arg, as){ if(!tag){ return {to: onto} } - var tag = (this.tag || (this.tag = {}))[tag] || + var u, tag = (this.tag || (this.tag = {}))[tag] || (this.tag[tag] = {tag: tag, to: onto._ = { next: function(arg){ var tmp; if((tmp = this.to)){ @@ -200,7 +200,7 @@ (be.back = tag.last || tag).to = be; return tag.last = be; } - (tag = tag.to).next(arg); + if((tag = tag.to) && u !== arg){ tag.next(arg) } return tag; }; })(USE, './onto'); @@ -685,7 +685,7 @@ var gun = at.gun.opt(at.opt); if(!at.once){ at.on('in', root, at); - at.on('out', root, at); + at.on('out', root, obj_to(at, {out: root})); Gun.on('create', at); at.on('create', at); } @@ -697,7 +697,13 @@ var ev = this, at = ev.as, gun = at.gun, dup, tmp; //if(!msg.gun){ msg.gun = at.gun } if(!(tmp = msg['#'])){ tmp = msg['#'] = text_rand(9) } - if((dup = at.dup).check(tmp)){ return } + if((dup = at.dup).check(tmp)){ + if(at.out === msg.out){ + msg.out = u; + ev.to.next(msg); + } + return; + } dup.track(tmp); //msg = obj_to(msg);//, {gun: at.gun}); // can we delete this now? if(!at.ask(msg['@'], msg)){ @@ -710,7 +716,11 @@ //at.on('put', put(msg)); } } - at.on('out', msg); + ev.to.next(msg); + if(!at.out){ + msg.out = root; + at.on('out', msg); + } } }()); @@ -1746,7 +1756,7 @@ If you update anything here, consider updating the other adapters as well. */ - Gun.on('opt', function(root){ + Gun.on('create', function(root){ // This code is used to queue offline writes for resync. // See the next 'opt' code below for actual saving of data. var ev = this.to, opt = root.opt; @@ -1811,7 +1821,7 @@ } }); - Gun.on('opt', function(root){ + Gun.on('create', function(root){ this.to.next(root); var opt = root.opt; if(root.once){ return } @@ -1886,6 +1896,7 @@ mesh.out = function(msg){ var tmp; //console.log("count:", msg['#'], msg); if(this.to){ this.to.next(msg) } + //if(mesh.last != msg['#']){ return mesh.last = msg['#'], this.to.next(msg) } if((tmp = msg['@']) && (tmp = ctx.dup.s[tmp]) && (tmp = tmp.it) @@ -1976,11 +1987,11 @@ } function send(raw, peer){ - //console.log("send:", raw.slice(raw.indexOf('#'), 20)); var wire = peer.wire; try{ if(wire.send){ if(wire.readyState === wire.OPEN){ + //console.log("send:", raw); wire.send(raw); } else { (peer.queue = peer.queue || []).push(raw); @@ -2094,10 +2105,10 @@ opt.WebSocket = websocket; var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); - //root.on('create', function(at){ - //this.to.next(at); + root.on('create', function(at){ + this.to.next(at); root.on('out', mesh.out); - //}); + }); opt.wire = opt.wire || open; function open(peer){ diff --git a/lib/radisk.js b/lib/radisk.js index 1c4075fd..708447f9 100644 --- a/lib/radisk.js +++ b/lib/radisk.js @@ -204,7 +204,7 @@ function Radisk(opt){ } g.ack = function(as){ if(!as.ack){ return } - as.ack(g.err, RAD(as.key)); + as.ack(g.err, (RAD || noop)(as.key)); } opt.store.list(g.lex); } @@ -257,7 +257,7 @@ function Radisk(opt){ opt.store.get(file, p.read); } - var q = {}, RAD, u; + var q = {}, noop = function(){}, RAD, u; return r; } diff --git a/lib/server.js b/lib/server.js index eec4ba66..9ecc84e3 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,6 +1,7 @@ ;(function(){ var Gun = require('../gun'), u; Gun.serve = require('./serve'); + process.env.GUN_ENV = process.env.GUN_ENV || 'debug'; Gun.on('opt', function(root){ this.to.next(root); if(root.once){ return }