Skip to content

Commit

Permalink
maybe use web pack?
Browse files Browse the repository at this point in the history
  • Loading branch information
amark committed May 4, 2016
1 parent 92c5919 commit dfbfa2e
Show file tree
Hide file tree
Showing 35 changed files with 4,597 additions and 1,454 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,6 @@ npm-debug.log
*.db
.idea/
*.bak
*.new
*.new
src/webpacked.js

5 changes: 0 additions & 5 deletions CHANGELOG.md
@@ -1,10 +1,5 @@
# CHANGELOG

## 0.4.0

- `val` has changed significantly.
- Implicit `put` occur up to the last `get`, not everything on the chain (like gets chained off of other gets).

## 0.3.7

- Catch localStorage errors.
Expand Down
1,437 changes: 1,437 additions & 0 deletions gun copy.js

Large diffs are not rendered by default.

2,957 changes: 1,517 additions & 1,440 deletions gun.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.4.0",
"version": "0.3.9",
"description": "Graph engine",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions src/HAM.js
@@ -0,0 +1,34 @@
module.exports = function(machineState, incomingState, currentState, incomingValue, currentValue){ // TODO: Lester's comments on roll backs could be vulnerable to divergence, investigate!
if(machineState < incomingState){
// the incoming value is outside the boundary of the machine's state, it must be reprocessed in another state.
return {defer: true};
}
if(incomingState < currentState){
// the incoming value is within the boundary of the machine's state, but not within the range.
return {historical: true};
}
if(currentState < incomingState){
// the incoming value is within both the boundary and the range of the machine's state.
return {converge: true, incoming: true};
}
if(incomingState === currentState){
if(incomingValue === currentValue){ // Note: while these are practically the same, the deltas could be technically different
return {state: true};
}
/*
The following is a naive implementation, but will always work.
Never change it unless you have specific needs that absolutely require it.
If changed, your data will diverge unless you guarantee every peer's algorithm has also been changed to be the same.
As a result, it is highly discouraged to modify despite the fact that it is naive,
because convergence (data integrity) is generally more important.
Any difference in this algorithm must be given a new and different name.
*/
if(String(incomingValue) < String(currentValue)){ // String only works on primitive values!
return {converge: true, current: true};
}
if(String(currentValue) < String(incomingValue)){ // String only works on primitive values!
return {converge: true, incoming: true};
}
}
return {err: "you have not properly handled recursion through your data or filtered it as JSON"};
}
46 changes: 46 additions & 0 deletions src/at.js
@@ -0,0 +1,46 @@
var Gun = require('./gun');

Gun.on.at = function(on){ // On event emitter customized for gun.
var proxy = function(e){ return proxy.e = e, proxy }
proxy.emit = function(at){
if(at.soul){
at.hash = Gun.on.at.hash(at);
//Gun.obj.as(proxy.mem, proxy.e)[at.soul] = at;
Gun.obj.as(proxy.mem, proxy.e)[at.hash] = at;
}
if(proxy.all.cb){ proxy.all.cb(at, proxy.e) }
on(proxy.e).emit(at);
return {chain: function(c){
if(!c || !c._ || !c._.at){ return }
return c._.at(proxy.e).emit(at)
}};
}
proxy.only = function(cb){
if(proxy.only.cb){ return }
return proxy.event(proxy.only.cb = cb);
}
proxy.all = function(cb){
proxy.all.cb = cb;
Gun.obj.map(proxy.mem, function(mem, e){
Gun.obj.map(mem, function(at, i){
cb(at, e);
});
});
}
proxy.event = function(cb, i){
i = on(proxy.e).event(cb, i);
return Gun.obj.map(proxy.mem[proxy.e], function(at){
i.stat = {first: true};
cb.call(i, at);
}), i.stat = {}, i;
}
proxy.map = function(cb, i){
return proxy.event(cb, i);
};
proxy.mem = {};
return proxy;
}

Gun.on.at.hash = function(at){ return (at.at && at.at.soul)? at.at.soul + (at.at.field || '') : at.soul + (at.field || '') }

