From 77d71a0c94d16dfe15e6f272c74a4ae6b95d0c86 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Fri, 27 Jan 2017 04:58:16 -0800 Subject: [PATCH] pushing! --- examples/json/index.html | 4 ++- examples/todo/index.html | 2 +- gun.js | 73 ++++++++++++++++------------------------ lib/file.js | 3 ++ lib/server.js | 6 ++-- lib/wsp/client.js | 10 +++--- lib/wsp/server-push.js | 5 +-- lib/wsp/server.js | 7 ++-- 8 files changed, 53 insertions(+), 57 deletions(-) diff --git a/examples/json/index.html b/examples/json/index.html index 6cecab38..33a2bac4 100644 --- a/examples/json/index.html +++ b/examples/json/index.html @@ -34,10 +34,12 @@ document.onkeyup = function(e){ if(!e || !e.target){ return } // ignore if no element! 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. .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! for(var field in data){ var val = String(data[field]), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe. diff --git a/examples/todo/index.html b/examples/todo/index.html index e00f8c34..42f36fa3 100644 --- a/examples/todo/index.html +++ b/examples/todo/index.html @@ -21,7 +21,7 @@ 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. 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 += '
  • ' + clean(list[field]) + '' diff --git a/gun.js b/gun.js index 10ede262..57bfd68b 100644 --- a/gun.js +++ b/gun.js @@ -771,7 +771,7 @@ Dup.prototype.gc = function(){ var de = this, now = Type.time.is(), oldest = now, maxAge = 5 * 60 * 1000; // 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); if ((now - time) < maxAge){ return } Type.obj.del(de.cache, id); @@ -824,10 +824,10 @@ ;(function(){ Gun.create = function(at){ at.on = at.on || Gun.on; - var gun = at.gun.opt(at.opt); - at.root = at.root || gun; + at.root = at.root || at.gun; at.graph = at.graph || {}; at.dup = at.dup || new Gun.dup; + var gun = at.gun.opt(at.opt); if(!at.once){ at.on('in', input, at); at.on('out', output, at); @@ -924,8 +924,8 @@ tmp = obj_map(tmp, function(url, i, map){ 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. Gun.on('opt', at); return gun; @@ -1105,7 +1105,7 @@ } if(u === change){ ev.to.next(at); - echo(cat, at); + echo(cat, at, ev); if(cat.field || cat.soul){ not(cat, at); } 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. } ev.to.next(at); - echo(cat, at); + echo(cat, at, ev); return; } if(cat.field){ @@ -1145,16 +1145,17 @@ } } ev.to.next(at); - echo(cat, at); + echo(cat, at, ev); obj_map(change, map, {at: at, cat: cat}); return; } 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; - function echo(cat, at){ + function echo(cat, at, ev){ if(!cat.echo){ return } + at.event = ev; obj_map(cat.echo, function(cat){ // TODO: PERF! Cache cat.on('in', at); }); @@ -1331,7 +1332,7 @@ 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. }*/ - as.use(at, ev); + as.use(at, at.event || ev); ev.to.next(at); } var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to; @@ -1426,10 +1427,10 @@ }, {as: as, at: at}); } - function soul(at, ev){ var as = this.as, as = as.as, cat = as.at; - ev.stun(); // TODO: BUG!? + function soul(at, ev){ var as = this.as, cat = as.at; as = as.as; + //ev.stun(); // TODO: BUG!? 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.batch(); } @@ -1488,30 +1489,10 @@ ev.off(); if(!as.not && !(as.soul = Gun.node.soul(data))){ 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 { - /* - TODO: CLEAN UP! Is any of this necessary? - */ - 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; + //as.data = obj_put({}, as.gun._.get, as.data); + as.soul = at.soul; } } if(as.ref !== as.gun && !as.not){ @@ -1708,7 +1689,7 @@ opt = opt || {}; opt.key = index; 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; gun.on(key, {as: opt}); if(!opt.data){ @@ -1738,7 +1719,7 @@ keyed._ = '##'; Gun.on('next', function(at){ 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('out', normalize, gun._); }); @@ -1750,7 +1731,7 @@ 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){ if(!Gun.node.is(graph['#'+soul+'#'], function each(rel,id){ if(id !== Gun.val.rel.is(rel)){ return } @@ -1822,7 +1803,7 @@ if(!at.put){ return } var rel = Gun.val.rel.is(at.put[keyed._]); 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)); root.get(rel).on(each, {change: true}); function each(change){ @@ -1950,7 +1931,7 @@ } 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){ return; } @@ -1979,7 +1960,7 @@ }); } } - if((tmp = gun.Back(-1)) === back){ + if((tmp = gun.back(-1)) === back){ obj_del(tmp.graph, at.get); } if(at.ons && (tmp = at.ons['@$'])){ @@ -2070,7 +2051,7 @@ var Gun = require('./core'); Gun.chain.init = function(){ // TODO: DEPRECATE? (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'); @@ -2097,6 +2078,7 @@ var store = root.localStorage || {setItem: noop, removeItem: noop, getItem: noop}; 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/'; Gun.graph.is(at.put, function(node, soul){ //try{store.setItem(opt.prefix + soul, Gun.text.ify(node)); @@ -2110,6 +2092,7 @@ } } function get(at){ + this.to.next(at); var gun = at.gun, lex = at.get, soul, data, opt, u; //setTimeout(function(){ (opt = at.opt || {}).prefix = opt.prefix || at.gun.back('opt.prefix') || 'gun/'; @@ -2348,6 +2331,7 @@ // Define client instances as gun needs them. // Sockets will not be opened until absolutely necessary. Gun.on('opt', function (ctx) { + this.to.next(ctx); var gun = ctx.gun; var peers = gun.back('opt.peers') || {}; @@ -2360,7 +2344,7 @@ 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. Client.pool[url] = client; @@ -2393,6 +2377,7 @@ // Broadcast the messages. Gun.on('out', function (ctx) { + this.to.next(ctx); var gun = ctx.gun; var peers = gun.back('opt.peers') || {}; // Validate. diff --git a/lib/file.js b/lib/file.js index 1aacb12f..31f0a802 100644 --- a/lib/file.js +++ b/lib/file.js @@ -7,6 +7,7 @@ var Gun = require('../gun'), file = {}; Gun.on('put', function(at){ + this.to.next(at); if(!file.use){ return } var graph = at.put, Graph = file.gun._.graph, opt = at.opt || {}; Gun.obj.map(graph, function(node, soul){ @@ -24,6 +25,7 @@ Gun.on('put', function(at){ }); Gun.on('get', function(at){ + this.to.next(at); if(!file.use){ return } var soul = at.get['#']; if(!soul){ return } @@ -52,6 +54,7 @@ function field(node, field){ } Gun.on('opt', function(at){ + this.to.next(at); var gun = at.gun, opt = at.opt; if ((opt.file === false) || (opt.s3 && opt.s3.key)) { return; // don't use this plugin if S3 is being used. diff --git a/lib/server.js b/lib/server.js index fbe858a1..35f4ee0e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,6 +1,6 @@ ;(function(){ var Gun = require('../gun'); - //require('./s3'); + //require('./s3'); require('./wsp/server'); require('./file'); Gun.log( @@ -8,6 +8,6 @@ 'I\'m mark@gunDB.io, message me for help or with hatemail. ' + '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; -}()); +}()); diff --git a/lib/wsp/client.js b/lib/wsp/client.js index 62b6e8d3..d410f931 100644 --- a/lib/wsp/client.js +++ b/lib/wsp/client.js @@ -46,9 +46,10 @@ function getSocketSubset (peers) { } Gun.on('out', function (ctx) { + this.to.next(ctx); var gun = ctx.gun; var opt = ctx.opt || {}; - var peers = opt.peers || gun.Back('opt.peers'); + var peers = opt.peers || gun.back('opt.peers'); if (!peers) { return; @@ -66,15 +67,15 @@ Gun.on('out', function (ctx) { // adding them to the global pool. Gun.on('opt', function (context) { 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) { if (sockets[url]) { 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); sockets.add(url, socket); @@ -96,4 +97,5 @@ Gun.on('opt', function (context) { root.on('in', request.body); }); }); + this.to.next(context); }); diff --git a/lib/wsp/server-push.js b/lib/wsp/server-push.js index 727f8d1e..772cd452 100644 --- a/lib/wsp/server-push.js +++ b/lib/wsp/server-push.js @@ -8,7 +8,7 @@ var Gun = require('../../gun.js'); * @return {Boolean} - Whether it's attached. */ function isUsingServer (gun, server) { - var servers = gun.Back(-1)._.servers; + var servers = gun.back(-1)._.servers; return servers ? servers.indexOf(server) !== -1 : false; } @@ -58,7 +58,7 @@ function send (msg, clients) { * @return {server} - The socket server. */ function attach (gun, server) { - var root = gun.Back(-1); + var root = gun.back(-1); root._.servers = root._.servers || []; root._.servers.push(server); var pool = {}; @@ -82,6 +82,7 @@ function attach (gun, server) { }); Gun.on('out', function (context) { + this.to.next(context); if (!isUsingServer(context.gun, server) || Gun.obj.empty(pool)) { return; } diff --git a/lib/wsp/server.js b/lib/wsp/server.js index 3c56a425..6da51795 100644 --- a/lib/wsp/server.js +++ b/lib/wsp/server.js @@ -11,6 +11,7 @@ var attach = require('./server-push'); require('./client.js'); Gun.on('opt', function (at) { + this.to.next(at); var gun = at.gun, opt = at.opt; gun.__ = at.root._; 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) { var ws = this; 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 (!msg || (msg.headers && msg.headers['gun-sid'] === ws.sid)) { return; } if (msg && msg.headers) { delete msg.headers['ws-rid']; } @@ -97,7 +99,8 @@ Gun.on('opt', function (at) { stream.reply = stream.queue = null; 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 (!req || (req.headers && req.headers['gun-sid'] === stream.sid)) { return; } (stream.queue = stream.queue || []).push(req);