goodbye experiment

This commit is contained in:
Mark Nadal 2017-08-25 03:39:37 -07:00
parent 3ec7d81f85
commit b3e59d243e
10 changed files with 85 additions and 23 deletions

3
gun.js
View File

@ -886,6 +886,7 @@
if(!at.gun){ if(!at.gun){
at.gun = gun; at.gun = gun;
} }
this.to.next(at);
if(get = at.get){ if(get = at.get){
if(tmp = get[_soul]){ if(tmp = get[_soul]){
tmp = (root.get(tmp)._); tmp = (root.get(tmp)._);
@ -966,7 +967,7 @@
proxy.at.on('in', proxy.at); proxy.at.on('in', proxy.at);
}); });
} }
if(cat.ack){ if(cat.ack >= 0){
if(!obj_has(cat, 'put')){ // u !== cat.put instead? if(!obj_has(cat, 'put')){ // u !== cat.put instead?
//if(u !== cat.put){ //if(u !== cat.put){
return; return;

38
lib/bye.js Normal file
View File

@ -0,0 +1,38 @@
if(typeof window !== "undefined"){
var Gun = window.Gun;
} else {
var Gun = require('../gun');
}
Gun.on('opt', function(ctx){
this.to.next(ctx);
if(ctx.once){ return }
ctx.on('in', function(msg){
if(!msg.peer || !msg.BYE){ return this.to.next(msg) }
var peer = msg.peer();
(peer.bye = peer.bye || []).push(msg.BYE);
})
ctx.on('bye', function(peer){
if(!peer.bye){ return }
var gun = ctx.gun;
Gun.obj.map(peer.bye, function(data){
Gun.obj.map(data, function(put, soul){
gun.get(soul).put(put);
});
});
peer.bye = [];
});
});
Gun.chain.bye = function(){
var gun = this, bye = gun.chain(), root = gun.back(-1), put = bye.put;
bye.put = function(data){
gun.back(function(at){
if(!at.get){ return }
var tmp = data;
(data = {})[at.get] = tmp;
});
root.on('out', {BYE: data});
}
return bye;
}

View File

@ -40,6 +40,7 @@ Gun.on('opt', function(ctx){
//setTimeout(function(){ //setTimeout(function(){
if(!lex || !(soul = lex[Gun._.soul])){ return } if(!lex || !(soul = lex[Gun._.soul])){ return }
//if(0 >= at.cap){ return } //if(0 >= at.cap){ return }
if(Gun.obj.is(soul)){ return match(at) }
var field = lex['.']; var field = lex['.'];
data = disk[soul] || u; data = disk[soul] || u;
if(data && field){ if(data && field){
@ -74,4 +75,14 @@ Gun.on('opt', function(ctx){
if(1 < tmp){ flush() } if(1 < tmp){ flush() }
}); });
} }
function match(at){
var rgx = at.get['#'], has = at.get['.'];
Gun.obj.map(disk, function(node, soul, put){
if(!Gun.text.match(soul, rgx)){ return }
if(has){ node = Gun.state.to(node, has) }
(put = {})[soul] = node;
ctx.on('in', {put: put, '@': at['#']});
});
}
}); });

View File

