Commit 198fa7fcacacdb6a915c1f998c047c40742efbf2
generate recently updated list in sbot.patchwork plugin
avoid loading 4000 messages at startupMatt McKegg committed on 7/3/2017, 5:58:42 AM
Parent: 7047d966b1282237ee4af159b092da67d58fc56d
Files changed
modules/page/html/render/public.js | changed |
modules/profile/obs/rank.js | changed |
modules/profile/obs/recently-updated.js | changed |
sbot/index.js | changed |
sbot/recent-feeds.js | added |
modules/page/html/render/public.js | ||
---|---|---|
@@ -95,9 +95,9 @@ | ||
95 | 95 … | return result |
96 | 96 … | |
97 | 97 … | function getSidebar () { |
98 | 98 … | var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => { |
99 | - return Array.from(recent).filter(x => x !== id && !following.has(x) && !peers.includes(x)).slice(0, 10) | |
99 … | + return recent.filter(x => x !== id && !following.has(x) && !peers.includes(x)).slice(0, 10) | |
100 | 100 … | }) |
101 | 101 … | return [ |
102 | 102 … | h('button -pub -full', { |
103 | 103 … | 'ev-click': api.invite.sheet |
modules/profile/obs/rank.js | ||
---|---|---|
@@ -13,14 +13,14 @@ | ||
13 | 13 … | |
14 | 14 … | var result = computed([ids, recent], (ids, recent) => { |
15 | 15 … | var result = [] |
16 | 16 … | ids.forEach((id) => { |
17 | - if (recent.has(id)) { | |
17 … | + if (recent.includes(id)) { | |
18 | 18 … | result.push(id) |
19 | 19 … | } |
20 | 20 … | }) |
21 | 21 … | ids.forEach((id) => { |
22 | - if (!recent.has(id)) { | |
22 … | + if (!recent.includes(id)) { | |
23 | 23 … | result.push(id) |
24 | 24 … | } |
25 | 25 … | }) |
26 | 26 … | if (max) { |
@@ -33,16 +33,4 @@ | ||
33 | 33 … | |
34 | 34 … | return result |
35 | 35 … | }) |
36 | 36 … | } |
37 | - | |
38 | -function compare (a, b, recent) { | |
39 | - var hasA = recent.has(a) | |
40 | - var hasB = recent.has(b) | |
41 | - if (hasA && hasB) { | |
42 | - return 0 | |
43 | - } else if (hasA && !hasB) { | |
44 | - return -1 | |
45 | - } else { | |
46 | - return 1 | |
47 | - } | |
48 | -} |
modules/profile/obs/recently-updated.js | ||
---|---|---|
@@ -1,14 +1,13 @@ | ||
1 | 1 … | var pull = require('pull-stream') |
2 | -var pullCat = require('pull-cat') | |
2 … | +var Value = require('mutant/value') | |
3 | 3 … | var computed = require('mutant/computed') |
4 | -var MutantPullReduce = require('mutant-pull-reduce') | |
5 | 4 … | var throttle = require('mutant/throttle') |
6 | 5 … | var nest = require('depnest') |
7 | 6 … | var hr = 60 * 60 * 1000 |
8 | 7 … | |
9 | 8 … | exports.needs = nest({ |
10 | - 'sbot.pull.feed': 'first' | |
9 … | + 'sbot.pull.stream': 'first' | |
11 | 10 … | }) |
12 | 11 … | |
13 | 12 … | exports.gives = nest('profile.obs.recentlyUpdated') |
14 | 13 … | |
@@ -22,29 +21,41 @@ | ||
22 | 21 … | |
23 | 22 … | function load () { |
24 | 23 … | if (instance) return |
25 | 24 … | |
26 | - var stream = pull( | |
27 | - pullCat([ | |
28 | - api.sbot.pull.feed({reverse: true, limit: 4000}), | |
29 | - api.sbot.pull.feed({old: false}) | |
30 | - ]) | |
25 … | + var sync = Value(false) | |
26 … | + var result = Value([]) | |
27 … | + var max = 1000 | |
28 … | + | |
29 … | + pull( | |
30 … | + api.sbot.pull.stream(sbot => sbot.patchwork.recentFeeds({ | |
31 … | + since: Date.now() - (7 * 24 * hr), | |
32 … | + live: true | |
33 … | + })), | |
34 … | + pull.drain((id) => { | |
35 … | + if (id.sync) { | |
36 … | + result.set(result()) | |
37 … | + sync.set(true) | |
38 … | + } else { | |
39 … | + var values = result() | |
40 … | + var index = values.indexOf(id) | |
41 … | + if (sync()) { | |
42 … | + values.length = Math.max(values.length, max) | |
43 … | + if (~index) values.splice(index, 1) | |
44 … | + values.unshift(id) | |
45 … | + result.set(values) | |
46 … | + } else if (values.length < max) { | |
47 … | + values.push(id) | |
48 … | + // don't broadcast until sync | |
49 … | + } | |
50 … | + } | |
51 … | + }) | |
31 | 52 … | ) |
32 | 53 … | |
33 | - var result = MutantPullReduce(stream, (result, msg) => { | |
34 | - if (msg.value.timestamp && Date.now() - msg.value.timestamp < (7 * 24 * hr)) { | |
35 | - result.add(msg.value.author) | |
36 | - } | |
37 | - return result | |
38 | - }, { | |
39 | - startValue: new Set(), | |
40 | - nextTick: true | |
41 | - }) | |
42 | - | |
43 | 54 … | instance = throttle(result, 2000) |
44 | - instance.sync = result.sync | |
55 … | + instance.sync = sync | |
45 | 56 … | |
46 | 57 … | instance.has = function (value) { |
47 | - return computed(instance, x => x.has(value)) | |
58 … | + return computed(instance, x => x.includes(value)) | |
48 | 59 … | } |
49 | 60 … | } |
50 | 61 … | } |
sbot/index.js | ||
---|---|---|
@@ -2,8 +2,9 @@ | ||
2 | 2 … | var Subscriptions = require('./subscriptions') |
3 | 3 … | var Roots = require('./roots') |
4 | 4 … | var Progress = require('./progress') |
5 | 5 … | var Search = require('./search') |
6 … | +var RecentFeeds = require('./recent-feeds') | |
6 | 7 … | |
7 | 8 … | exports.name = 'patchwork' |
8 | 9 … | exports.version = require('../package.json').version |
9 | 10 … | exports.manifest = { |
@@ -12,8 +13,9 @@ | ||
12 | 13 … | roots: 'source', |
13 | 14 … | latest: 'source', |
14 | 15 … | linearSearch: 'source', |
15 | 16 … | progress: 'source', |
17 … | + recentFeeds: 'source', | |
16 | 18 … | getSubscriptions: 'async', |
17 | 19 … | getChannels: 'async' |
18 | 20 … | } |
19 | 21 … | |
@@ -22,15 +24,17 @@ | ||
22 | 24 … | var channels = Channels(ssb, config) |
23 | 25 … | var subscriptions = Subscriptions(ssb, config) |
24 | 26 … | var roots = Roots(ssb, config) |
25 | 27 … | var search = Search(ssb, config) |
28 … | + var recentFeeds = RecentFeeds(ssb, config) | |
26 | 29 … | |
27 | 30 … | return { |
28 | 31 … | channels: channels.stream, |
29 | 32 … | subscriptions: subscriptions.stream, |
30 | 33 … | roots: roots.read, |
31 | 34 … | latest: roots.latest, |
32 | 35 … | progress: progress.stream, |
36 … | + recentFeeds: recentFeeds.stream, | |
33 | 37 … | linearSearch: search.linear, |
34 | 38 … | getSubscriptions: subscriptions.get, |
35 | 39 … | getChannels: channels.get |
36 | 40 … | } |
sbot/recent-feeds.js | ||
---|---|---|
@@ -1,0 +1,23 @@ | ||
1 … | +var pull = require('pull-stream') | |
2 … | +var pullCat = require('pull-cat') | |
3 … | + | |
4 … | +module.exports = function (sbot, config) { | |
5 … | + return { | |
6 … | + stream: function ({live, since} = {}) { | |
7 … | + return pullCat([ | |
8 … | + pull( | |
9 … | + sbot.createFeedStream({reverse: true, gt: since}), | |
10 … | + pull.map(msg => msg.value.author), | |
11 … | + pull.unique() | |
12 … | + ), | |
13 … | + | |
14 … | + // live | |
15 … | + live ? pull.values([{sync: true}]) : pull.empty(), | |
16 … | + live ? pull( | |
17 … | + sbot.createFeedStream({old: false}), | |
18 … | + pull.map(msg => msg.value.author) | |
19 … | + ) : pull.empty() | |
20 … | + ]) | |
21 … | + } | |
22 … | + } | |
23 … | +} |
Built with git-ssb-web