UNPKG

3.4 kBJavaScriptView Raw
1
2var Gun = require('./core');
3Gun.chain.on = function(tag, arg, eas, as){
4 var gun = this, at = gun._, tmp, act, off;
5 if(typeof tag === 'string'){
6 if(!arg){ return at.on(tag) }
7 act = at.on(tag, arg, eas || at, as);
8 if(eas && eas.gun){
9 (eas.subs || (eas.subs = [])).push(act);
10 }
11 off = function() {
12 if (act && act.off) act.off();
13 off.off();
14 };
15 off.off = gun.off.bind(gun) || noop;
16 gun.off = off;
17 return gun;
18 }
19 var opt = arg;
20 opt = (true === opt)? {change: true} : opt || {};
21 opt.ok = tag;
22 opt.last = {};
23 gun.get(ok, opt); // TODO: PERF! Event listener leak!!!????
24 return gun;
25}
26
27function ok(at, ev){ var opt = this;
28 var gun = at.gun, cat = gun._, data = cat.put || at.put, tmp = opt.last, id = cat.id+at.get, tmp;
29 if(u === data){
30 return;
31 }
32 if(data && data[rel._] && (tmp = rel.is(data))){
33 tmp = (cat.root.get(tmp)._);
34 if(u === tmp.put){
35 return;
36 }
37 data = tmp.put;
38 }
39 if(opt.change){ // TODO: BUG? Move above the undef checks?
40 data = at.put;
41 }
42 // DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE
43 if(tmp.put === data && tmp.get === id && !Gun.node.soul(data)){ return }
44 tmp.put = data;
45 tmp.get = id;
46 // DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE
47 cat.last = data;
48 if(opt.as){
49 opt.ok.call(opt.as, at, ev);
50 } else {
51 opt.ok.call(gun, data, at.get, at, ev);
52 }
53}
54
55Gun.chain.val = function(cb, opt){
56 var gun = this, at = gun._, data = at.put;
57 if(0 < at.ack && u !== data && cb){
58 cb.call(gun, data, at.get);
59 return gun;
60 }
61 if(cb){
62 (opt = opt || {}).ok = cb;
63 opt.cat = at;
64 gun.get(val, {as: opt});
65 opt.async = at.stun? 1 : true;
66 } else {
67 Gun.log.once("valonce", "Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
68 var chain = gun.chain();
69 chain._.val = gun.val(function(){
70 chain._.on('in', gun._);
71 });
72 return chain;
73 }
74 return gun;
75}
76
77function val(at, ev, to){
78 var opt = this.as, cat = opt.cat, gun = at.gun, coat = gun._, data = coat.put || at.put, tmp;
79 if(u === data){
80 return;
81 }
82 if(data && data[rel._] && (tmp = rel.is(data))){
83 tmp = (cat.root.get(tmp)._);
84 if(u === tmp.put){
85 return;
86 }
87 data = tmp.put;
88 }
89 if(ev.wait){ clearTimeout(ev.wait) }
90 if(!to && (!(0 < coat.ack) || ((true === opt.async) && 0 !== opt.wait))){
91 ev.wait = setTimeout(function(){
92 val.call({as:opt}, at, ev, ev.wait || 1)
93 }, opt.wait || 99);
94 return;
95 }
96 if(cat.field || cat.soul){
97 if(ev.off()){ return } // if it is already off, don't call again!
98 } else {
99 if((opt.seen = opt.seen || {})[coat.id]){ return }
100 opt.seen[coat.id] = true;
101 }
102 opt.ok.call(at.gun || opt.gun, data, at.get);
103}
104
105Gun.chain.off = function(){
106 var gun = this, at = gun._, tmp;
107 var back = at.back || {}, cat = back._;
108 if(!cat){ return }
109 if(tmp = cat.next){
110 if(tmp[at.get]){
111 obj_del(tmp, at.get);
112 } else {
113 obj_map(tmp, function(path, key){
114 if(gun !== path){ return }
115 obj_del(tmp, key);
116 });
117 }
118 }
119 if((tmp = gun.back(-1)) === back){
120 obj_del(tmp.graph, at.get);
121 }
122 if(at.ons && (tmp = at.ons['@$'])){
123 obj_map(tmp.s, function(ev){
124 ev.off();
125 });
126 }
127 return gun;
128}
129var obj = Gun.obj, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to;
130var rel = Gun.val.rel;
131var empty = {}, u;
132
\No newline at end of file