-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathham.js
56 lines (53 loc) · 1.97 KB
/
ham.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
function HAM(machineState, incomingState, currentState, incomingValue, currentValue){
if(machineState < incomingState){
return {defer: true};
}
if(incomingState < currentState){
return {historical: true};
}
if(currentState < incomingState){
return {converge: true, incoming: true};
}
if(incomingState === currentState){
incomingValue = Lexical(incomingValue) || "";
currentValue = Lexical(currentValue) || "";
if(incomingValue === currentValue){
return {state: true};
}
if(incomingValue < currentValue){
return {converge: true, current: true};
}
if(currentValue < incomingValue){
return {converge: true, incoming: true};
}
}
return {err: "Invalid CRDT Data: "+ incomingValue +" to "+ currentValue +" at "+ incomingState +" to "+ currentState +"!"};
}
var Lexical = JSON.stringify
HAM.mix = function(change, graph){
var machine = (+new Date), diff;
Object.keys(change).forEach(function(soul){
var node = change[soul];
Object.keys(node).forEach(function(key){
var val = node[key];
if('_' == key){ return }
var state = node._['>'][key];
var was = (graph[soul]||{_:{'>':{}}})._['>'][key] || -Infinity;
var known = (graph[soul]||{})[key];
var ham = HAM(machine, state, was, val, known);
if(!ham.incoming){
if(ham.defer){
console.log("DEFER", key, val);
// you'd need to implement this yourself.
}
return;
}
(diff || (diff = {}))[soul] = diff[soul] || {_:{'#':soul, '>':{}}};
graph[soul] = graph[soul] || {_:{'#':soul, '>':{}}};
graph[soul][key] = diff[soul][key] = val;
graph[soul]._['>'][key] = diff[soul]._['>'][key] = state;
})
});
return diff;
}
try{module.exports = HAM}catch(e){}