forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
63 lines (55 loc) · 1.36 KB
/
socket.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
57
58
59
60
61
62
63
module.exports = function(opts) {
var anaMoveTimeout;
var anaDestsTimeout;
var anaDestsCache = {};
var handlers = {
node: function(data) {
clearTimeout(anaMoveTimeout);
opts.addNode(data.node, data.path);
},
stepFailure: function(data) {
clearTimeout(anaMoveTimeout);
opts.reset();
},
dests: function(data) {
anaDestsCache[data.path] = data;
opts.addDests(data.dests, data.path, data.opening);
clearTimeout(anaDestsTimeout);
},
destsFailure: function(data) {
console.log(data);
clearTimeout(anaDestsTimeout);
}
};
var sendAnaMove = function(req) {
clearTimeout(anaMoveTimeout);
opts.send('anaMove', req);
anaMoveTimeout = setTimeout(function() {
sendAnaMove(req);
}, 3000);
};
var sendAnaDests = function(req) {
clearTimeout(anaDestsTimeout);
if (anaDestsCache[req.path]) setTimeout(function() {
handlers.dests(anaDestsCache[req.path]);
}, 10);
else {
opts.send('anaDests', req);
anaDestsTimeout = setTimeout(function() {
sendAnaDests(req);
}, 3000);
}
};
return {
send: opts.send,
receive: function(type, data) {
if (handlers[type]) {
handlers[type](data);
return true;
}
return false;
},
sendAnaMove: sendAnaMove,
sendAnaDests: sendAnaDests
};
}