mostly working solution to orphan-child-unsubscribe-from-table problem, although causality questions, going to try another idea.

This commit is contained in:
Mark Nadal 2016-12-19 23:58:30 -08:00
commit 631eb6d023
10 changed files with 224 additions and 246 deletions

7
.gitignore vendored
View File

@ -1,4 +1,4 @@
node_modules/* node_modules
npm-debug.log npm-debug.log
gun.min.js gun.min.js
yarn.lock yarn.lock
@ -7,4 +7,7 @@ yarn.lock
.idea/ .idea/
*.bak *.bak
*.new *.new
*.DS_store *.DS_store
# Unbuilt files.
src/

11
e2e/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "gun-tests-e2e",
"version": "0.0.0",
"description": "End to end tests for gun",
"private": true,
"main": "distributed.js",
"devDependencies": {
"panic-server": "~>0.3.0",
"selenium-webdriver": "~>2.53.2"
}
}

141
gun.js
View File

@ -910,8 +910,7 @@
} }
at.once = true; at.once = true;
return gun; return gun;
}; }
function output(at){ function output(at){
var cat = this, gun = cat.gun, tmp; var cat = this, gun = cat.gun, tmp;
// TODO: BUG! Outgoing `get` to read from in memory!!! // TODO: BUG! Outgoing `get` to read from in memory!!!
@ -1112,7 +1111,6 @@
}); });
return; return;
} }
console.debug(103, 'out', cat.get);
at = obj_to(at, {get: {}}); at = obj_to(at, {get: {}});
} else { } else {
if(cat.ask){ return } if(cat.ask){ return }
@ -1120,14 +1118,12 @@
if(cat.get){ if(cat.get){
if(cat.soul){ if(cat.soul){
cat.ask = -1; cat.ask = -1;
console.debug(104, 'out', cat.get, cat.ask);
gun.on('out', { gun.on('out', {
get: {'#': cat.soul}, get: {'#': cat.soul},
'#': Gun.on.ask(Gun.HAM.synth, gun), '#': Gun.on.ask(Gun.HAM.synth, gun),
}); });
return; return;
} }
console.debug(102, 'any out', cat.get, cat.ask);
cat.back.on('out', { cat.back.on('out', {
get: obj_put({}, _field, cat.get), get: obj_put({}, _field, cat.get),
gun: gun gun: gun
@ -1142,20 +1138,27 @@
function input(at, ev){ function input(at, ev){
at = at._ || at; at = at._ || at;
var cat = this, gun = at.gun, coat = gun._, change = at.put, tmp; var cat = this, gun = at.gun, coat = gun._, change = at.put, tmp;
console.debug(119, 'in', cat.get, change, cat.next, cat.maps); console.debug(107, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(117, 'in', cat.get, change, cat.next, cat.maps); console.debug(106, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(116, 'in', cat.get, change, cat.next, cat.maps); console.debug(105, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(115, 'in', cat.get, change, cat.next, cat.maps); console.debug(102, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(109, 'in', cat.get, change, cat.next); console.debug(15, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(107, 'in', cat.get, change, cat.next, cat.maps); console.debug(9, 'in', cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(6, 'in', cat.get, change, cat.next, cat.maps); console.debug(8, 'in', cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(5, 'in', cat.get, change, cat.next, cat.maps); console.debug(6, 'in', cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(3, 'in', cat.get, change, cat.next, cat.maps); console.debug(4, 'in', cat.get, change, cat.next, cat.maps);
console.debug(22, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
console.debug(21, 'in', cat, cat.get, change, cat.next, cat.maps, cat.soul);
if(cat.maps){ if(cat.maps){
var proxy = obj_to(at, {get: cat.get, gun: cat.gun}); // TODO: BUG! Broken via!
console.debug(14, 'in', cat, cat.get, change, proxy.put);
obj_map(cat.maps, function(cat){ obj_map(cat.maps, function(cat){
cat.on('in', at); cat.on('in', proxy);
}) });
console.debug(18, 'in', cat, cat.get, change, proxy.put);
console.debug(24, "REMEMBER", cat.get, change, cat.next, cat.maps);
} }
console.debug(11, 'in', cat.get, change, cat.next, cat.maps, cat.soul);
if(value.call(cat, at, ev)){ if(value.call(cat, at, ev)){
return; return;
} }
@ -1168,43 +1171,45 @@
return; return;
} }
if(u === put){ if(u === put){
//not(cat, at); if(cat === coat){
console.debug(19, 'end');
not(cat, at);
} else {
console.debug(16, 'off');
Gun.obj.del(coat.maps, cat.id);
}
return true; return true;
} }
if(!(rel = Gun.val.rel.is(put))){ if(!(rel = Gun.val.rel.is(put))){
/*if(cat.proxy && cat.proxy.at !== at){ // TODO: CLEAN UP? Cleaner approach?
if(cat.proxy.rel){
cat.put = coat.put;
}
cat.on('in', cat.proxy.at = obj_to(at, {get: cat.get, gun: coat.gun, via: at})); // TODO: BUG!!! Re-using at as via will create the wrong at via path!
return true;
}*/
if(cat.proxy){ if(cat.proxy){
cat.put = coat.put; cat.put = coat.put;
} }
console.debug(7, '!', cat);
if(Gun.val.is(put)){ if(Gun.val.is(put)){
//not(coat, at); if(cat === coat){
not(cat, at); not(cat, at);
} else {
(coat.maps || (coat.maps = {}))[cat.id || (cat.id = Math.random())] = cat;
}
return true; return true;
} }
if(!cat.proxy && cat !== coat){
console.debug(108, '????', cat !== coat, u !== coat.put);
if(u !== coat.put){ // TODO: BUG!? Is this a safe condition?
console.debug(23, 'values', cat.get, put, cat !== coat, u !== coat.put);
return true;
}
}
console.debug(25, 'values', cat.get, put, !cat.proxy, cat !== coat);
return; return;
}; };
// TODO: MARK! COME BACK HERE! What if we INVERT the model? Rather than map subbing to all refs that come it way, the refs know of the map? // TODO: MARK! COME BACK HERE! What if we INVERT the model? Rather than map subbing to all refs that come it way, the refs know of the map?
if(coat !== cat){ if(coat !== cat){
console.debug(110, 'values', coat !== cat, coat.ask, cat.ask);
(coat.maps || (coat.maps = {}))[cat.id || (cat.id = Math.random())] = cat; (coat.maps || (coat.maps = {}))[cat.id || (cat.id = Math.random())] = cat;
/* if(coat.proxy){
if(!(cat.proxy || (cat.proxy = {}))[coat.id = coat.id || Gun.text.random(6)]){ console.debug(10, 'values', cat.get, rel, coat.proxy);
cat.proxy[coat.id] = {ref: coat.gun, sub: gun._.on('in', input, cat)}; //return true;
} else {
solve(coat, cat);
ask(cat, rel);
} }
*/
//return true;
} }
console.debug(7, 'values', cat.rel, coat.proxy);
if(coat.proxy){ if(coat.proxy){
if(rel === coat.proxy.rel){ if(rel === coat.proxy.rel){
ev.stun(); ev.stun();
@ -1213,53 +1218,34 @@
at.put = coat.put = tmp.put; at.put = coat.put = tmp.put;
return true; return true;
} }
//Gun.obj.del(cat.on('in').map, coat.proxy.ref._.id);
tmp = coat.proxy; tmp = coat.proxy;
console.debug(12, 'values', cat.get, rel, coat.proxy);
not(coat, at); not(coat, at);
//not(cat, at);
} }
tmp = coat.proxy = {rel: rel, ref: coat.root.get(rel), was: tmp}; tmp = coat.proxy = {rel: rel, ref: coat.root.get(rel), was: tmp};
console.debug(20, 'values', cat.get, rel, coat.proxy);
tmp.sub = tmp.ref._.on('in', input, coat); tmp.sub = tmp.ref._.on('in', input, coat);
console.debug(21, 'values', cat.get, rel, coat.proxy);
tmp = coat.put; tmp = coat.put;
console.debug(111, 'values', rel, coat.proxy); console.debug(103, 'values', coat.proxy);
ask(cat, rel); ask(cat, rel);
if(tmp !== coat.put){ ev.stun() } if(tmp !== coat.put){ ev.stun() }
/*if(cat.maps){
obj_map(cat.maps, function(cat){
cat.on('in', at);
})
}*/
return true; return true;
} }
function solve(coat, cat){
if(!coat.proxy){ return }
var was = coat.proxy.was;
if(!was){ return }
was = was.ref._;
obj_map(cat.next, function(sub, key){
var a = was.next[key];
if(!a){ return }
var id = a._.id, proxy = sub._.proxy;
if(!(sub = sub._.proxy)){ return }
if(!sub[id]){ return }
if(sub[id].sub){ sub[id].sub.off() }
Gun.obj.del(sub, id);
})
}
function map(data, key){ // Map over only the changes on every update. function map(data, key){ // Map over only the changes on every update.
if(node_ === key){ return } if(node_ === key){ return }
var cat = this.cat, next = cat.next || {}, via = this.at, gun, chain, at, tmp; var cat = this.cat, next = cat.next || {}, via = this.at, gun, chain, at, tmp;
console.debug(26, 'map -->>', key, data, next);
if(!(gun = next[key])){ return } if(!(gun = next[key])){ return }
at = gun._; at = gun._;
if(cat.soul){ if(at.field){
at.put = data; at.put = data;
at.field = key; //at.field = key;
chain = gun; chain = gun;
} else { } else {
chain = via.gun.get(key, null, {path: true}); // TODO: path won't be needed with 0.5 chain = via.gun.get(key, null, {path: true}); // TODO: path won't be needed with 0.5
} }
console.debug(118, '-->>', key, data, gun, chain); console.debug(7, 'map -->>', key, data, chain);
console.debug(4, '-->>', key, data);
gun.on('in', { gun.on('in', {
put: data, put: data,
get: key, get: key,
@ -1283,7 +1269,7 @@
if(obj_has(at,'put')){ if(obj_has(at,'put')){
at.put = u; at.put = u;
} }
console.debug(8, 'woot!', at); console.debug(13, 'not', key, u);
gun.on('in', { gun.on('in', {
get: key, get: key,
put: u, put: u,
@ -1305,8 +1291,8 @@
return; return;
} }
if(0 === cat.ask){ return } if(0 === cat.ask){ return }
console.debug(104, 'ask', soul, cat.next);
obj_map(cat.next, function(gun, key){ obj_map(cat.next, function(gun, key){
console.debug(112, 'ask', soul, key);
gun.on('out', { gun.on('out', {
get: {'#': soul, '.': key}, get: {'#': soul, '.': key},
gun: gun, gun: gun,
@ -1383,7 +1369,7 @@
var opt = opt || {}, gun = opt.gun = this; var opt = opt || {}, gun = opt.gun = this;
if(opt.change){ opt.change = 1 } if(opt.change){ opt.change = 1 }
opt.any = cb; opt.any = cb;
console.debug(101, 'any!'); console.debug(100, 'lazy eval');
return gun.on('in', any, opt).on('out', {get: opt}); return gun.on('in', any, opt).on('out', {get: opt});
} }
function any(at, ev){ var opt = this; function any(at, ev){ var opt = this;
@ -1411,6 +1397,7 @@
if(opt.as){ if(opt.as){
opt.any.call(opt.as, at, ev); opt.any.call(opt.as, at, ev);
} else { } else {
console.debug(17, 'any!', at, data);
opt.any.call(gun, at.err, data, at.get, at, ev); opt.any.call(gun, at.err, data, at.get, at, ev);
} }
} }
@ -1476,7 +1463,7 @@
function batch(){ var as = this; function batch(){ var as = this;
if(!as.graph || obj_map(as.stun, no)){ return } if(!as.graph || obj_map(as.stun, no)){ return }
console.debug(1, 'PUT!', as.env.graph); console.debug(1, 'OUT!', as.env.graph);
(as.res||iife)(function(){ (as.res||iife)(function(){
as.ref.on('out', { as.ref.on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, gun: as.ref, put: as.out = as.env.graph, opt: as.opt,
@ -1668,8 +1655,9 @@
} }
Gun.HAM.union = function(vertex, node, opt){ Gun.HAM.union = function(vertex, node, opt){
if(!node || !vertex || !node._ || !vertex._){ return } if(!node || !vertex || !node._ || !vertex._){ return }
opt = num_is(opt)? {machine: opt} : {machine: (+new Date)}; opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
opt.union = Gun.obj.copy(vertex); // TODO: PERF! This will slow things down! opt.union = Gun.obj.copy(vertex); // TODO: PERF! This will slow things down!
// TODO: PERF! Biggest slowdown (after localStorage) is the above line. Fix! Fix!
opt.vertex = vertex; opt.vertex = vertex;
opt.node = node; opt.node = node;
obj_map(node._, meta, opt.union); obj_map(node._, meta, opt.union);
@ -1679,7 +1667,7 @@
return opt.union; return opt.union;
} }
Gun.HAM.delta = function(vertex, node, opt){ Gun.HAM.delta = function(vertex, node, opt){
opt = num_is(opt)? {machine: opt} : {machine: (+new Date)}; opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
if(!vertex){ return Gun.obj.copy(node) } if(!vertex){ return Gun.obj.copy(node) }
opt.soul = Gun.node.soul(opt.vertex = vertex); opt.soul = Gun.node.soul(opt.vertex = vertex);
if(!opt.soul){ return } if(!opt.soul){ return }
@ -1694,6 +1682,7 @@
var HAM = Gun.HAM(opt.machine, is, cs, value, vertex[field]); var HAM = Gun.HAM(opt.machine, is, cs, value, vertex[field]);
// TODO: BUG!!!! WHAT ABOUT DEFERRED!??? // TODO: BUG!!!! WHAT ABOUT DEFERRED!???
@ -1712,9 +1701,9 @@
}, root); }, root);
obj_map(put, function(node, soul){ obj_map(put, function(node, soul){
var root = this, next = root.next || (root.next = {}), gun = next[soul] || (next[soul] = root.gun.get(soul)); var root = this, next = root.next || (root.next = {}), gun = next[soul] || (next[soul] = root.gun.get(soul));
gun._.put = root.graph[soul]; gun._.put = root.graph[soul]; // TODO: BUG! Clone!
console.debug(114, 'ack', soul, node); console.debug(5, 'IN ->', soul, node);
console.debug(106, 'ack', soul, node); console.debug(3, 'IN ->', soul, node);
gun.on('in', { gun.on('in', {
put: node, put: node,
get: soul, get: soul,
@ -2109,7 +2098,6 @@
console.log("map!"); console.log("map!");
chain.on(cb); chain.on(cb);
} }
console.debug(100, 'to map');
return chain; return chain;
} }
function map(at,ev){ function map(at,ev){
@ -2121,7 +2109,7 @@
var gun = this.gun, cat = this.cat; var gun = this.gun, cat = this.cat;
//console.log("-- EACH -->", f, v); //console.log("-- EACH -->", f, v);
var id = this.id;if(cat.set[id+f]){ return } cat.set[id+f] = 1; var id = this.id;if(cat.set[id+f]){ return } cat.set[id+f] = 1;
console.debug(108, "-- EACH -->", f, v); console.debug(101, "-- EACH -->", f, v);
cat.on('in', {gun: gun.get(f, null, {path: true}), get: f, put: v, via: this.at}); cat.on('in', {gun: gun.get(f, null, {path: true}), get: f, put: v, via: this.at});
} }
var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._; var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._;
@ -2161,6 +2149,7 @@
(opt = {}).prefix = (at.opt || opt).prefix || at.gun.Back('opt.prefix') || 'gun/'; (opt = {}).prefix = (at.opt || opt).prefix || at.gun.Back('opt.prefix') || 'gun/';
Gun.graph.is(at.put, function(node, soul){ Gun.graph.is(at.put, function(node, soul){
//try{store.setItem(opt.prefix + soul, Gun.text.ify(node)); //try{store.setItem(opt.prefix + soul, Gun.text.ify(node));
// TODO: BUG! PERF! Biggest slowdown is because of localStorage stringifying larger and larger nodes!
try{store.setItem(opt.prefix + soul, Gun.text.ify(root._.graph[soul]||node)); try{store.setItem(opt.prefix + soul, Gun.text.ify(root._.graph[soul]||node));
}catch(e){ err = e || "localStorage failure" } }catch(e){ err = e || "localStorage failure" }
}); });
@ -2182,8 +2171,6 @@
return; return;
} }
if(Gun.obj.has(lex, '.')){var tmp = data[lex['.']];data = {_: data._};if(u !== tmp){data[lex['.']] = tmp}} if(Gun.obj.has(lex, '.')){var tmp = data[lex['.']];data = {_: data._};if(u !== tmp){data[lex['.']] = tmp}}
console.debug(113, 'lS get', data);
console.debug(105, 'lS get', data);
//console.log('@@@@@@@@@@@@local get', data, at); //console.log('@@@@@@@@@@@@local get', data, at);
gun.Back(-1).on('in', {'@': at['#'], put: Gun.graph.node(data)}); gun.Back(-1).on('in', {'@': at['#'], put: Gun.graph.node(data)});
//},100); //},100);

View File

@ -7,6 +7,7 @@
"start": "node examples/http.js 8080", "start": "node examples/http.js 8080",
"prepublish": "npm run unbuild && uglifyjs gun.js -o gun.min.js -c -m", "prepublish": "npm run unbuild && uglifyjs gun.js -o gun.min.js -c -m",
"test": "mocha", "test": "mocha",
"e2e": "mocha e2e/distributed.js",
"docker": "hooks/build", "docker": "hooks/build",
"unbuild": "node lib/unbuild.js" "unbuild": "node lib/unbuild.js"
}, },
@ -53,8 +54,6 @@
"devDependencies": { "devDependencies": {
"express": "~>4.13.4", "express": "~>4.13.4",
"mocha": "~>1.9.0", "mocha": "~>1.9.0",
"panic-server": "~>0.3.0", "uglify-js": "~>2.2.0"
"selenium-webdriver": "~>2.53.2",
"uglify-js": "^2.2.0"
} }
} }

File diff suppressed because it is too large Load Diff