Commit 6bf1d61b158ce574e9cc6736605236ab1895c14f
sort backlinks by asserted time (or branch if available)
fixes #590Matt McKegg committed on 7/18/2017, 4:29:22 AM
Parent: 8aaece459cf35d19c7635e39fdd6996ae3e029d2
Files changed
overrides/patchcore/backlinks/obs.js | changed |
overrides/patchcore/backlinks/obs.js | ||
---|---|---|
@@ -4,8 +4,9 @@ | ||
4 | 4 … | var computed = require('mutant/computed') |
5 | 5 … | var resolve = require('mutant/resolve') |
6 | 6 … | var pull = require('pull-stream') |
7 | 7 … | var onceIdle = require('mutant/once-idle') |
8 … | +var sorted = require('sorted-array-functions') | |
8 | 9 … | |
9 | 10 … | exports.needs = nest({ |
10 | 11 … | 'sbot.pull.backlinks': 'first', |
11 | 12 … | 'sbot.obs.connection': 'first', |
@@ -64,9 +65,9 @@ | ||
64 | 65 … | index: 'DTA' // use asserted timestamps |
65 | 66 … | }), |
66 | 67 … | pull.drain((msg) => { |
67 | 68 … | var value = resolve(collection) |
68 | - value.push(msg) | |
69 … | + sorted.add(value, msg, compareAsserted) | |
69 | 70 … | collection.set(value) |
70 | 71 … | }, () => { |
71 | 72 … | sync.set(true) |
72 | 73 … | }) |
@@ -117,4 +118,26 @@ | ||
117 | 118 … | function unsubscribe (id) { |
118 | 119 … | onceTrue(api.sbot.obs.connection(), (sbot) => sbot.patchwork.liveBacklinks.unsubscribe(id)) |
119 | 120 … | } |
120 | 121 … | } |
122 … | + | |
123 … | +function compareAsserted (a, b) { | |
124 … | + if (isReplyTo(a, b)) { | |
125 … | + return -1 | |
126 … | + } else if (isReplyTo(b, a)) { | |
127 … | + return 1 | |
128 … | + } else { | |
129 … | + return a.value.timestamp - b.value.timestamp | |
130 … | + } | |
131 … | +} | |
132 … | + | |
133 … | +function isReplyTo (maybeReply, msg) { | |
134 … | + return (includesOrEquals(maybeReply.branch, msg.key)) | |
135 … | +} | |
136 … | + | |
137 … | +function includesOrEquals (array, value) { | |
138 … | + if (Array.isArray(array)) { | |
139 … | + return array.includes(value) | |
140 … | + } else { | |
141 … | + return array === value | |
142 … | + } | |
143 … | +} |
Built with git-ssb-web