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