1 |
|
2 |
|
3 | var On = require('./onto');
|
4 |
|
5 | function 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;
|
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){
|
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 |
|
105 |
|
106 |
|
107 |
|
108 |
|
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 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | on.on('emit', function(ev){
|
142 | var gun = ev.arg.gun;
|
143 | if('in' === ev.tag && gun && !gun._.soul){
|
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 | }
|
150 | module.exports = Chain;
|
151 | |
\ | No newline at end of file |