-
Notifications
You must be signed in to change notification settings - Fork 1
/
sb
218 lines (214 loc) · 13 KB
/
sb
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
<div className={'round bg-white root class'}>
<div className={'object-children'}>
{(window.conflictChecker = (data)=>{
if (data.name !== "Server") data = data.model.$Server;
let arr = data.$changes.values.map(c=> ({id: c.id, a:c.$action.value[0], from:c.$from.value, c: c.$clientCounter.value, s:c.$serverCounter.value}));
// let arr = [{id:1, a:"a", c:101, s:104, from:"C"}, {id:2, a:"a", c:100, s:105, from:"D"}];
let conflicts = [];
for (let i = 0; i < arr.length; i++) {
// super inefficient O(N^2) but this is just a demo
let a1 = arr[i];
for (let j = 0; j < arr.length; j++) {
if (i == j) continue;
let a2 = arr[j];
if (a2.from === a1.from || a2.a !== a1.a) continue;
if (a2.c >= a1.c && a2.c < a1.s) { conflicts.push([a1, a2]); }
}
}
let colors = ["#faa", "#aaf", "#aaa", "#fcc", "#ccf", "#faf", "#fcf", "#ccc"];
let conflictmap = {};
let i = 0;
for (let c of conflicts){
conflictmap[c[0].id] = {...c[0], color: colors[i % colors.length]};
conflictmap[c[1].id] = {...c[1], color: colors[i % colors.length]};
i++;
}
window.conflictmap = conflictmap;
window.conflicts = conflicts;
return conflictmap;
})(data) && (window.sendToServer = (data, child)=>{
console.log("sendtoserver 0", {name:data.name, tr:document.getElementById("TD"+child.id)});
if(data.name=="Server") return;
let tr0 = document.getElementById("TD"+child.id);
if (!tr0) return;
let tr = tr0.cloneNode(true);
tr0.style.opacity=0.0;
let style0 = tr.attributes.style.value;
let id = '1699809153345_Pointer1699808470290_5270^graph0^1699809163160_Pointer1699808470290_7839~1699809164286_Pointer1699808470290_14109';
let edge = LEdge.wrap(id);
console.log("sendtoserver 1", {edge, id});
let path = 'offset-path: path("' + edge.segments.all[0].d +'"); ';
tr.setAttribute("style", path+tr.dataset.style);
let model = data.model;
$("[data-dataid='1699809153345_Pointer1699808470290_5270'] .edges").append(tr);
setTimeout(() => {
let server = model.$Server;
let serverchanges = server.$changes;
let v = [...serverchanges.values];
v.push(child);
//tr.setAttribute("style", "display: none");
tr.parentElement.removeChild(tr);
serverchanges.values = v;
let servercounter = 0;
for (let ctr of v){
let ctrv = ctr.$serverCounter.value;
if (ctrv > servercounter) servercounter = ctrv;
}
let sc = server.$serverCounter.value;
if (sc > servercounter) servercounter = sc;
child.$serverCounter.value = servercounter+1;
server.$serverCounter.value = servercounter+1;
data.$serverCounter.value = servercounter+1;
tr.style.display = "none";
/*
oldParent.append(tr);
function afterparentchange(delay) {
if (oldParent !== tr.parentElement) {
setTimeout(()=>afterparentchange(delay), delay);
return;
}
serverchanges.values = v;
}*/
//afterparentchange();
// data.father = model.$Server.id;
// tr.parentElement.removeChild(tr);
}, animationDuration);
})&&
(window.addAction = (data, aname, avalue, sender) => {
let model = data.model;
let o = data;//model["$ClientC"];
let instance = data.$changes.type; // model.instanceof.$default.$Change
let TRANSACTION = (f) => f();
console.log("new change made 1:");
TRANSACTION(() => {
let a = model.addObject(instance, "C" + data.$changes.values.length)
console.log("new change made 2:", {a});
setTimeout(()=>TRANSACTION(() => {
console.log("new change made 22:", {a, instance});
let la = LObject.wrap(a.id);
console.log("new change made 3:", {a, la, instance, ainstance: a.instanceof, cc:la.$clientCounter, sc:la.$serverCounter});
la.instanceof = instance.id;
setTimeout(()=>TRANSACTION(() => {
la = la.r;
console.log("new change made 4:", {a, la, instance, ainstance: a.instanceof, cc:la.$clientCounter, sc:la.$serverCounter});
let maxcounter = data.$serverCounter.value || 0;
console.log("new change made 5:", {maxcounter,vals:o.$changes.values.map(c => c&&c.$clientCounter.value), sc:o.$serverCounter.value});
la.$action.value = aname + " = " + (avalue || maxcounter);
la.$from.value = sender || (data.name.indexOf("C") != -1 && "C" || "D");
if (data.name=="Server") {
// maxcounter += 1;
la.$serverCounter.value = maxcounter + 1;
data.$serverCounter = maxcounter + 1;
}
la.$clientCounter.value = maxcounter;
// la.$serverCounter.value = undefined; // +o.$serverCounter.value || 0;
console.log("new change made 8:", {o, a, la, instance, ainstance: a.instanceof, cc:la.$clientCounter, sc:la.$serverCounter});
let childd = [...o.$changes.__raw.values]; //.map(c => c.id)];
childd.push(a.id);
o.$changes.values = childd;
console.log("new change made 9:", {a, la, instance, ainstance: a.instanceof, cc:la.$clientCounter, sc:la.$serverCounter});
}, 3))
}, 3))
})
}) && <b />
}
<table style={{textAlign: "center"}}>
{data.$changes.values.reverse().slice(0, 10).map((child, index) => {
if (!child || !child.instanceof) return "";
return [
<tr id={"TD"+child.id} style={{borderBottom: "1px solid black",
backgroundColor: conflictmap[child.id] && conflictmap[child.id].color || 'white'
}} data-style={
'animation: animationoffset ' + (animationDuration/1000) + 's infinite linear; '+
'animation-play-state: running; '+
'background: white;' +
'outline: 1px solid black;'
} key={"V"+child.id}>
{data.name=="Server"&&<td style={{borderRight: "1px solid black"}}>
{child.$from.value}
</td>}
<td style={{borderLeft: "1px solid transparent"}}>
<Input className="" style={{display: "inline-block", width: "70px"}}
inputStyle={{display: "inline-block", width: "65px", textAlign:"center", background:"inherit"}}
data={child.$action} field="value"/>
</td>
<td style={{borderLeft: "1px solid black"}}>{
data.name === "ClientC" ? <div onClick={()=>{
window.child = child;
console.log(child, sendToServer);
sendToServer(data, child)}}>---</div> :
<Input className="" style={{display: "inline-block", width: "70px"}}
inputStyle={{display: "inline-block", width: "65px", textAlign:"center", background:"inherit"}}
data={child.$serverCounter} field="value"/>
}</td>
<td style={{borderLeft: "1px solid black"}}>
<Input className="" style={{display: "inline-block", width: "70px"}}
inputStyle={{display: "inline-block", width: "65px", textAlign:"center", background:"inherit"}}
data={child.$clientCounter} field="value"/>
</td>
</tr>]
})}
<tr>{data.name=="Server" && <th rowspan="2" style={{borderRight: "1px solid black", whiteSpace: "pre"}}>From</th>}
<th className="" rowSpan="1"
style={{borderLeft: "1px solid transparent", whiteSpace: "pre"}}>
<button className="btn-primary btn p-1 m-1 bi bi-play" onClick={() => {
let letters = ["a", "b", "c", "d", "e","f","g"];
let numbers = [1,2,3,4,5,6,7,8,9];
let timers = [];
let times = 3;
let maxDelay = 1000;
TRANSACTION(()=>{
while(times--){
let lefti = Math.floor(Math.random() * (letters.length));
let righti = Math.floor(Math.random() * (numbers.length));
setTimeout(()=>{addAction(data, letters[lefti], numbers[righti])}, maxDelay * Math.random());
}
})
function clearChildrens(data, delay=200){
data = data.r;
console.log("sendtoserver loop");
let children = [...data.$changes.values.filter(c=>!!c)];
if (!data.$isConnected.value) {data.$isConnected.value = true;}
//setTimeout(()=> {data.$serverCounter.value = data.model.$Server.$serverCounter.value}, 2)
setTimeout(()=> {data.$isConnected.value = false}, 9000+maxDelay);
//console.clear();
for (let i = 0; i < children.length; i++){
setTimeout(()=>{sendToServer(data, children[i])}, maxDelay * Math.random());
}
if (children.length) setTimeout(()=>()=>TRANSACTION(()=>clearChildrens(data, delay)), delay);
else setTimeout(()=> {data.$isConnected.value = false}, delay);
}
if(data.name!=="Server") setTimeout(()=>TRANSACTION(()=>clearChildrens(data)), maxDelay*2);
return 1;
}}></button>
<button className="btn-success btn p-1 m-1"
onClick={()=>{addAction(data, "a", undefined);}}>+</button>
</th>
<td className="" colspan={(data.name=="Server" || true) && "2" || "1"} style={{borderLeft: "1px solid black"}}>{data.$serverCounter.value}</td>
{(data.name!=="Server" && false) && <td className="" style={{borderLeft: "1px solid black"}}>{data.$changes.values[0] && data.$changes.values[0].name || data.$serverCounter.value}</td>}
</tr>
<tr>
<th className="" rowSpan="2"
style={{borderLeft: "1px solid transparent", whiteSpace: "pre"}}>Actions
</th>
<th className="" colspan={data.name=="Server" && "2" || "2"} style={{borderLeft: "1px solid black"}}>
<div className="m-auto" style={{width: "min-content"}}>Server Counter</div>
</th>
{(false && data.name!=="Server") && <th className="" style={{borderLeft: "1px solid black"}}>
<div className="m-auto" style={{width: "min-content"}}>Client Counter</div>
</th>}
</tr>
</table>
</div>
<hr/>
<label className={'ms-1'}>
<Input jsxLabel={
<b className={'my-auto class-name'} onClick={()=>{
let arr = data.$changes.values;
data.$changes.values = [];
for ( let c of arr) c&&c.delete();
data.$changes.__raw.values = [];
}}> {data.instanceof ? data.instanceof.name : "Object"}:</b>}
data={data.id} field={'name'} hidden={true} autosize={true}/>
</label>
</div>