UNPKG

1.76 kBJavaScriptView Raw
1
2var Gun = require('./root');
3Gun.chain.get = function(key, cb, as){
4 //if(!as || !as.path){ var back = this._.root; } // TODO: CHANGING API! Remove this line!
5 if(typeof key === 'string'){
6 var gun, back = back || this, cat = back._;
7 var next = cat.next || empty, tmp;
8 if(!(gun = next[key])){
9 gun = cache(key, back);
10 }
11 } else
12 if(key instanceof Function){
13 var gun = this, at = gun._;
14 as = cb || {};
15 as.use = key;
16 as.out = as.out || {};
17 as.out.get = as.out.get || {};
18 (at.root._).now = true;
19 at.on('in', use, as);
20 at.on('out', as.out);
21 (at.root._).now = false;
22 return gun;
23 } else
24 if(num_is(key)){
25 return this.get(''+key, cb, as);
26 } else {
27 (as = this.chain())._.err = {err: Gun.log('Invalid get request!', key)}; // CLEAN UP
28 if(cb){ cb.call(as, as._.err) }
29 return as;
30 }
31 if(tmp = cat.stun){ // TODO: Refactor?
32 gun._.stun = gun._.stun || tmp;
33 }
34 if(cb && cb instanceof Function){
35 gun.get(cb, as);
36 }
37 return gun;
38}
39function cache(key, back){
40 var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
41 if(!next){ next = cat.next = {} }
42 next[at.get = key] = gun;
43 if(cat.root === back){ at.soul = key }
44 else if(cat.soul || cat.field){ at.field = key }
45 return gun;
46}
47function use(at){
48 var ev = this, as = ev.as, gun = at.gun, cat = gun._, data = at.put, tmp;
49 if(u === data){
50 data = cat.put;
51 }
52 if((tmp = data) && tmp[rel._] && (tmp = rel.is(tmp))){
53 tmp = (cat.root.get(tmp)._);
54 if(u !== tmp.put){
55 at = obj_to(at, {put: tmp.put});
56 }
57 }
58 as.use(at, at.event || ev);
59 ev.to.next(at);
60}
61var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to;
62var num_is = Gun.num.is;
63var rel = Gun.val.rel, node_ = Gun.node._;
64var empty = {}, u;
65
\No newline at end of file