Gun.on.at.copy = function(at){ return Gun.obj.del(at, 'hash'), Gun.obj.map(at, function(v,f,t){t(f,v)}) }
7 changes: 7 additions & 0 deletions src/bi.js
@@ -0,0 +1,7 @@
// Booleans
module.exports = {
is: function(b){
return (b instanceof Boolean || typeof b == 'boolean');
}
}

11 changes: 11 additions & 0 deletions src/chain.js
@@ -0,0 +1,11 @@
var Gun = require('./gun');

Gun.chain.chain = function(s){
var from = this, gun = !from.back? from : Gun(from);
gun.back = gun.back || from;
gun.__ = gun.__ || from.__;
gun._ = gun._ || {};
gun._.on = gun._.on || Gun.on.create();
gun._.at = gun._.at || Gun.on.at(gun._.on);
return gun;
}
41 changes: 41 additions & 0 deletions src/event.js
@@ -0,0 +1,41 @@
// On event emitter generic javascript utility.

var list = require('./list');

function On(){};

On.create = function(){

var on = function(e){
on.event.e = e;
on.event.s[e] = on.event.s[e] || [];
return on;
};

on.emit = function(a){
var e = on.event.e, s = on.event.s[e], args = arguments, l = args.length;
list.map(s, function(hear, i){
if(!hear.fn){ s.splice(i-1, 0); return; }
if(1 === l){ hear.fn(a); return; }
hear.fn.apply(hear, args);
});
if(!s.length){ delete on.event.s[e] }
}

on.event = function(fn, i){
var s = on.event.s[on.event.e]; if(!s){ return }
var e = {fn: fn, i: i || 0, off: function(){ return !(e.fn = false) }};
return s.push(e), i? s.sort(sort) : i, e;
}

on.event.s = {};

return on;
}

var sort = list.sort('i');

//exports.on = On.create();
//exports.on.create = On.create;

module.exports = On;
6 changes: 6 additions & 0 deletions src/fn.js
@@ -0,0 +1,6 @@
// Functions
module.exports = {
is: function(fn){
return (fn instanceof Function);
}
};
73 changes: 73 additions & 0 deletions src/get.js
@@ -0,0 +1,73 @@
var Gun = require('./gun');

Gun.chain.get = (function(){
Gun.on('operating').event(function(gun, at){
if(!gun.__.by(at.soul).node){ gun.__.by(at.soul).node = gun.__.graph[at.soul] }
if(at.field){ return } // TODO: It would be ideal to reuse HAM's field emit.
gun.__.on(at.soul).emit(at);
});
Gun.on('get').event(function(gun, at, ctx, opt, cb){
if(ctx.halt){ return } // TODO: CLEAN UP with event emitter option?
at.change = at.change || gun.__.by(at.soul).node;
if(opt.raw){ return cb.call(opt.on, at) }
if(!ctx.cb.no){ cb.call(ctx.by.chain, null, Gun.obj.copy(ctx.node || gun.__.by(at.soul).node)) }
gun._.at('soul').emit(at).chain(opt.chain);
},0);
Gun.on('get').event(function(gun, at, ctx){
if(ctx.halt){ ctx.halt = false; return } // TODO: CLEAN UP with event emitter option?
}, Infinity);
return function(lex, cb, opt){ // get opens up a reference to a node and loads it.
var gun = this, ctx = {
opt: opt || {},
cb: cb || function(){},
lex: (Gun.text.is(lex) || Gun.num.is(lex))? Gun.is.rel.ify(lex) : lex,
};
ctx.force = ctx.opt.force;
if(cb !== ctx.cb){ ctx.cb.no = true }
if(!Gun.obj.is(ctx.lex)){ return ctx.cb.call(gun = gun.chain(), {err: Gun.log('Invalid get request!', lex)}), gun }
if(!(ctx.soul = ctx.lex[Gun._.soul])){ return ctx.cb.call(gun = this.chain(), {err: Gun.log('No soul to get!')}), gun } // TODO: With `.all` it'll be okay to not have an exact match!
ctx.by = gun.__.by(ctx.soul);
ctx.by.chain = ctx.by.chain || gun.chain();
function load(lex){
var soul = lex[Gun._.soul];
var cached = gun.__.by(soul).node || gun.__.graph[soul];
if(ctx.force){ ctx.force = false }
else if(cached){ return false }
wire(lex, stream, ctx.opt);
return true;
}
function stream(err, data, info){
//console.log("wire.get <--", err, data);
Gun.on('wire.get').emit(ctx.by.chain, ctx, err, data, info);
if(err){
Gun.log(err.err || err);
ctx.cb.call(ctx.by.chain, err);
return ctx.by.chain._.at('err').emit({soul: ctx.soul, err: err.err || err}).chain(ctx.opt.chain);
}
if(!data){
ctx.cb.call(ctx.by.chain, null);
return ctx.by.chain._.at('null').emit({soul: ctx.soul, not: true}).chain(ctx.opt.chain);
}
if(Gun.obj.empty(data)){ return }
if(err = Gun.union(ctx.by.chain, data).err){
ctx.cb.call(ctx.by.chain, err);
return ctx.by.chain._.at('err').emit({soul: Gun.is.node.soul(data) || ctx.soul, err: err.err || err}).chain(ctx.opt.chain);
}
}
function wire(lex, cb, opt){
Gun.on('get.wire').emit(ctx.by.chain, ctx, lex, cb, opt);
if(Gun.fns.is(gun.__.opt.wire.get)){ return gun.__.opt.wire.get(lex, cb, opt) }
if(!Gun.log.count('no-wire-get')){ Gun.log("Warning! You have no persistence layer to get from!") }
cb(null); // This is in memory success, hardly "success" at all.
}
function on(at){
if(on.ran = true){ ctx.opt.on = this }
if(load(ctx.lex)){ return }
Gun.on('get').emit(ctx.by.chain, at, ctx, ctx.opt, ctx.cb, ctx.lex);
}
ctx.opt.on = (ctx.opt.at || gun.__.at)(ctx.soul).event(on);
ctx.by.chain._.get = ctx.lex;
if(!ctx.opt.ran && !on.ran){ on.call(ctx.opt.on, {soul: ctx.soul}) }
return ctx.by.chain;
}
}());
85 changes: 85 additions & 0 deletions src/gun.js
@@ -0,0 +1,85 @@
function Gun(o){
var gun = this;
if(!Gun.is(gun)){ return new Gun(o) }
if(Gun.is(o)){ return gun }
return gun.opt(o);
}