@ -9,7 +9,7 @@ Gun.chain.open = function(cb, opt, at){
opt.doc = opt.doc || {}; opt.doc = opt.doc || {};
opt.ids = opt.ids || {}; opt.ids = opt.ids || {};
return this.on(function(data, key){ return this.on(function(data, key){
delete (data = Gun.obj.copy(data))._; delete ((data = Gun.obj.copy(data))||{})._;
clearTimeout(opt.to); clearTimeout(opt.to);
opt.to = setTimeout(function(){ opt.to = setTimeout(function(){
cb(opt.doc); cb(opt.doc);

View File

@ -6,5 +6,6 @@
try{require('./ws');}catch(e){require('./wsp/server');} try{require('./ws');}catch(e){require('./wsp/server');}
require('./verify'); require('./verify');
require('./file'); require('./file');
require('./bye');
module.exports = Gun; module.exports = Gun;
}()); }());

View File

@ -12,4 +12,7 @@ Gun.chain.on = function(a,b,c){
return this.open(b,c); return this.open(b,c);
} }
return _on.call(this, a,b,c); return _on.call(this, a,b,c);
} }
Gun.chain.bye || require('gun/lib/bye');
Gun.chain.onDisconnect = Gun.chain.bye;

View File

@ -18,17 +18,20 @@ Gun.on('opt', function mount(ctx){
ws.web = new WebSocket.Server(ws); ws.web = new WebSocket.Server(ws);
ws.web.on('connection', function(peer){ ws.web.on('connection', function(wire){
peer.upgradeReq = peer.upgradeReq || {}; wire.upgradeReq = wire.upgradeReq || {};
peer.url = url.parse(peer.upgradeReq.url||'', true); wire.url = url.parse(wire.upgradeReq.url||'', true);
peer.id = peer.id || Gun.text.random(6); wire.id = wire.id || Gun.text.random(6);
opt.peers[peer.id] = {wire: peer}; var peer = opt.peers[wire.id] = {wire: wire};
peer.on('message', function(msg){ peer.wire = function(){ return peer };
ctx.on('hi', peer);
wire.on('message', function(msg){
//console.log("MESSAGE", msg); //console.log("MESSAGE", msg);
receive(msg, peer, ctx); // diff: peer is wire. receive(msg, wire, ctx); // diff: wire is wire.
}); });
peer.on('close', function(){ wire.on('close', function(){
Gun.obj.del(opt.peers, peer.id); ctx.on('bye', peer);
Gun.obj.del(opt.peers, wire.id);
}); });
}); });
@ -73,6 +76,7 @@ Gun.on('opt', function mount(ctx){
} }
return; return;
} }
msg.peer = wire.peer;
ctx.on('in', msg); ctx.on('in', msg);
} }
function open(peer, as){ function open(peer, as){

View File

@ -16,17 +16,20 @@ Gun.on('opt', function mount(ctx){
ws.web = new WebSocket.Server(ws); ws.web = new WebSocket.Server(ws);
ws.web.on('connection', function(peer){ ws.web.on('connection', function(wire){
peer.upgradeReq = peer.upgradeReq || {}; wire.upgradeReq = wire.upgradeReq || {};
peer.url = url.parse(peer.upgradeReq.url||'', true); wire.url = url.parse(wire.upgradeReq.url||'', true);
peer.id = peer.id || Gun.text.random(6); wire.id = wire.id || Gun.text.random(6);
opt.peers[peer.id] = {wire: peer}; var peer = opt.peers[wire.id] = {wire: wire};
peer.on('message', function(msg){ wire.peer = function(){ return peer };
ctx.on('hi', peer);
wire.on('message', function(msg){
//console.log("MESSAGE", msg); //console.log("MESSAGE", msg);
receive(msg, peer, ctx); // diff: peer is wire. receive(msg, wire, ctx); // diff: wire is wire.
}); });
peer.on('close', function(){ wire.on('close', function(){
Gun.obj.del(opt.peers, peer.id); ctx.on('bye', peer);
Gun.obj.del(opt.peers, wire.id);
}); });
}); });
@ -71,6 +74,7 @@ Gun.on('opt', function mount(ctx){
} }
return; return;
} }
msg.peer = wire.peer;
ctx.on('in', msg); ctx.on('in', msg);
} }
function open(peer, as){ function open(peer, as){

View File

@ -1,6 +1,6 @@
{ {
"name": "gun", "name": "gun",
"version": "0.8.3", "version": "0.8.4",
"description": "Graph engine", "description": "Graph engine",
"main": "index.js", "main": "index.js",
"browser": "gun.min.js", "browser": "gun.min.js",

View File

@ -105,7 +105,7 @@ describe("Sync all data from one server to another!", function(){
test.async(); test.async();
var raw = require('fs').readFileSync(env.i+'alldata'); var raw = require('fs').readFileSync(env.i+'alldata');
var json = JSON.parse(raw); var json = JSON.parse(raw);
var graph = json.graph; var graph = json;
console.log("Bob's graph on disk:", json); console.log("Bob's graph on disk:", json);
if(!graph.a1 || graph.a1.a !== 1){ if(!graph.a1 || graph.a1.a !== 1){