This commit is contained in:
Mark Nadal 2017-01-27 04:58:16 -08:00
parent 37ac5e28e4
commit 77d71a0c94
8 changed files with 53 additions and 57 deletions

View File

@ -34,10 +34,12 @@
document.onkeyup = function(e){ document.onkeyup = function(e){
if(!e || !e.target){ return } // ignore if no element! if(!e || !e.target){ return } // ignore if no element!
if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable! if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable!
console.log("save to", e.target.previousElementSibling.innerHTML, ":", e.target.innerHTML);
ref.path(clean(e.target.previousElementSibling.innerHTML)) // grab the label, which is in the previous element. ref.path(clean(e.target.previousElementSibling.innerHTML)) // grab the label, which is in the previous element.
.put( clean(e.target.innerHTML) ); // insert the value of the text in the current element. .put( clean(e.target.innerHTML) ); // insert the value of the text in the current element.
} }
ref.on(function(data){ ref.on(function(json){
var data = Gun.obj.copy(json); // make a snapshot!
delete data._; // skip meta data! delete data._; // skip meta data!
for(var field in data){ for(var field in data){
var val = String(data[field]), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe. var val = String(data[field]), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe.

View File

@ -21,7 +21,7 @@
todo.on(function(list){ // subscribe and listen to all updates on the todo. todo.on(function(list){ // subscribe and listen to all updates on the todo.
var html = ''; // old school HTML strings! You should probably use a real template system. var html = ''; // old school HTML strings! You should probably use a real template system.
for(field in list) { // iterate over the list to generate the HTML. for(field in list) { // iterate over the list to generate the HTML.
if(!list[field] || field == Gun._.meta) continue; // ignore nulled out values and metadata. if(!list[field] || field == '_') continue; // ignore nulled out values and metadata.
html += '<li>' html += '<li>'
+ clean(list[field]) + clean(list[field])
+ '<button style="float:right;" onclick=todone("'+field+'")>X</button>' + '<button style="float:right;" onclick=todone("'+field+'")>X</button>'

73
gun.js
View File

@ -771,7 +771,7 @@
Dup.prototype.gc = function(){ Dup.prototype.gc = function(){
var de = this, now = Type.time.is(), oldest = now, maxAge = 5 * 60 * 1000; var de = this, now = Type.time.is(), oldest = now, maxAge = 5 * 60 * 1000;
// TODO: Gun.scheduler already does this? Reuse that. // TODO: Gun.scheduler already does this? Reuse that.
Gun.obj.map(de.cache, function(time, id){ Type.obj.map(de.cache, function(time, id){
oldest = Math.min(now, time); oldest = Math.min(now, time);
if ((now - time) < maxAge){ return } if ((now - time) < maxAge){ return }
Type.obj.del(de.cache, id); Type.obj.del(de.cache, id);
@ -824,10 +824,10 @@
;(function(){ ;(function(){
Gun.create = function(at){ Gun.create = function(at){
at.on = at.on || Gun.on; at.on = at.on || Gun.on;
var gun = at.gun.opt(at.opt); at.root = at.root || at.gun;
at.root = at.root || gun;
at.graph = at.graph || {}; at.graph = at.graph || {};
at.dup = at.dup || new Gun.dup; at.dup = at.dup || new Gun.dup;
var gun = at.gun.opt(at.opt);
if(!at.once){ if(!at.once){
at.on('in', input, at); at.on('in', input, at);
at.on('out', output, at); at.on('out', output, at);
@ -924,8 +924,8 @@
tmp = obj_map(tmp, function(url, i, map){ tmp = obj_map(tmp, function(url, i, map){
map(url, {}); map(url, {});
}); });
at.opt.peers = obj_to(tmp, at.opt.peers || {});
} }
at.opt.peers = obj_to(tmp, at.opt.peers || {});
obj_to(opt, at.opt); // copies options on to `at.opt` only if not already taken. obj_to(opt, at.opt); // copies options on to `at.opt` only if not already taken.
Gun.on('opt', at); Gun.on('opt', at);
return gun; return gun;
@ -1105,7 +1105,7 @@
} }
if(u === change){ if(u === change){
ev.to.next(at); ev.to.next(at);
echo(cat, at); echo(cat, at, ev);
if(cat.field || cat.soul){ if(cat.field || cat.soul){
not(cat, at); not(cat, at);
} else { } else {
@ -1129,7 +1129,7 @@
//if(u === coat.put){ return } // Not necessary but improves performance. If we have it but coat does not, that means we got things out of order and coat will get it. Once coat gets it, it will tell us again. //if(u === coat.put){ return } // Not necessary but improves performance. If we have it but coat does not, that means we got things out of order and coat will get it. Once coat gets it, it will tell us again.
} }
ev.to.next(at); ev.to.next(at);
echo(cat, at); echo(cat, at, ev);
return; return;
} }
if(cat.field){ if(cat.field){
@ -1145,16 +1145,17 @@
} }
} }
ev.to.next(at); ev.to.next(at);
echo(cat, at); echo(cat, at, ev);
obj_map(change, map, {at: at, cat: cat}); obj_map(change, map, {at: at, cat: cat});
return; return;
} }
if(relate(cat, at, ev)){ return } // if return not necessary but improves performance. if(relate(cat, at, ev)){ return } // if return not necessary but improves performance.
echo(cat, at); echo(cat, at, ev);
} }
Gun.chain.chain.input = input; Gun.chain.chain.input = input;
function echo(cat, at){ function echo(cat, at, ev){
if(!cat.echo){ return } if(!cat.echo){ return }
at.event = ev;
obj_map(cat.echo, function(cat){ // TODO: PERF! Cache obj_map(cat.echo, function(cat){ // TODO: PERF! Cache
cat.on('in', at); cat.on('in', at);
}); });
@ -1331,7 +1332,7 @@
if(u === cat.put && u !== at.put){ // TODO: Use state instead? if(u === cat.put && u !== at.put){ // TODO: Use state instead?
return ev.to.next(at); // For a field that has a value, but nothing on its context, then that means we have received the update out of order and we will receive it from the context, so we can deduplicate this one. return ev.to.next(at); // For a field that has a value, but nothing on its context, then that means we have received the update out of order and we will receive it from the context, so we can deduplicate this one.
}*/ }*/
as.use(at, ev); as.use(at, at.event || ev);
ev.to.next(at); ev.to.next(at);
} }
var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to; var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to;
@ -1426,10 +1427,10 @@
}, {as: as, at: at}); }, {as: as, at: at});
} }
function soul(at, ev){ var as = this.as, as = as.as, cat = as.at; function soul(at, ev){ var as = this.as, cat = as.at; as = as.as;
ev.stun(); // TODO: BUG!? //ev.stun(); // TODO: BUG!?
ev.off(); ev.off();
cat.soul(Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || ((as.opt||{}).uuid || as.gun.Back('opt.uuid') || Gun.text.random)()); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? cat.soul(Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || ((as.opt||{}).uuid || as.gun.back('opt.uuid') || Gun.text.random)()); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
as.stun[cat.path] = false; as.stun[cat.path] = false;
as.batch(); as.batch();
} }
@ -1488,30 +1489,10 @@
ev.off(); ev.off();
if(!as.not && !(as.soul = Gun.node.soul(data))){ if(!as.not && !(as.soul = Gun.node.soul(data))){
if(as.path && obj_is(as.data)){ // Apparently necessary if(as.path && obj_is(as.data)){ // Apparently necessary
as.soul = (opt.uuid || as.gun.Back('opt.uuid') || Gun.text.random)(); as.soul = (opt.uuid || as.gun.back('opt.uuid') || Gun.text.random)();
} else { } else {
/* //as.data = obj_put({}, as.gun._.get, as.data);
TODO: CLEAN UP! Is any of this necessary? as.soul = at.soul;
*/
if(!at.get){
console.log("Please report this as an issue! Put.any.no.soul");
return;
}
(as.next = as.next || Gun.on.next(as.ref))(function(next){
// TODO: BUG! Maybe don't go back up 1 because .put already does that if ref isn't already specified?
(root = as.ref.Back(1)).put(data = obj_put({}, at.get, as.data), opt.any, opt, {
opt: opt,
ref: root
});
//Gun.obj.to(opt, {
// ref: null,
// gun: null,
// next: null,
// data: data
//}));
//next(); // TODO: BUG! Needed? Not needed?
});
return;
} }
} }
if(as.ref !== as.gun && !as.not){ if(as.ref !== as.gun && !as.not){
@ -1708,7 +1689,7 @@
opt = opt || {}; opt = opt || {};
opt.key = index; opt.key = index;
opt.any = cb || function(){}; opt.any = cb || function(){};
opt.ref = gun.Back(-1).get(opt.key); opt.ref = gun.back(-1).get(opt.key);
opt.gun = opt.gun || gun; opt.gun = opt.gun || gun;
gun.on(key, {as: opt}); gun.on(key, {as: opt});
if(!opt.data){ if(!opt.data){
@ -1738,7 +1719,7 @@
keyed._ = '##'; keyed._ = '##';
Gun.on('next', function(at){ Gun.on('next', function(at){
var gun = at.gun; var gun = at.gun;
if(gun.Back(-1) !== at.back){ return } if(gun.back(-1) !== at.back){ return }
gun.on('in', pseudo, gun._); gun.on('in', pseudo, gun._);
gun.on('out', normalize, gun._); gun.on('out', normalize, gun._);
}); });
@ -1750,7 +1731,7 @@
return; return;
} }
if(at.opt && at.opt.key){ return } if(at.opt && at.opt.key){ return }
var put = at.put, graph = cat.gun.Back(-1)._.graph; var put = at.put, graph = cat.gun.back(-1)._.graph;
Gun.graph.is(put, function(node, soul){ Gun.graph.is(put, function(node, soul){
if(!Gun.node.is(graph['#'+soul+'#'], function each(rel,id){ if(!Gun.node.is(graph['#'+soul+'#'], function each(rel,id){
if(id !== Gun.val.rel.is(rel)){ return } if(id !== Gun.val.rel.is(rel)){ return }
@ -1822,7 +1803,7 @@
if(!at.put){ return } if(!at.put){ return }
var rel = Gun.val.rel.is(at.put[keyed._]); var rel = Gun.val.rel.is(at.put[keyed._]);
if(!rel){ return } if(!rel){ return }
var soul = Gun.node.soul(at.put), resume = ev.stun(resume), root = cat.gun.Back(-1), seen = cat.seen = {}; var soul = Gun.node.soul(at.put), resume = ev.stun(resume), root = cat.gun.back(-1), seen = cat.seen = {};
cat.pseudo = cat.put = Gun.state.ify(Gun.node.ify({}, soul)); cat.pseudo = cat.put = Gun.state.ify(Gun.node.ify({}, soul));
root.get(rel).on(each, {change: true}); root.get(rel).on(each, {change: true});
function each(change){ function each(change){
@ -1950,7 +1931,7 @@
} }
function val(at, ev, to){ function val(at, ev, to){
var opt = this.as, cat = opt.cat, data = cat.put || at.put; var opt = this.as, gun = at.gun, cat = gun._, data = cat.put || at.put;
if(u === data){ if(u === data){
return; return;
} }
@ -1979,7 +1960,7 @@
}); });
} }
} }
if((tmp = gun.Back(-1)) === back){ if((tmp = gun.back(-1)) === back){
obj_del(tmp.graph, at.get); obj_del(tmp.graph, at.get);
} }
if(at.ons && (tmp = at.ons['@$'])){ if(at.ons && (tmp = at.ons['@$'])){
@ -2070,7 +2051,7 @@
var Gun = require('./core'); var Gun = require('./core');
Gun.chain.init = function(){ // TODO: DEPRECATE? Gun.chain.init = function(){ // TODO: DEPRECATE?
(this._.opt = this._.opt || {}).init = true; (this._.opt = this._.opt || {}).init = true;
return this.Back(-1).put(Gun.node.ify({}, this._.get), null, this._.get); return this.back(-1).put(Gun.node.ify({}, this._.get), null, this._.get);
} }
})(require, './init'); })(require, './init');
@ -2097,6 +2078,7 @@
var store = root.localStorage || {setItem: noop, removeItem: noop, getItem: noop}; var store = root.localStorage || {setItem: noop, removeItem: noop, getItem: noop};
function put(at){ var err, id, opt, root = at.gun._.root; function put(at){ var err, id, opt, root = at.gun._.root;
this.to.next(at);
(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));
@ -2110,6 +2092,7 @@
} }
} }
function get(at){ function get(at){
this.to.next(at);
var gun = at.gun, lex = at.get, soul, data, opt, u; var gun = at.gun, lex = at.get, soul, data, opt, u;
//setTimeout(function(){ //setTimeout(function(){
(opt = at.opt || {}).prefix = opt.prefix || at.gun.back('opt.prefix') || 'gun/'; (opt = at.opt || {}).prefix = opt.prefix || at.gun.back('opt.prefix') || 'gun/';
@ -2348,6 +2331,7 @@
// Define client instances as gun needs them. // Define client instances as gun needs them.
// Sockets will not be opened until absolutely necessary. // Sockets will not be opened until absolutely necessary.
Gun.on('opt', function (ctx) { Gun.on('opt', function (ctx) {
this.to.next(ctx);
var gun = ctx.gun; var gun = ctx.gun;
var peers = gun.back('opt.peers') || {}; var peers = gun.back('opt.peers') || {};
@ -2360,7 +2344,7 @@
return; return;
} }
var client = new Client(url, options.backoff, gun.Back('opt.wsc') || {protocols:null}); var client = new Client(url, options.backoff, gun.back('opt.wsc') || {protocols:null});
// Add it to the pool. // Add it to the pool.
Client.pool[url] = client; Client.pool[url] = client;
@ -2393,6 +2377,7 @@
// Broadcast the messages. // Broadcast the messages.
Gun.on('out', function (ctx) { Gun.on('out', function (ctx) {
this.to.next(ctx);
var gun = ctx.gun; var gun = ctx.gun;
var peers = gun.back('opt.peers') || {}; var peers = gun.back('opt.peers') || {};
// Validate. // Validate.

View File

@ -7,6 +7,7 @@ var Gun = require('../gun'),
file = {}; file = {};
Gun.on('put', function(at){ Gun.on('put', function(at){
this.to.next(at);
if(!file.use){ return } if(!file.use){ return }
var graph = at.put, Graph = file.gun._.graph, opt = at.opt || {}; var graph = at.put, Graph = file.gun._.graph, opt = at.opt || {};
Gun.obj.map(graph, function(node, soul){ Gun.obj.map(graph, function(node, soul){
@ -24,6 +25,7 @@ Gun.on('put', function(at){
}); });
Gun.on('get', function(at){ Gun.on('get', function(at){
this.to.next(at);
if(!file.use){ return } if(!file.use){ return }
var soul = at.get['#']; var soul = at.get['#'];
if(!soul){ return } if(!soul){ return }
@ -52,6 +54,7 @@ function field(node, field){
} }
Gun.on('opt', function(at){ Gun.on('opt', function(at){
this.to.next(at);
var gun = at.gun, opt = at.opt; var gun = at.gun, opt = at.opt;
if ((opt.file === false) || (opt.s3 && opt.s3.key)) { if ((opt.file === false) || (opt.s3 && opt.s3.key)) {
return; // don't use this plugin if S3 is being used. return; // don't use this plugin if S3 is being used.

View File

@ -8,6 +8,6 @@
'I\'m mark@gunDB.io, message me for help or with hatemail. ' + 'I\'m mark@gunDB.io, message me for help or with hatemail. ' +
'I want to hear from you! <3' 'I want to hear from you! <3'
); );
Gun.log('TODO: MARK! UPDATE S3 DRIVER BEFORE PUBLISHING!'); Gun.log('NOTE: S3 driver not updated to 0.5 yet!');
module.exports = Gun; module.exports = Gun;
}()); }());

View File

@ -46,9 +46,10 @@ function getSocketSubset (peers) {
} }
Gun.on('out', function (ctx) { Gun.on('out', function (ctx) {
this.to.next(ctx);
var gun = ctx.gun; var gun = ctx.gun;
var opt = ctx.opt || {}; var opt = ctx.opt || {};
var peers = opt.peers || gun.Back('opt.peers'); var peers = opt.peers || gun.back('opt.peers');
if (!peers) { if (!peers) {
return; return;
@ -66,15 +67,15 @@ Gun.on('out', function (ctx) {
// adding them to the global pool. // adding them to the global pool.
Gun.on('opt', function (context) { Gun.on('opt', function (context) {
var gun = context.gun; var gun = context.gun;
var root = gun.Back(Infinity); var root = gun.back(Infinity);
var peers = gun.Back('opt.peers') || {}; var peers = gun.back('opt.peers') || {};
Gun.obj.map(peers, function (options, url) { Gun.obj.map(peers, function (options, url) {
if (sockets[url]) { if (sockets[url]) {
return; return;
} }
if (!options.wsc) options.wsc = gun.Back('opt.wsc') || { protocols:null }; if (!options.wsc){ options.wsc = gun.back('opt.wsc') || { protocols:null }; }
var socket = Socket(url, options); var socket = Socket(url, options);
sockets.add(url, socket); sockets.add(url, socket);
@ -96,4 +97,5 @@ Gun.on('opt', function (context) {
root.on('in', request.body); root.on('in', request.body);
}); });
}); });
this.to.next(context);
}); });

View File

@ -8,7 +8,7 @@ var Gun = require('../../gun.js');
* @return {Boolean} - Whether it's attached. * @return {Boolean} - Whether it's attached.
*/ */
function isUsingServer (gun, server) { function isUsingServer (gun, server) {
var servers = gun.Back(-1)._.servers; var servers = gun.back(-1)._.servers;
return servers ? servers.indexOf(server) !== -1 : false; return servers ? servers.indexOf(server) !== -1 : false;
} }
@ -58,7 +58,7 @@ function send (msg, clients) {
* @return {server} - The socket server. * @return {server} - The socket server.
*/ */
function attach (gun, server) { function attach (gun, server) {
var root = gun.Back(-1); var root = gun.back(-1);
root._.servers = root._.servers || []; root._.servers = root._.servers || [];
root._.servers.push(server); root._.servers.push(server);
var pool = {}; var pool = {};
@ -82,6 +82,7 @@ function attach (gun, server) {
}); });
Gun.on('out', function (context) { Gun.on('out', function (context) {
this.to.next(context);
if (!isUsingServer(context.gun, server) || Gun.obj.empty(pool)) { if (!isUsingServer(context.gun, server) || Gun.obj.empty(pool)) {
return; return;
} }

View File

@ -11,6 +11,7 @@ var attach = require('./server-push');
require('./client.js'); require('./client.js');
Gun.on('opt', function (at) { Gun.on('opt', function (at) {
this.to.next(at);
var gun = at.gun, opt = at.opt; var gun = at.gun, opt = at.opt;
gun.__ = at.root._; gun.__ = at.root._;
gun.__.opt.ws = opt.ws = gun.__.opt.ws || opt.ws || {}; gun.__.opt.ws = opt.ws = gun.__.opt.ws || opt.ws || {};
@ -30,7 +31,8 @@ Gun.on('opt', function (at) {
require('./ws')(gun.wsp.ws, function (req, res) { require('./ws')(gun.wsp.ws, function (req, res) {
var ws = this; var ws = this;
req.headers['gun-sid'] = ws.sid = ws.sid ? ws.sid : req.headers['gun-sid']; req.headers['gun-sid'] = ws.sid = ws.sid ? ws.sid : req.headers['gun-sid'];
ws.sub = ws.sub || gun.wsp.on('network', function (msg, ev) { ws.sub = ws.sub || gun.wsp.on('network', function (msg) {
var ev = this; ev.to.next(msg);
if (!ws || !ws.send || !ws._socket || !ws._socket.writable) { return ev.off(); } if (!ws || !ws.send || !ws._socket || !ws._socket.writable) { return ev.off(); }
if (!msg || (msg.headers && msg.headers['gun-sid'] === ws.sid)) { return; } if (!msg || (msg.headers && msg.headers['gun-sid'] === ws.sid)) { return; }
if (msg && msg.headers) { delete msg.headers['ws-rid']; } if (msg && msg.headers) { delete msg.headers['ws-rid']; }
@ -97,7 +99,8 @@ Gun.on('opt', function (at) {
stream.reply = stream.queue = null; stream.reply = stream.queue = null;
return true; return true;
}; };
stream.sub = stream.sub || gun.wsp.on('network', function (req, ev) { stream.sub = stream.sub || gun.wsp.on('network', function (req) {
var ev = this; ev.to.next(req);
if (!stream) { return ev.off(); } // self cleans up after itself! if (!stream) { return ev.off(); } // self cleans up after itself!
if (!req || (req.headers && req.headers['gun-sid'] === stream.sid)) { return; } if (!req || (req.headers && req.headers['gun-sid'] === stream.sid)) { return; }
(stream.queue = stream.queue || []).push(req); (stream.queue = stream.queue || []).push(req);