UNPKG

5.21 kBJavaScriptView Raw
1
2
3var Gun = require('./root');
4module.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 } // it is true that this is an invalid HAM comparison.
16 var iv = value, cv = vertex[field];
17
18
19
20
21
22
23
24
25 // TODO: BUG! Need to compare relation to not relation, and choose the relation if there is a state conflict.
26
27
28
29
30
31
32
33
34 if(!val_is(iv) && u !== iv){ return true } // Undefined is okay since a value might not exist on both nodes. // it is true that this is an invalid HAM comparison.
35 if(!val_is(cv) && u !== cv){ return true } // Undefined is okay since a value might not exist on both nodes. // it is true that this is an invalid HAM comparison.
36 var HAM = Gun.HAM(machine, is, cs, iv, cv);
37 if(HAM.err){
38 console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", HAM.err); // this error should never happen.
39 return;
40 }
41 if(HAM.state || HAM.historical || HAM.current){ // TODO: BUG! Not implemented.
42 //opt.lower(vertex, {field: field, value: value, state: is});
43 return;
44 }
45 if(HAM.incoming){
46 union[field] = value;
47 state_ify(union, field, is);
48 return;
49 }
50 if(HAM.defer){ // TODO: BUG! Not implemented.
51 union[field] = value; // WRONG! BUG! Need to implement correct algorithm.
52 state_ify(union, field, is); // WRONG! BUG! Need to implement correct algorithm.
53 // filler algorithm for now.
54 return;
55 /*upper.wait = true;
56 opt.upper.call(state, vertex, field, incoming, ctx.incoming.state); // signals that there are still future modifications.
57 Gun.schedule(ctx.incoming.state, function(){
58 update(incoming, field);
59 if(ctx.incoming.state === upper.max){ (upper.last || function(){})() }
60 }, gun.__.opt.state);*/
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); // TODO: PERF! This will slow things down!
69 // TODO: PERF! Biggest slowdown (after 1ocalStorage) is the above line. Fix! Fix!
70 opt.vertex = vertex;
71 opt.node = node;
72 //obj_map(node._, meta, opt.union); // TODO: Review at some point?
73 if(obj_map(node, map, opt)){ // if this returns true then something was invalid.
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 // TODO: BUG!!!! WHAT ABOUT DEFERRED!???
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 //if(cat.ack){
107 // cat.ack = 1;
108 //}
109 if(!at.put){
110 //if(obj_has(cat, 'put')){ return }
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}); // TODO: PERF! SEE IF WE CAN OPTIMIZE THIS BY MERGING UNION INTO DELTA!
123 graph[soul] = Gun.HAM.union(graph[soul], node) || graph[soul];
124 }, root);
125 // TODO: PERF! Have options to determine if this data should even be in memory on this peer!
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]; // TODO: BUG! Clone!
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
144var Type = Gun;
145var num = Type.num, num_is = num.is;
146var obj = Type.obj, obj_has = obj.has, obj_put = obj.put, obj_to = obj.to, obj_map = obj.map;
147var node = Gun.node, node_soul = node.soul, node_is = node.is, node_ify = node.ify;
148var state = Gun.state, state_is = state.is, state_ify = state.ify;
149var val = Gun.val, val_is = val.is, rel_is = val.rel.is;
150var u;
151
\No newline at end of file