mirror of
https://github.com/amark/gun.git
synced 2025-06-05 21:56:51 +00:00
goodbye experiment
This commit is contained in:
parent
3ec7d81f85
commit
b3e59d243e
3
gun.js
3
gun.js
@ -886,6 +886,7 @@
|
||||
if(!at.gun){
|
||||
at.gun = gun;
|
||||
}
|
||||
this.to.next(at);
|
||||
if(get = at.get){
|
||||
if(tmp = get[_soul]){
|
||||
tmp = (root.get(tmp)._);
|
||||
@ -966,7 +967,7 @@
|
||||
proxy.at.on('in', proxy.at);
|
||||
});
|
||||
}
|
||||
if(cat.ack){
|
||||
if(cat.ack >= 0){
|
||||
if(!obj_has(cat, 'put')){ // u !== cat.put instead?
|
||||
//if(u !== cat.put){
|
||||
return;
|
||||
|
38
lib/bye.js
Normal file
38
lib/bye.js
Normal 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;
|
||||
}
|
11
lib/file.js
11
lib/file.js
@ -40,6 +40,7 @@ Gun.on('opt', function(ctx){
|
||||
//setTimeout(function(){
|
||||
if(!lex || !(soul = lex[Gun._.soul])){ return }
|
||||
//if(0 >= at.cap){ return }
|
||||
if(Gun.obj.is(soul)){ return match(at) }
|
||||
var field = lex['.'];
|
||||
data = disk[soul] || u;
|
||||
if(data && field){
|
||||
@ -74,4 +75,14 @@ Gun.on('opt', function(ctx){
|
||||
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['#']});
|
||||
});
|
||||
}
|
||||
});
|
@ -9,7 +9,7 @@ Gun.chain.open = function(cb, opt, at){
|
||||
opt.doc = opt.doc || {};
|
||||
opt.ids = opt.ids || {};
|
||||
return this.on(function(data, key){
|
||||
delete (data = Gun.obj.copy(data))._;
|
||||
delete ((data = Gun.obj.copy(data))||{})._;
|
||||
clearTimeout(opt.to);
|
||||
opt.to = setTimeout(function(){
|
||||
cb(opt.doc);
|
||||
|
@ -6,5 +6,6 @@
|
||||
try{require('./ws');}catch(e){require('./wsp/server');}
|
||||
require('./verify');
|
||||
require('./file');
|
||||
require('./bye');
|
||||
module.exports = Gun;
|
||||
}());
|
||||
|
@ -13,3 +13,6 @@ Gun.chain.on = function(a,b,c){
|
||||
}
|
||||
return _on.call(this, a,b,c);
|
||||
}
|
||||
|
||||
Gun.chain.bye || require('gun/lib/bye');
|
||||
Gun.chain.onDisconnect = Gun.chain.bye;
|
22
lib/uws.js
22
lib/uws.js
@ -18,17 +18,20 @@ Gun.on('opt', function mount(ctx){
|
||||
|
||||
ws.web = new WebSocket.Server(ws);
|
||||
|
||||
ws.web.on('connection', function(peer){
|
||||
peer.upgradeReq = peer.upgradeReq || {};
|
||||
peer.url = url.parse(peer.upgradeReq.url||'', true);
|
||||
peer.id = peer.id || Gun.text.random(6);
|
||||
opt.peers[peer.id] = {wire: peer};
|
||||
peer.on('message', function(msg){
|
||||
ws.web.on('connection', function(wire){
|
||||
wire.upgradeReq = wire.upgradeReq || {};
|
||||
wire.url = url.parse(wire.upgradeReq.url||'', true);
|
||||
wire.id = wire.id || Gun.text.random(6);
|
||||
var peer = opt.peers[wire.id] = {wire: wire};
|
||||
peer.wire = function(){ return peer };
|
||||
ctx.on('hi', peer);
|
||||
wire.on('message', function(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(){
|
||||
Gun.obj.del(opt.peers, peer.id);
|
||||
wire.on('close', function(){
|
||||
ctx.on('bye', peer);
|
||||
Gun.obj.del(opt.peers, wire.id);
|
||||
});
|
||||
});
|
||||
|
||||
@ -73,6 +76,7 @@ Gun.on('opt', function mount(ctx){
|
||||
}
|
||||
return;
|
||||
}
|
||||
msg.peer = wire.peer;
|
||||
ctx.on('in', msg);
|
||||
}
|
||||
function open(peer, as){
|
||||
|
22
lib/ws.js
22
lib/ws.js
@ -16,17 +16,20 @@ Gun.on('opt', function mount(ctx){
|
||||
|
||||
ws.web = new WebSocket.Server(ws);
|
||||
|
||||
ws.web.on('connection', function(peer){
|
||||
peer.upgradeReq = peer.upgradeReq || {};
|
||||
peer.url = url.parse(peer.upgradeReq.url||'', true);
|
||||
peer.id = peer.id || Gun.text.random(6);
|
||||
opt.peers[peer.id] = {wire: peer};
|
||||
peer.on('message', function(msg){
|
||||
ws.web.on('connection', function(wire){
|
||||
wire.upgradeReq = wire.upgradeReq || {};
|
||||
wire.url = url.parse(wire.upgradeReq.url||'', true);
|
||||
wire.id = wire.id || Gun.text.random(6);
|
||||
var peer = opt.peers[wire.id] = {wire: wire};
|
||||
wire.peer = function(){ return peer };
|
||||
ctx.on('hi', peer);
|
||||
wire.on('message', function(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(){
|
||||
Gun.obj.del(opt.peers, peer.id);
|
||||
wire.on('close', function(){
|
||||
ctx.on('bye', peer);
|
||||
Gun.obj.del(opt.peers, wire.id);
|
||||
});
|
||||
});
|
||||
|
||||
@ -71,6 +74,7 @@ Gun.on('opt', function mount(ctx){
|
||||
}
|
||||
return;
|
||||
}
|
||||
msg.peer = wire.peer;
|
||||
ctx.on('in', msg);
|
||||
}
|
||||
function open(peer, as){
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gun",
|
||||
"version": "0.8.3",
|
||||
"version": "0.8.4",
|
||||
"description": "Graph engine",
|
||||
"main": "index.js",
|
||||
"browser": "gun.min.js",
|
||||
|
@ -105,7 +105,7 @@ describe("Sync all data from one server to another!", function(){
|
||||
test.async();
|
||||
var raw = require('fs').readFileSync(env.i+'alldata');
|
||||
var json = JSON.parse(raw);
|
||||
var graph = json.graph;
|
||||
var graph = json;
|
||||
console.log("Bob's graph on disk:", json);
|
||||
|
||||
if(!graph.a1 || graph.a1.a !== 1){
|
||||
|
Loading…
x
Reference in New Issue
Block a user