-
Notifications
You must be signed in to change notification settings - Fork 0
/
gun.js
2547 lines (2434 loc) · 109 KB
/
gun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(function(){
/* UNBUILD */
function USE(arg, req){
return req? require(arg) : arg.slice? USE[R(arg)] : function(mod, path){
arg(mod = {exports: {}});
USE[R(path)] = mod.exports;
}
function R(p){
return p.split('/').slice(-1).toString().replace('.js','');
}
}
if(typeof module !== "undefined"){ var MODULE = module }
/* UNBUILD */
;USE(function(module){
// Shim for generic javascript utilities.
String.random = function(l, c){
var s = '';
l = l || 24; // you are not going to make a 0 length random number, so no need to check type
c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
while(l-- > 0){ s += c.charAt(Math.floor(Math.random() * c.length)) }
return s;
}
String.match = function(t, o){ var tmp, u;
if('string' !== typeof t){ return false }
if('string' == typeof o){ o = {'=': o} }
o = o || {};
tmp = (o['='] || o['*'] || o['>'] || o['<']);
if(t === tmp){ return true }
if(u !== o['=']){ return false }
tmp = (o['*'] || o['>']);
if(t.slice(0, (tmp||'').length) === tmp){ return true }
if(u !== o['*']){ return false }
if(u !== o['>'] && u !== o['<']){
return (t >= o['>'] && t <= o['<'])? true : false;
}
if(u !== o['>'] && t >= o['>']){ return true }
if(u !== o['<'] && t <= o['<']){ return true }
return false;
}
String.hash = function(s, c){ // via SO
if(typeof s !== 'string'){ return }
c = c || 0; // CPU schedule hashing by
if(!s.length){ return c }
for(var i=0,l=s.length,n; i<l; ++i){
n = s.charCodeAt(i);
c = ((c<<5)-c)+n;
c |= 0;
}
return c;
}
var has = Object.prototype.hasOwnProperty;
Object.plain = function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }
Object.empty = function(o, n){
for(var k in o){ if(has.call(o, k) && (!n || -1==n.indexOf(k))){ return false } }
return true;
}
Object.keys = Object.keys || function(o){
var l = [];
for(var k in o){ if(has.call(o, k)){ l.push(k) } }
return l;
}
;(function(){
var u, sT = setTimeout, l = 0, c = 0
, sI = (typeof setImmediate !== ''+u && setImmediate) || (function(c,f){
if(typeof MessageChannel == ''+u){ return sT }
(c = new MessageChannel()).port1.onmessage = function(e){ ''==e.data && f() }
return function(q){ f=q;c.port2.postMessage('') }
}()), check = sT.check = sT.check || (typeof performance !== ''+u && performance)
|| {now: function(){ return +new Date }};
sT.hold = sT.hold || 9; // half a frame benchmarks faster than < 1ms?
sT.poll = sT.poll || function(f){
if((sT.hold >= (check.now() - l)) && c++ < 3333){ f(); return }
sI(function(){ l = check.now(); f() },c=0)
}
}());
;(function(){ // Too many polls block, this "threads" them in turns over a single thread in time.
var sT = setTimeout, t = sT.turn = sT.turn || function(f){ 1 == s.push(f) && p(T) }
, s = t.s = [], p = sT.poll, i = 0, f, T = function(){
if(f = s[i++]){ f() }
if(i == s.length || 99 == i){
s = t.s = s.slice(i);
i = 0;
}
if(s.length){ p(T) }
}
}());
;(function(){
var u, sT = setTimeout, T = sT.turn;
(sT.each = sT.each || function(l,f,e,S){ S = S || 9; (function t(s,L,r){
if(L = (s = (l||[]).splice(0,S)).length){
for(var i = 0; i < L; i++){
if(u !== (r = f(s[i]))){ break }
}
if(u === r){ T(t); return }
} e && e(r);
}())})();
}());
})(USE, './shim');
;USE(function(module){
// On event emitter generic javascript utility.
module.exports = function onto(tag, arg, as){
if(!tag){ return {to: onto} }
var u, f = 'function' == typeof arg, tag = (this.tag || (this.tag = {}))[tag] || f && (
this.tag[tag] = {tag: tag, to: onto._ = { next: function(arg){ var tmp;
if(tmp = this.to){ tmp.next(arg) }
}}});
if(f){
var be = {
off: onto.off ||
(onto.off = function(){
if(this.next === onto._.next){ return !0 }
if(this === this.the.last){
this.the.last = this.back;
}
this.to.back = this.back;
this.next = onto._.next;
this.back.to = this.to;
if(this.the.last === this.the){
delete this.on.tag[this.the.tag];
}
}),
to: onto._,
next: arg,
the: tag,
on: this,
as: as,
};
(be.back = tag.last || tag).to = be;
return tag.last = be;
}
if((tag = tag.to) && u !== arg){ tag.next(arg) }
return tag;
};
})(USE, './onto');
;USE(function(module){
// TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function.
// Book is a replacement for JS objects, maps, dictionaries.
var sT = setTimeout, B = sT.Book || (sT.Book = function(text){
var b = function book(word, is){
var has = b.all[word], p;
if(is === undefined){ return (has && has.is) || b.get(has || word) }
if(has){
if(p = has.page){
p.size += size(is) - size(has.is);
p.text = '';
}
has.text = '';
has.is = is;
return b;
}
//b.all[word] = {is: word}; return b;
return b.set(word, is);
};
// TODO: if from text, preserve the separator symbol.
b.list = [{from: text, size: (text||'').length, substring: sub, toString: to, book: b, get: b, read: list}];
b.page = page;
b.set = set;
b.get = get;
b.all = {};
return b;
}), PAGE = 2**12;
function page(word){
var b = this, l = b.list, i = spot(word, l, b.parse), p = l[i];
if('string' == typeof p){ l[i] = p = {size: -1, first: b.parse? b.parse(p) : p, substring: sub, toString: to, book: b, get: b, read: list} } // TODO: test, how do we arrive at this condition again?
//p.i = i;
return p;
// TODO: BUG! What if we get the page, it turns out to be too big & split, we must then RE get the page!
}
function get(word){
if(!word){ return }
if(undefined !== word.is){ return word.is } // JS falsey values!
var b = this, has = b.all[word];
if(has){ return has.is }
// get does an exact match, so we would have found it already, unless parseless page:
var page = b.page(word), l, has, a, i;
if(!page || !page.from){ return } // no parseless data
return got(word, page);
}
function got(word, page){
var b = page.book, l, has, a, i;
if(l = from(page)){ has = l[got.i = i = spot(word, l, B.decode)]; } // TODO: POTENTIAL BUG! This assumes that each word on a page uses the same serializer/formatter/structure. // TOOD: BUG!!! Not actually, but if we want to do non-exact radix-like closest-word lookups on a page, we need to check limbo & potentially sort first.
// parseless may return -1 from actual value, so we may need to test both. // TODO: Double check? I think this is correct.
if(has && word == has.word){ return (b.all[word] = has).is }
if('string' != typeof has){ has = l[got.i = i+=1] }
if(has && word == has.word){ return (b.all[word] = has).is }
a = slot(has) // Escape!
if(word != B.decode(a[0])){
has = l[got.i = i+=1]; // edge case bug?
a = slot(has); // edge case bug?
if(word != B.decode(a[0])){ return }
}
has = l[i] = b.all[word] = {word: ''+word, is: B.decode(a[1]), page: page, substring: subt, toString: tot}; // TODO: convert to a JS value!!! Maybe index! TODO: BUG word needs a page!!!! TODO: Check for other types!!!
return has.is;
}
function spot(word, sorted, parse){ parse = parse || spot.no || (spot.no = function(t){ return t }); // TODO: BUG???? Why is there substring()||0 ? // TODO: PERF!!! .toString() is +33% faster, can we combine it with the export?
var L = sorted, min = 0, page, found, l = (word=''+word).length, max = L.length, i = max/2;
while(((word < (page = (parse(L[i=i>>0])||'').substring())) || ((parse(L[i+1])||'').substring() <= word)) && i != min){ // L[i] <= word < L[i+1]
i += (page <= word)? (max - (min = i))/2 : -((max = i) - min)/2;
}
return i;
}
function from(a, t, l){
if('string' != typeof a.from){ return a.from }
//(l = a.from = (t = a.from||'').substring(1, t.length-1).split(t[0])); // slot
(l = a.from = slot(t = t||a.from||''));
return l;
}
function list(each){ each = each || function(x){return x}
var i = 0, l = sort(this), w, r = [], p = this.book.parse || function(){};
//while(w = l[i++]){ r.push(each(slot(w)[1], p(w)||w, this)) }
while(w = l[i++]){ r.push(each(this.get(w = w.word||p(w)||w), w, this)) } // TODO: BUG! PERF?
return r;
}
function set(word, is){
// TODO: Perf on random write is decent, but short keys or seq seems significantly slower.
var b = this, has = b.all[word];
if(has){ return b(word, is) } // updates to in-memory items will always match exactly.
var page = b.page(word=''+word), tmp; // before we assume this is an insert tho, we need to check
if(page && page.from){ // if it could be an update to an existing word from parseless.
b.get(word);
if(b.all[word]){ return b(word, is) }
}
// MUST be an insert:
has = b.all[word] = {word: word, is: is, page: page, substring: subt, toString: tot};
page.first = (page.first < word)? page.first : word;
if(!page.limbo){ (page.limbo = []) }
page.limbo.push(has);
b(word, is);
page.size += size(word) + size(is);
if((b.PAGE || PAGE) < page.size){ split(page, b) }
return b;
}
function split(p, b){ // TODO: use closest hash instead of half.
//console.time();
//var S = performance.now();
var L = sort(p), l = L.length, i = l/2 >> 0, j = i, half = L[j], tmp;
//console.timeEnd();
var next = {first: half.substring(), size: 0, substring: sub, toString: to, book: b, get: b, read: list}, f = next.from = [];
while(tmp = L[i++]){
f.push(tmp);
next.size += (tmp.is||'').length||1;
tmp.page = next;
}
p.from = p.from.slice(0, j);
p.size -= next.size;
b.list.splice(spot(next.first, b.list)+1, 0, next); // TODO: BUG! Make sure next.first is decoded text. // TODO: BUG! spot may need parse too?
//console.timeEnd();
if(b.split){ b.split(next, p) }
//console.log(S = (performance.now() - S), 'split');
//console.BIG = console.BIG > S? console.BIG : S;
}
function slot(t){ return heal((t=t||'').substring(1, t.length-1).split(t[0]), t[0]) } B.slot = slot; // TODO: check first=last & pass `s`.
function heal(l, s){ var i, e;
if(0 > (i = l.indexOf(''))){ return l } // ~700M ops/sec on 4KB of Math.random()s, even faster if escape does exist.
if('' == l[0] && 1 == l.length){ return [] } // annoying edge cases! how much does this slow us down?
//if((c=i+2+parseInt(l[i+1])) != c){ return [] } // maybe still faster than below?
if((e=i+2+parseInt((e=l[i+1]).substring(0, e.indexOf('"'))||e)) != e){ return [] } // NaN check in JS is weird.
l[i] = l.slice(i, e).join(s||'|'); // rejoin the escaped value
return l.slice(0,i+1).concat(heal(l.slice(e), s)); // merge left with checked right.
}
function size(t){ return (t||'').length||1 } // bits/numbers less size? Bug or feature?
function subt(i,j){ return this.word }
//function tot(){ return this.text = this.text || "'"+(this.word)+"'"+(this.is)+"'" }
function tot(){ var tmp = {};
//if((tmp = this.page) && tmp.saving){ delete tmp.book.all[this.word]; } // TODO: BUG! Book can't know about RAD, this was from RAD, so this MIGHT be correct but we need to refactor. Make sure to add tests that will re-trigger this.
return this.text = this.text || ":"+B.encode(this.word)+":"+B.encode(this.is)+":";
tmp[this.word] = this.is;
return this.text = this.text || B.encode(tmp,'|',':').slice(1,-1);
//return this.text = this.text || "'"+(this.word)+"'"+(this.is)+"'";
}
function sub(i,j){ return (this.first||this.word||B.decode((from(this)||'')[0]||'')).substring(i,j) }
function to(){ return this.text = this.text || text(this) }
function text(p){ // PERF: read->[*] : text->"*" no edit waste 1 time perf.
if(p.limbo){ sort(p) } // TODO: BUG? Empty page meaning? undef, '', '||'?
return ('string' == typeof p.from)? p.from : '|'+(p.from||[]).join('|')+'|';
}
function sort(p, l){
var f = p.from = ('string' == typeof p.from)? slot(p.from) : p.from||[];
if(!(l = l || p.limbo)){ return f }
return mix(p).sort(function(a,b){
return (a.word||B.decode(''+a)) < (b.word||B.decode(''+b))? -1:1;
});
}
function mix(p, l){ // TODO: IMPROVE PERFORMANCE!!!! l[j] = i is 5X+ faster than .push(
l = l || p.limbo || []; p.limbo = null;
var j = 0, i, f = p.from;
while(i = l[j++]){
if(got(i.word, p)){
f[got.i] = i; // TODO: Trick: allow for a GUN'S HAM CRDT hook here.
} else {
f.push(i);
}
}
return f;
}
B.encode = function(d, s, u){ s = s || "|"; u = u || String.fromCharCode(32);
switch(typeof d){
case 'string': // text
var i = d.indexOf(s), c = 0;
while(i != -1){ c++; i = d.indexOf(s, i+1) }
return (c?s+c:'')+ '"' + d;
case 'number': return (d < 0)? ''+d : '+'+d;
case 'boolean': return d? '+' : '-';
case 'object': if(!d){ return ' ' } // TODO: BUG!!! Nested objects don't slot correctly
var l = Object.keys(d).sort(), i = 0, t = s, k, v;
while(k = l[i++]){ t += u+B.encode(k,s,u)+u+B.encode(d[k],s,u)+u+s }
return t;
}
}
B.decode = function(t, s){ s = s || "|";
if('string' != typeof t){ return }
switch(t){ case ' ': return null; case '-': return false; case '+': return true; }
switch(t[0]){
case '-': case '+': return parseFloat(t);
case '"': return t.slice(1);
}
return t.slice(t.indexOf('"')+1);
}
B.hash = function(s, c){ // via SO
if(typeof s !== 'string'){ return }
c = c || 0; // CPU schedule hashing by
if(!s.length){ return c }
for(var i=0,l=s.length,n; i<l; ++i){
n = s.charCodeAt(i);
c = ((c<<5)-c)+n;
c |= 0;
}
return c;
}
function record(key, val){ return key+B.encode(val)+"%"+key.length }
function decord(t){
var o = {}, i = t.lastIndexOf("%"), c = parseFloat(t.slice(i+1));
o[t.slice(0,c)] = B.decode(t.slice(c,i));
return o;
}
try{module.exports=B}catch(e){}
})(USE, './book');
;USE(function(module){
// Valid values are a subset of JSON: null, binary, number (!Infinity), text,
// or a soul relation. Arrays need special algorithms to handle concurrency,
// so they are not supported directly. Use an extension that supports them if
// needed but research their problems first.
module.exports = function(v){
// "deletes", nulling out keys.
return v === null ||
"string" === typeof v ||
"boolean" === typeof v ||
// we want +/- Infinity to be, but JSON does not support it, sad face.
// can you guess what v === v checks for? ;)
("number" === typeof v && v != Infinity && v != -Infinity && v === v) ||
(!!v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
}
})(USE, './valid');
;USE(function(module){
USE('./shim');
function State(){
var t = +new Date;
if(last < t){
return N = 0, last = t + State.drift;
}
return last = t + ((N += 1) / D) + State.drift;
}
State.drift = 0;
var NI = -Infinity, N = 0, D = 999, last = NI, u; // WARNING! In the future, on machines that are D times faster than 2016AD machines, you will want to increase D by another several orders of magnitude so the processing speed never out paces the decimal resolution (increasing an integer effects the state accuracy).
State.is = function(n, k, o){ // convenience function to get the state on a key on a node and return it.
var tmp = (k && n && n._ && n._['>']) || o;
if(!tmp){ return }
return ('number' == typeof (tmp = tmp[k]))? tmp : NI;
}
State.ify = function(n, k, s, v, soul){ // put a key's state on a node.
(n = n || {})._ = n._ || {}; // safety check or init.
if(soul){ n._['#'] = soul } // set a soul if specified.
var tmp = n._['>'] || (n._['>'] = {}); // grab the states data.
if(u !== k && k !== '_'){
if('number' == typeof s){ tmp[k] = s } // add the valid state.
if(u !== v){ n[k] = v } // Note: Not its job to check for valid values!
}
return n;
}
module.exports = State;
})(USE, './state');
;USE(function(module){
USE('./shim');
function Dup(opt){
var dup = {s:{}}, s = dup.s;
opt = opt || {max: 999, age: 1000 * 9};//*/ 1000 * 9 * 3};
dup.check = function(id){
if(!s[id]){ return false }
return dt(id);
}
var dt = dup.track = function(id){
var it = s[id] || (s[id] = {});
it.was = dup.now = +new Date;
if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) }
if(dt.ed){ dt.ed(id) }
return it;
}
dup.drop = function(age){
dup.to = null;
dup.now = +new Date;
var l = Object.keys(s);
console.STAT && console.STAT(dup.now, +new Date - dup.now, 'dup drop keys'); // prev ~20% CPU 7% RAM 300MB // now ~25% CPU 7% RAM 500MB
setTimeout.each(l, function(id){ var it = s[id]; // TODO: .keys( is slow?
if(it && (age || opt.age) > (dup.now - it.was)){ return }
delete s[id];
},0,99);
}
return dup;
}
module.exports = Dup;
})(USE, './dup');
;USE(function(module){
// request / response module, for asking and acking messages.
USE('./onto'); // depends upon onto!
module.exports = function ask(cb, as){
if(!this.on){ return }
var lack = (this.opt||{}).lack || 9000;
if(!('function' == typeof cb)){
if(!cb){ return }
var id = cb['#'] || cb, tmp = (this.tag||'')[id];
if(!tmp){ return }
if(as){
tmp = this.on(id, as);
clearTimeout(tmp.err);
tmp.err = setTimeout(function(){ tmp.off() }, lack);
}
return true;
}
var id = (as && as['#']) || random(9);
if(!cb){ return id }
var to = this.on(id, cb, as);
to.err = to.err || setTimeout(function(){ to.off();
to.next({err: "Error: No ACK yet.", lack: true});
}, lack);
return id;
}
var random = String.random || function(){ return Math.random().toString(36).slice(2) }
})(USE, './ask');
;USE(function(module){
function Gun(o){
if(o instanceof Gun){ return (this._ = {$: this}).$ }
if(!(this instanceof Gun)){ return new Gun(o) }
return Gun.create(this._ = {$: this, opt: o});
}
Gun.is = function($){ return ($ instanceof Gun) || ($ && $._ && ($ === $._.$)) || false }
Gun.version = 0.2020;
Gun.chain = Gun.prototype;
Gun.chain.toJSON = function(){};
USE('./shim');
Gun.valid = USE('./valid');
Gun.state = USE('./state');
Gun.on = USE('./onto');
Gun.dup = USE('./dup');
Gun.ask = USE('./ask');
;(function(){
Gun.create = function(at){
at.root = at.root || at;
at.graph = at.graph || {};
at.on = at.on || Gun.on;
at.ask = at.ask || Gun.ask;
at.dup = at.dup || Gun.dup();
var gun = at.$.opt(at.opt);
if(!at.once){
at.on('in', universe, at);
at.on('out', universe, at);
at.on('put', map, at);
Gun.on('create', at);
at.on('create', at);
}
at.once = 1;
return gun;
}
function universe(msg){
// TODO: BUG! msg.out = null being set!
//if(!F){ var eve = this; setTimeout(function(){ universe.call(eve, msg,1) },Math.random() * 100);return; } // ADD F TO PARAMS!
if(!msg){ return }
if(msg.out === universe){ this.to.next(msg); return }
var eve = this, as = eve.as, at = as.at || as, gun = at.$, dup = at.dup, tmp, DBG = msg.DBG;
(tmp = msg['#']) || (tmp = msg['#'] = text_rand(9));
if(dup.check(tmp)){ return } dup.track(tmp);
tmp = msg._; msg._ = ('function' == typeof tmp)? tmp : function(){};
(msg.$ && (msg.$ === (msg.$._||'').$)) || (msg.$ = gun);
if(msg['@'] && !msg.put){ ack(msg) }
if(!at.ask(msg['@'], msg)){ // is this machine listening for an ack?
DBG && (DBG.u = +new Date);
if(msg.put){ put(msg); return } else
if(msg.get){ Gun.on.get(msg, gun) }
}
DBG && (DBG.uc = +new Date);
eve.to.next(msg);
DBG && (DBG.ua = +new Date);
if(msg.nts || msg.NTS){ return } // TODO: This shouldn't be in core, but fast way to prevent NTS spread. Delete this line after all peers have upgraded to newer versions.
msg.out = universe; at.on('out', msg);
DBG && (DBG.ue = +new Date);
}
function put(msg){
if(!msg){ return }
var ctx = msg._||'', root = ctx.root = ((ctx.$ = msg.$||'')._||'').root;
if(msg['@'] && ctx.faith && !ctx.miss){ // TODO: AXE may split/route based on 'put' what should we do here? Detect @ in AXE? I think we don't have to worry, as DAM will route it on @.
msg.out = universe;
root.on('out', msg);
return;
}
ctx.latch = root.hatch; ctx.match = root.hatch = [];
var put = msg.put;
var DBG = ctx.DBG = msg.DBG, S = +new Date; CT = CT || S;
if(put['#'] && put['.']){ /*root && root.on('put', msg);*/ return } // TODO: BUG! This needs to call HAM instead.
DBG && (DBG.p = S);
ctx['#'] = msg['#'];
ctx.msg = msg;
ctx.all = 0;
ctx.stun = 1;
var nl = Object.keys(put);//.sort(); // TODO: This is unbounded operation, large graphs will be slower. Write our own CPU scheduled sort? Or somehow do it in below? Keys itself is not O(1) either, create ES5 shim over ?weak map? or custom which is constant.
console.STAT && console.STAT(S, ((DBG||ctx).pk = +new Date) - S, 'put sort');
var ni = 0, nj, kl, soul, node, states, err, tmp;
(function pop(o){
if(nj != ni){ nj = ni;
if(!(soul = nl[ni])){
console.STAT && console.STAT(S, ((DBG||ctx).pd = +new Date) - S, 'put');
fire(ctx);
return;
}
if(!(node = put[soul])){ err = ERR+cut(soul)+"no node." } else
if(!(tmp = node._)){ err = ERR+cut(soul)+"no meta." } else
if(soul !== tmp['#']){ err = ERR+cut(soul)+"soul not same." } else
if(!(states = tmp['>'])){ err = ERR+cut(soul)+"no state." }
kl = Object.keys(node||{}); // TODO: .keys( is slow
}
if(err){
msg.err = ctx.err = err; // invalid data should error and stun the message.
fire(ctx);
//console.log("handle error!", err) // handle!
return;
}
var i = 0, key; o = o || 0;
while(o++ < 9 && (key = kl[i++])){
if('_' === key){ continue }
var val = node[key], state = states[key];
if(u === state){ err = ERR+cut(key)+"on"+cut(soul)+"no state."; break }
if(!valid(val)){ err = ERR+cut(key)+"on"+cut(soul)+"bad "+(typeof val)+cut(val); break }
//ctx.all++; //ctx.ack[soul+key] = '';
ham(val, key, soul, state, msg);
++C; // courtesy count;
}
if((kl = kl.slice(i)).length){ turn(pop); return }
++ni; kl = null; pop(o);
}());
} Gun.on.put = put;
// TODO: MARK!!! clock below, reconnect sync, SEA certify wire merge, User.auth taking multiple times, // msg put, put, say ack, hear loop...
// WASIS BUG! local peer not ack. .off other people: .open
function ham(val, key, soul, state, msg){
var ctx = msg._||'', root = ctx.root, graph = root.graph, lot, tmp;
var vertex = graph[soul] || empty, was = state_is(vertex, key, 1), known = vertex[key];
var DBG = ctx.DBG; if(tmp = console.STAT){ if(!graph[soul] || !known){ tmp.has = (tmp.has || 0) + 1 } }
var now = State(), u;
if(state > now){
setTimeout(function(){ ham(val, key, soul, state, msg) }, (tmp = state - now) > MD? MD : tmp); // Max Defer 32bit. :(
console.STAT && console.STAT(((DBG||ctx).Hf = +new Date), tmp, 'future');
return;
}
if(state < was){ /*old;*/ if(true || !ctx.miss){ return } } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future. // for AXE this would reduce rebroadcast, but GUN does it on message forwarding. // TURNS OUT CACHE MISS WAS NOT NEEDED FOR NEW CHAINS ANYMORE!!! DANGER DANGER DANGER, ALWAYS RETURN! (or am I missing something?)
if(!ctx.faith){ // TODO: BUG? Can this be used for cache miss as well? // Yes this was a bug, need to check cache miss for RAD tests, but should we care about the faith check now? Probably not.
if(state === was && (val === known || L(val) <= L(known))){ /*console.log("same");*/ /*same;*/ if(!ctx.miss){ return } } // same
}
ctx.stun++; // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
var aid = msg['#']+ctx.all++, id = {toString: function(){ return aid }, _: ctx}; id.toJSON = id.toString; // this *trick* makes it compatible between old & new versions.
root.dup.track(id)['#'] = msg['#']; // fixes new OK acks for RPC like RTC.
DBG && (DBG.ph = DBG.ph || +new Date);
root.on('put', {'#': id, '@': msg['@'], put: {'#': soul, '.': key, ':': val, '>': state}, ok: msg.ok, _: ctx});
}
function map(msg){
var DBG; if(DBG = (msg._||'').DBG){ DBG.pa = +new Date; DBG.pm = DBG.pm || +new Date}
var eve = this, root = eve.as, graph = root.graph, ctx = msg._, put = msg.put, soul = put['#'], key = put['.'], val = put[':'], state = put['>'], id = msg['#'], tmp;
if((tmp = ctx.msg) && (tmp = tmp.put) && (tmp = tmp[soul])){ state_ify(tmp, key, state, val, soul) } // necessary! or else out messages do not get SEA transforms.
//var bytes = ((graph[soul]||'')[key]||'').length||1;
graph[soul] = state_ify(graph[soul], key, state, val, soul);
if(tmp = (root.next||'')[soul]){
//tmp.bytes = (tmp.bytes||0) + ((val||'').length||1) - bytes;
//if(tmp.bytes > 2**13){ Gun.log.once('byte-limit', "Note: In the future, GUN peers will enforce a ~4KB query limit. Please see https://gun.eco/docs/Page") }
tmp.on('in', msg)
}
fire(ctx);
eve.to.next(msg);
}
function fire(ctx, msg){ var root;
if(ctx.stop){ return }
if(!ctx.err && 0 < --ctx.stun){ return } // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
ctx.stop = 1;
if(!(root = ctx.root)){ return }
var tmp = ctx.match; tmp.end = 1;
if(tmp === root.hatch){ if(!(tmp = ctx.latch) || tmp.end){ delete root.hatch } else { root.hatch = tmp } }
ctx.hatch && ctx.hatch(); // TODO: rename/rework how put & this interact.
setTimeout.each(ctx.match, function(cb){cb && cb()});
if(!(msg = ctx.msg) || ctx.err || msg.err){ return }
msg.out = universe;
ctx.root.on('out', msg);
CF(); // courtesy check;
}
function ack(msg){ // aggregate ACKs.
var id = msg['@'] || '', ctx, ok, tmp;
if(!(ctx = id._)){
var dup = (dup = msg.$) && (dup = dup._) && (dup = dup.root) && (dup = dup.dup);
if(!(dup = dup.check(id))){ return }
msg['@'] = dup['#'] || msg['@']; // This doesn't do anything anymore, backtrack it to something else?
return;
}
ctx.acks = (ctx.acks||0) + 1;
if(ctx.err = msg.err){
msg['@'] = ctx['#'];
fire(ctx); // TODO: BUG? How it skips/stops propagation of msg if any 1 item is error, this would assume a whole batch/resync has same malicious intent.
}
ctx.ok = msg.ok || ctx.ok;
if(!ctx.stop && !ctx.crack){ ctx.crack = ctx.match && ctx.match.push(function(){back(ctx)}) } // handle synchronous acks. NOTE: If a storage peer ACKs synchronously then the PUT loop has not even counted up how many items need to be processed, so ctx.STOP flags this and adds only 1 callback to the end of the PUT loop.
back(ctx);
}
function back(ctx){
if(!ctx || !ctx.root){ return }
if(ctx.stun || ctx.acks !== ctx.all){ return }
ctx.root.on('in', {'@': ctx['#'], err: ctx.err, ok: ctx.err? u : ctx.ok || {'':1}});
}
var ERR = "Error: Invalid graph!";
var cut = function(s){ return " '"+(''+s).slice(0,9)+"...' " }
var L = JSON.stringify, MD = 2147483647, State = Gun.state;
var C = 0, CT, CF = function(){if(C>999 && (C/-(CT - (CT = +new Date))>1)){Gun.window && console.log("Warning: You're syncing 1K+ records a second, faster than DOM can update - consider limiting query.");CF=function(){C=0}}};
}());
;(function(){
Gun.on.get = function(msg, gun){
var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'];
var next = root.next || (root.next = {}), at = next[soul];
// TODO: Azarattum bug, what is in graph is not same as what is in next. Fix!
// queue concurrent GETs?
// TODO: consider tagging original message into dup for DAM.
// TODO: ^ above? In chat app, 12 messages resulted in same peer asking for `#user.pub` 12 times. (same with #user GET too, yipes!) // DAM note: This also resulted in 12 replies from 1 peer which all had same ##hash but none of them deduped because each get was different.
// TODO: Moving quick hacks fixing these things to axe for now.
// TODO: a lot of GET #foo then GET #foo."" happening, why?
// TODO: DAM's ## hash check, on same get ACK, producing multiple replies still, maybe JSON vs YSON?
// TMP note for now: viMZq1slG was chat LEX query #.
/*if(gun !== (tmp = msg.$) && (tmp = (tmp||'')._)){
if(tmp.Q){ tmp.Q[msg['#']] = ''; return } // chain does not need to ask for it again.
tmp.Q = {};
}*/
/*if(u === has){
if(at.Q){
//at.Q[msg['#']] = '';
//return;
}
at.Q = {};
}*/
var ctx = msg._||{}, DBG = ctx.DBG = msg.DBG;
DBG && (DBG.g = +new Date);
//console.log("GET:", get, node, has, at);
//if(!node && !at){ return root.on('get', msg) }
//if(has && node){ // replace 2 below lines to continue dev?
if(!node){ return root.on('get', msg) }
if(has){
if('string' != typeof has || u === node[has]){
if(!((at||'').next||'')[has]){ root.on('get', msg); return }
}
node = state_ify({}, has, state_is(node, has), node[has], soul);
// If we have a key in-memory, do we really need to fetch?
// Maybe... in case the in-memory key we have is a local write
// we still need to trigger a pull/merge from peers.
}
//Gun.window? Gun.obj.copy(node) : node; // HNPERF: If !browser bump Performance? Is this too dangerous to reference root graph? Copy / shallow copy too expensive for big nodes. Gun.obj.to(node); // 1 layer deep copy // Gun.obj.copy(node); // too slow on big nodes
node && ack(msg, node);
root.on('get', msg); // send GET to storage adapters.
}
function ack(msg, node){
var S = +new Date, ctx = msg._||{}, DBG = ctx.DBG = msg.DBG;
var to = msg['#'], id = text_rand(9), keys = Object.keys(node||'').sort(), soul = ((node||'')._||'')['#'], kl = keys.length, j = 0, root = msg.$._.root, F = (node === root.graph[soul]);
console.STAT && console.STAT(S, ((DBG||ctx).gk = +new Date) - S, 'got keys');
// PERF: Consider commenting this out to force disk-only reads for perf testing? // TODO: .keys( is slow
node && (function go(){
S = +new Date;
var i = 0, k, put = {}, tmp;
while(i < 9 && (k = keys[i++])){
state_ify(put, k, state_is(node, k), node[k], soul);
}
keys = keys.slice(i);
(tmp = {})[soul] = put; put = tmp;
var faith; if(F){ faith = function(){}; faith.ram = faith.faith = true; } // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
tmp = keys.length;
console.STAT && console.STAT(S, -(S - (S = +new Date)), 'got copied some');
DBG && (DBG.ga = +new Date);
root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG});
console.STAT && console.STAT(S, +new Date - S, 'got in');
if(!tmp){ return }
setTimeout.turn(go);
}());
if(!node){ root.on('in', {'@': msg['#']}) } // TODO: I don't think I like this, the default lS adapter uses this but "not found" is a sensitive issue, so should probably be handled more carefully/individually.
} Gun.on.get.ack = ack;
}());
;(function(){
Gun.chain.opt = function(opt){
opt = opt || {};
var gun = this, at = gun._, tmp = opt.peers || opt;
if(!Object.plain(opt)){ opt = {} }
if(!Object.plain(at.opt)){ at.opt = opt }
if('string' == typeof tmp){ tmp = [tmp] }
if(!Object.plain(at.opt.peers)){ at.opt.peers = {}}
if(tmp instanceof Array){
opt.peers = {};
tmp.forEach(function(url){
var p = {}; p.id = p.url = url;
opt.peers[url] = at.opt.peers[url] = at.opt.peers[url] || p;
})
}
obj_each(opt, function each(k){ var v = this[k];
if((this && this.hasOwnProperty(k)) || 'string' == typeof v || Object.empty(v)){ this[k] = v; return }
if(v && v.constructor !== Object && !(v instanceof Array)){ return }
obj_each(v, each);
});
at.opt.from = opt;
Gun.on('opt', at);
at.opt.uuid = at.opt.uuid || function uuid(l){ return Gun.state().toString(36).replace('.','') + String.random(l||12) }
return gun;
}
}());
var obj_each = function(o,f){ Object.keys(o).forEach(f,o) }, text_rand = String.random, turn = setTimeout.turn, valid = Gun.valid, state_is = Gun.state.is, state_ify = Gun.state.ify, u, empty = {}, C;
Gun.log = function(){ return (!Gun.log.off && C.log.apply(C, arguments)), [].slice.call(arguments).join(' ') };
Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) };
if(typeof window !== "undefined"){ (window.GUN = window.Gun = Gun).window = window }
try{ if(typeof MODULE !== "undefined"){ MODULE.exports = Gun } }catch(e){}
module.exports = Gun;
(Gun.window||{}).console = (Gun.window||{}).console || {log: function(){}};
(C = console).only = function(i, s){ return (C.only.i && i === C.only.i && C.only.i++) && (C.log.apply(C, arguments) || s) };
;"Please do not remove welcome log unless you are paying for a monthly sponsorship, thanks!";
Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!");
})(USE, './root');
;USE(function(module){
var Gun = USE('./root');
Gun.chain.back = function(n, opt){ var tmp;
n = n || 1;
if(-1 === n || Infinity === n){
return this._.root.$;
} else
if(1 === n){
return (this._.back || this._).$;
}
var gun = this, at = gun._;
if(typeof n === 'string'){
n = n.split('.');
}
if(n instanceof Array){
var i = 0, l = n.length, tmp = at;
for(i; i < l; i++){
tmp = (tmp||empty)[n[i]];
}
if(u !== tmp){
return opt? gun : tmp;
} else
if((tmp = at.back)){
return tmp.$.back(n, opt);
}
return;
}
if('function' == typeof n){
var yes, tmp = {back: at};
while((tmp = tmp.back)
&& u === (yes = n(tmp, opt))){}
return yes;
}
if('number' == typeof n){
return (at.back || at).$.back(n - 1);
}
return this;
}
var empty = {}, u;
})(USE, './back');
;USE(function(module){
// WARNING: GUN is very simple, but the JavaScript chaining API around GUN
// is complicated and was extremely hard to build. If you port GUN to another
// language, consider implementing an easier API to build.
var Gun = USE('./root');
Gun.chain.chain = function(sub){
var gun = this, at = gun._, chain = new (sub || gun).constructor(gun), cat = chain._, root;
cat.root = root = at.root;
cat.id = ++root.once;
cat.back = gun._;
cat.on = Gun.on;
cat.on('in', Gun.on.in, cat); // For 'in' if I add my own listeners to each then I MUST do it before in gets called. If I listen globally for all incoming data instead though, regardless of individual listeners, I can transform the data there and then as well.
cat.on('out', Gun.on.out, cat); // However for output, there isn't really the global option. I must listen by adding my own listener individually BEFORE this one is ever called.
return chain;
}
function output(msg){
var put, get, at = this.as, back = at.back, root = at.root, tmp;
if(!msg.$){ msg.$ = at.$ }
this.to.next(msg);
if(at.err){ at.on('in', {put: at.put = u, $: at.$}); return }
if(get = msg.get){
/*if(u !== at.put){
at.on('in', at);
return;
}*/
if(root.pass){ root.pass[at.id] = at; } // will this make for buggy behavior elsewhere?
if(at.lex){ Object.keys(at.lex).forEach(function(k){ tmp[k] = at.lex[k] }, tmp = msg.get = msg.get || {}) }
if(get['#'] || at.soul){
get['#'] = get['#'] || at.soul;
//root.graph[get['#']] = root.graph[get['#']] || {_:{'#':get['#'],'>':{}}};
msg['#'] || (msg['#'] = text_rand(9)); // A3120 ?
back = (root.$.get(get['#'])._);
if(!(get = get['.'])){ // soul
tmp = back.ask && back.ask['']; // check if we have already asked for the full node
(back.ask || (back.ask = {}))[''] = back; // add a flag that we are now.
if(u !== back.put){ // if we already have data,
back.on('in', back); // send what is cached down the chain
if(tmp){ return } // and don't ask for it again.
}
msg.$ = back.$;
} else
if(obj_has(back.put, get)){ // TODO: support #LEX !
tmp = back.ask && back.ask[get];
(back.ask || (back.ask = {}))[get] = back.$.get(get)._;
back.on('in', {get: get, put: {'#': back.soul, '.': get, ':': back.put[get], '>': state_is(root.graph[back.soul], get)}});
if(tmp){ return }
}
/*put = (back.$.get(get)._);
if(!(tmp = put.ack)){ put.ack = -1 }
back.on('in', {
$: back.$,
put: Gun.state.ify({}, get, Gun.state(back.put, get), back.put[get]),
get: back.get
});
if(tmp){ return }
} else
if('string' != typeof get){
var put = {}, meta = (back.put||{})._;
Gun.obj.map(back.put, function(v,k){
if(!Gun.text.match(k, get)){ return }
put[k] = v;
})
if(!Gun.obj.empty(put)){
put._ = meta;
back.on('in', {$: back.$, put: put, get: back.get})
}
if(tmp = at.lex){
tmp = (tmp._) || (tmp._ = function(){});
if(back.ack < tmp.ask){ tmp.ask = back.ack }
if(tmp.ask){ return }
tmp.ask = 1;
}
}
*/
root.ask(ack, msg); // A3120 ?
return root.on('in', msg);
}
//if(root.now){ root.now[at.id] = root.now[at.id] || true; at.pass = {} }
if(get['.']){
if(at.get){
msg = {get: {'.': at.get}, $: at.$};
(back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
return back.on('out', msg);
}
msg = {get: at.lex? msg.get : {}, $: at.$};
return back.on('out', msg);
}
(at.ask || (at.ask = {}))[''] = at; //at.ack = at.ack || -1;
if(at.get){
get['.'] = at.get;
(back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
return back.on('out', msg);
}
}
return back.on('out', msg);
}; Gun.on.out = output;
function input(msg, cat){ cat = cat || this.as; // TODO: V8 may not be able to optimize functions with different parameter calls, so try to do benchmark to see if there is any actual difference.
var root = cat.root, gun = msg.$ || (msg.$ = cat.$), at = (gun||'')._ || empty, tmp = msg.put||'', soul = tmp['#'], key = tmp['.'], change = (u !== tmp['='])? tmp['='] : tmp[':'], state = tmp['>'] || -Infinity, sat; // eve = event, at = data at, cat = chain at, sat = sub at (children chains).
if(u !== msg.put && (u === tmp['#'] || u === tmp['.'] || (u === tmp[':'] && u === tmp['=']) || u === tmp['>'])){ // convert from old format
if(!valid(tmp)){
if(!(soul = ((tmp||'')._||'')['#'])){ console.log("chain not yet supported for", tmp, '...', msg, cat); return; }
gun = cat.root.$.get(soul);
return setTimeout.each(Object.keys(tmp).sort(), function(k){ // TODO: .keys( is slow // BUG? ?Some re-in logic may depend on this being sync?
if('_' == k || u === (state = state_is(tmp, k))){ return }
cat.on('in', {$: gun, put: {'#': soul, '.': k, '=': tmp[k], '>': state}, VIA: msg});
});
}
cat.on('in', {$: at.back.$, put: {'#': soul = at.back.soul, '.': key = at.has || at.get, '=': tmp, '>': state_is(at.back.put, key)}, via: msg}); // TODO: This could be buggy! It assumes/approxes data, other stuff could have corrupted it.
return;
}
if((msg.seen||'')[cat.id]){ return } (msg.seen || (msg.seen = function(){}))[cat.id] = cat; // help stop some infinite loops
if(cat !== at){ // don't worry about this when first understanding the code, it handles changing contexts on a message. A soul chain will never have a different context.
Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] }, tmp = {}); // make copy of message
tmp.get = cat.get || tmp.get;
if(!cat.soul && !cat.has){ // if we do not recognize the chain type
tmp.$$$ = tmp.$$$ || cat.$; // make a reference to wherever it came from.
} else
if(at.soul){ // a has (property) chain will have a different context sometimes if it is linked (to a soul chain). Anything that is not a soul or has chain, will always have different contexts.
tmp.$ = cat.$;
tmp.$$ = tmp.$$ || at.$;
}
msg = tmp; // use the message with the new context instead;
}
unlink(msg, cat);
if(((cat.soul/* && (cat.ask||'')['']*/) || msg.$$) && state >= state_is(root.graph[soul], key)){ // The root has an in-memory cache of the graph, but if our peer has asked for the data then we want a per deduplicated chain copy of the data that might have local edits on it.
(tmp = root.$.get(soul)._).put = state_ify(tmp.put, key, state, change, soul);
}
if(!at.soul /*&& (at.ask||'')['']*/ && state >= state_is(root.graph[soul], key) && (sat = (root.$.get(soul)._.next||'')[key])){ // Same as above here, but for other types of chains. // TODO: Improve perf by preventing echoes recaching.
sat.put = change; // update cache
if('string' == typeof (tmp = valid(change))){
sat.put = root.$.get(tmp)._.put || change; // share same cache as what we're linked to.
}
}
this.to && this.to.next(msg); // 1st API job is to call all chain listeners.
// TODO: Make input more reusable by only doing these (some?) calls if we are a chain we recognize? This means each input listener would be responsible for when listeners need to be called, which makes sense, as they might want to filter.
cat.any && setTimeout.each(Object.keys(cat.any), function(any){ (any = cat.any[any]) && any(msg) },0,99); // 1st API job is to call all chain listeners. // TODO: .keys( is slow // BUG: Some re-in logic may depend on this being sync.
cat.echo && setTimeout.each(Object.keys(cat.echo), function(lat){ (lat = cat.echo[lat]) && lat.on('in', msg) },0,99); // & linked at chains // TODO: .keys( is slow // BUG: Some re-in logic may depend on this being sync.
if(((msg.$$||'')._||at).soul){ // comments are linear, but this line of code is non-linear, so if I were to comment what it does, you'd have to read 42 other comments first... but you can't read any of those comments until you first read this comment. What!? // shouldn't this match link's check?
// is there cases where it is a $$ that we do NOT want to do the following?
if((sat = cat.next) && (sat = sat[key])){ // TODO: possible trick? Maybe have `ionmap` code set a sat? // TODO: Maybe we should do `cat.ask` instead? I guess does not matter.
tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] });
tmp.$ = (msg.$$||msg.$).get(tmp.get = key); delete tmp.$$; delete tmp.$$$;
sat.on('in', tmp);
}
}
link(msg, cat);
}; Gun.on.in = input;
function link(msg, cat){ cat = cat || this.as || msg.$._;
if(msg.$$ && this !== Gun.on){ return } // $$ means we came from a link, so we are at the wrong level, thus ignore it unless overruled manually by being called directly.
if(!msg.put || cat.soul){ return } // But you cannot overrule being linked to nothing, or trying to link a soul chain - that must never happen.
var put = msg.put||'', link = put['=']||put[':'], tmp;
var root = cat.root, tat = root.$.get(put['#']).get(put['.'])._;
if('string' != typeof (link = valid(link))){
if(this === Gun.on){ (tat.echo || (tat.echo = {}))[cat.id] = cat } // allow some chain to explicitly force linking to simple data.
return; // by default do not link to data that is not a link.
}
if((tat.echo || (tat.echo = {}))[cat.id] // we've already linked ourselves so we do not need to do it again. Except... (annoying implementation details)
&& !(root.pass||'')[cat.id]){ return } // if a new event listener was added, we need to make a pass through for it. The pass will be on the chain, not always the chain passed down.
if(tmp = root.pass){ if(tmp[link+cat.id]){ return } tmp[link+cat.id] = 1 } // But the above edge case may "pass through" on a circular graph causing infinite passes, so we hackily add a temporary check for that.
(tat.echo||(tat.echo={}))[cat.id] = cat; // set ourself up for the echo! // TODO: BUG? Echo to self no longer causes problems? Confirm.
if(cat.has){ cat.link = link }
var sat = root.$.get(tat.link = link)._; // grab what we're linking to.
(sat.echo || (sat.echo = {}))[tat.id] = tat; // link it.
var tmp = cat.ask||''; // ask the chain for what needs to be loaded next!
if(tmp[''] || cat.lex){ // we might need to load the whole thing // TODO: cat.lex probably has edge case bugs to it, need more test coverage.
sat.on('out', {get: {'#': link}});
}
setTimeout.each(Object.keys(tmp), function(get, sat){ // if sub chains are asking for data. // TODO: .keys( is slow // BUG? ?Some re-in logic may depend on this being sync?
if(!get || !(sat = tmp[get])){ return }
sat.on('out', {get: {'#': link, '.': get}}); // go get it.
},0,99);
}; Gun.on.link = link;
function unlink(msg, cat){ // ugh, so much code for seemingly edge case behavior.
var put = msg.put||'', change = (u !== put['='])? put['='] : put[':'], root = cat.root, link, tmp;
if(u === change){ // 1st edge case: If we have a brand new database, no data will be found.
// TODO: BUG! because emptying cache could be async from below, make sure we are not emptying a newer cache. So maybe pass an Async ID to check against?
// TODO: BUG! What if this is a map? // Warning! Clearing things out needs to be robust against sync/async ops, or else you'll see `map val get put` test catastrophically fail because map attempts to link when parent graph is streamed before child value gets set. Need to differentiate between lack acks and force clearing.