mirror of
https://github.com/amark/gun.git
synced 2025-10-13 16:49:26 +00:00
yc!
This commit is contained in:
parent
7b36b54421
commit
42198c2e61
@ -9,7 +9,6 @@ module.exports=require('theory')
|
|||||||
AWS.config.sslEnabled = conf.sslEnabled = false;
|
AWS.config.sslEnabled = conf.sslEnabled = false;
|
||||||
}
|
}
|
||||||
AWS.config.update(conf);
|
AWS.config.update(conf);
|
||||||
|
|
||||||
function s3(db){
|
function s3(db){
|
||||||
db = db || conf.s3Bucket || (conf.s3Bucket = process.env.s3Bucket);
|
db = db || conf.s3Bucket || (conf.s3Bucket = process.env.s3Bucket);
|
||||||
if(dev){
|
if(dev){
|
||||||
@ -73,7 +72,7 @@ module.exports=require('theory')
|
|||||||
,Key: key
|
,Key: key
|
||||||
};
|
};
|
||||||
if(S3.batch(m,cb)){ console.log('no!', m.Bucket + m.Key); return }
|
if(S3.batch(m,cb)){ console.log('no!', m.Bucket + m.Key); return }
|
||||||
console.log('YES!', m.Bucket + m.Key);
|
console.log("s3 info:", m);
|
||||||
S3().getObject(m, function(e,r){
|
S3().getObject(m, function(e,r){
|
||||||
var d, t, r = r || (this && this.httpResponse);
|
var d, t, r = r || (this && this.httpResponse);
|
||||||
if(e || !r){ return S3.batch.reply(m,e) }
|
if(e || !r){ return S3.batch.reply(m,e) }
|
||||||
|
63
gun.js
63
gun.js
@ -18,14 +18,14 @@ module.exports = require('theory')
|
|||||||
, g, n, w = w || a.time.now();
|
, g, n, w = w || a.time.now();
|
||||||
if(gun.is(this)){
|
if(gun.is(this)){
|
||||||
n = this;
|
n = this;
|
||||||
g = n._.graph || function(){ return {} };
|
g = n._.clip || function(){ return {} };
|
||||||
if(a.text.is(p)){
|
if(a.text.is(p)){
|
||||||
if(args >= 2){ // set
|
if(args >= 2){ // set
|
||||||
var ref = {}
|
var ref = {}
|
||||||
, val = gun.at(n,p,ref);
|
, val = gun.at(n,p,ref);
|
||||||
if(!ref.node || !ref.node._ || !ref.node._[gun._.id]){
|
if(!ref.cartridge || !ref.cartridge._ || !ref.cartridge._[gun._.id]){
|
||||||
return;
|
return;
|
||||||
} ref.id = ref.node._[gun._.id] +'.'+ ref.path;
|
} ref.id = ref.cartridge._[gun._.id] +'.'+ ref.path;
|
||||||
if(a.gun.ham && a.gun.ham.call(g,n,p,v,w,val)){
|
if(a.gun.ham && a.gun.ham.call(g,n,p,v,w,val)){
|
||||||
console.log("HAM REJECTION", p, v, val);
|
console.log("HAM REJECTION", p, v, val);
|
||||||
return;
|
return;
|
||||||
@ -48,7 +48,7 @@ module.exports = require('theory')
|
|||||||
delete ref.at[ref.prop];
|
delete ref.at[ref.prop];
|
||||||
}
|
}
|
||||||
var del = {}; del[ref.id] = null;
|
var del = {}; del[ref.id] = null;
|
||||||
theory.on(gun.event).emit(w, del, g[gun._.id]);
|
gun.fire(del, g[gun._.id], w);
|
||||||
v = ref.at;
|
v = ref.at;
|
||||||
} else {
|
} else {
|
||||||
v = gun.at(v);
|
v = gun.at(v);
|
||||||
@ -65,7 +65,7 @@ module.exports = require('theory')
|
|||||||
ref.at[ref.prop] = v;
|
ref.at[ref.prop] = v;
|
||||||
}
|
}
|
||||||
var diff = {}; diff[ref.id] = v;
|
var diff = {}; diff[ref.id] = v;
|
||||||
theory.on(gun.event).emit(w, diff, g[gun._.id]);
|
gun.fire(diff, g[gun._.id], w);
|
||||||
v = j || v;
|
v = j || v;
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
@ -81,9 +81,9 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
n = a.obj.is(v)? v : a.obj.is(p)? p : null;
|
n = a.obj.is(v)? v : a.obj.is(p)? p : null;
|
||||||
p = a.text.is(p)? p : gun.id();
|
p = a.text.is(p)? p : gun.id();
|
||||||
if(a.obj.is(n)){ // create a new graph from this object
|
if(a.obj.is(n)){ // create a clip from this object
|
||||||
g = gun.ify(n);
|
g = gun.ify(n);
|
||||||
var graph = gun.clip[p] = function(p,v,w){
|
var clip = gun.magazine[p] = function(p,v,w){
|
||||||
var args, id, path, n, w = w || a.time.now();
|
var args, id, path, n, w = w || a.time.now();
|
||||||
if(a.text.is(p)){
|
if(a.text.is(p)){
|
||||||
id = a.text(p).clip('.',0,1);
|
id = a.text(p).clip('.',0,1);
|
||||||
@ -99,7 +99,7 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
if(path){
|
if(path){
|
||||||
if(n){
|
if(n){
|
||||||
n._.graph = n._.graph || graph;
|
n._.clip = n._.clip || clip;
|
||||||
return gun.apply(n, args);
|
return gun.apply(n, args);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -111,13 +111,13 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
if(n){
|
if(n){
|
||||||
if(args.length === 1){
|
if(args.length === 1){
|
||||||
n._.graph = n._.graph || graph;
|
n._.clip = n._.clip || clip;
|
||||||
return fn;
|
return fn;
|
||||||
}
|
}
|
||||||
if(v === null){
|
if(v === null){
|
||||||
delete g[n._[gun._.id]];
|
delete g[n._[gun._.id]];
|
||||||
var del = {}; del[n._[gun._.id]] = n = null;
|
var del = {}; del[n._[gun._.id]] = n = null;
|
||||||
theory.on(gun.event).emit(w, del, g[gun._.id]);
|
gun.fire(del, g[gun._.id], w);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -130,16 +130,16 @@ module.exports = require('theory')
|
|||||||
v = p;
|
v = p;
|
||||||
}
|
}
|
||||||
if(a.obj.is(v)){
|
if(a.obj.is(v)){
|
||||||
n = gun.ify(v,u,{}); // can only add one node with this method!
|
n = gun.ify(v,u,{}); // a clip cannot be created from this, only a single cartridge
|
||||||
n._ = n._ || gun.id({});
|
n._ = n._ || gun.id({});
|
||||||
n._.graph = graph; // JSONifying excludes functions.
|
n._.clip = clip; // JSONifying excludes functions.
|
||||||
var add = {}; add[n._[gun._.id]] = g[n._[gun._.id]] = n;
|
var add = {}; add[n._[gun._.id]] = g[n._[gun._.id]] = n;
|
||||||
theory.on(gun.event).emit(w, add, graph[gun._.id]);
|
gun.fire(add, clip[gun._.id], w);
|
||||||
return fn;
|
return fn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
graph[gun._.id] = p;
|
clip[gun._.id] = p;
|
||||||
return graph;
|
return clip;
|
||||||
}
|
}
|
||||||
} var u;
|
} var u;
|
||||||
gun._ = {
|
gun._ = {
|
||||||
@ -164,10 +164,10 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
ref = ref || {};
|
ref = ref || {};
|
||||||
var pp = a.list.is(p)? p : (p||'').split('.')
|
var pp = a.list.is(p)? p : (p||'').split('.')
|
||||||
, g = a.fns.is(n._.graph)? n._.graph() : this
|
, g = a.fns.is(n._.clip)? n._.clip() : this
|
||||||
, i = 0, l = pp.length, v = n
|
, i = 0, l = pp.length, v = n
|
||||||
, x, y, z;
|
, x, y, z;
|
||||||
ref.node = n;
|
ref.cartridge = n;
|
||||||
ref.prop = pp[l-1];
|
ref.prop = pp[l-1];
|
||||||
ref.path = pp.slice(i).join('.');
|
ref.path = pp.slice(i).join('.');
|
||||||
while(i < l && v !== u){
|
while(i < l && v !== u){
|
||||||
@ -228,7 +228,7 @@ module.exports = require('theory')
|
|||||||
} else {
|
} else {
|
||||||
f[i] = n;
|
f[i] = n;
|
||||||
}
|
}
|
||||||
opt.seen.push({node: n, prop: i, from: f, src: o});
|
opt.seen.push({cartridge: n, prop: i, from: f, src: o});
|
||||||
a.obj(o).each(function(v,j){
|
a.obj(o).each(function(v,j){
|
||||||
ify(v,j,n,{},f);
|
ify(v,j,n,{},f);
|
||||||
});
|
});
|
||||||
@ -247,7 +247,7 @@ module.exports = require('theory')
|
|||||||
if(a.obj.is(v)){
|
if(a.obj.is(v)){
|
||||||
if(seen = ify.seen(v)){
|
if(seen = ify.seen(v)){
|
||||||
ify.be(seen);
|
ify.be(seen);
|
||||||
if(gun.is(seen.node)){ t(seen.node._[gun._.id]) }
|
if(gun.is(seen.cartridge)){ t(seen.cartridge._[gun._.id]) }
|
||||||
} else {
|
} else {
|
||||||
gun.ify(v, opt, n);
|
gun.ify(v, opt, n);
|
||||||
if(gun.is(n)){
|
if(gun.is(n)){
|
||||||
@ -270,7 +270,7 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ify.be = function(seen){
|
ify.be = function(seen){
|
||||||
var n = seen.node;
|
var n = seen.cartridge;
|
||||||
n._ = n._||{};
|
n._ = n._||{};
|
||||||
n._[gun._.id] = n._[gun._.id]||gun.id();
|
n._[gun._.id] = n._[gun._.id]||gun.id();
|
||||||
g[n._[gun._.id]] = n;
|
g[n._[gun._.id]] = n;
|
||||||
@ -283,14 +283,14 @@ module.exports = require('theory')
|
|||||||
if(v && v.src === o){ return v }
|
if(v && v.src === o){ return v }
|
||||||
}) || false;
|
}) || false;
|
||||||
}
|
}
|
||||||
var is = true, node = n || {};
|
var is = true, cartridge = n || {};
|
||||||
a.obj(o).each(function(v,i){
|
a.obj(o).each(function(v,i){
|
||||||
if(!gun.is(v)){ is = false }
|
if(!gun.is(v)){ is = false }
|
||||||
ify(v, i, node, {});
|
ify(v, i, cartridge, {});
|
||||||
});
|
});
|
||||||
if(!is){
|
if(!is){
|
||||||
ify.be({node: node});
|
ify.be({cartridge: cartridge});
|
||||||
g[node._[gun._.id]] = node;
|
g[cartridge._[gun._.id]] = cartridge;
|
||||||
}
|
}
|
||||||
if(n){
|
if(n){
|
||||||
return n;
|
return n;
|
||||||
@ -337,8 +337,21 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
gun.fire = function(bullet,c,w,op){
|
||||||
|
bullet = bullet.what? bullet : {what: bullet};
|
||||||
|
bullet.where = c || bullet.where;
|
||||||
|
bullet.when = w || bullet.when;
|
||||||
|
if(!a.obj.is(bullet.what)){ return gun.fire.jam("No ammo.", bullet) }
|
||||||
|
if(!a.num.is(bullet.when)){ return gun.fire.jam("No time.", bullet) }
|
||||||
|
if(!a.text.is(bullet.where)){ return gun.fire.jam("No location.", bullet) }
|
||||||
|
bullet.how = bullet.how || {};
|
||||||
|
bullet.how.gun = op || 1;
|
||||||
|
theory.on(gun.event).emit(bullet);
|
||||||
|
}
|
||||||
|
gun.fire.jam = function(s,b){ if(b){ return console.log("Gun jam:",s,b) } console.log("Gun jam:",s) }
|
||||||
|
gun.shots = function(hear,s){ return theory.on(gun.event+(s?'.'+s:'')).event(hear) }
|
||||||
gun.event = 'gun';
|
gun.event = 'gun';
|
||||||
gun.clip = {};
|
gun.magazine = {};
|
||||||
return gun;
|
return gun;
|
||||||
})();
|
})();
|
||||||
/* Hypothetical Amnesia Machine
|
/* Hypothetical Amnesia Machine
|
||||||
|
17
init.js
17
init.js
@ -1,17 +1,12 @@
|
|||||||
(function(){
|
(function(){
|
||||||
|
|
||||||
process.env.rootdir = __dirname;
|
process.env.rootdir = __dirname;
|
||||||
var LIVE = process.env.LIVE || (process.env.NODE_ENV === 'production')
|
var LIVE = process.env.LIVE || (process.env.NODE_ENV === 'production'), web, opt = {};
|
||||||
, web, opt = {}
|
opt.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT;
|
||||||
opt.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || 8888;
|
opt.host = process.env.OPENSHIFT_NODEJS_IP;
|
||||||
opt.host = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
|
if(opt.port || opt.host){ LIVE = true }
|
||||||
|
opt.port = opt.port || 8888;
|
||||||
|
opt.host = opt.host || '0.0.0.0';
|
||||||
opt.hook = {
|
|
||||||
pre: (function(req,res){
|
|
||||||
//console.log("--------- "+req.flow+" : "+req.url.pathname +" ---------------");
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
if(LIVE){
|
if(LIVE){
|
||||||
//process.env['redis-install'] = '/tmp';
|
//process.env['redis-install'] = '/tmp';
|
||||||
|
136
shot.js
136
shot.js
@ -6,6 +6,7 @@ module.exports = require('theory')
|
|||||||
opt.batch = opt.batch || 0;
|
opt.batch = opt.batch || 0;
|
||||||
opt.throttle = opt.throttle || 0;
|
opt.throttle = opt.throttle || 0;
|
||||||
opt.src = opt.src || (this && this.com) || '';
|
opt.src = opt.src || (this && this.com) || '';
|
||||||
|
opt.cache = a.num.is(opt.cache)? opt.cache : opt.cache || 1;
|
||||||
if(root.node){ return require(opt.path+'/shots')(opt); }
|
if(root.node){ return require(opt.path+'/shots')(opt); }
|
||||||
var u, shot = {},
|
var u, shot = {},
|
||||||
store = window.amplify && window.amplify.store? amplify.store
|
store = window.amplify && window.amplify.store? amplify.store
|
||||||
@ -13,51 +14,14 @@ module.exports = require('theory')
|
|||||||
if(data === u){ return store[src] }
|
if(data === u){ return store[src] }
|
||||||
return store[src] = data;
|
return store[src] = data;
|
||||||
}
|
}
|
||||||
store.reply = {};
|
store.batch = [];
|
||||||
shot.meta = function(m, w, g){
|
store.last = a.time.now();
|
||||||
if(!m){ return }
|
store.list = function(){
|
||||||
if(m.when && m.where){ return m }
|
|
||||||
if(!w || !g){ return }
|
|
||||||
m.when = w;
|
|
||||||
m.where = g;
|
|
||||||
m.how = {gun:1};
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
theory.on(a.gun.event).event(shot.fire = function(w, m, g){
|
|
||||||
if(m){
|
|
||||||
m = shot.meta(m, w, g);
|
|
||||||
}
|
|
||||||
if(!m){ return }
|
|
||||||
if(w && g){
|
|
||||||
if(shot.add(m, g, shot.batch) === 2){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(opt.src && opt.src.send){
|
|
||||||
if(!shot.meta(m)){ return }
|
|
||||||
console.log("to server!", m);
|
|
||||||
return opt.src.send(m);
|
|
||||||
} // below is fallback. TODO: Unfinished!
|
|
||||||
if(shot.lock){ return }
|
|
||||||
var now = a.time.now();
|
|
||||||
if(now - shot.last < opt.throttle // this entire if satement is probably wrong. Redo entirely.
|
|
||||||
&& shot.batch.length < opt.batch){ return }
|
|
||||||
console.log('sending!', shot.batch);
|
|
||||||
$.post(opt.src, {b:a.text.ify(shot.batch)}, function(e,r){
|
|
||||||
console.log('reply', e,r);
|
|
||||||
shot.del(m, g);
|
|
||||||
});
|
|
||||||
shot.last = w;
|
|
||||||
shot.batch = [];
|
|
||||||
});
|
|
||||||
shot.batch = [];
|
|
||||||
shot.last = a.time.now();
|
|
||||||
shot.list = function(){
|
|
||||||
var g = store(a.gun.event) || {}
|
var g = store(a.gun.event) || {}
|
||||||
, z = function(l,g,i){
|
, z = function(l,g,i){
|
||||||
if(i !== 0 && !i){ return }
|
if(i !== 0 && !i){ return }
|
||||||
if(!l || !l[i]){ return }
|
if(!l || !l[i]){ return }
|
||||||
shot.fire(null, l[i]);
|
shot.fire(l[i], 1);
|
||||||
console.log("re-sent", l[i]);
|
console.log("re-sent", l[i]);
|
||||||
a.time.wait(function(){ z(l,g,i+1) },1);
|
a.time.wait(function(){ z(l,g,i+1) },1);
|
||||||
}
|
}
|
||||||
@ -65,13 +29,21 @@ module.exports = require('theory')
|
|||||||
z(l,g,0);
|
z(l,g,0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
shot.add = function(m, g, b){
|
store.sort = function(A,B){
|
||||||
|
if(!A || !B){ return 0 }
|
||||||
|
A = ((A||{})._||{})[a.gun._.ham]; B = ((B||{})._||{})[a.gun._.ham];
|
||||||
|
if(A < B){ return -1 }
|
||||||
|
else if(A > B){ return 1 }
|
||||||
|
else { return 0 }
|
||||||
|
}
|
||||||
|
store.add = function(m, g, b){
|
||||||
if(!m){ return }
|
if(!m){ return }
|
||||||
|
g = '_' + (a(g,'at') || g || a(m,'where.at') || m.where);
|
||||||
var gs = store(a.gun.event) || {}
|
var gs = store(a.gun.event) || {}
|
||||||
, when = m.when || a(m,'_.'+a.gun._.ham) || a(m,'what._.'+a.gun._.ham);
|
, when = shot.when(m);
|
||||||
gs[g] = gs[g] || [];
|
gs[g] = gs[g] || [];
|
||||||
if(a.list(gs[g]).each(function(v){
|
if(a.list(gs[g]).each(function(v){
|
||||||
var w = v.when || a(v,'_.'+a.gun._.ham) || a(v,'what._.'+a.gun._.ham);
|
var w = shot.when(v);
|
||||||
if(w === when){
|
if(w === when){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -81,15 +53,15 @@ module.exports = require('theory')
|
|||||||
store(a.gun.event, gs);
|
store(a.gun.event, gs);
|
||||||
return gs[g];
|
return gs[g];
|
||||||
}
|
}
|
||||||
shot.del = function(m, g){
|
store.del = function(m, g){
|
||||||
if(!m){ return }
|
if(!m){ return }
|
||||||
var gs = store(a.gun.event) || {}
|
var gs = store(a.gun.event) || {}
|
||||||
, when = m.when || a(m,'_.'+a.gun._.ham) || a(m,'what._.'+a.gun._.ham);
|
, when = shot.when(m);
|
||||||
g = m.where.at || m.where;
|
g = '_'+(m.where.at || m.where || g);
|
||||||
console.log("clear queue", g, m);
|
console.log("clear queue", g, m);
|
||||||
gs[g] = gs[g] || [];
|
gs[g] = gs[g] || [];
|
||||||
gs[g] = a.list(gs[g]).each(function(v,i,t){
|
gs[g] = a.list(gs[g]).each(function(v,i,t){
|
||||||
var w = v.when || a(v,'_.'+a.gun._.ham) || a(v,'what._.'+a.gun._.ham);
|
var w = shot.when(v);
|
||||||
if(w === when){
|
if(w === when){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -97,20 +69,57 @@ module.exports = require('theory')
|
|||||||
});
|
});
|
||||||
store(a.gun.event, gs);
|
store(a.gun.event, gs);
|
||||||
}
|
}
|
||||||
shot.sort = function(A,B){
|
store.set = function(key, val){
|
||||||
if(!A || !B){ return 0 }
|
var s = store(a.gun.event) || {};
|
||||||
A = ((A||{})._||{})[a.gun._.ham]; B = ((B||{})._||{})[a.gun._.ham];
|
s[key] = val;
|
||||||
if(A < B){ return -1 }
|
store(a.gun.event, s);
|
||||||
else if(A > B){ return 1 }
|
|
||||||
else { return 0 }
|
|
||||||
}
|
}
|
||||||
|
store.get = function(key, cb){
|
||||||
|
var s = store(a.gun.event) || {};
|
||||||
|
s = s[key];
|
||||||
|
if(cb){
|
||||||
|
return cb(null, s);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
a.gun.shots(shot.fire = function(m, r){
|
||||||
|
if(!m || !m.where){ return }
|
||||||
|
if(!r){
|
||||||
|
if(store.add(m, m.where, store.batch) === 2){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(opt.src && opt.src.send){
|
||||||
|
m = opt.src.meta(m);
|
||||||
|
if(!m.what || !m.where || !m.when){ return }
|
||||||
|
console.log("to server!", m);
|
||||||
|
return opt.src.send(m);
|
||||||
|
}
|
||||||
|
return; // below should be a fallback. TODO: Unfinished!
|
||||||
|
});
|
||||||
|
shot.when = function(m){ return a(m,'what._.'+a.gun._.ham) || a(m,'_.'+a.gun._.ham) || m.when }
|
||||||
shot.load = function(where,cb,o){
|
shot.load = function(where,cb,o){
|
||||||
if(!where){ return }
|
if(!where){ return }
|
||||||
o = o || {};
|
o = o || {};
|
||||||
var m = {what: where, how: {gun:3}};
|
var m = {what: where, how: {gun:3}}
|
||||||
|
, g = a.gun.magazine[where] || store.get(where);
|
||||||
|
g = a.fns.is(g)? g : a.obj.is(g)? a.gun(where, g) : null;
|
||||||
|
if(g){
|
||||||
|
//cb(g);
|
||||||
|
}
|
||||||
|
//console.log("!!! ASK !!!");
|
||||||
if(opt.src && opt.src.ask){
|
if(opt.src && opt.src.ask){
|
||||||
opt.src.ask(m,function(m){
|
opt.src.ask(m,function(m){
|
||||||
|
if(g){
|
||||||
|
//console.log("!!! double load !!!");
|
||||||
|
//return; // prevent load from calling twice! Add sync comparison.
|
||||||
|
}
|
||||||
if(!m || !m.what){ cb(null) }
|
if(!m || !m.what){ cb(null) }
|
||||||
|
if(o.cache !== 0){
|
||||||
|
if(o.cache || opt.cache){ // make options more configurable.
|
||||||
|
store.set(where, m.what);
|
||||||
|
}
|
||||||
|
}
|
||||||
m = a.gun(where, m.what);
|
m = a.gun(where, m.what);
|
||||||
cb(m);
|
cb(m);
|
||||||
});
|
});
|
||||||
@ -130,21 +139,10 @@ module.exports = require('theory')
|
|||||||
shot.spray.action = function(m){
|
shot.spray.action = function(m){
|
||||||
if(!m || !m.how || !m.how.gun){ return }
|
if(!m || !m.how || !m.how.gun){ return }
|
||||||
if(m.how.gun === -1){
|
if(m.how.gun === -1){
|
||||||
shot.del(m);
|
store.del(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shot.list();
|
store.list();
|
||||||
return {gun: a.gun
|
return shot;
|
||||||
,spray: shot.spray
|
|
||||||
,load: shot.load
|
|
||||||
,fire: shot.fire
|
|
||||||
,wait: function(){
|
|
||||||
shot.lock = 1;
|
|
||||||
}
|
|
||||||
,go: function(){
|
|
||||||
shot.last = shot.lock = 0;
|
|
||||||
shot.fire();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
},['./gun'])
|
},['./gun'])
|
53
shots.js
53
shots.js
@ -3,6 +3,7 @@ module.exports = require('theory')
|
|||||||
var s3 = require(__dirname+'/gate/s3')
|
var s3 = require(__dirname+'/gate/s3')
|
||||||
, store = require(__dirname+'/gate/redis');
|
, store = require(__dirname+'/gate/redis');
|
||||||
return function(opt){
|
return function(opt){
|
||||||
|
console.log("***** SHOTS *****");
|
||||||
opt = opt || {};
|
opt = opt || {};
|
||||||
var u, shot = {};
|
var u, shot = {};
|
||||||
opt.path = opt.path || '.'
|
opt.path = opt.path || '.'
|
||||||
@ -30,17 +31,23 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
opt.s3 = opt.s3 || {};
|
opt.s3 = opt.s3 || {};
|
||||||
opt.s3.Bucket = a.text.is(opt.s3.bucket)? opt.s3.bucket : '';
|
opt.s3.Bucket = a.text.is(opt.s3.bucket)? opt.s3.bucket : (process.env.s3Bucket || '');
|
||||||
opt.s3.bucket = a.fns.is(opt.s3.bucket)? opt.s3.bucket : function(key){
|
opt.s3.bucket = a.fns.is(opt.s3.bucket)? opt.s3.bucket : function(key){
|
||||||
return opt.s3.Bucket || a.text(key).clip('/',0,1);
|
return opt.s3.Bucket || a.text(key).clip('/',0,1);
|
||||||
}
|
}
|
||||||
opt.s3.key = a.fns.is(opt.s3.key)? opt.s3.key : function(key){
|
opt.s3.key = a.fns.is(opt.s3.key)? opt.s3.key : function(key){
|
||||||
if(opt.s3.Bucket){ return key }
|
if(key.slice(0, opt.s3.Bucket.length) === opt.s3.Bucket){
|
||||||
return a.text(key).clip('/',1);
|
key = key.slice(opt.s3.Bucket.length)||'';
|
||||||
|
if(key.charAt(0) === '/'){
|
||||||
|
key = key.slice(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
store.batch = [];
|
store.batch = [];
|
||||||
store.last = a.time.now();
|
store.last = a.time.now();
|
||||||
store.push = function(key, score, val, cb){
|
store.push = function(key, score, val, cb){
|
||||||
|
if(!val){ return }
|
||||||
store.client.zadd(key, score, val, function(e,r){
|
store.client.zadd(key, score, val, function(e,r){
|
||||||
if(e){
|
if(e){
|
||||||
store.clienf.zadd(key, score, val, cb);
|
store.clienf.zadd(key, score, val, cb);
|
||||||
@ -49,10 +56,11 @@ module.exports = require('theory')
|
|||||||
if(cb){ cb(e,r) }
|
if(cb){ cb(e,r) }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
store.when = function(m){ return a(m,'what._.'+a.gun._.ham) || a(m,'_.'+a.gun._.ham) || m.when }
|
||||||
store.add = function(m, g){
|
store.add = function(m, g){
|
||||||
if(!m){ return }
|
if(!m){ return }
|
||||||
g = '_' + (g || a(m,'where.at') || m.where);
|
g = '_' + (g || a(m,'where.at') || m.where);
|
||||||
store.push(g, (m.when || a(m,'what._.'+a.gun._.ham) || 0), a.text.ify(m));
|
store.push(g, store.when(m) || 0, a.text.ify(m));
|
||||||
}
|
}
|
||||||
store.del = function(m, g){
|
store.del = function(m, g){
|
||||||
|
|
||||||
@ -86,12 +94,13 @@ module.exports = require('theory')
|
|||||||
store.wait = null;
|
store.wait = null;
|
||||||
a.obj(store.batch).each(function(g,where){
|
a.obj(store.batch).each(function(g,where){
|
||||||
if(!g || !where){ return }
|
if(!g || !where){ return }
|
||||||
console.log('*************** save', where, '*******************');
|
//console.log('*************** save', where, '*******************');
|
||||||
s3(opt.s3.bucket(where)).put(opt.s3.key(where),g,function(e,r){
|
s3(opt.s3.bucket(where)).put(opt.s3.key(where),g,function(e,r){
|
||||||
a.list(store.batched[where]).each(function(cb){
|
a.list(store.batched[where]).each(function(cb){
|
||||||
if(a.fns.is(cb)){ cb(e,r) }
|
if(a.fns.is(cb)){ cb(e,r) }
|
||||||
console.log('*** end ***');
|
console.log('*** saved ***');
|
||||||
});
|
});
|
||||||
|
console.log(store.batched[where]);
|
||||||
delete store.batched[where];
|
delete store.batched[where];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -100,7 +109,7 @@ module.exports = require('theory')
|
|||||||
opt.redis.config();
|
opt.redis.config();
|
||||||
var val = a.text.is(value)? value : a.text.ify(value);
|
var val = a.text.is(value)? value : a.text.ify(value);
|
||||||
if(a.fns.is(fn)){ store.stay(key, value, fn) }
|
if(a.fns.is(fn)){ store.stay(key, value, fn) }
|
||||||
console.log("potential setex:", key, opt.redis.expire, val);
|
//console.log("potential setex:", key, opt.redis.expire, val);
|
||||||
if(opt.redis.max){
|
if(opt.redis.max){
|
||||||
store.client.set(key, val, function(e,r){
|
store.client.set(key, val, function(e,r){
|
||||||
if(e){
|
if(e){
|
||||||
@ -126,17 +135,15 @@ module.exports = require('theory')
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
shot.load = function(where,cb,o){
|
shot.load = function(where,cb,o){
|
||||||
console.log("shot.load >", where);
|
//console.log("shot.load >", where);
|
||||||
if(!where){ return }
|
if(!where){ return }
|
||||||
where = a.text.is(where)? where : (where.at || where);
|
where = a.text.is(where)? where : (where.at || where);
|
||||||
console.log("shot.load?", where);
|
|
||||||
if(!a.text.is(where)){ return }
|
if(!a.text.is(where)){ return }
|
||||||
if(a.fns.is(a.gun.clip[where])){
|
if(a.fns.is(a.gun.magazine[where])){
|
||||||
console.log('via memory', where);
|
console.log('via memory', where);
|
||||||
cb(a.gun.clip[where]); // TODO: Need to delete these at some point, too!
|
cb(a.gun.magazine[where]); // TODO: Need to delete these at some point, too!
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("from external...");
|
|
||||||
store.get(where, function(e,r){
|
store.get(where, function(e,r){
|
||||||
if(e || !r){
|
if(e || !r){
|
||||||
return s3(opt.s3.bucket(where)).get(opt.s3.key(where),function(e,r,t){
|
return s3(opt.s3.bucket(where)).get(opt.s3.key(where),function(e,r,t){
|
||||||
@ -154,6 +161,7 @@ module.exports = require('theory')
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
shot.spray = function(m){
|
shot.spray = function(m){
|
||||||
|
console.log(">> shot.spray");
|
||||||
if(m && m.how){
|
if(m && m.how){
|
||||||
shot.spray.action(m);
|
shot.spray.action(m);
|
||||||
return shot;
|
return shot;
|
||||||
@ -166,15 +174,12 @@ module.exports = require('theory')
|
|||||||
}
|
}
|
||||||
shot.spray.transform = function(g,m,d){if(d){d()}}
|
shot.spray.transform = function(g,m,d){if(d){d()}}
|
||||||
shot.spray.action = function(m){
|
shot.spray.action = function(m){
|
||||||
|
console.log(">>> shot.spray.action");
|
||||||
if(!m || !m.how){ return }
|
if(!m || !m.how){ return }
|
||||||
var where = a.text.is(m.where)? m.where : m.where.at;
|
var where = a.text.is(m.where)? m.where : m.where.at;
|
||||||
console.log("spray", where, m);
|
|
||||||
if(m.how.gun === 3){
|
if(m.how.gun === 3){
|
||||||
console.log("load...");
|
|
||||||
shot.load(m.what, function(g,e){
|
shot.load(m.what, function(g,e){
|
||||||
console.log("got it!");
|
|
||||||
shot.pump.action(g, m, function(){ // receive custom edited copy here and send it down instead.
|
shot.pump.action(g, m, function(){ // receive custom edited copy here and send it down instead.
|
||||||
console.log("reply");
|
|
||||||
if(!opt.src || !opt.src.reply){ return }
|
if(!opt.src || !opt.src.reply){ return }
|
||||||
m.what = a.fns.is(g)? g() : {};
|
m.what = a.fns.is(g)? g() : {};
|
||||||
m.how.gun = -(m.how.gun||3);
|
m.how.gun = -(m.how.gun||3);
|
||||||
@ -187,9 +192,9 @@ module.exports = require('theory')
|
|||||||
store.add(m);
|
store.add(m);
|
||||||
shot.load(where, function(g,e){
|
shot.load(where, function(g,e){
|
||||||
var done = function(){
|
var done = function(){
|
||||||
var u, s, w = m.when || 0, r = {}, cb;
|
var u, s, w = store.when(m) || 0, r = {}, cb;
|
||||||
m.how.gun = -(m.how.gun||1);
|
m.how.gun = -(m.how.gun||1);
|
||||||
g = a.fns.is(g)? g : (a.gun.clip[where] || function(){});
|
g = a.fns.is(g)? g : (a.gun.magazine[where] || function(){});
|
||||||
a.obj(m.what).each(function(v,p){
|
a.obj(m.what).each(function(v,p){
|
||||||
if(g(p,v,w) === u){
|
if(g(p,v,w) === u){
|
||||||
r[p] = 0; // Error code
|
r[p] = 0; // Error code
|
||||||
@ -243,6 +248,18 @@ module.exports = require('theory')
|
|||||||
: !a.obj.empty(a(m,'what.url.query'))? a(m,'what.url.query')
|
: !a.obj.empty(a(m,'what.url.query'))? a(m,'what.url.query')
|
||||||
: false ;
|
: false ;
|
||||||
};
|
};
|
||||||
|
shot.chamber = function(){
|
||||||
|
var index = process.env.gun_chamber;
|
||||||
|
if(!index){
|
||||||
|
index = process.env.gun_chamber = a.text.r(12);
|
||||||
|
}
|
||||||
|
s3(opt.s3.bucket(index)).get(opt.s3.key(index),function(e,r,t){
|
||||||
|
if(!r){
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
shot.gun = a.gun;
|
||||||
return shot;
|
return shot;
|
||||||
}
|
}
|
||||||
},[__dirname+'/gun'])
|
},[__dirname+'/gun'])
|
@ -71,6 +71,13 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="id space">Loading...</div>
|
<div class="id space">Loading...</div>
|
||||||
|
<small><small>
|
||||||
|
Warning: This is an incredibly buggy and unfinished demo, manually refreshing the page will be required.
|
||||||
|
Please rely on the demo video if this breaks.
|
||||||
|
Load <a href="http://gunjs.herokuapp.com/test/shoot.html">heroku</a> and <a href="http://gunjs-amark.rhcloud.com/test/shoot.html">openshift</a>,
|
||||||
|
and make an edit on one (click on some text value, and modify it),
|
||||||
|
then refresh the page on the other and it should be updated (despite these being run on two completely different servers).
|
||||||
|
</small></small>
|
||||||
<ul id="graph"></ul>
|
<ul id="graph"></ul>
|
||||||
<ul id="model">
|
<ul id="model">
|
||||||
<li class="node field" id="model-node">
|
<li class="node field" id="model-node">
|
||||||
|
@ -2,10 +2,11 @@ module.exports=require('theory')
|
|||||||
('shoot',function(a){
|
('shoot',function(a){
|
||||||
if(root.node){
|
if(root.node){
|
||||||
var shot = require('../shots')({src: a.com, batch: 999}).pump(function(g, m, done){
|
var shot = require('../shots')({src: a.com, batch: 999}).pump(function(g, m, done){
|
||||||
|
console.log('>>> pump!');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
return shot.spray(function(g, m, done){
|
return shot.spray(function(g, m, done){
|
||||||
//console.log('>>>>>>>>>> gun');
|
console.log('>>> shoot!');
|
||||||
var gPrime = {};
|
var gPrime = {};
|
||||||
done(gPrime); // allow me to send custom modified filtered version
|
done(gPrime); // allow me to send custom modified filtered version
|
||||||
//console.log(g());
|
//console.log(g());
|
||||||
@ -21,6 +22,44 @@ module.exports=require('theory')
|
|||||||
, $sub = $("#model-obj")
|
, $sub = $("#model-obj")
|
||||||
, $url = $('body>.id')
|
, $url = $('body>.id')
|
||||||
, g;
|
, g;
|
||||||
|
shoot.clone = function($e, $to, id, name){
|
||||||
|
var $r = $e.clone().attr('id',id).appendTo($to);
|
||||||
|
name = name || id;
|
||||||
|
if(name){ $r.attr('name', name) }
|
||||||
|
return $r;
|
||||||
|
}
|
||||||
|
shoot.graph = function(g, url){
|
||||||
|
if(url){ $url.text(url) }
|
||||||
|
a.obj(g).each(function(n,id){
|
||||||
|
var $n = $node.clone().attr('id',id).attr('name',id).appendTo($graph);
|
||||||
|
$('.id', $n).text(id);
|
||||||
|
shoot.field($('.sub', $n), n);
|
||||||
|
});
|
||||||
|
$('.node .sub .sub').slideUp();
|
||||||
|
}
|
||||||
|
shoot.field = function($n, n, p){
|
||||||
|
a.obj(n).each(function(val,key){
|
||||||
|
if(key === '_'){ return }
|
||||||
|
if(a.obj.is(val)){
|
||||||
|
var $val = shoot.clone($sub, $n, key);
|
||||||
|
$('.key', $val).text(key);
|
||||||
|
shoot.field($('.sub', $val), val);
|
||||||
|
} else {
|
||||||
|
var $val = shoot.clone($has, $n, key)
|
||||||
|
, $v = $('.val', $val).text(val);
|
||||||
|
$('.key', $val).text(key);
|
||||||
|
if(a.bi.is(val)){
|
||||||
|
$v.addClass('binary');
|
||||||
|
} else
|
||||||
|
if(a.num.is(val)){
|
||||||
|
$v.addClass('number');
|
||||||
|
} else
|
||||||
|
if(a.text.is(val)){
|
||||||
|
$v.addClass('text');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
shot.load('gunjs.herokuapp.com/tests/package.json',function(graph){
|
shot.load('gunjs.herokuapp.com/tests/package.json',function(graph){
|
||||||
if(!graph){ return $url.text("Something went wrong :( please reload.") }
|
if(!graph){ return $url.text("Something went wrong :( please reload.") }
|
||||||
g = graph;
|
g = graph;
|
||||||
@ -132,11 +171,13 @@ module.exports=require('theory')
|
|||||||
if(shoot.nav.out){
|
if(shoot.nav.out){
|
||||||
return $('.val',on).trigger('blur');
|
return $('.val',on).trigger('blur');
|
||||||
}
|
}
|
||||||
var s = window.getSelection()
|
a.test(function(){
|
||||||
, r = s.getRangeAt(0);
|
var s = window.getSelection()
|
||||||
if(!r.startOffset){
|
, r = s.getRangeAt(0);
|
||||||
shoot.nav.out = true;
|
if(!r.startOffset){
|
||||||
}
|
shoot.nav.out = true;
|
||||||
|
}
|
||||||
|
})();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(on.find('.field:visible').length){
|
if(on.find('.field:visible').length){
|
||||||
@ -153,43 +194,5 @@ module.exports=require('theory')
|
|||||||
$(e.where).closest('.field').addClass('on');
|
$(e.where).closest('.field').addClass('on');
|
||||||
},'.field');
|
},'.field');
|
||||||
});
|
});
|
||||||
shoot.clone = function($e, $to, id, name){
|
|
||||||
var $r = $e.clone().attr('id',id).appendTo($to);
|
|
||||||
name = name || id;
|
|
||||||
if(name){ $r.attr('name', name) }
|
|
||||||
return $r;
|
|
||||||
}
|
|
||||||
shoot.graph = function(g, url){
|
|
||||||
if(url){ $url.text(url) }
|
|
||||||
a.obj(g).each(function(n,id){
|
|
||||||
var $n = $node.clone().attr('id',id).attr('name',id).appendTo($graph);
|
|
||||||
$('.id', $n).text(id);
|
|
||||||
shoot.field($('.sub', $n), n);
|
|
||||||
});
|
|
||||||
$('.node .sub .sub').slideUp();
|
|
||||||
}
|
|
||||||
shoot.field = function($n, n, p){
|
|
||||||
a.obj(n).each(function(val,key){
|
|
||||||
if(key === '_'){ return }
|
|
||||||
if(a.obj.is(val)){
|
|
||||||
var $val = shoot.clone($sub, $n, key);
|
|
||||||
$('.key', $val).text(key);
|
|
||||||
shoot.field($('.sub', $val), val);
|
|
||||||
} else {
|
|
||||||
var $val = shoot.clone($has, $n, key)
|
|
||||||
, $v = $('.val', $val).text(val);
|
|
||||||
$('.key', $val).text(key);
|
|
||||||
if(a.bi.is(val)){
|
|
||||||
$v.addClass('binary');
|
|
||||||
} else
|
|
||||||
if(a.num.is(val)){
|
|
||||||
$v.addClass('number');
|
|
||||||
} else
|
|
||||||
if(a.text.is(val)){
|
|
||||||
$v.addClass('text');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return shot.spray;
|
return shot.spray;
|
||||||
},['../shot']);
|
},['../shot']);
|
Loading…
x
Reference in New Issue
Block a user