1 |
|
2 |
|
3 | function Gun(o){
|
4 | if(o instanceof Gun){ return (this._ = {gun: this}).gun }
|
5 | if(!(this instanceof Gun)){ return new Gun(o) }
|
6 | return Gun.create(this._ = {gun: this, opt: o});
|
7 | }
|
8 |
|
9 | Gun.is = function(gun){ return (gun instanceof Gun) }
|
10 |
|
11 | Gun.version = 0.6;
|
12 |
|
13 | Gun.chain = Gun.prototype;
|
14 | Gun.chain.toJSON = function(){};
|
15 |
|
16 | var Type = require('./type');
|
17 | Type.obj.to(Type, Gun);
|
18 | Gun.HAM = require('./HAM');
|
19 | Gun.val = require('./val');
|
20 | Gun.node = require('./node');
|
21 | Gun.state = require('./state');
|
22 | Gun.graph = require('./graph');
|
23 | Gun.dup = require('./dup');
|
24 | Gun.on = require('./onify')();
|
25 |
|
26 | Gun._ = {
|
27 | node: Gun.node._
|
28 | ,soul: Gun.val.rel._
|
29 | ,state: Gun.state._
|
30 | ,field: '.'
|
31 | ,value: '='
|
32 | }
|
33 |
|
34 | ;(function(){
|
35 | Gun.create = function(at){
|
36 | at.on = at.on || Gun.on;
|
37 | at.root = at.root || at.gun;
|
38 | at.graph = at.graph || {};
|
39 | at.dup = at.dup || new Gun.dup;
|
40 | var gun = at.gun.opt(at.opt);
|
41 | if(!at.once){
|
42 | at.on('in', input, at);
|
43 | at.on('out', output, at);
|
44 | }
|
45 | at.once = 1;
|
46 | return gun;
|
47 | }
|
48 | function output(at){
|
49 |
|
50 | var cat = this.as, gun = cat.gun, tmp;
|
51 |
|
52 | if(at.get && get(at, cat)){ return }
|
53 | cat.on('in', obj_to(at, {gun: cat.gun}));
|
54 | if(at['#']){
|
55 | cat.dup.track(at['#']);
|
56 | }
|
57 | if(!at.gun){
|
58 | at = obj_to(at, {gun: gun});
|
59 | }
|
60 | Gun.on('out', at);
|
61 | }
|
62 | function get(at, cat){
|
63 | var soul = at.get[_soul], node = cat.graph[soul], field = at.get[_field], tmp;
|
64 | var next = cat.next || (cat.next = {}), as = (next[soul] || (next[soul] = cat.gun.get(soul)))._;
|
65 | if(!node){ return }
|
66 | if(field){
|
67 | if(!obj_has(node, field)){ return }
|
68 | tmp = Gun.obj.put(Gun.node.soul.ify({}, soul), field, node[field]);
|
69 | node = Gun.state.ify(tmp, field, Gun.state.is(node, field));
|
70 | }
|
71 | as.on('in', {
|
72 | put: node,
|
73 | get: as.soul,
|
74 | gun: as.gun
|
75 | });
|
76 | if(0 < as.ack){
|
77 | return true;
|
78 | }
|
79 | }
|
80 | function input(at){
|
81 |
|
82 | var ev = this, cat = ev.as;
|
83 | if(!at.gun){ at.gun = cat.gun }
|
84 | if(!at['#'] && at['@']){
|
85 | at['#'] = Gun.text.random();
|
86 | if(Gun.on.ack(at['@'], at)){ return }
|
87 | cat.dup.track(at['#']);
|
88 | cat.on('out', at);
|
89 | return;
|
90 | }
|
91 | if(at['#'] && cat.dup.check(at['#'])){ return }
|
92 | cat.dup.track(at['#']);
|
93 | if(Gun.on.ack(at['@'], at)){ return }
|
94 | if(at.put){
|
95 | Gun.HAM.synth(at, ev, cat.gun);
|
96 | Gun.on('put', at);
|
97 | }
|
98 | if(at.get){ Gun.on('get', at) }
|
99 | Gun.on('out', at);
|
100 | }
|
101 | }());
|
102 |
|
103 | ;(function(){
|
104 | var ask = Gun.on.ask = function(cb, as){
|
105 | var id = Gun.text.random();
|
106 | if(cb){ ask.on(id, cb, as) }
|
107 | return id;
|
108 | }
|
109 | ask.on = Gun.on;
|
110 | Gun.on.ack = function(at, reply){
|
111 | if(!at || !reply || !ask.on){ return }
|
112 | var id = at['#'] || at;
|
113 | if(!ask.tag || !ask.tag[id]){ return }
|
114 | ask.on(id, reply);
|
115 | return true;
|
116 | }
|
117 | }());
|
118 |
|
119 | ;(function(){
|
120 | Gun.chain.opt = function(opt){
|
121 | opt = opt || {};
|
122 | var gun = this, at = gun._, tmp = opt.peers || opt;
|
123 | if(!obj_is(opt)){ opt = {} }
|
124 | if(!obj_is(at.opt)){ at.opt = opt }
|
125 | if(text_is(tmp)){ tmp = [tmp] }
|
126 | if(list_is(tmp)){
|
127 | tmp = obj_map(tmp, function(url, i, map){
|
128 | map(url, {url: url});
|
129 | });
|
130 | if(!obj_is(at.opt.peers)){ at.opt.peers = {}}
|
131 | at.opt.peers = obj_to(tmp, at.opt.peers);
|
132 | }
|
133 | at.opt.peers = at.opt.peers || {};
|
134 | obj_to(opt, at.opt);
|
135 | Gun.on('opt', at);
|
136 | return gun;
|
137 | }
|
138 | }());
|
139 |
|
140 | var text_is = Gun.text.is;
|
141 | var list_is = Gun.list.is;
|
142 | var obj = Gun.obj, obj_is = obj.is, obj_has = obj.has, obj_to = obj.to, obj_map = obj.map;
|
143 | var _soul = Gun._.soul, _field = Gun._.field;
|
144 |
|
145 |
|
146 | console.debug = function(i, s){ return (console.debug.i && i === console.debug.i && console.debug.i++) && (console.log.apply(console, arguments) || s) };
|
147 |
|
148 | Gun.log = function(){ return (!Gun.log.off && console.log.apply(console, arguments)), [].slice.call(arguments).join(' ') }
|
149 | Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) }
|
150 |
|
151 |
|
152 | 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'!");
|
153 |
|
154 |
|
155 | if(typeof window !== "undefined"){ window.Gun = Gun }
|
156 | if(typeof common !== "undefined"){ common.exports = Gun }
|
157 | module.exports = Gun;
|
158 | |
\ | No newline at end of file |