1 | ;(function(){
|
2 |
|
3 |
|
4 | var root;
|
5 | if(typeof window !== "undefined"){ root = window }
|
6 | if(typeof global !== "undefined"){ root = global }
|
7 | root = root || {};
|
8 | var console = root.console || {log: function(){}};
|
9 | function require(arg){
|
10 | return arg.slice? require[resolve(arg)] : function(mod, path){
|
11 | arg(mod = {exports: {}});
|
12 | require[resolve(path)] = mod.exports;
|
13 | }
|
14 | function resolve(path){
|
15 | return path.split('/').slice(-1).toString().replace('.js','');
|
16 | }
|
17 | }
|
18 | if(typeof module !== "undefined"){ var common = module }
|
19 |
|
20 |
|
21 | ;require(function(module){
|
22 |
|
23 | var Type = {};
|
24 |
|
25 | Type.fns = Type.fn = {is: function(fn){ return (!!fn && 'function' == typeof fn) }}
|
26 | Type.bi = {is: function(b){ return (b instanceof Boolean || typeof b == 'boolean') }}
|
27 | Type.num = {is: function(n){ return !list_is(n) && ((n - parseFloat(n) + 1) >= 0 || Infinity === n || -Infinity === n) }}
|
28 | Type.text = {is: function(t){ return (typeof t == 'string') }}
|
29 | Type.text.ify = function(t){
|
30 | if(Type.text.is(t)){ return t }
|
31 | if(typeof JSON !== "undefined"){ return JSON.stringify(t) }
|
32 | return (t && t.toString)? t.toString() : t;
|
33 | }
|
34 | Type.text.random = function(l, c){
|
35 | var s = '';
|
36 | l = l || 24;
|
37 | c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
|
38 | while(l > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); l-- }
|
39 | return s;
|
40 | }
|
41 | Type.text.match = function(t, o){ var r = false;
|
42 | t = t || '';
|
43 | o = Type.text.is(o)? {'=': o} : o || {};
|
44 | if(Type.obj.has(o,'~')){ t = t.toLowerCase(); o['='] = (o['='] || o['~']).toLowerCase() }
|
45 | if(Type.obj.has(o,'=')){ return t === o['='] }
|
46 | if(Type.obj.has(o,'*')){ if(t.slice(0, o['*'].length) === o['*']){ r = true; t = t.slice(o['*'].length) } else { return false }}
|
47 | if(Type.obj.has(o,'!')){ if(t.slice(-o['!'].length) === o['!']){ r = true } else { return false }}
|
48 | if(Type.obj.has(o,'+')){
|
49 | if(Type.list.map(Type.list.is(o['+'])? o['+'] : [o['+']], function(m){
|
50 | if(t.indexOf(m) >= 0){ r = true } else { return true }
|
51 | })){ return false }
|
52 | }
|
53 | if(Type.obj.has(o,'-')){
|
54 | if(Type.list.map(Type.list.is(o['-'])? o['-'] : [o['-']], function(m){
|
55 | if(t.indexOf(m) < 0){ r = true } else { return true }
|
56 | })){ return false }
|
57 | }
|
58 | if(Type.obj.has(o,'>')){ if(t > o['>']){ r = true } else { return false }}
|
59 | if(Type.obj.has(o,'<')){ if(t < o['<']){ r = true } else { return false }}
|
60 | function fuzzy(t,f){ var n = -1, i = 0, c; for(;c = f[i++];){ if(!~(n = t.indexOf(c, n+1))){ return false }} return true }
|
61 | if(Type.obj.has(o,'?')){ if(fuzzy(t, o['?'])){ r = true } else { return false }}
|
62 | return r;
|
63 | }
|
64 | Type.list = {is: function(l){ return (l instanceof Array) }}
|
65 | Type.list.slit = Array.prototype.slice;
|
66 | Type.list.sort = function(k){
|
67 | return function(A,B){
|
68 | if(!A || !B){ return 0 } A = A[k]; B = B[k];
|
69 | if(A < B){ return -1 }else if(A > B){ return 1 }
|
70 | else { return 0 }
|
71 | }
|
72 | }
|
73 | Type.list.map = function(l, c, _){ return obj_map(l, c, _) }
|
74 | Type.list.index = 1;
|
75 | Type.obj = {is: function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }}
|
76 | Type.obj.put = function(o, f, v){ return (o||{})[f] = v, o }
|
77 | Type.obj.has = function(o, f){ return o && Object.prototype.hasOwnProperty.call(o, f) }
|
78 | Type.obj.del = function(o, k){
|
79 | if(!o){ return }
|
80 | o[k] = null;
|
81 | delete o[k];
|
82 | return o;
|
83 | }
|
84 | Type.obj.as = function(o, f, v, u){ return o[f] = o[f] || (u === v? {} : v) }
|
85 | Type.obj.ify = function(o){
|
86 | if(obj_is(o)){ return o }
|
87 | try{o = JSON.parse(o);
|
88 | }catch(e){o={}};
|
89 | return o;
|
90 | }
|
91 | ;(function(){ var u;
|
92 | function map(v,f){
|
93 | if(obj_has(this,f) && u !== this[f]){ return }
|
94 | this[f] = v;
|
95 | }
|
96 | Type.obj.to = function(from, to){
|
97 | to = to || {};
|
98 | obj_map(from, map, to);
|
99 | return to;
|
100 | }
|
101 | }());
|
102 | Type.obj.copy = function(o){
|
103 | return !o? o : JSON.parse(JSON.stringify(o));
|
104 | }
|
105 | ;(function(){
|
106 | function empty(v,i){ var n = this.n;
|
107 | if(n && (i === n || (obj_is(n) && obj_has(n, i)))){ return }
|
108 | if(i){ return true }
|
109 | }
|
110 | Type.obj.empty = function(o, n){
|
111 | if(!o){ return true }
|
112 | return obj_map(o,empty,{n:n})? false : true;
|
113 | }
|
114 | }());
|
115 | ;(function(){
|
116 | function t(k,v){
|
117 | if(2 === arguments.length){
|
118 | t.r = t.r || {};
|
119 | t.r[k] = v;
|
120 | return;
|
121 | } t.r = t.r || [];
|
122 | t.r.push(k);
|
123 | };
|
124 | var keys = Object.keys;
|
125 | Type.obj.map = function(l, c, _){
|
126 | var u, i = 0, x, r, ll, lle, f = fn_is(c);
|
127 | t.r = null;
|
128 | if(keys && obj_is(l)){
|
129 | ll = Object.keys(l); lle = true;
|
130 | }
|
131 | if(list_is(l) || ll){
|
132 | x = (ll || l).length;
|
133 | for(;i < x; i++){
|
134 | var ii = (i + Type.list.index);
|
135 | if(f){
|
136 | r = lle? c.call(_ || this, l[ll[i]], ll[i], t) : c.call(_ || this, l[i], ii, t);
|
137 | if(r !== u){ return r }
|
138 | } else {
|
139 |
|
140 | if(c === l[lle? ll[i] : i]){ return ll? ll[i] : ii }
|
141 | }
|
142 | }
|
143 | } else {
|
144 | for(i in l){
|
145 | if(f){
|
146 | if(obj_has(l,i)){
|
147 | r = _? c.call(_, l[i], i, t) : c(l[i], i, t);
|
148 | if(r !== u){ return r }
|
149 | }
|
150 | } else {
|
151 |
|
152 | if(c === l[i]){ return i }
|
153 | }
|
154 | }
|
155 | }
|
156 | return f? t.r : Type.list.index? 0 : -1;
|
157 | }
|
158 | }());
|
159 | Type.time = {};
|
160 | Type.time.is = function(t){ return t? t instanceof Date : (+new Date().getTime()) }
|
161 |
|
162 | var fn_is = Type.fn.is;
|
163 | var list_is = Type.list.is;
|
164 | var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
|
165 | module.exports = Type;
|
166 | })(require, './type');
|
167 |
|
168 | ;require(function(module){
|
169 |
|
170 | module.exports = function onto(tag, arg, as){
|
171 | if(!tag){ return {to: onto} }
|
172 | var tag = (this.tag || (this.tag = {}))[tag] ||
|
173 | (this.tag[tag] = {tag: tag, to: onto._ = {
|
174 | next: function(){}
|
175 | }});
|
176 | if(arg instanceof Function){
|
177 | var be = {
|
178 | off: onto.off ||
|
179 | (onto.off = function(){
|
180 | if(this.next === onto._.next){ return !0 }
|
181 | if(this === this.the.last){
|
182 | this.the.last = this.back;
|
183 | }
|
184 | this.to.back = this.back;
|
185 | this.next = onto._.next;
|
186 | this.back.to = this.to;
|
187 | }),
|
188 | to: onto._,
|
189 | next: arg,
|
190 | the: tag,
|
191 | on: this,
|
192 | as: as,
|
193 | };
|
194 | (be.back = tag.last || tag).to = be;
|
195 | return tag.last = be;
|
196 | }
|
197 | (tag = tag.to).next(arg);
|
198 | return tag;
|
199 | };
|
200 | })(require, './onto');
|
201 |
|
202 | ;require(function(module){
|
203 |
|
204 | var On = require('./onto');
|
205 |
|
206 | function Chain(create, opt){
|
207 | opt = opt || {};
|
208 | opt.id = opt.id || '#';
|
209 | opt.rid = opt.rid || '@';
|
210 | opt.uuid = opt.uuid || function(){
|
211 | return (+new Date()) + Math.random();
|
212 | };
|
213 | var on = On;
|
214 |
|
215 | on.stun = function(chain){
|
216 | var stun = function(ev){
|
217 | if(stun.off && stun === this.stun){
|
218 | this.stun = null;
|
219 | return false;
|
220 | }
|
221 | if(on.stun.skip){
|
222 | return false;
|
223 | }
|
224 | if(ev){
|
225 | ev.cb = ev.fn;
|
226 | ev.off();
|
227 | res.queue.push(ev);
|
228 | }
|
229 | return true;
|
230 | }, res = stun.res = function(tmp, as){
|
231 | if(stun.off){ return }
|
232 | if(tmp instanceof Function){
|
233 | on.stun.skip = true;
|
234 | tmp.call(as);
|
235 | on.stun.skip = false;
|
236 | return;
|
237 | }
|
238 | stun.off = true;
|
239 | var i = 0, q = res.queue, l = q.length, act;
|
240 | res.queue = [];
|
241 | if(stun === at.stun){
|
242 | at.stun = null;
|
243 | }
|
244 | for(i; i < l; i++){ act = q[i];
|
245 | act.fn = act.cb;
|
246 | act.cb = null;
|
247 | on.stun.skip = true;
|
248 | act.ctx.on(act.tag, act.fn, act);
|
249 | on.stun.skip = false;
|
250 | }
|
251 | }, at = chain._;
|
252 | res.back = at.stun || (at.back||{_:{}})._.stun;
|
253 | if(res.back){
|
254 | res.back.next = stun;
|
255 | }
|
256 | res.queue = [];
|
257 | at.stun = stun;
|
258 | return res;
|
259 | }
|
260 | return on;
|
261 | return;
|
262 | return;
|
263 | return;
|
264 | return;
|
265 | var ask = on.ask = function(cb, as){
|
266 | if(!ask.on){ ask.on = On.scope() }
|
267 | var id = opt.uuid();
|
268 | if(cb){ ask.on(id, cb, as) }
|
269 | return id;
|
270 | }
|
271 | ask._ = opt.id;
|
272 | on.ack = function(at, reply){
|
273 | if(!at || !reply || !ask.on){ return }
|
274 | var id = at[opt.id] || at;
|
275 | if(!ask.ons[id]){ return }
|
276 | ask.on(id, reply);
|
277 | return true;
|
278 | }
|
279 | on.ack._ = opt.rid;
|
280 |
|
281 |
|
282 | return on;
|
283 | return;
|
284 | return;
|
285 | return;
|
286 | return;
|
287 | on.on('event', function event(act){
|
288 | var last = act.on.last, tmp;
|
289 | if('in' === act.tag && Gun.chain.chain.input !== act.fn){
|
290 | if((tmp = act.ctx) && tmp.stun){
|
291 | if(tmp.stun(act)){
|
292 | return;
|
293 | }
|
294 | }
|
295 | }
|
296 | if(!last){ return }
|
297 | if(act.on.map){
|
298 | var map = act.on.map, v;
|
299 | for(var f in map){ v = map[f];
|
300 | if(v){
|
301 | emit(v, act, event);
|
302 | }
|
303 | }
|
304 | |
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | } else {
|
313 | emit(last, act, event);
|
314 | }
|
315 | if(last !== act.on.last){
|
316 | event(act);
|
317 | }
|
318 | });
|
319 | function emit(last, act, event, ev){
|
320 | if(last instanceof Array){
|
321 | act.fn.apply(act.as, last.concat(ev||act));
|
322 | } else {
|
323 | act.fn.call(act.as, last, ev||act);
|
324 | }
|
325 | }
|
326 |
|
327 | |
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | on.on('emit', function(ev){
|
343 | var gun = ev.arg.gun;
|
344 | if('in' === ev.tag && gun && !gun._.soul){
|
345 | (ev.on.map = ev.on.map || {})[gun._.id || (gun._.id = Math.random())] = ev.arg;
|
346 | }
|
347 | ev.on.last = ev.arg;
|
348 | });
|
349 | return on;
|
350 | }
|
351 | module.exports = Chain;
|
352 | })(require, './onify');
|
353 |
|
354 | ;require(function(module){
|
355 |
|
356 | var Type = require('./type');
|
357 | function s(state, cb, time){
|
358 | s.time = time || Gun.time.is;
|
359 | s.waiting.push({when: state, event: cb || function(){}});
|
360 | if(s.soonest < state){ return }
|
361 | s.set(state);
|
362 | }
|
363 | s.waiting = [];
|
364 | s.soonest = Infinity;
|
365 | s.sort = Type.list.sort('when');
|
366 | s.set = function(future){
|
367 | if(Infinity <= (s.soonest = future)){ return }
|
368 | var now = s.time();
|
369 | future = (future <= now)? 0 : (future - now);
|
370 | clearTimeout(s.id);
|
371 | s.id = setTimeout(s.check, future);
|
372 | }
|
373 | s.each = function(wait, i, map){
|
374 | var ctx = this;
|
375 | if(!wait){ return }
|
376 | if(wait.when <= ctx.now){
|
377 | if(wait.event instanceof Function){
|
378 | setTimeout(function(){ wait.event() },0);
|
379 | }
|
380 | } else {
|
381 | ctx.soonest = (ctx.soonest < wait.when)? ctx.soonest : wait.when;
|
382 | map(wait);
|
383 | }
|
384 | }
|
385 | s.check = function(){
|
386 | var ctx = {now: s.time(), soonest: Infinity};
|
387 | s.waiting.sort(s.sort);
|
388 | s.waiting = Type.list.map(s.waiting, s.each, ctx) || [];
|
389 | s.set(ctx.soonest);
|
390 | }
|
391 | module.exports = s;
|
392 | })(require, './schedule');
|
393 |
|
394 | ;require(function(module){
|
395 |
|
396 | function HAM(machineState, incomingState, currentState, incomingValue, currentValue){
|
397 | if(machineState < incomingState){
|
398 | return {defer: true};
|
399 | }
|
400 | if(incomingState < currentState){
|
401 | return {historical: true};
|
402 |
|
403 | }
|
404 | if(currentState < incomingState){
|
405 | return {converge: true, incoming: true};
|
406 |
|
407 | }
|
408 | if(incomingState === currentState){
|
409 | if(Lexical(incomingValue) === Lexical(currentValue)){
|
410 | return {state: true};
|
411 | }
|
412 | |
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | if(Lexical(incomingValue) < Lexical(currentValue)){
|
421 | return {converge: true, current: true};
|
422 | }
|
423 | if(Lexical(currentValue) < Lexical(incomingValue)){
|
424 | return {converge: true, incoming: true};
|
425 | }
|
426 | }
|
427 | return {err: "you have not properly handled recursion through your data or filtered it as JSON"};
|
428 | }
|
429 | if(typeof JSON === 'undefined'){
|
430 | throw new Error(
|
431 | 'JSON is not included in this browser. Please load it first: ' +
|
432 | 'ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js'
|
433 | );
|
434 | }
|
435 | var Lexical = JSON.stringify, undefined;
|
436 | module.exports = HAM;
|
437 | })(require, './HAM');
|
438 |
|
439 | ;require(function(module){
|
440 | var Type = require('./type');
|
441 | var Val = {};
|
442 | Val.is = function(v){
|
443 | if(v === u){ return false }
|
444 | if(v === null){ return true }
|
445 | if(v === Infinity){ return false }
|
446 | if(text_is(v)
|
447 | || bi_is(v)
|
448 | || num_is(v)){
|
449 | return true;
|
450 | }
|
451 | return Val.rel.is(v) || false;
|
452 | }
|
453 | Val.rel = {_: '#'};
|
454 | ;(function(){
|
455 | Val.rel.is = function(v){
|
456 | if(v && v[rel_] && !v._ && obj_is(v)){
|
457 | var o = {};
|
458 | obj_map(v, map, o);
|
459 | if(o.id){
|
460 | return o.id;
|
461 | }
|
462 | }
|
463 | return false;
|
464 | }
|
465 | function map(s, f){ var o = this;
|
466 | if(o.id){ return o.id = false }
|
467 | if(f == rel_ && text_is(s)){
|
468 | o.id = s;
|
469 | } else {
|
470 | return o.id = false;
|
471 | }
|
472 | }
|
473 | }());
|
474 | Val.rel.ify = function(t){ return obj_put({}, rel_, t) }
|
475 | var rel_ = Val.rel._, u;
|
476 | var bi_is = Type.bi.is;
|
477 | var num_is = Type.num.is;
|
478 | var text_is = Type.text.is;
|
479 | var obj = Type.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
|
480 | module.exports = Val;
|
481 | })(require, './val');
|
482 |
|
483 | ;require(function(module){
|
484 | var Type = require('./type');
|
485 | var Val = require('./val');
|
486 | var Node = {_: '_'};
|
487 | Node.soul = function(n, o){ return (n && n._ && n._[o || soul_]) }
|
488 | Node.soul.ify = function(n, o){
|
489 | o = (typeof o === 'string')? {soul: o} : o || {};
|
490 | n = n || {};
|
491 | n._ = n._ || {};
|
492 | n._[soul_] = o.soul || n._[soul_] || text_random();
|
493 | return n;
|
494 | }
|
495 | ;(function(){
|
496 | Node.is = function(n, cb, as){ var s;
|
497 | if(!obj_is(n)){ return false }
|
498 | if(s = Node.soul(n)){
|
499 | return !obj_map(n, map, {as:as,cb:cb,s:s,n:n});
|
500 | }
|
501 | return false;
|
502 | }
|
503 | function map(v, f){
|
504 | if(f === Node._){ return }
|
505 | if(!Val.is(v)){ return true }
|
506 | if(this.cb){ this.cb.call(this.as, v, f, this.n, this.s) }
|
507 | }
|
508 | }());
|
509 | ;(function(){
|
510 | Node.ify = function(obj, o, as){
|
511 | if(!o){ o = {} }
|
512 | else if(typeof o === 'string'){ o = {soul: o} }
|
513 | else if(o instanceof Function){ o = {map: o} }
|
514 | if(o.map){ o.node = o.map.call(as, obj, u, o.node || {}) }
|
515 | if(o.node = Node.soul.ify(o.node || {}, o)){
|
516 | obj_map(obj, map, {o:o,as:as});
|
517 | }
|
518 | return o.node;
|
519 | }
|
520 | function map(v, f){ var o = this.o, tmp, u;
|
521 | if(o.map){
|
522 | tmp = o.map.call(this.as, v, ''+f, o.node);
|
523 | if(u === tmp){
|
524 | obj_del(o.node, f);
|
525 | } else
|
526 | if(o.node){ o.node[f] = tmp }
|
527 | return;
|
528 | }
|
529 | if(Val.is(v)){
|
530 | o.node[f] = v;
|
531 | }
|
532 | }
|
533 | }());
|
534 | var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_map = obj.map;
|
535 | var text = Type.text, text_random = text.random;
|
536 | var soul_ = Val.rel._;
|
537 | var u;
|
538 | module.exports = Node;
|
539 | })(require, './node');
|
540 |
|
541 | ;require(function(module){
|
542 | var Type = require('./type');
|
543 | var Node = require('./node');
|
544 | function State(){
|
545 | var t = time();
|
546 | if(last < t){
|
547 | return N = 0, last = t;
|
548 | }
|
549 | return last = t + ((N += 1) / D);
|
550 | }
|
551 | var time = Type.time.is, last = -Infinity, N = 0, D = 1000;
|
552 | State._ = '>';
|
553 | State.ify = function(n, f, s, v, soul){
|
554 | if(!n || !n[N_]){
|
555 | if(!soul){
|
556 | return;
|
557 | }
|
558 | n = Node.soul.ify(n, soul);
|
559 | }
|
560 | var tmp = obj_as(n[N_], State._);
|
561 | if(u !== f && f !== N_){
|
562 | if(num_is(s)){
|
563 | tmp[f] = s;
|
564 | }
|
565 | if(u !== v){
|
566 | n[f] = v;
|
567 | }
|
568 | }
|
569 | return n;
|
570 | }
|
571 | State.is = function(n, f, o){
|
572 | var tmp = (f && n && n[N_] && n[N_][State._]) || o;
|
573 | if(!tmp){ return }
|
574 | return num_is(tmp[f])? tmp[f] : -Infinity;
|
575 | }
|
576 | ;(function(){
|
577 | State.map = function(cb, s, as){ var u;
|
578 | var o = obj_is(o = cb || s)? o : null;
|
579 | cb = fn_is(cb = cb || s)? cb : null;
|
580 | if(o && !cb){
|
581 | s = num_is(s)? s : State();
|
582 | o[N_] = o[N_] || {};
|
583 | obj_map(o, map, {o:o,s:s});
|
584 | return o;
|
585 | }
|
586 | as = as || obj_is(s)? s : u;
|
587 | s = num_is(s)? s : State();
|
588 | return function(v, f, o, opt){
|
589 | if(!cb){
|
590 | map.call({o: o, s: s}, v,f);
|
591 | return v;
|
592 | }
|
593 | cb.call(as || this || {}, v, f, o, opt);
|
594 | if(obj_has(o,f) && u === o[f]){ return }
|
595 | map.call({o: o, s: s}, v,f);
|
596 | }
|
597 | }
|
598 | function map(v,f){
|
599 | if(N_ === f){ return }
|
600 | State.ify(this.o, f, this.s) ;
|
601 | }
|
602 | }());
|
603 | var obj = Type.obj, obj_as = obj.as, obj_has = obj.has, obj_is = obj.is, obj_map = obj.map;
|
604 | var num = Type.num, num_is = num.is;
|
605 | var fn = Type.fn, fn_is = fn.is;
|
606 | var N_ = Node._, u;
|
607 | module.exports = State;
|
608 | })(require, './state');
|
609 |
|
610 | ;require(function(module){
|
611 | var Type = require('./type');
|
612 | var Val = require('./val');
|
613 | var Node = require('./node');
|
614 | var Graph = {};
|
615 | ;(function(){
|
616 | Graph.is = function(g, cb, fn, as){
|
617 | if(!g || !obj_is(g) || obj_empty(g)){ return false }
|
618 | return !obj_map(g, map, {cb:cb,fn:fn,as:as});
|
619 | }
|
620 | function map(n, s){
|
621 | if(!n || s !== Node.soul(n) || !Node.is(n, this.fn)){ return true }
|
622 | if(!this.cb){ return }
|
623 | nf.n = n; nf.as = this.as;
|
624 | this.cb.call(nf.as, n, s, nf);
|
625 | }
|
626 | function nf(fn){
|
627 | if(fn){ Node.is(nf.n, fn, nf.as) }
|
628 | }
|
629 | }());
|
630 | ;(function(){
|
631 | Graph.ify = function(obj, env, as){
|
632 | var at = {path: [], obj: obj};
|
633 | if(!env){
|
634 | env = {};
|
635 | } else
|
636 | if(typeof env === 'string'){
|
637 | env = {soul: env};
|
638 | } else
|
639 | if(env instanceof Function){
|
640 | env.map = env;
|
641 | }
|
642 | if(env.soul){
|
643 | at.rel = Val.rel.ify(env.soul);
|
644 | }
|
645 | env.graph = env.graph || {};
|
646 | env.seen = env.seen || [];
|
647 | env.as = env.as || as;
|
648 | node(env, at);
|
649 | env.root = at.node;
|
650 | return env.graph;
|
651 | }
|
652 | function node(env, at){ var tmp;
|
653 | if(tmp = seen(env, at)){ return tmp }
|
654 | at.env = env;
|
655 | at.soul = soul;
|
656 | if(Node.ify(at.obj, map, at)){
|
657 |
|
658 | env.graph[Val.rel.is(at.rel)] = at.node;
|
659 | }
|
660 | return at;
|
661 | }
|
662 | function map(v,f,n){
|
663 | var at = this, env = at.env, is, tmp;
|
664 | if(Node._ === f && obj_has(v,Val.rel._)){
|
665 | return n._;
|
666 | }
|
667 | if(!(is = valid(v,f,n, at,env))){ return }
|
668 | if(!f){
|
669 | at.node = at.node || n || {};
|
670 | if(obj_has(v, Node._)){
|
671 | at.node._ = obj_copy(v._);
|
672 | }
|
673 | at.node = Node.soul.ify(at.node, Val.rel.is(at.rel));
|
674 | at.rel = at.rel || Val.rel.ify(Node.soul(at.node));
|
675 | }
|
676 | if(tmp = env.map){
|
677 | tmp.call(env.as || {}, v,f,n, at);
|
678 | if(obj_has(n,f)){
|
679 | v = n[f];
|
680 | if(u === v){
|
681 | obj_del(n, f);
|
682 | return;
|
683 | }
|
684 | if(!(is = valid(v,f,n, at,env))){ return }
|
685 | }
|
686 | }
|
687 | if(!f){ return at.node }
|
688 | if(true === is){
|
689 | return v;
|
690 | }
|
691 | tmp = node(env, {obj: v, path: at.path.concat(f)});
|
692 | if(!tmp.node){ return }
|
693 | return tmp.rel;
|
694 | }
|
695 | function soul(id){ var at = this;
|
696 | var prev = Val.rel.is(at.rel), graph = at.env.graph;
|
697 | at.rel = at.rel || Val.rel.ify(id);
|
698 | at.rel[Val.rel._] = id;
|
699 | if(at.node && at.node[Node._]){
|
700 | at.node[Node._][Val.rel._] = id;
|
701 | }
|
702 | if(obj_has(graph, prev)){
|
703 | graph[id] = graph[prev];
|
704 | obj_del(graph, prev);
|
705 | }
|
706 | }
|
707 | function valid(v,f,n, at,env){ var tmp;
|
708 | if(Val.is(v)){ return true }
|
709 | if(obj_is(v)){ return 1 }
|
710 | if(tmp = env.invalid){
|
711 | v = tmp.call(env.as || {}, v,f,n);
|
712 | return valid(v,f,n, at,env);
|
713 | }
|
714 | env.err = "Invalid value at '" + at.path.concat(f).join('.') + "'!";
|
715 | }
|
716 | function seen(env, at){
|
717 | var arr = env.seen, i = arr.length, has;
|
718 | while(i--){ has = arr[i];
|
719 | if(at.obj === has.obj){ return has }
|
720 | }
|
721 | arr.push(at);
|
722 | }
|
723 | }());
|
724 | Graph.node = function(node){
|
725 | var soul = Node.soul(node);
|
726 | if(!soul){ return }
|
727 | return obj_put({}, soul, node);
|
728 | }
|
729 | ;(function(){
|
730 | Graph.to = function(graph, root, opt){
|
731 | if(!graph){ return }
|
732 | var obj = {};
|
733 | opt = opt || {seen: {}};
|
734 | obj_map(graph[root], map, {obj:obj, graph: graph, opt: opt});
|
735 | return obj;
|
736 | }
|
737 | function map(v,f){ var tmp, obj;
|
738 | if(Node._ === f){
|
739 | if(obj_empty(v, Val.rel._)){
|
740 | return;
|
741 | }
|
742 | this.obj[f] = obj_copy(v);
|
743 | return;
|
744 | }
|
745 | if(!(tmp = Val.rel.is(v))){
|
746 | this.obj[f] = v;
|
747 | return;
|
748 | }
|
749 | if(obj = this.opt.seen[tmp]){
|
750 | this.obj[f] = obj;
|
751 | return;
|
752 | }
|
753 | this.obj[f] = this.opt.seen[tmp] = Graph.to(this.graph, tmp, this.opt);
|
754 | }
|
755 | }());
|
756 | var fn_is = Type.fn.is;
|
757 | var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_has = obj.has, obj_empty = obj.empty, obj_put = obj.put, obj_map = obj.map, obj_copy = obj.copy;
|
758 | var u;
|
759 | module.exports = Graph;
|
760 | })(require, './graph');
|
761 |
|
762 | ;require(function(module){
|
763 | var Type = require('./type');
|
764 | function Dup(){
|
765 | this.cache = {};
|
766 | }
|
767 | Dup.prototype.track = function(id){
|
768 | this.cache[id] = Type.time.is();
|
769 | if (!this.to) {
|
770 | this.gc();
|
771 | }
|
772 | return id;
|
773 | };
|
774 | Dup.prototype.check = function(id){
|
775 |
|
776 | return Type.obj.has(this.cache, id)? this.track(id) : false;
|
777 | }
|
778 | Dup.prototype.gc = function(){
|
779 | var de = this, now = Type.time.is(), oldest = now, maxAge = 5 * 60 * 1000;
|
780 |
|
781 | Type.obj.map(de.cache, function(time, id){
|
782 | oldest = Math.min(now, time);
|
783 | if ((now - time) < maxAge){ return }
|
784 | Type.obj.del(de.cache, id);
|
785 | });
|
786 | var done = Type.obj.empty(de.cache);
|
787 | if(done){
|
788 | de.to = null;
|
789 | return;
|
790 | }
|
791 | var elapsed = now - oldest;
|
792 | var nextGC = maxAge - elapsed;
|
793 | de.to = setTimeout(function(){ de.gc() }, nextGC);
|
794 | }
|
795 | module.exports = Dup;
|
796 | })(require, './dup');
|
797 |
|
798 | ;require(function(module){
|
799 |
|
800 | function Gun(o){
|
801 | if(o instanceof Gun){ return (this._ = {gun: this}).gun }
|
802 | if(!(this instanceof Gun)){ return new Gun(o) }
|
803 | return Gun.create(this._ = {gun: this, opt: o});
|
804 | }
|
805 |
|
806 | Gun.is = function(gun){ return (gun instanceof Gun) }
|
807 |
|
808 | Gun.version = 0.6;
|
809 |
|
810 | Gun.chain = Gun.prototype;
|
811 | Gun.chain.toJSON = function(){};
|
812 |
|
813 | var Type = require('./type');
|
814 | Type.obj.to(Type, Gun);
|
815 | Gun.HAM = require('./HAM');
|
816 | Gun.val = require('./val');
|
817 | Gun.node = require('./node');
|
818 | Gun.state = require('./state');
|
819 | Gun.graph = require('./graph');
|
820 | Gun.dup = require('./dup');
|
821 | Gun.on = require('./onify')();
|
822 |
|
823 | Gun._ = {
|
824 | node: Gun.node._
|
825 | ,soul: Gun.val.rel._
|
826 | ,state: Gun.state._
|
827 | ,field: '.'
|
828 | ,value: '='
|
829 | }
|
830 |
|
831 | ;(function(){
|
832 | Gun.create = function(at){
|
833 | at.on = at.on || Gun.on;
|
834 | at.root = at.root || at.gun;
|
835 | at.graph = at.graph || {};
|
836 | at.dup = at.dup || new Gun.dup;
|
837 | var gun = at.gun.opt(at.opt);
|
838 | if(!at.once){
|
839 | at.on('in', input, at);
|
840 | at.on('out', output, at);
|
841 | }
|
842 | at.once = 1;
|
843 | return gun;
|
844 | }
|
845 | function output(at){
|
846 |
|
847 | var cat = this.as, gun = cat.gun, tmp;
|
848 |
|
849 | if(at.get && get(at, cat)){ return }
|
850 | cat.on('in', obj_to(at, {gun: cat.gun}));
|
851 | if(at['#']){
|
852 | cat.dup.track(at['#']);
|
853 | }
|
854 | if(!at.gun){
|
855 | at = obj_to(at, {gun: gun});
|
856 | }
|
857 | Gun.on('out', at);
|
858 | }
|
859 | function get(at, cat){
|
860 | var soul = at.get[_soul], node = cat.graph[soul], field = at.get[_field], tmp;
|
861 | var next = cat.next || (cat.next = {}), as = (next[soul] || (next[soul] = cat.gun.get(soul)))._;
|
862 | if(!node){ return }
|
863 | if(field){
|
864 | if(!obj_has(node, field)){ return }
|
865 | tmp = Gun.obj.put(Gun.node.soul.ify({}, soul), field, node[field]);
|
866 | node = Gun.state.ify(tmp, field, Gun.state.is(node, field));
|
867 | }
|
868 | as.on('in', {
|
869 | put: node,
|
870 | get: as.soul,
|
871 | gun: as.gun
|
872 | });
|
873 | if(0 < as.ack){
|
874 | return true;
|
875 | }
|
876 | }
|
877 | function input(at){
|
878 |
|
879 | var ev = this, cat = ev.as;
|
880 | if(!at.gun){ at.gun = cat.gun }
|
881 | if(!at['#'] && at['@']){
|
882 | at['#'] = Gun.text.random();
|
883 | if(Gun.on.ack(at['@'], at)){ return }
|
884 | cat.dup.track(at['#']);
|
885 | cat.on('out', at);
|
886 | return;
|
887 | }
|
888 | if(at['#'] && cat.dup.check(at['#'])){ return }
|
889 | cat.dup.track(at['#']);
|
890 | if(Gun.on.ack(at['@'], at)){ return }
|
891 | if(at.put){
|
892 | Gun.HAM.synth(at, ev, cat.gun);
|
893 | Gun.on('put', at);
|
894 | }
|
895 | if(at.get){ Gun.on('get', at) }
|
896 | Gun.on('out', at);
|
897 | }
|
898 | }());
|
899 |
|
900 | ;(function(){
|
901 | var ask = Gun.on.ask = function(cb, as){
|
902 | var id = Gun.text.random();
|
903 | if(cb){ ask.on(id, cb, as) }
|
904 | return id;
|
905 | }
|
906 | ask.on = Gun.on;
|
907 | Gun.on.ack = function(at, reply){
|
908 | if(!at || !reply || !ask.on){ return }
|
909 | var id = at['#'] || at;
|
910 | if(!ask.tag || !ask.tag[id]){ return }
|
911 | ask.on(id, reply);
|
912 | return true;
|
913 | }
|
914 | }());
|
915 |
|
916 | ;(function(){
|
917 | Gun.chain.opt = function(opt){
|
918 | opt = opt || {};
|
919 | var gun = this, at = gun._, tmp = opt.peers || opt;
|
920 | if(!obj_is(opt)){ opt = {} }
|
921 | if(!obj_is(at.opt)){ at.opt = opt }
|
922 | if(text_is(tmp)){ tmp = [tmp] }
|
923 | if(list_is(tmp)){
|
924 | tmp = obj_map(tmp, function(url, i, map){
|
925 | map(url, {url: url});
|
926 | });
|
927 | if(!obj_is(at.opt.peers)){ at.opt.peers = {}}
|
928 | at.opt.peers = obj_to(tmp, at.opt.peers);
|
929 | }
|
930 | at.opt.peers = at.opt.peers || {};
|
931 | obj_to(opt, at.opt);
|
932 | Gun.on('opt', at);
|
933 | return gun;
|
934 | }
|
935 | }());
|
936 |
|
937 | var text_is = Gun.text.is;
|
938 | var list_is = Gun.list.is;
|
939 | var obj = Gun.obj, obj_is = obj.is, obj_has = obj.has, obj_to = obj.to, obj_map = obj.map;
|
940 | var _soul = Gun._.soul, _field = Gun._.field;
|
941 |
|
942 |
|
943 | console.debug = function(i, s){ return (console.debug.i && i === console.debug.i && console.debug.i++) && (console.log.apply(console, arguments) || s) };
|
944 |
|
945 | Gun.log = function(){ return (!Gun.log.off && console.log.apply(console, arguments)), [].slice.call(arguments).join(' ') }
|
946 | Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) }
|
947 |
|
948 |
|
949 | Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!");
|
950 |
|
951 |
|
952 | if(typeof window !== "undefined"){ window.Gun = Gun }
|
953 | if(typeof common !== "undefined"){ common.exports = Gun }
|
954 | module.exports = Gun;
|
955 | })(require, './root');
|
956 |
|
957 | ;require(function(module){
|
958 | var Gun = require('./root');
|
959 | Gun.chain.back = function(n, opt){ var tmp;
|
960 | if(-1 === n || Infinity === n){
|
961 | return this._.root;
|
962 | } else
|
963 | if(1 === n){
|
964 | return this._.back || this;
|
965 | }
|
966 | var gun = this, at = gun._;
|
967 | if(typeof n === 'string'){
|
968 | n = n.split('.');
|
969 | }
|
970 | if(n instanceof Array){
|
971 | var i = 0, l = n.length, tmp = at;
|
972 | for(i; i < l; i++){
|
973 | tmp = (tmp||empty)[n[i]];
|
974 | }
|
975 | if(u !== tmp){
|
976 | return opt? gun : tmp;
|
977 | } else
|
978 | if((tmp = at.back)){
|
979 | return tmp.back(n, opt);
|
980 | }
|
981 | return;
|
982 | }
|
983 | if(n instanceof Function){
|
984 | var yes, tmp = {back: gun};
|
985 | while((tmp = tmp.back)
|
986 | && (tmp = tmp._)
|
987 | && !(yes = n(tmp, opt))){}
|
988 | return yes;
|
989 | }
|
990 | }
|
991 | var empty = {}, u;
|
992 | })(require, './back');
|
993 |
|
994 | ;require(function(module){
|
995 | var Gun = require('./root');
|
996 | Gun.chain.chain = function(){
|
997 | var at = this._, chain = new this.constructor(this), cat = chain._;
|
998 | cat.root = root = at.root;
|
999 | cat.id = ++root._.once;
|
1000 | cat.back = this;
|
1001 | cat.on = Gun.on;
|
1002 | Gun.on('chain', cat);
|
1003 | cat.on('in', input, cat);
|
1004 | cat.on('out', output, cat);
|
1005 | return chain;
|
1006 | }
|
1007 | function output(at){
|
1008 | var cat = this.as, gun = cat.gun, root = gun.back(-1), put, get, now, tmp;
|
1009 | if(!at.gun){
|
1010 | at.gun = gun;
|
1011 | }
|
1012 | if(get = at.get){
|
1013 | if(!get[_soul]){
|
1014 | if(obj_has(get, _field)){
|
1015 | get = get[_field];
|
1016 | var next = get? (gun.get(get)._) : cat;
|
1017 | // TODO: BUG! Handle plural chains by iterating over them.
|
1018 | if(obj_has(next, 'put')){
|
1019 |
|
1020 |
|
1021 | next.on('in', next);
|
1022 | return;
|
1023 | }
|
1024 | if(obj_has(cat, 'put')){
|
1025 |
|
1026 | var val = cat.put, rel;
|
1027 | if(rel = Gun.node.soul(val)){
|
1028 | val = Gun.val.rel.ify(rel);
|
1029 | }
|
1030 | if(rel = Gun.val.rel.is(val)){
|
1031 | if(!at.gun._){ return }
|
1032 | (at.gun._).on('out', {
|
1033 | get: {'#': rel, '.': get},
|
1034 | '#': Gun.on.ask(Gun.HAM.synth, at.gun),
|
1035 | gun: at.gun
|
1036 | });
|
1037 | return;
|
1038 | }
|
1039 | if(u === val || Gun.val.is(val)){
|
1040 | if(!at.gun._){ return }
|
1041 | (at.gun._).on('in', {
|
1042 | get: get,
|
1043 | gun: at.gun
|
1044 | });
|
1045 | return;
|
1046 | }
|
1047 | } else
|
1048 | if(cat.map){
|
1049 | obj_map(cat.map, function(proxy){
|
1050 | proxy.at.on('in', proxy.at);
|
1051 | });
|
1052 | }
|
1053 | if(cat.soul){
|
1054 | if(!at.gun._){ return }
|
1055 | (at.gun._).on('out', {
|
1056 | get: {'#': cat.soul, '.': get},
|
1057 | '#': Gun.on.ask(Gun.HAM.synth, at.gun),
|
1058 | gun: at.gun
|
1059 | });
|
1060 | return;
|
1061 | }
|
1062 | if(cat.get){
|
1063 | if(!cat.back._){ return }
|
1064 | (cat.back._).on('out', {
|
1065 | get: obj_put({}, _field, cat.get),
|
1066 | gun: gun
|
1067 | });
|
1068 | return;
|
1069 | }
|
1070 | at = obj_to(at, {get: {}});
|
1071 | } else {
|
1072 | if(obj_has(cat, 'put')){
|
1073 |
|
1074 | cat.on('in', cat);
|
1075 | } else
|
1076 | if(cat.map){
|
1077 | obj_map(cat.map, function(proxy){
|
1078 | proxy.at.on('in', proxy.at);
|
1079 | });
|
1080 | }
|
1081 | if(cat.ack){
|
1082 | if(!obj_has(cat, 'put')){
|
1083 | return;
|
1084 | }
|
1085 | }
|
1086 | cat.ack = -1;
|
1087 | if(cat.soul){
|
1088 | cat.on('out', {
|
1089 | get: {'#': cat.soul},
|
1090 | '#': Gun.on.ask(Gun.HAM.synth, cat.gun),
|
1091 | });
|
1092 | return;
|
1093 | }
|
1094 | if(cat.get){
|
1095 | if(!cat.back._){ return }
|
1096 | (cat.back._).on('out', {
|
1097 | get: obj_put({}, _field, cat.get),
|
1098 | gun: cat.gun
|
1099 | });
|
1100 | return;
|
1101 | }
|
1102 | }
|
1103 | }
|
1104 | }
|
1105 | (cat.back._).on('out', at);
|
1106 | }
|
1107 | function input(at){
|
1108 | at = at._ || at;
|
1109 | var ev = this, cat = this.as, gun = at.gun, coat = gun._, change = at.put, back = cat.back._ || empty, rel, tmp;
|
1110 | if(0 > cat.ack && at.via && !Gun.val.rel.is(change)){
|
1111 | cat.ack = 1;
|
1112 | }
|
1113 | if(cat.get && at.get !== cat.get){
|
1114 | at = obj_to(at, {get: cat.get});
|
1115 | }
|
1116 | if(cat.field && coat !== cat){
|
1117 | at = obj_to(at, {gun: cat.gun});
|
1118 | if(coat.ack){
|
1119 | cat.ack = cat.ack || coat.ack;
|
1120 | }
|
1121 | }
|
1122 | if(u === change){
|
1123 | ev.to.next(at);
|
1124 | if(cat.soul){ return }
|
1125 | echo(cat, at, ev);
|
1126 | if(cat.field){
|
1127 | not(cat, at);
|
1128 | }
|
1129 | obj_del(coat.echo, cat.id);
|
1130 | obj_del(cat.map, coat.id);
|
1131 | return;
|
1132 | }
|
1133 | if(cat.soul){
|
1134 | ev.to.next(at);
|
1135 | echo(cat, at, ev);
|
1136 | obj_map(change, map, {at: at, cat: cat});
|
1137 | return;
|
1138 | }
|
1139 | if(!(rel = Gun.val.rel.is(change))){
|
1140 | if(Gun.val.is(change)){
|
1141 | if(cat.field || cat.soul){
|
1142 | not(cat, at);
|
1143 | } else
|
1144 | if(coat.field || coat.soul){
|
1145 | (coat.echo || (coat.echo = {}))[cat.id] = cat;
|
1146 | (cat.map || (cat.map = {}))[coat.id] = cat.map[coat.id] || {at: coat};
|
1147 |
|
1148 | }
|
1149 | ev.to.next(at);
|
1150 | echo(cat, at, ev);
|
1151 | return;
|
1152 | }
|
1153 | if(cat.field && coat !== cat && obj_has(coat, 'put')){
|
1154 | cat.put = coat.put;
|
1155 | };
|
1156 | if((rel = Gun.node.soul(change)) && coat.field){
|
1157 | coat.put = (cat.root.get(rel)._).put;
|
1158 | }
|
1159 | ev.to.next(at);
|
1160 | echo(cat, at, ev);
|
1161 | relate(cat, at, coat, rel);
|
1162 | obj_map(change, map, {at: at, cat: cat});
|
1163 | return;
|
1164 | }
|
1165 | relate(cat, at, coat, rel);
|
1166 | ev.to.next(at);
|
1167 | echo(cat, at, ev);
|
1168 | }
|
1169 | Gun.chain.chain.input = input;
|
1170 | function relate(cat, at, coat, rel){
|
1171 | if(!rel || node_ === cat.get){ return }
|
1172 | var tmp = (cat.root.get(rel)._);
|
1173 | if(cat.field){
|
1174 | coat = tmp;
|
1175 | } else
|
1176 | if(coat.field){
|
1177 | relate(coat, at, coat, rel);
|
1178 | }
|
1179 | if(coat === cat){ return }
|
1180 | (coat.echo || (coat.echo = {}))[cat.id] = cat;
|
1181 | if(cat.field && !(cat.map||empty)[coat.id]){
|
1182 | not(cat, at);
|
1183 | }
|
1184 | tmp = (cat.map || (cat.map = {}))[coat.id] = cat.map[coat.id] || {at: coat};
|
1185 | if(rel !== tmp.rel){
|
1186 | ask(cat, tmp.rel = rel);
|
1187 | }
|
1188 | }
|
1189 | function echo(cat, at, ev){
|
1190 | if(!cat.echo){ return }
|
1191 | if(cat.field){ at = obj_to(at, {event: ev}) }
|
1192 | obj_map(cat.echo, reverb, at);
|
1193 | }
|
1194 | function reverb(cat){
|
1195 | cat.on('in', this);
|
1196 | }
|
1197 | function map(data, key){
|
1198 | var cat = this.cat, next = cat.next || empty, via = this.at, gun, chain, at, tmp;
|
1199 | if(node_ === key && !next[key]){ return }
|
1200 | if(!(gun = next[key])){
|
1201 | return;
|
1202 | }
|
1203 | at = (gun._);
|
1204 |
|
1205 |
|
1206 |
|
1207 | if(at.field){
|
1208 | if(!(data && data[_soul] && Gun.val.rel.is(data) === Gun.node.soul(at.put))){
|
1209 | at.put = data;
|
1210 | }
|
1211 | chain = gun;
|
1212 | } else {
|
1213 | chain = via.gun.get(key);
|
1214 | }
|
1215 | at.on('in', {
|
1216 | put: data,
|
1217 | get: key,
|
1218 | gun: chain,
|
1219 | via: via
|
1220 | });
|
1221 | }
|
1222 | function not(cat, at){
|
1223 | if(!(cat.field || cat.soul)){ return }
|
1224 | var tmp = cat.map;
|
1225 | cat.map = null;
|
1226 | if(null === tmp){ return }
|
1227 | if(u === tmp && cat.put !== u){ return }
|
1228 | obj_map(tmp, function(proxy){
|
1229 | if(!(proxy = proxy.at)){ return }
|
1230 | obj_del(proxy.echo, cat.id);
|
1231 | });
|
1232 | obj_map(cat.next, function(gun, key){
|
1233 | var coat = (gun._);
|
1234 | coat.put = u;
|
1235 | if(coat.ack){
|
1236 | coat.ack = -1;
|
1237 | }
|
1238 | coat.on('in', {
|
1239 | get: key,
|
1240 | gun: gun,
|
1241 | put: u
|
1242 | });
|
1243 | });
|
1244 | }
|
1245 | function ask(cat, soul){
|
1246 | var tmp = (cat.root.get(soul)._);
|
1247 | if(cat.ack){
|
1248 | tmp.ack = tmp.ack || -1;
|
1249 | tmp.on('out', {
|
1250 | get: {'#': soul},
|
1251 | '#': Gun.on.ask(Gun.HAM.synth, tmp.gun),
|
1252 | gun: tmp.gun
|
1253 | });
|
1254 | return;
|
1255 | }
|
1256 | obj_map(cat.next, function(gun, key){
|
1257 | (gun._).on('out', {
|
1258 | get: {'#': soul, '.': key},
|
1259 | '#': Gun.on.ask(Gun.HAM.synth, tmp.gun),
|
1260 | gun: gun
|
1261 | });
|
1262 | });
|
1263 | }
|
1264 | var empty = {}, u;
|
1265 | var obj = Gun.obj, obj_has = obj.has, obj_put = obj.put, obj_del = obj.del, obj_to = obj.to, obj_map = obj.map;
|
1266 | var _soul = Gun._.soul, _field = Gun._.field, node_ = Gun.node._;
|
1267 | })(require, './chain');
|
1268 |
|
1269 | ;require(function(module){
|
1270 | var Gun = require('./root');
|
1271 | Gun.chain.get = function(key, cb, as){
|
1272 |
|
1273 | if(typeof key === 'string'){
|
1274 | var gun, back = back || this, cat = back._;
|
1275 | var next = cat.next || empty, tmp;
|
1276 | if(!(gun = next[key])){
|
1277 | gun = cache(key, back);
|
1278 | }
|
1279 | } else
|
1280 | if(key instanceof Function){
|
1281 | var gun = this, at = gun._;
|
1282 | as = cb || {};
|
1283 | as.use = key;
|
1284 | as.out = as.out || {};
|
1285 | as.out.get = as.out.get || {};
|
1286 | (at.root._).now = true;
|
1287 | at.on('in', use, as);
|
1288 | at.on('out', as.out);
|
1289 | (at.root._).now = false;
|
1290 | return gun;
|
1291 | } else
|
1292 | if(num_is(key)){
|
1293 | return this.get(''+key, cb, as);
|
1294 | } else {
|
1295 | (as = this.chain())._.err = {err: Gun.log('Invalid get request!', key)};
|
1296 | if(cb){ cb.call(as, as._.err) }
|
1297 | return as;
|
1298 | }
|
1299 | if(tmp = cat.stun){
|
1300 | gun._.stun = gun._.stun || tmp;
|
1301 | }
|
1302 | if(cb && cb instanceof Function){
|
1303 | gun.get(cb, as);
|
1304 | }
|
1305 | return gun;
|
1306 | }
|
1307 | function cache(key, back){
|
1308 | var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
|
1309 | if(!next){ next = cat.next = {} }
|
1310 | next[at.get = key] = gun;
|
1311 | if(cat.root === back){ at.soul = key }
|
1312 | else if(cat.soul || cat.field){ at.field = key }
|
1313 | return gun;
|
1314 | }
|
1315 | function use(at){
|
1316 | var ev = this, as = ev.as, gun = at.gun, cat = gun._, data = at.put, tmp;
|
1317 | if(u === data){
|
1318 | data = cat.put;
|
1319 | }
|
1320 | if((tmp = data) && tmp[rel._] && (tmp = rel.is(tmp))){
|
1321 | tmp = (cat.root.get(tmp)._);
|
1322 | if(u !== tmp.put){
|
1323 | at = obj_to(at, {put: tmp.put});
|
1324 | }
|
1325 | }
|
1326 | as.use(at, at.event || ev);
|
1327 | ev.to.next(at);
|
1328 | }
|
1329 | var obj = Gun.obj, obj_has = obj.has, obj_to = Gun.obj.to;
|
1330 | var num_is = Gun.num.is;
|
1331 | var rel = Gun.val.rel, node_ = Gun.node._;
|
1332 | var empty = {}, u;
|
1333 | })(require, './get');
|
1334 |
|
1335 | ;require(function(module){
|
1336 | var Gun = require('./root');
|
1337 | Gun.chain.put = function(data, cb, as){
|
1338 |
|
1339 |
|
1340 |
|
1341 | var gun = this, root = (gun._).root, tmp;
|
1342 | as = as || {};
|
1343 | as.data = data;
|
1344 | as.gun = as.gun || gun;
|
1345 | if(typeof cb === 'string'){
|
1346 | as.soul = cb;
|
1347 | } else {
|
1348 | as.ack = cb;
|
1349 | }
|
1350 | if(as.soul || root === gun){
|
1351 | if(!obj_is(as.data)){
|
1352 | (opt.any||noop).call(opt.as || gun, as.out = {err: Gun.log("No field to put", (typeof as.data), '"' + as.data + '" on!')});
|
1353 | if(as.res){ as.res() }
|
1354 | return gun;
|
1355 | }
|
1356 | as.gun = gun = root.get(as.soul = as.soul || (as.not = Gun.node.soul(as.data) || ((root._).opt.uuid || Gun.text.random)()));
|
1357 | as.ref = as.gun;
|
1358 | ify(as);
|
1359 | return gun;
|
1360 | }
|
1361 | if(Gun.is(data)){
|
1362 | data.get(function(at,ev){ev.off();
|
1363 | var s = Gun.node.soul(at.put);
|
1364 | if(!s){Gun.log("Can only save a node, not a property.");return}
|
1365 | gun.put(Gun.val.rel.ify(s), cb, as);
|
1366 | });
|
1367 | return gun;
|
1368 | }
|
1369 | as.ref = as.ref || (root === (tmp = (gun._).back))? gun : tmp;
|
1370 | as.ref.get('_').get(any, {as: as});
|
1371 | if(!as.out){
|
1372 |
|
1373 | as.res = as.res || Gun.on.stun(as.ref);
|
1374 | as.gun._.stun = as.ref._.stun;
|
1375 | }
|
1376 | return gun;
|
1377 | };
|
1378 |
|
1379 | function ify(as){
|
1380 | as.batch = batch;
|
1381 | var opt = as.opt||{}, env = as.env = Gun.state.map(map, opt.state);
|
1382 | env.soul = as.soul;
|
1383 | as.graph = Gun.graph.ify(as.data, env, as);
|
1384 | if(env.err){
|
1385 | (as.ack||noop).call(opt.as || as.gun, as.out = {err: Gun.log(env.err)});
|
1386 | if(as.res){ as.res() }
|
1387 | return;
|
1388 | }
|
1389 | as.batch();
|
1390 | }
|
1391 |
|
1392 | function batch(){ var as = this;
|
1393 | if(!as.graph || obj_map(as.stun, no)){ return }
|
1394 | (as.res||iife)(function(){
|
1395 | (as.ref._).on('out', {
|
1396 | gun: as.ref, put: as.out = as.env.graph, opt: as.opt,
|
1397 | '#': Gun.on.ask(function(ack){ this.off();
|
1398 | if(!as.ack){ return }
|
1399 | as.ack(ack, this);
|
1400 | }, as.opt)
|
1401 | });
|
1402 | }, as);
|
1403 | if(as.res){ as.res() }
|
1404 | } function no(v,f){ if(v){ return true } }
|
1405 |
|
1406 | function map(v,f,n, at){ var as = this;
|
1407 | if(f || !at.path.length){ return }
|
1408 | (as.res||iife)(function(){
|
1409 | var path = at.path, ref = as.ref, opt = as.opt;
|
1410 | var i = 0, l = path.length;
|
1411 | for(i; i < l; i++){
|
1412 | ref = ref.get(path[i]);
|
1413 | }
|
1414 | if(as.not || Gun.node.soul(at.obj)){
|
1415 | at.soul(Gun.node.soul(at.obj) || ((as.opt||{}).uuid || as.gun.back('opt.uuid') || Gun.text.random)());
|
1416 | return;
|
1417 | }
|
1418 | (as.stun = as.stun || {})[path] = true;
|
1419 | ref.get('_').get(soul, {as: {at: at, as: as}});
|
1420 | }, {as: as, at: at});
|
1421 | }
|
1422 |
|
1423 | function soul(at, ev){ var as = this.as, cat = as.at; as = as.as;
|
1424 |
|
1425 | if(!at.gun || !at.gun._.back){ return }
|
1426 | ev.off();
|
1427 | at = (at.gun._.back._);
|
1428 | cat.soul(Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || ((as.opt||{}).uuid || as.gun.back('opt.uuid') || Gun.text.random)());
|
1429 | as.stun[cat.path] = false;
|
1430 | as.batch();
|
1431 | }
|
1432 |
|
1433 | function any(at, ev){
|
1434 | var as = this.as;
|
1435 | if(!at.gun || !at.gun._){ return }
|
1436 | if(at.err){
|
1437 | console.log("Please report this as an issue! Put.any.err");
|
1438 | return;
|
1439 | }
|
1440 | var cat = (at.gun._.back._), data = cat.put, opt = as.opt||{}, root, tmp;
|
1441 | ev.off();
|
1442 | if(as.ref !== as.gun){
|
1443 | tmp = (as.gun._).get;
|
1444 | if(!tmp){
|
1445 | console.log("Please report this as an issue! Put.no.get");
|
1446 | return;
|
1447 | }
|
1448 | as.data = obj_put({}, tmp, as.data);
|
1449 | tmp = null;
|
1450 | }
|
1451 | if(u === data){
|
1452 | if(!cat.get){ return }
|
1453 | if(!cat.soul){
|
1454 | tmp = cat.gun.back(function(at){
|
1455 | if(at.soul){ return at.soul }
|
1456 | as.data = obj_put({}, at.get, as.data);
|
1457 | });
|
1458 | }
|
1459 | tmp = tmp || cat.get;
|
1460 | cat = (cat.root.get(tmp)._);
|
1461 | as.not = as.soul = tmp;
|
1462 | data = as.data;
|
1463 | }
|
1464 | if(!as.not && !(as.soul = Gun.node.soul(data))){
|
1465 | if(as.path && obj_is(as.data)){
|
1466 | as.soul = (opt.uuid || as.gun.back('opt.uuid') || Gun.text.random)();
|
1467 | } else {
|
1468 |
|
1469 | as.soul = at.soul;
|
1470 | }
|
1471 | }
|
1472 | as.ref.put(as.data, as.soul, as);
|
1473 | }
|
1474 | var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
|
1475 | var u, empty = {}, noop = function(){}, iife = function(fn,as){fn.call(as||empty)};
|
1476 | })(require, './put');
|
1477 |
|
1478 | ;require(function(module){
|
1479 |
|
1480 | var Gun = require('./root');
|
1481 | module.exports = Gun;
|
1482 |
|
1483 | ;(function(){
|
1484 | function meta(v,f){
|
1485 | if(obj_has(Gun.__._, f)){ return }
|
1486 | obj_put(this._, f, v);
|
1487 | }
|
1488 | function map(value, field){
|
1489 | if(Gun._.node === field){ return }
|
1490 | var node = this.node, vertex = this.vertex, union = this.union, machine = this.machine;
|
1491 | var is = state_is(node, field), cs = state_is(vertex, field);
|
1492 | if(u === is || u === cs){ return true }
|
1493 | var iv = value, cv = vertex[field];
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 |
|
1509 |
|
1510 |
|
1511 | if(!val_is(iv) && u !== iv){ return true }
|
1512 | if(!val_is(cv) && u !== cv){ return true }
|
1513 | var HAM = Gun.HAM(machine, is, cs, iv, cv);
|
1514 | if(HAM.err){
|
1515 | console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", HAM.err);
|
1516 | return;
|
1517 | }
|
1518 | if(HAM.state || HAM.historical || HAM.current){
|
1519 |
|
1520 | return;
|
1521 | }
|
1522 | if(HAM.incoming){
|
1523 | union[field] = value;
|
1524 | state_ify(union, field, is);
|
1525 | return;
|
1526 | }
|
1527 | if(HAM.defer){
|
1528 | union[field] = value;
|
1529 | state_ify(union, field, is);
|
1530 |
|
1531 | return;
|
1532 | |
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 |
|
1538 | }
|
1539 | }
|
1540 | Gun.HAM.union = function(vertex, node, opt){
|
1541 | if(!node || !node._){ return }
|
1542 | vertex = vertex || Gun.node.soul.ify({_:{'>':{}}}, Gun.node.soul(node));
|
1543 | if(!vertex || !vertex._){ return }
|
1544 | opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
|
1545 | opt.union = vertex || Gun.obj.copy(vertex);
|
1546 |
|
1547 | opt.vertex = vertex;
|
1548 | opt.node = node;
|
1549 |
|
1550 | if(obj_map(node, map, opt)){
|
1551 | return;
|
1552 | }
|
1553 | return opt.union;
|
1554 | }
|
1555 | Gun.HAM.delta = function(vertex, node, opt){
|
1556 | opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
|
1557 | if(!vertex){ return Gun.obj.copy(node) }
|
1558 | opt.soul = Gun.node.soul(opt.vertex = vertex);
|
1559 | if(!opt.soul){ return }
|
1560 | opt.delta = Gun.node.soul.ify({}, opt.soul);
|
1561 | obj_map(opt.node = node, diff, opt);
|
1562 | return opt.delta;
|
1563 | }
|
1564 | function diff(value, field){ var opt = this;
|
1565 | if(Gun._.node === field){ return }
|
1566 | if(!val_is(value)){ return }
|
1567 | var node = opt.node, vertex = opt.vertex, is = state_is(node, field, true), cs = state_is(vertex, field, true), delta = opt.delta;
|
1568 | var HAM = Gun.HAM(opt.machine, is, cs, value, vertex[field]);
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 | if(HAM.incoming){
|
1577 | delta[field] = value;
|
1578 | state_ify(delta, field, is);
|
1579 | }
|
1580 | }
|
1581 | Gun.HAM.synth = function(at, ev, as){ var gun = this.as || as;
|
1582 | var cat = gun._, root = cat.root._, put = {}, tmp;
|
1583 |
|
1584 |
|
1585 |
|
1586 | if(!at.put){
|
1587 |
|
1588 | if(cat.put !== u){ return }
|
1589 | cat.on('in', {
|
1590 | get: cat.get,
|
1591 | put: cat.put = u,
|
1592 | gun: gun,
|
1593 | via: at
|
1594 | })
|
1595 | return;
|
1596 | }
|
1597 | // TODO: PERF! Have options to determine if this data should even be in memory on this peer!
|
1598 | obj_map(at.put, function(node, soul){ var graph = this.graph;
|
1599 | put[soul] = Gun.HAM.delta(graph[soul], node, {graph: graph});
|
1600 | graph[soul] = Gun.HAM.union(graph[soul], node) || graph[soul];
|
1601 | }, root);
|
1602 |
|
1603 | obj_map(put, function(node, soul){
|
1604 | var root = this, next = root.next || (root.next = {}), gun = next[soul] || (next[soul] = root.gun.get(soul)), coat = (gun._);
|
1605 | coat.put = root.graph[soul];
|
1606 | if(cat.field && !obj_has(node, cat.field)){
|
1607 | (at = obj_to(at, {})).put = u;
|
1608 | Gun.HAM.synth(at, ev, cat.gun);
|
1609 | return;
|
1610 | }
|
1611 | coat.on('in', {
|
1612 | put: node,
|
1613 | get: soul,
|
1614 | gun: gun,
|
1615 | via: at
|
1616 | });
|
1617 | }, root);
|
1618 | }
|
1619 | }());
|
1620 |
|
1621 | var Type = Gun;
|
1622 | var num = Type.num, num_is = num.is;
|
1623 | var obj = Type.obj, obj_has = obj.has, obj_put = obj.put, obj_to = obj.to, obj_map = obj.map;
|
1624 | var node = Gun.node, node_soul = node.soul, node_is = node.is, node_ify = node.ify;
|
1625 | var state = Gun.state, state_is = state.is, state_ify = state.ify;
|
1626 | var val = Gun.val, val_is = val.is, rel_is = val.rel.is;
|
1627 | var u;
|
1628 | })(require, './index');
|
1629 |
|
1630 | ;require(function(module){
|
1631 | var Gun = require('./root');
|
1632 | require('./index');
|
1633 | require('./opt');
|
1634 | require('./chain');
|
1635 | require('./back');
|
1636 | require('./put');
|
1637 | require('./get');
|
1638 | module.exports = Gun;
|
1639 | })(require, './core');
|
1640 |
|
1641 | ;require(function(module){
|
1642 | var Gun = require('./core');
|
1643 | var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map, obj_empty = obj.empty;
|
1644 | var num = Gun.num, num_is = num.is;
|
1645 | var _soul = Gun.val.rel._, _field = '.';
|
1646 |
|
1647 |
|
1648 | ;(function(){
|
1649 | Gun.chain.key = function(index, cb, opt){
|
1650 | if(!index){
|
1651 | if(cb){
|
1652 | cb.call(this, {err: Gun.log('No key!')});
|
1653 | }
|
1654 | return this;
|
1655 | }
|
1656 | var gun = this;
|
1657 | if(typeof opt === 'string'){
|
1658 | console.log("Please report this as an issue! key.opt.string");
|
1659 | return gun;
|
1660 | }
|
1661 | if(gun === gun._.root){if(cb){cb({err: Gun.log("Can't do that on root instance.")})};return gun}
|
1662 | opt = opt || {};
|
1663 | opt.key = index;
|
1664 | opt.any = cb || function(){};
|
1665 | opt.ref = gun.back(-1).get(opt.key);
|
1666 | opt.gun = opt.gun || gun;
|
1667 | gun.on(key, {as: opt});
|
1668 | if(!opt.data){
|
1669 | opt.res = Gun.on.stun(opt.ref);
|
1670 | }
|
1671 | return gun;
|
1672 | }
|
1673 | function key(at, ev){ var opt = this;
|
1674 | ev.off();
|
1675 | opt.soul = Gun.node.soul(at.put);
|
1676 | if(!opt.soul || opt.key === opt.soul){ return opt.data = {} }
|
1677 | opt.data = obj_put({}, keyed._, Gun.node.ify(obj_put({}, opt.soul, Gun.val.rel.ify(opt.soul)), '#'+opt.key+'#'));
|
1678 | (opt.res||iffe)(function(){
|
1679 | opt.ref.put(opt.data, opt.any, {soul: opt.key, key: opt.key});
|
1680 | },opt);
|
1681 | if(opt.res){
|
1682 | opt.res();
|
1683 | }
|
1684 | }
|
1685 | function iffe(fn,as){fn.call(as||{})}
|
1686 | function keyed(f){
|
1687 | if(!f || !('#' === f[0] && '#' === f[f.length-1])){ return }
|
1688 | var s = f.slice(1,-1);
|
1689 | if(!s){ return }
|
1690 | return s;
|
1691 | }
|
1692 | keyed._ = '##';
|
1693 | Gun.on('next', function(at){
|
1694 | var gun = at.gun;
|
1695 | if(gun.back(-1) !== at.back){ return }
|
1696 | gun.on('in', pseudo, gun._);
|
1697 | gun.on('out', normalize, gun._);
|
1698 | });
|
1699 | function normalize(at){ var cat = this;
|
1700 | if(!at.put){
|
1701 | if(at.get){
|
1702 | search.call(at.gun? at.gun._ : cat, at);
|
1703 | }
|
1704 | return;
|
1705 | }
|
1706 | if(at.opt && at.opt.key){ return }
|
1707 | var put = at.put, graph = cat.gun.back(-1)._.graph;
|
1708 | Gun.graph.is(put, function(node, soul){
|
1709 | if(!Gun.node.is(graph['#'+soul+'#'], function each(rel,id){
|
1710 | if(id !== Gun.val.rel.is(rel)){ return }
|
1711 | if(rel = graph['#'+id+'#']){
|
1712 | Gun.node.is(rel, each);
|
1713 | return;
|
1714 | }
|
1715 | Gun.node.soul.ify(rel = put[id] = Gun.obj.copy(node), id);
|
1716 | })){ return }
|
1717 | Gun.obj.del(put, soul);
|
1718 | });
|
1719 | }
|
1720 | function search(at){ var cat = this;
|
1721 | var tmp;
|
1722 | if(!Gun.obj.is(tmp = at.get)){ return }
|
1723 | if(!Gun.obj.has(tmp, '#')){ return }
|
1724 | if((tmp = at.get) && (null === tmp['.'])){
|
1725 | tmp['.'] = '##';
|
1726 | return;
|
1727 | }
|
1728 | if((tmp = at.get) && Gun.obj.has(tmp, '.')){
|
1729 | if(tmp['#']){
|
1730 | cat = cat.root.gun.get(tmp['#'])._;
|
1731 | }
|
1732 | tmp = at['#'];
|
1733 | at['#'] = Gun.on.ask(proxy);
|
1734 | }
|
1735 | var tried = {};
|
1736 | function proxy(ack, ev){
|
1737 | var put = ack.put, lex = at.get;
|
1738 | if(!cat.pseudo || ack.via){
|
1739 |
|
1740 |
|
1741 | return Gun.on.ack(tmp, ack);
|
1742 | }
|
1743 | if(ack.put){
|
1744 | if(!lex['.']){
|
1745 | ev.off();
|
1746 | return Gun.on.ack(tmp, ack);
|
1747 | }
|
1748 | if(obj_has(ack.put[lex['#']], lex['.'])){
|
1749 | ev.off();
|
1750 | return Gun.on.ack(tmp, ack);
|
1751 | }
|
1752 | }
|
1753 | Gun.obj.map(cat.seen, function(ref,id){
|
1754 | if(tried[id]){
|
1755 | return Gun.on.ack(tmp, ack);
|
1756 | }
|
1757 | tried[id] = true;
|
1758 | ref.on('out', {
|
1759 | gun: ref,
|
1760 | get: id = {'#': id, '.': at.get['.']},
|
1761 | '#': Gun.on.ask(proxy)
|
1762 | });
|
1763 | });
|
1764 | }
|
1765 | }
|
1766 | function pseudo(at, ev){ var cat = this;
|
1767 |
|
1768 | if(cat.pseudo){
|
1769 |
|
1770 | if(cat.pseudo === at.put){ return }
|
1771 | ev.stun();
|
1772 | cat.change = cat.changed || cat.pseudo;
|
1773 | cat.on('in', Gun.obj.to(at, {put: cat.put = cat.pseudo}));
|
1774 | return;
|
1775 | }
|
1776 | if(!at.put){ return }
|
1777 | var rel = Gun.val.rel.is(at.put[keyed._]);
|
1778 | if(!rel){ return }
|
1779 | var soul = Gun.node.soul(at.put), resume = ev.stun(resume), root = cat.gun.back(-1), seen = cat.seen = {};
|
1780 | cat.pseudo = cat.put = Gun.state.ify(Gun.node.ify({}, soul));
|
1781 | root.get(rel).on(each, {change: true});
|
1782 | function each(change){
|
1783 | Gun.node.is(change, map);
|
1784 | }
|
1785 | function map(rel, soul){
|
1786 | if(soul !== Gun.val.rel.is(rel)){ return }
|
1787 | if(seen[soul]){ return }
|
1788 | seen[soul] = root.get(soul).on(on, true);
|
1789 | }
|
1790 | function on(put){
|
1791 | if(!put){ return }
|
1792 | cat.pseudo = Gun.HAM.union(cat.pseudo, put) || cat.pseudo;
|
1793 | cat.change = cat.changed = put;
|
1794 | cat.put = cat.pseudo;
|
1795 | resume({
|
1796 | gun: cat.gun,
|
1797 | put: cat.pseudo,
|
1798 | get: soul
|
1799 | //via: this.at
|
1800 | });
|
1801 | }
|
1802 | }
|
1803 | var obj = Gun.obj, obj_has = obj.has;
|
1804 | }());
|
1805 |
|
1806 | })(require, './key');
|
1807 |
|
1808 | ;require(function(module){
|
1809 | var Gun = require('./core');
|
1810 | Gun.chain.path = function(field, cb, opt){
|
1811 | var back = this, gun = back, tmp;
|
1812 | opt = opt || {}; opt.path = true;
|
1813 | if(gun === gun._.root){if(cb){cb({err: Gun.log("Can't do that on root instance.")})}return gun}
|
1814 | if(typeof field === 'string'){
|
1815 | tmp = field.split(opt.split || '.');
|
1816 | if(1 === tmp.length){
|
1817 | gun = back.get(field, cb, opt);
|
1818 | gun._.opt = opt;
|
1819 | return gun;
|
1820 | }
|
1821 | field = tmp;
|
1822 | }
|
1823 | if(field instanceof Array){
|
1824 | if(field.length > 1){
|
1825 | gun = back;
|
1826 | var i = 0, l = field.length;
|
1827 | for(i; i < l; i++){
|
1828 | gun = gun.get(field[i], (i+1 === l)? cb : null, opt);
|
1829 | }
|
1830 |
|
1831 | } else {
|
1832 | gun = back.get(field[0], cb, opt);
|
1833 | }
|
1834 | gun._.opt = opt;
|
1835 | return gun;
|
1836 | }
|
1837 | if(!field && 0 != field){
|
1838 | return back;
|
1839 | }
|
1840 | gun = back.get(''+field, cb, opt);
|
1841 | gun._.opt = opt;
|
1842 | return gun;
|
1843 | }
|
1844 | })(require, './path');
|
1845 |
|
1846 | ;require(function(module){
|
1847 | var Gun = require('./core');
|
1848 | Gun.chain.on = function(tag, arg, eas, as){
|
1849 | var gun = this, at = gun._, tmp, act, off;
|
1850 | if(typeof tag === 'string'){
|
1851 | if(!arg){ return at.on(tag) }
|
1852 | act = at.on(tag, arg, eas || at, as);
|
1853 | if(eas && eas.gun){
|
1854 | (eas.subs || (eas.subs = [])).push(act);
|
1855 | }
|
1856 | off = function() {
|
1857 | if (act && act.off) act.off();
|
1858 | off.off();
|
1859 | };
|
1860 | off.off = gun.off.bind(gun) || noop;
|
1861 | gun.off = off;
|
1862 | return gun;
|
1863 | }
|
1864 | var opt = arg;
|
1865 | opt = (true === opt)? {change: true} : opt || {};
|
1866 | opt.ok = tag;
|
1867 | opt.last = {};
|
1868 | gun.get(ok, opt);
|
1869 | return gun;
|
1870 | }
|
1871 |
|
1872 | function ok(at, ev){ var opt = this;
|
1873 | var gun = at.gun, cat = gun._, data = cat.put || at.put, tmp = opt.last, id = cat.id+at.get, tmp;
|
1874 | if(u === data){
|
1875 | return;
|
1876 | }
|
1877 | if(data && data[rel._] && (tmp = rel.is(data))){
|
1878 | tmp = (cat.root.get(tmp)._);
|
1879 | if(u === tmp.put){
|
1880 | return;
|
1881 | }
|
1882 | data = tmp.put;
|
1883 | }
|
1884 | if(opt.change){
|
1885 | data = at.put;
|
1886 | }
|
1887 |
|
1888 | if(tmp.put === data && tmp.get === id && !Gun.node.soul(data)){ return }
|
1889 | tmp.put = data;
|
1890 | tmp.get = id;
|
1891 |
|
1892 | cat.last = data;
|
1893 | if(opt.as){
|
1894 | opt.ok.call(opt.as, at, ev);
|
1895 | } else {
|
1896 | opt.ok.call(gun, data, at.get, at, ev);
|
1897 | }
|
1898 | }
|
1899 |
|
1900 | Gun.chain.val = function(cb, opt){
|
1901 | var gun = this, at = gun._, data = at.put;
|
1902 | if(0 < at.ack && u !== data && cb){
|
1903 | cb.call(gun, data, at.get);
|
1904 | return gun;
|
1905 | }
|
1906 | if(cb){
|
1907 | (opt = opt || {}).ok = cb;
|
1908 | opt.cat = at;
|
1909 | gun.get(val, {as: opt});
|
1910 | opt.async = at.stun? 1 : true;
|
1911 | } else {
|
1912 | 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.");
|
1913 | var chain = gun.chain();
|
1914 | chain._.val = gun.val(function(){
|
1915 | chain._.on('in', gun._);
|
1916 | });
|
1917 | return chain;
|
1918 | }
|
1919 | return gun;
|
1920 | }
|
1921 |
|
1922 | function val(at, ev, to){
|
1923 | var opt = this.as, cat = opt.cat, gun = at.gun, coat = gun._, data = coat.put || at.put, tmp;
|
1924 | if(u === data){
|
1925 | return;
|
1926 | }
|
1927 | if(data && data[rel._] && (tmp = rel.is(data))){
|
1928 | tmp = (cat.root.get(tmp)._);
|
1929 | if(u === tmp.put){
|
1930 | return;
|
1931 | }
|
1932 | data = tmp.put;
|
1933 | }
|
1934 | if(ev.wait){ clearTimeout(ev.wait) }
|
1935 | if(!to && (!(0 < coat.ack) || ((true === opt.async) && 0 !== opt.wait))){
|
1936 | ev.wait = setTimeout(function(){
|
1937 | val.call({as:opt}, at, ev, ev.wait || 1)
|
1938 | }, opt.wait || 99);
|
1939 | return;
|
1940 | }
|
1941 | if(cat.field || cat.soul){
|
1942 | if(ev.off()){ return }
|
1943 | } else {
|
1944 | if((opt.seen = opt.seen || {})[coat.id]){ return }
|
1945 | opt.seen[coat.id] = true;
|
1946 | }
|
1947 | opt.ok.call(at.gun || opt.gun, data, at.get);
|
1948 | }
|
1949 |
|
1950 | Gun.chain.off = function(){
|
1951 | var gun = this, at = gun._, tmp;
|
1952 | var back = at.back || {}, cat = back._;
|
1953 | if(!cat){ return }
|
1954 | if(tmp = cat.next){
|
1955 | if(tmp[at.get]){
|
1956 | obj_del(tmp, at.get);
|
1957 | } else {
|
1958 | obj_map(tmp, function(path, key){
|
1959 | if(gun !== path){ return }
|
1960 | obj_del(tmp, key);
|
1961 | });
|
1962 | }
|
1963 | }
|
1964 | if((tmp = gun.back(-1)) === back){
|
1965 | obj_del(tmp.graph, at.get);
|
1966 | }
|
1967 | if(at.ons && (tmp = at.ons['@$'])){
|
1968 | obj_map(tmp.s, function(ev){
|
1969 | ev.off();
|
1970 | });
|
1971 | }
|
1972 | return gun;
|
1973 | }
|
1974 | var obj = Gun.obj, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to;
|
1975 | var rel = Gun.val.rel;
|
1976 | var empty = {}, u;
|
1977 | })(require, './on');
|
1978 |
|
1979 | ;require(function(module){
|
1980 | var Gun = require('./core'), u;
|
1981 | Gun.chain.not = function(cb, opt, t){
|
1982 | return this.get(ought, {not: cb});
|
1983 | }
|
1984 | function ought(at, ev){ ev.off();
|
1985 | if(at.err || (u !== at.put)){ return }
|
1986 | if(!this.not){ return }
|
1987 | this.not.call(at.gun, at.get, function(){ console.log("Please report this bug on https://gitter.im/amark/gun and in the issues."); need.to.implement; });
|
1988 | }
|
1989 | })(require, './not');
|
1990 |
|
1991 | ;require(function(module){
|
1992 | var Gun = require('./core');
|
1993 | Gun.chain.map = function(cb, opt, t){
|
1994 | var gun = this, cat = gun._, chain;
|
1995 | if(!cb){
|
1996 | if(chain = cat.fields){ return chain }
|
1997 | chain = cat.fields = gun.chain();
|
1998 | chain._.val = gun.back('val');
|
1999 | gun.on('in', map, chain._);
|
2000 | return chain;
|
2001 | }
|
2002 | Gun.log.once("mapfn", "Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
|
2003 | chain = gun.chain();
|
2004 | gun.map().on(function(data, key, at, ev){
|
2005 | var next = (cb||noop).call(this, data, key, at, ev);
|
2006 | if(u === next){ return }
|
2007 | if(Gun.is(next)){
|
2008 | chain._.on('in', next._);
|
2009 | return;
|
2010 | }
|
2011 | chain._.on('in', {get: key, put: next, gun: chain});
|
2012 | });
|
2013 | return chain;
|
2014 | }
|
2015 | function map(at){
|
2016 | if(!at.put || Gun.val.is(at.put)){ return }
|
2017 | if(this.as.val){ this.off() }
|
2018 | obj_map(at.put, each, {cat: this.as, gun: at.gun});
|
2019 | this.to.next(at);
|
2020 | }
|
2021 | function each(v,f){
|
2022 | if(n_ === f){ return }
|
2023 | var cat = this.cat, gun = this.gun.get(f), at = (gun._);
|
2024 | (at.echo || (at.echo = {}))[cat.id] = cat;
|
2025 | }
|
2026 | var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u;
|
2027 | })(require, './map');
|
2028 |
|
2029 | ;require(function(module){
|
2030 | var Gun = require('./core');
|
2031 | Gun.chain.init = function(){
|
2032 | (this._.opt = this._.opt || {}).init = true;
|
2033 | return this.back(-1).put(Gun.node.ify({}, this._.get), null, this._.get);
|
2034 | }
|
2035 | })(require, './init');
|
2036 |
|
2037 | ;require(function(module){
|
2038 | var Gun = require('./core');
|
2039 | Gun.chain.set = function(item, cb, opt){
|
2040 | var gun = this, soul;
|
2041 | cb = cb || function(){};
|
2042 | if(soul = Gun.node.soul(item)){ return gun.set(gun.back(-1).get(soul), cb, opt) }
|
2043 | if(Gun.obj.is(item) && !Gun.is(item)){ return gun.set(gun._.root.put(item), cb, opt) }
|
2044 | return item.get('_').get(function(at, ev){
|
2045 | if(!at.gun || !at.gun._.back);
|
2046 | ev.off();
|
2047 | at = (at.gun._.back._);
|
2048 | var put = {}, node = at.put, soul = Gun.node.soul(node);
|
2049 | if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + node + '"!')}) }
|
2050 | gun.put(Gun.obj.put(put, soul, Gun.val.rel.ify(soul)), cb, opt);
|
2051 | },{wait:0});
|
2052 | }
|
2053 | })(require, './set');
|
2054 |
|
2055 | ;require(function(module){
|
2056 | if(typeof Gun === 'undefined'){ return }
|
2057 |
|
2058 | var root, noop = function(){}, u;
|
2059 | if(typeof window !== 'undefined'){ root = window }
|
2060 | var store = root.localStorage || {setItem: noop, removeItem: noop, getItem: noop};
|
2061 |
|
2062 | Gun.on('put', function(at){ var err, id, opt, root = at.gun._.root;
|
2063 | this.to.next(at);
|
2064 | (opt = {}).prefix = (at.opt || opt).prefix || at.gun.back('opt.prefix') || 'gun/';
|
2065 | Gun.graph.is(at.put, function(node, soul, map){
|
2066 | var keys = Gun.obj.ify(store.getItem(opt.prefix + soul+'_')||{});
|
2067 | map(function(val, key){
|
2068 | keys[key] = 1;
|
2069 | var state = Gun.state.is(node, key);
|
2070 |
|
2071 | try{store.setItem(opt.prefix + soul+key, JSON.stringify([val,state]));
|
2072 | }catch(e){ err = e || "localStorage failure" }
|
2073 | });
|
2074 | try{store.setItem(opt.prefix + soul+'_', JSON.stringify(keys));
|
2075 | }catch(e){ err = e || "localStorage failure" }
|
2076 | });
|
2077 | if(Gun.obj.empty(at.gun.back('opt.peers'))){
|
2078 | Gun.on.ack(at, {err: err, ok: 0});
|
2079 | }
|
2080 | });
|
2081 | Gun.on('get', function(at){
|
2082 | this.to.next(at);
|
2083 | var gun = at.gun, lex = at.get, soul, data, opt, u;
|
2084 |
|
2085 | (opt = at.opt || {}).prefix = opt.prefix || at.gun.back('opt.prefix') || 'gun/';
|
2086 | if(!lex || !(soul = lex[Gun._.soul])){ return }
|
2087 | var field = lex['.'];
|
2088 | if(field){
|
2089 | if(data = Gun.obj.ify(store.getItem(opt.prefix + soul+field)||null)||u){
|
2090 | data = Gun.state.ify(u, field, data[1], data[0], soul);
|
2091 | }
|
2092 | } else {
|
2093 | Gun.obj.map(Gun.obj.ify(store.getItem(opt.prefix + soul+'_')), function(v,field){
|
2094 | v = Gun.obj.ify(store.getItem(opt.prefix + soul+field)||{});
|
2095 | data = Gun.state.ify(data, field, v[1], v[0], soul);
|
2096 | });
|
2097 | }
|
2098 | gun.back(-1).on('in', {'@': at['#'], put: Gun.graph.node(data)});
|
2099 |
|
2100 | });
|
2101 | })(require, './adapters/localStorage');
|
2102 |
|
2103 | ;require(function(module){
|
2104 | var Gun = require('./core');
|
2105 |
|
2106 |
|
2107 | if (typeof JSON === 'undefined') {
|
2108 | throw new Error(
|
2109 | 'Gun depends on JSON. Please load it first:\n' +
|
2110 | 'ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js'
|
2111 | );
|
2112 | }
|
2113 |
|
2114 | function Client (url, options, wscOptions ) {
|
2115 | if (!(this instanceof Client)) {
|
2116 | return new Client(url, options, wscOptions);
|
2117 | }
|
2118 |
|
2119 | this.url = Client.formatURL(url);
|
2120 | this.socket = null;
|
2121 | this.queue = [];
|
2122 | this.sid = Gun.text.random(10);
|
2123 |
|
2124 | this.on = Gun.on;
|
2125 |
|
2126 | this.options = options || {};
|
2127 | this.options.wsc = wscOptions;
|
2128 | this.resetBackoff();
|
2129 | }
|
2130 |
|
2131 | Client.prototype = {
|
2132 | constructor: Client,
|
2133 |
|
2134 | drainQueue: function () {
|
2135 | var queue = this.queue;
|
2136 | var client = this;
|
2137 |
|
2138 |
|
2139 | this.queue = [];
|
2140 |
|
2141 |
|
2142 | queue.forEach(function (msg) {
|
2143 | client.send(msg);
|
2144 | });
|
2145 |
|
2146 | return queue.length;
|
2147 | },
|
2148 |
|
2149 | connect: function () {
|
2150 | var client = this;
|
2151 | var socket = new Client.WebSocket(this.url, this.options.wsc.protocols, this.options.wsc );
|
2152 | this.socket = socket;
|
2153 |
|
2154 |
|
2155 | socket.addEventListener('message', function (msg) {
|
2156 | client.on('message', msg);
|
2157 | });
|
2158 |
|
2159 |
|
2160 | socket.addEventListener('close', function () {
|
2161 | client.scheduleReconnect();
|
2162 | });
|
2163 |
|
2164 |
|
2165 | this.ready(function () {
|
2166 | client.drainQueue();
|
2167 | });
|
2168 |
|
2169 | return socket;
|
2170 | },
|
2171 |
|
2172 | resetBackoff: function () {
|
2173 | var backoff = this.options;
|
2174 |
|
2175 | this.backoff = {
|
2176 | time: backoff.time || 100,
|
2177 | max: backoff.max || 2000,
|
2178 | factor: backoff.factor || 2
|
2179 | };
|
2180 |
|
2181 | return this.backoff;
|
2182 | },
|
2183 |
|
2184 | nextBackoff: function () {
|
2185 | var backoff = this.backoff;
|
2186 | var next = backoff.time * backoff.factor;
|
2187 | var max = backoff.max;
|
2188 |
|
2189 | if (next > max) {
|
2190 | next = max;
|
2191 | }
|
2192 |
|
2193 | return (backoff.time = next);
|
2194 | },
|
2195 |
|
2196 |
|
2197 | scheduleReconnect: function () {
|
2198 | var client = this;
|
2199 | var time = this.backoff.time;
|
2200 | this.nextBackoff();
|
2201 |
|
2202 | setTimeout(function () {
|
2203 | client.connect();
|
2204 |
|
2205 | client.ready(function () {
|
2206 | client.resetBackoff();
|
2207 | });
|
2208 | }, time);
|
2209 | },
|
2210 |
|
2211 | isClosed: function () {
|
2212 | var socket = this.socket;
|
2213 |
|
2214 | if (!socket) {
|
2215 | return true;
|
2216 | }
|
2217 |
|
2218 | var state = socket.readyState;
|
2219 |
|
2220 | if (state === socket.CLOSING || state === socket.CLOSED) {
|
2221 | return true;
|
2222 | }
|
2223 |
|
2224 | return false;
|
2225 | },
|
2226 |
|
2227 | ready: function (callback) {
|
2228 | var socket = this.socket;
|
2229 | var state = socket.readyState;
|
2230 |
|
2231 | if (state === socket.OPEN) {
|
2232 | callback();
|
2233 | return;
|
2234 | }
|
2235 |
|
2236 | if (state === socket.CONNECTING) {
|
2237 | socket.addEventListener('open', callback);
|
2238 | }
|
2239 | },
|
2240 |
|
2241 | send: function (msg) {
|
2242 | if (this.isClosed()) {
|
2243 | this.queue.push(msg);
|
2244 |
|
2245 |
|
2246 | this.connect();
|
2247 | return false;
|
2248 | }
|
2249 |
|
2250 | var socket = this.socket;
|
2251 |
|
2252 |
|
2253 | this.ready(function () {
|
2254 | socket.send(msg);
|
2255 | });
|
2256 |
|
2257 | return true;
|
2258 | }
|
2259 | };
|
2260 |
|
2261 | if (typeof window !== 'undefined') {
|
2262 | Client.WebSocket = window.WebSocket ||
|
2263 | window.webkitWebSocket ||
|
2264 | window.mozWebSocket ||
|
2265 | null;
|
2266 | }
|
2267 |
|
2268 | Client.isSupported = !!Client.WebSocket;
|
2269 |
|
2270 | if(!Client.isSupported){ return }
|
2271 |
|
2272 |
|
2273 | Client.formatURL = function (url) {
|
2274 | return url.replace(/^http/, 'ws');
|
2275 | };
|
2276 |
|
2277 |
|
2278 | Client.broadcast = function (urls, msg) {
|
2279 | var pool = Client.pool;
|
2280 | msg.headers = msg.headers || {};
|
2281 |
|
2282 | Gun.obj.map(urls, function (options, addr) {
|
2283 |
|
2284 | var url = Client.formatURL(addr);
|
2285 |
|
2286 | var peer = pool[url];
|
2287 |
|
2288 | var envelope = {
|
2289 | headers: Gun.obj.to(msg.headers, {
|
2290 | 'gun-sid': peer.sid
|
2291 | }),
|
2292 | body: msg.body
|
2293 | };
|
2294 |
|
2295 | var serialized = Gun.text.ify(envelope);
|
2296 |
|
2297 | peer.send(serialized);
|
2298 | });
|
2299 |
|
2300 | };
|
2301 |
|
2302 |
|
2303 | Client.pool = {};
|
2304 |
|
2305 |
|
2306 | if (typeof window !== 'undefined') {
|
2307 | window.addEventListener('unload', function () {
|
2308 | Gun.obj.map(Client.pool, function (client) {
|
2309 | if (client.isClosed()) {
|
2310 | return;
|
2311 | }
|
2312 |
|
2313 | client.socket.close();
|
2314 | });
|
2315 | });
|
2316 | }
|
2317 |
|
2318 |
|
2319 |
|
2320 | Gun.on('opt', function (ctx) {
|
2321 | this.to.next(ctx);
|
2322 |
|
2323 | var gun = ctx.gun;
|
2324 | var peers = gun.back('opt.peers') || {};
|
2325 |
|
2326 | Gun.obj.map(peers, function (options, addr) {
|
2327 | var url = Client.formatURL(addr);
|
2328 |
|
2329 |
|
2330 | if (Client.pool.hasOwnProperty(url)) {
|
2331 | return;
|
2332 | }
|
2333 |
|
2334 | var client = new Client(url, options.backoff, gun.back('opt.wsc') || {protocols:null});
|
2335 |
|
2336 |
|
2337 | Client.pool[url] = client;
|
2338 |
|
2339 |
|
2340 | client.on('message', function (msg) {
|
2341 | var data;
|
2342 |
|
2343 | try {
|
2344 | data = Gun.obj.ify(msg.data);
|
2345 | } catch (err) {
|
2346 |
|
2347 | return;
|
2348 | }
|
2349 |
|
2350 | if (!data || !data.body) {
|
2351 | return;
|
2352 | }
|
2353 |
|
2354 | gun.on('in', data.body);
|
2355 | });
|
2356 | });
|
2357 | });
|
2358 |
|
2359 | function request (peers, ctx) {
|
2360 | if (Client.isSupported) {
|
2361 | Client.broadcast(peers, ctx);
|
2362 | }
|
2363 | }
|
2364 |
|
2365 |
|
2366 | Gun.on('out', function (ctx) {
|
2367 | this.to.next(ctx);
|
2368 | var gun = ctx.gun;
|
2369 | var peers = gun.back('opt.peers') || {};
|
2370 | var headers = gun.back('opt.headers') || {};
|
2371 |
|
2372 | if (Gun.obj.empty(peers)) {
|
2373 | return;
|
2374 | }
|
2375 |
|
2376 | request(peers, {body: ctx, headers: headers});
|
2377 | });
|
2378 |
|
2379 | request.jsonp = function (opt, cb) {
|
2380 | request.jsonp.ify(opt, function (url) {
|
2381 | if (!url) {
|
2382 | return;
|
2383 | }
|
2384 | request.jsonp.send(url, function (err, reply) {
|
2385 | cb(err, reply);
|
2386 | request.jsonp.poll(opt, reply);
|
2387 | }, opt.jsonp);
|
2388 | });
|
2389 | };
|
2390 | request.jsonp.send = function (url, cb, id) {
|
2391 | var js = document.createElement('script');
|
2392 | js.src = url;
|
2393 | js.onerror = function () {
|
2394 | (window[js.id] || function () {})(null, {
|
2395 | err: 'JSONP failed!'
|
2396 | });
|
2397 | };
|
2398 | window[js.id = id] = function (res, err) {
|
2399 | cb(err, res);
|
2400 | cb.id = js.id;
|
2401 | js.parentNode.removeChild(js);
|
2402 | delete window[cb.id];
|
2403 | };
|
2404 | js.async = true;
|
2405 | document.getElementsByTagName('head')[0].appendChild(js);
|
2406 | return js;
|
2407 | };
|
2408 | request.jsonp.poll = function (opt, res) {
|
2409 | if (!opt || !opt.base || !res || !res.headers || !res.headers.poll) {
|
2410 | return;
|
2411 | }
|
2412 | var polls = request.jsonp.poll.s = request.jsonp.poll.s || {};
|
2413 | polls[opt.base] = polls[opt.base] || setTimeout(function () {
|
2414 | var msg = {
|
2415 | base: opt.base,
|
2416 | headers: { pull: 1 }
|
2417 | };
|
2418 |
|
2419 | request.each(opt.headers, function (header, name) {
|
2420 | msg.headers[name] = header;
|
2421 | });
|
2422 |
|
2423 | request.jsonp(msg, function (err, reply) {
|
2424 | delete polls[opt.base];
|
2425 |
|
2426 | var body = reply.body || [];
|
2427 | while (body.length && body.shift) {
|
2428 | var res = reply.body.shift();
|
2429 | if (res && res.body) {
|
2430 | request.createServer.ing(res, function () {
|
2431 | request(opt.base, null, null, res);
|
2432 | });
|
2433 | }
|
2434 | }
|
2435 | });
|
2436 | }, res.headers.poll);
|
2437 | };
|
2438 | request.jsonp.ify = function (opt, cb) {
|
2439 | var uri = encodeURIComponent, query = '?';
|
2440 | if (opt.url && opt.url.pathname) {
|
2441 | query = opt.url.pathname + query;
|
2442 | }
|
2443 | query = opt.base + query;
|
2444 | request.each((opt.url || {}).query, function (value, key) {
|
2445 | query += (uri(key) + '=' + uri(value) + '&');
|
2446 | });
|
2447 | if (opt.headers) {
|
2448 | query += uri('`') + '=' + uri(
|
2449 | JSON.stringify(opt.headers)
|
2450 | ) + '&';
|
2451 | }
|
2452 | if (request.jsonp.max < query.length) {
|
2453 | return cb();
|
2454 | }
|
2455 | var random = Math.floor(Math.random() * (0xffff + 1));
|
2456 | query += (uri('jsonp') + '=' + uri(opt.jsonp = 'P' + random));
|
2457 | if (opt.body) {
|
2458 | query += '&';
|
2459 | var w = opt.body, wls = function (w, l, s) {
|
2460 | return uri('%') + '=' + uri(w+'-'+(l||w)+'/'+(s||w)) + '&' + uri('$') + '=';
|
2461 | }
|
2462 | if (typeof w != 'string') {
|
2463 | w = JSON.stringify(w);
|
2464 | query += uri('^') + '=' + uri('json') + '&';
|
2465 | }
|
2466 | w = uri(w);
|
2467 | var i = 0, l = w.length
|
2468 | , s = request.jsonp.max - (query.length + wls(l.toString()).length);
|
2469 | if (s < 0){
|
2470 | return cb();
|
2471 | }
|
2472 | while (w) {
|
2473 | cb(query + wls(i, (i += s), l) + w.slice(0, i));
|
2474 | w = w.slice(i);
|
2475 | }
|
2476 | } else {
|
2477 | cb(query);
|
2478 | }
|
2479 | };
|
2480 | request.jsonp.max = 2000;
|
2481 | request.each = function (obj, cb, as) {
|
2482 | if (!obj || !cb) {
|
2483 | return;
|
2484 | }
|
2485 | for (var key in obj) {
|
2486 | if (obj.hasOwnProperty(key)) {
|
2487 | cb.call(as, obj[key], key);
|
2488 | }
|
2489 | }
|
2490 | };
|
2491 | module.exports = Client;
|
2492 | })(require, './polyfill/request');
|
2493 |
|
2494 | }());
|