Files: da8b90d365d449e737c606b1ba23cc612834778e / progress.js
1792 bytesRaw
1 | |
2 | //Question: can I use req to reliably track how many |
3 | //messages we are expecting to receive? |
4 | |
5 | //Math.max(null, null) is zero, but we want null |
6 | function max (a, b) { |
7 | return a == null ? b : b == null ? a : Math.max(a, b) |
8 | } |
9 | |
10 | function process(data, state) { |
11 | var local = state.local |
12 | var remote = state.remote |
13 | var _seq = remote.req == -1 ? -1 : max(remote.seq, remote.req) |
14 | var seq = local.req == -1 ? -1 : max(local.seq, local.req) |
15 | |
16 | //don't count this feed, because we do not expect |
17 | //to exchange anything. |
18 | //this should only happen when there are many connections. |
19 | if(!local.tx && seq > _seq) return data |
20 | |
21 | //req represents what they _know_ we have because |
22 | //we have either mentioned it in a note or sent it. |
23 | |
24 | if(seq == null) { |
25 | //we havn't decided if we want this feed yet |
26 | data.unknown ++ |
27 | } else if(seq === -1) { |
28 | //we have decided we do not want this feed. don't count it. |
29 | } else { |
30 | if(_seq == null) { |
31 | data.unknown ++ |
32 | } else if(_seq === -1) { |
33 | //they have told us they do not want it. |
34 | //this means we do not expect to send anything. |
35 | //so don't count this feed. |
36 | } else { |
37 | data.feeds ++ |
38 | |
39 | if(seq == _seq) |
40 | data.sync ++ |
41 | |
42 | data.total += Math.max( |
43 | seq - remote.req, |
44 | _seq - (local.req || local.seq) |
45 | ) |
46 | if(seq > _seq && local.tx) |
47 | data.send += seq - _seq |
48 | else if(seq < _seq && remote.tx) |
49 | data.recv += _seq - seq |
50 | } |
51 | } |
52 | |
53 | return data |
54 | } |
55 | |
56 | module.exports = function (states) { |
57 | var data = { |
58 | sync: 0, feeds: 0, |
59 | recv: 0, send: 0, total: 0, |
60 | unknown: 0, |
61 | } |
62 | |
63 | for(var k in states) |
64 | //Only count this as an out of sync feed if we either can send |
65 | //to it or receive from it. |
66 | data = process(data, states[k]) |
67 | |
68 | return data |
69 | } |
70 | |
71 | |
72 | |
73 | |
74 | |
75 |
Built with git-ssb-web