Commit 74b228c1c5b0fdbb83ac992185d15708791387ec
Put new notifications at the top of the list
cel committed on 12/1/2016, 4:55:54 AMParent: ef3f554c1d8d87d0d93d1c6b0f3cc21dfdc5022b
Files changed
lib/index-cache.js | changed |
lib/index-cache.js | ||
---|---|---|
@@ -56,20 +56,8 @@ | ||
56 | 56 … | }) |
57 | 57 … | } |
58 | 58 … | } |
59 | 59 … | |
60 | -function sortMsgs(obj) { | |
61 | - // sort the notifications, but keep them in an object. | |
62 | - // usually the javascript engine preserves the order of the keys. | |
63 | - var out = {} | |
64 | - ssbSort(Object.keys(obj).map(function (id) { | |
65 | - return obj[id] | |
66 | - })).reverse().forEach(function (msg) { | |
67 | - out[msg.key] = msg | |
68 | - }) | |
69 | - return out | |
70 | -} | |
71 | - | |
72 | 60 … | module.exports = function (sbot, web) { |
73 | 61 … | var start = new Date |
74 | 62 … | var feedId |
75 | 63 … | |
@@ -82,9 +70,10 @@ | ||
82 | 70 … | var issuesPRsDone = multicb({ pluck: 1, spread: true }) |
83 | 71 … | var issuesPRsCb = issuesPRsDone() |
84 | 72 … | var threads = {/* id */} |
85 | 73 … | var myRepos = {/* id */} |
86 | - var notifications = {/* id: msg */} | |
74 … | + var notifications = [/* msg */] | |
75 … | + var notificationIds = {/* id */} | |
87 | 76 … | var notificationsCount = 0 |
88 | 77 … | var notificationsReady = false |
89 | 78 … | |
90 | 79 … | var linksTo = {/* key: [key] */} |
@@ -182,9 +171,9 @@ | ||
182 | 171 … | issuesPRsCb() |
183 | 172 … | getNewLinks() |
184 | 173 … | removeLinked(function () { |
185 | 174 … | notificationsReady = true |
186 | - notifications = sortMsgs(notifications) | |
175 … | + ssbSort(notifications).reverse() | |
187 | 176 … | console.log('Notifications ready', (new Date - start)/1000 + 's') |
188 | 177 … | }) |
189 | 178 … | }) |
190 | 179 … | }) |
@@ -267,11 +256,12 @@ | ||
267 | 256 … | } |
268 | 257 … | |
269 | 258 … | function gotNotification(msg) { |
270 | 259 … | if (msg.value.author === feedId) return |
271 | - if (notifications[msg.key]) return | |
260 … | + if (notificationIds[msg.key]) return | |
261 … | + notificationIds[msg.key] = true | |
262 … | + notifications.unshift(msg) | |
272 | 263 … | notificationsCount++ |
273 | - notifications[msg.key] = msg | |
274 | 264 … | } |
275 | 265 … | |
276 | 266 … | function removeLinked(cb) { |
277 | 267 … | // remove messages that we acknowledged |
@@ -288,12 +278,21 @@ | ||
288 | 278 … | }) |
289 | 279 … | ) |
290 | 280 … | } |
291 | 281 … | |
282 … | + function takeMsg(arr, id) { | |
283 … | + for (var i = 0; i < arr.length; i++) { | |
284 … | + if (arr[i].key === id) { | |
285 … | + return arr.splice(i, 1)[0] | |
286 … | + } | |
287 … | + } | |
288 … | + } | |
289 … | + | |
292 | 290 … | function removeLink(link) { |
293 | 291 … | var id = link.dest || link.link |
294 | - var msg = notifications[id] | |
295 | - delete notifications[id] | |
292 … | + if (!notificationIds[id]) return | |
293 … | + delete notificationIds[id] | |
294 … | + var msg = takeMsg(notifications, id) | |
296 | 295 … | if (msg) { |
297 | 296 … | notificationsCount-- |
298 | 297 … | ssbMsgs.indexLinks(msg, {msg: true}, removeLink) |
299 | 298 … | } |
Built with git-ssb-web