UNPKG

3.49 kBJavaScriptView Raw
1
2// TODO: Needs to be redone.
3var On = require('./onto');
4
5function Chain(create, opt){
6 opt = opt || {};
7 opt.id = opt.id || '#';
8 opt.rid = opt.rid || '@';
9 opt.uuid = opt.uuid || function(){
10 return (+new Date()) + Math.random();
11 };
12 var on = On;//On.scope();
13
14 on.stun = function(chain){
15 var stun = function(ev){
16 if(stun.off && stun === this.stun){
17 this.stun = null;
18 return false;
19 }
20 if(on.stun.skip){
21 return false;
22 }
23 if(ev){
24 ev.cb = ev.fn;
25 ev.off();
26 res.queue.push(ev);
27 }
28 return true;
29 }, res = stun.res = function(tmp, as){
30 if(stun.off){ return }
31 if(tmp instanceof Function){
32 on.stun.skip = true;
33 tmp.call(as);
34 on.stun.skip = false;
35 return;
36 }
37 stun.off = true;
38 var i = 0, q = res.queue, l = q.length, act;
39 res.queue = [];
40 if(stun === at.stun){
41 at.stun = null;
42 }
43 for(i; i < l; i++){ act = q[i];
44 act.fn = act.cb;
45 act.cb = null;
46 on.stun.skip = true;
47 act.ctx.on(act.tag, act.fn, act);
48 on.stun.skip = false;
49 }
50 }, at = chain._;
51 res.back = at.stun || (at.back||{_:{}})._.stun;
52 if(res.back){
53 res.back.next = stun;
54 }
55 res.queue = [];
56 at.stun = stun;
57 return res;
58 }
59 return on;
60 return;
61 return;
62 return;
63 return;
64 var ask = on.ask = function(cb, as){
65 if(!ask.on){ ask.on = On.scope() }
66 var id = opt.uuid();
67 if(cb){ ask.on(id, cb, as) }
68 return id;
69 }
70 ask._ = opt.id;
71 on.ack = function(at, reply){
72 if(!at || !reply || !ask.on){ return }
73 var id = at[opt.id] || at;
74 if(!ask.ons[id]){ return }
75 ask.on(id, reply);
76 return true;
77 }
78 on.ack._ = opt.rid;
79
80
81 return on;
82 return;
83 return;
84 return;
85 return;
86 on.on('event', function event(act){
87 var last = act.on.last, tmp;
88 if('in' === act.tag && Gun.chain.chain.input !== act.fn){ // TODO: BUG! Gun is not available in this module.
89 if((tmp = act.ctx) && tmp.stun){
90 if(tmp.stun(act)){
91 return;
92 }
93 }
94 }
95 if(!last){ return }
96 if(act.on.map){
97 var map = act.on.map, v;
98 for(var f in map){ v = map[f];
99 if(v){
100 emit(v, act, event);
101 }
102 }
103 /*
104 Gun.obj.map(act.on.map, function(v,f){ // TODO: BUG! Gun is not available in this module.
105 //emit(v[0], act, event, v[1]); // below enables more control
106 //console.log("boooooooo", f,v);
107 emit(v, act, event);
108 //emit(v[1], act, event, v[2]);
109 });
110 */
111 } else {
112 emit(last, act, event);
113 }
114 if(last !== act.on.last){
115 event(act);
116 }
117 });
118 function emit(last, act, event, ev){
119 if(last instanceof Array){
120 act.fn.apply(act.as, last.concat(ev||act));
121 } else {
122 act.fn.call(act.as, last, ev||act);
123 }
124 }
125
126 /*on.on('emit', function(ev){
127 if(ev.on.map){
128 var id = ev.arg.via.gun._.id + ev.arg.get;
129 //
130 //ev.id = ev.id || Gun.text.random(6);
131 //ev.on.map[ev.id] = ev.arg;
132 //ev.proxy = ev.arg[1];
133 //ev.arg = ev.arg[0];
134 // below gives more control.
135 ev.on.map[id] = ev.arg;
136 //ev.proxy = ev.arg[2];
137 }
138 ev.on.last = ev.arg;
139 });*/
140
141 on.on('emit', function(ev){
142 var gun = ev.arg.gun;
143 if('in' === ev.tag && gun && !gun._.soul){ // TODO: BUG! Soul should be available. Currently not using it though, but should enable it (check for side effects if made available).
144 (ev.on.map = ev.on.map || {})[gun._.id || (gun._.id = Math.random())] = ev.arg;
145 }
146 ev.on.last = ev.arg;
147 });
148 return on;
149}
150module.exports = Chain;
151
\No newline at end of file