Gun.version = 0.3;

Gun._ = { // some reserved key words, these are not the only ones.
meta: '_' // all metadata of the node is stored in the meta property on the node.
,soul: '#' // a soul is a UUID of a node but it always points to the "latest" data known.
,field: '.' // a field is a property on a node which points to a value.
,state: '>' // other than the soul, we store HAM metadata.
,'#':'soul'
,'.':'field'
,'=':'value'
,'>':'state'
}

// check to see if it is a GUN instance.
Gun.is = function(gun){
return (gun instanceof Gun);
}

var root = this || {}; // safe for window, global, root, and 'use strict'.
root.console = root.console || {log: function(s){ return s }}; // safe for old browsers

if(typeof window !== "undefined"){
root = window;
window.Gun = Gun;
}
module.exports = Gun;


Gun.log = function(s){
return (!Gun.log.squelch && root.console.log.apply(root.console, arguments)), s;
}
Gun.log.count = function(s){ return Gun.log.count[s] = Gun.log.count[s] || 0, Gun.log.count[s]++ }

/*
var console = {
log: function(s){return root.console.log.apply(root.console, arguments), s},
Log: Gun.log
};
console.debug = function(i, s){ return (Gun.log.debug && i === Gun.log.debug && Gun.log.debug++) && root.console.log.apply(root.console, arguments), s };
*/


/*
Gun.fn = require('./fn');
Gun.bi = require('./bi');
Gun.num = require('./num');
Gun.text = require('./text');
Gun.list = require('./list');
Gun.obj = require('./obj');
Gun.time = require('./time');
Gun.schedule = require('./schedule');
var on = require('./event');
Gun.on = on.create();
Gun.on.create = on.create;
Gun.HAM = require('./HAM');
require('./ify');
require('./node');
require('./union');
// chain!
Gun.chain = Gun.prototype;
require('./opt');
require('./chain');
require('./put');
require('./get');
require('./key');
require('./on');
require('./path');
require('./map');
require('./val');
require('./not');
require('./set');
require('./init');
*/

0 comments on commit dfbfa2e

Please sign in to comment.