UNPKG

4.59 kBJavaScriptView Raw
1
2var Gun = require('./core');
3var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map, obj_empty = obj.empty;
4var num = Gun.num, num_is = num.is;
5var _soul = Gun.val.rel._, _field = '.';
6
7
8;(function(){
9 Gun.chain.key = function(index, cb, opt){
10 if(!index){
11 if(cb){
12 cb.call(this, {err: Gun.log('No key!')});
13 }
14 return this;
15 }
16 var gun = this;
17 if(typeof opt === 'string'){
18 console.log("Please report this as an issue! key.opt.string");
19 return gun;
20 }
21 if(gun === gun._.root){if(cb){cb({err: Gun.log("Can't do that on root instance.")})};return gun}
22 opt = opt || {};
23 opt.key = index;
24 opt.any = cb || function(){};
25 opt.ref = gun.back(-1).get(opt.key);
26 opt.gun = opt.gun || gun;
27 gun.on(key, {as: opt});
28 if(!opt.data){
29 opt.res = Gun.on.stun(opt.ref);
30 }
31 return gun;
32 }
33 function key(at, ev){ var opt = this;
34 ev.off();
35 opt.soul = Gun.node.soul(at.put);
36 if(!opt.soul || opt.key === opt.soul){ return opt.data = {} }
37 opt.data = obj_put({}, keyed._, Gun.node.ify(obj_put({}, opt.soul, Gun.val.rel.ify(opt.soul)), '#'+opt.key+'#'));
38 (opt.res||iffe)(function(){
39 opt.ref.put(opt.data, opt.any, {soul: opt.key, key: opt.key});
40 },opt);
41 if(opt.res){
42 opt.res();
43 }
44 }
45 function iffe(fn,as){fn.call(as||{})}
46 function keyed(f){
47 if(!f || !('#' === f[0] && '#' === f[f.length-1])){ return }
48 var s = f.slice(1,-1);
49 if(!s){ return }
50 return s;
51 }
52 keyed._ = '##';
53 Gun.on('next', function(at){
54 var gun = at.gun;
55 if(gun.back(-1) !== at.back){ return }
56 gun.on('in', pseudo, gun._);
57 gun.on('out', normalize, gun._);
58 });
59 function normalize(at){ var cat = this;
60 if(!at.put){
61 if(at.get){
62 search.call(at.gun? at.gun._ : cat, at);
63 }
64 return;
65 }
66 if(at.opt && at.opt.key){ return }
67 var put = at.put, graph = cat.gun.back(-1)._.graph;
68 Gun.graph.is(put, function(node, soul){
69 if(!Gun.node.is(graph['#'+soul+'#'], function each(rel,id){
70 if(id !== Gun.val.rel.is(rel)){ return }
71 if(rel = graph['#'+id+'#']){
72 Gun.node.is(rel, each); // correct params?
73 return;
74 }
75 Gun.node.soul.ify(rel = put[id] = Gun.obj.copy(node), id);
76 })){ return }
77 Gun.obj.del(put, soul);
78 });
79 }
80 function search(at){ var cat = this;
81 var tmp;
82 if(!Gun.obj.is(tmp = at.get)){ return }
83 if(!Gun.obj.has(tmp, '#')){ return }
84 if((tmp = at.get) && (null === tmp['.'])){
85 tmp['.'] = '##';
86 return;
87 }
88 if((tmp = at.get) && Gun.obj.has(tmp, '.')){
89 if(tmp['#']){
90 cat = cat.root.gun.get(tmp['#'])._;
91 }
92 tmp = at['#'];
93 at['#'] = Gun.on.ask(proxy);
94 }
95 var tried = {};
96 function proxy(ack, ev){
97 var put = ack.put, lex = at.get;
98 if(!cat.pseudo || ack.via){ // TODO: BUG! MEMORY PERF! What about unsubscribing?
99 //ev.off();
100 //ack.via = ack.via || {};
101 return Gun.on.ack(tmp, ack);
102 }
103 if(ack.put){
104 if(!lex['.']){
105 ev.off();
106 return Gun.on.ack(tmp, ack);
107 }
108 if(obj_has(ack.put[lex['#']], lex['.'])){
109 ev.off();
110 return Gun.on.ack(tmp, ack);
111 }
112 }
113 Gun.obj.map(cat.seen, function(ref,id){ // TODO: BUG! In-memory versus future?
114 if(tried[id]){
115 return Gun.on.ack(tmp, ack);
116 }
117 tried[id] = true;
118 ref.on('out', {
119 gun: ref,
120 get: id = {'#': id, '.': at.get['.']},
121 '#': Gun.on.ask(proxy)
122 });
123 });
124 }
125 }
126 function pseudo(at, ev){ var cat = this;
127 // TODO: BUG! Pseudo can't handle plurals!?
128 if(cat.pseudo){
129 //ev.stun();return;
130 if(cat.pseudo === at.put){ return }
131 ev.stun();
132 cat.change = cat.changed || cat.pseudo;
133 cat.on('in', Gun.obj.to(at, {put: cat.put = cat.pseudo}));
134 return;
135 }
136 if(!at.put){ return }
137 var rel = Gun.val.rel.is(at.put[keyed._]);
138 if(!rel){ return }
139 var soul = Gun.node.soul(at.put), resume = ev.stun(resume), root = cat.gun.back(-1), seen = cat.seen = {};
140 cat.pseudo = cat.put = Gun.state.ify(Gun.node.ify({}, soul));
141 root.get(rel).on(each, {change: true});
142 function each(change){
143 Gun.node.is(change, map);
144 }
145 function map(rel, soul){
146 if(soul !== Gun.val.rel.is(rel)){ return }
147 if(seen[soul]){ return }
148 seen[soul] = root.get(soul).on(on, true);
149 }
150 function on(put){
151 if(!put){ return }
152 cat.pseudo = Gun.HAM.union(cat.pseudo, put) || cat.pseudo;
153 cat.change = cat.changed = put;
154 cat.put = cat.pseudo;
155 resume({
156 gun: cat.gun,
157 put: cat.pseudo,
158 get: soul
159 //via: this.at
160 });
161 }
162 }
163 var obj = Gun.obj, obj_has = obj.has;
164}());
165
166
\No newline at end of file