1 |
|
2 |
|
3 | var Gun = require('./root');
|
4 | module.exports = Gun;
|
5 |
|
6 | ;(function(){
|
7 | function meta(v,f){
|
8 | if(obj_has(Gun.__._, f)){ return }
|
9 | obj_put(this._, f, v);
|
10 | }
|
11 | function map(value, field){
|
12 | if(Gun._.node === field){ return }
|
13 | var node = this.node, vertex = this.vertex, union = this.union, machine = this.machine;
|
14 | var is = state_is(node, field), cs = state_is(vertex, field);
|
15 | if(u === is || u === cs){ return true }
|
16 | var iv = value, cv = vertex[field];
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | if(!val_is(iv) && u !== iv){ return true }
|
35 | if(!val_is(cv) && u !== cv){ return true }
|
36 | var HAM = Gun.HAM(machine, is, cs, iv, cv);
|
37 | if(HAM.err){
|
38 | console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", HAM.err);
|
39 | return;
|
40 | }
|
41 | if(HAM.state || HAM.historical || HAM.current){
|
42 |
|
43 | return;
|
44 | }
|
45 | if(HAM.incoming){
|
46 | union[field] = value;
|
47 | state_ify(union, field, is);
|
48 | return;
|
49 | }
|
50 | if(HAM.defer){
|
51 | union[field] = value;
|
52 | state_ify(union, field, is);
|
53 |
|
54 | return;
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | }
|
62 | }
|
63 | Gun.HAM.union = function(vertex, node, opt){
|
64 | if(!node || !node._){ return }
|
65 | vertex = vertex || Gun.node.soul.ify({_:{'>':{}}}, Gun.node.soul(node));
|
66 | if(!vertex || !vertex._){ return }
|
67 | opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
|
68 | opt.union = vertex || Gun.obj.copy(vertex);
|
69 |
|
70 | opt.vertex = vertex;
|
71 | opt.node = node;
|
72 |
|
73 | if(obj_map(node, map, opt)){
|
74 | return;
|
75 | }
|
76 | return opt.union;
|
77 | }
|
78 | Gun.HAM.delta = function(vertex, node, opt){
|
79 | opt = num_is(opt)? {machine: opt} : {machine: Gun.state()};
|
80 | if(!vertex){ return Gun.obj.copy(node) }
|
81 | opt.soul = Gun.node.soul(opt.vertex = vertex);
|
82 | if(!opt.soul){ return }
|
83 | opt.delta = Gun.node.soul.ify({}, opt.soul);
|
84 | obj_map(opt.node = node, diff, opt);
|
85 | return opt.delta;
|
86 | }
|
87 | function diff(value, field){ var opt = this;
|
88 | if(Gun._.node === field){ return }
|
89 | if(!val_is(value)){ return }
|
90 | var node = opt.node, vertex = opt.vertex, is = state_is(node, field, true), cs = state_is(vertex, field, true), delta = opt.delta;
|
91 | var HAM = Gun.HAM(opt.machine, is, cs, value, vertex[field]);
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | if(HAM.incoming){
|
100 | delta[field] = value;
|
101 | state_ify(delta, field, is);
|
102 | }
|
103 | }
|
104 | Gun.HAM.synth = function(at, ev, as){ var gun = this.as || as;
|
105 | var cat = gun._, root = cat.root._, put = {}, tmp;
|
106 |
|
107 |
|
108 |
|
109 | if(!at.put){
|
110 |
|
111 | if(cat.put !== u){ return }
|
112 | cat.on('in', {
|
113 | get: cat.get,
|
114 | put: cat.put = u,
|
115 | gun: gun,
|
116 | via: at
|
117 | })
|
118 | return;
|
119 | }
|
120 | // TODO: PERF! Have options to determine if this data should even be in memory on this peer!
|
121 | obj_map(at.put, function(node, soul){ var graph = this.graph;
|
122 | put[soul] = Gun.HAM.delta(graph[soul], node, {graph: graph});
|
123 | graph[soul] = Gun.HAM.union(graph[soul], node) || graph[soul];
|
124 | }, root);
|
125 |
|
126 | obj_map(put, function(node, soul){
|
127 | var root = this, next = root.next || (root.next = {}), gun = next[soul] || (next[soul] = root.gun.get(soul)), coat = (gun._);
|
128 | coat.put = root.graph[soul];
|
129 | if(cat.field && !obj_has(node, cat.field)){
|
130 | (at = obj_to(at, {})).put = u;
|
131 | Gun.HAM.synth(at, ev, cat.gun);
|
132 | return;
|
133 | }
|
134 | coat.on('in', {
|
135 | put: node,
|
136 | get: soul,
|
137 | gun: gun,
|
138 | via: at
|
139 | });
|
140 | }, root);
|
141 | }
|
142 | }());
|
143 |
|
144 | var Type = Gun;
|
145 | var num = Type.num, num_is = num.is;
|
146 | var obj = Type.obj, obj_has = obj.has, obj_put = obj.put, obj_to = obj.to, obj_map = obj.map;
|
147 | var node = Gun.node, node_soul = node.soul, node_is = node.is, node_ify = node.ify;
|
148 | var state = Gun.state, state_is = state.is, state_ify = state.ify;
|
149 | var val = Gun.val, val_is = val.is, rel_is = val.rel.is;
|
150 | var u;
|
151 | |
\ | No newline at end of file |