git ssb

10+

Matt McKegg / patchwork



Commit 198fa7fcacacdb6a915c1f998c047c40742efbf2

generate recently updated list in sbot.patchwork plugin

avoid loading 4000 messages at startup
Matt McKegg committed on 7/3/2017, 5:58:42 AM
Parent: 7047d966b1282237ee4af159b092da67d58fc56d

Files changed

modules/page/html/render/public.jschanged
modules/profile/obs/rank.jschanged
modules/profile/obs/recently-updated.jschanged
sbot/index.jschanged
sbot/recent-feeds.jsadded
modules/page/html/render/public.jsView
@@ -95,9 +95,9 @@
9595 return result
9696
9797 function getSidebar () {
9898 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)
100100 })
101101 return [
102102 h('button -pub -full', {
103103 'ev-click': api.invite.sheet
modules/profile/obs/rank.jsView
@@ -13,14 +13,14 @@
1313
1414 var result = computed([ids, recent], (ids, recent) => {
1515 var result = []
1616 ids.forEach((id) => {
17- if (recent.has(id)) {
17 + if (recent.includes(id)) {
1818 result.push(id)
1919 }
2020 })
2121 ids.forEach((id) => {
22- if (!recent.has(id)) {
22 + if (!recent.includes(id)) {
2323 result.push(id)
2424 }
2525 })
2626 if (max) {
@@ -33,16 +33,4 @@
3333
3434 return result
3535 })
3636 }
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.jsView
@@ -1,14 +1,13 @@
11 var pull = require('pull-stream')
2-var pullCat = require('pull-cat')
2 +var Value = require('mutant/value')
33 var computed = require('mutant/computed')
4-var MutantPullReduce = require('mutant-pull-reduce')
54 var throttle = require('mutant/throttle')
65 var nest = require('depnest')
76 var hr = 60 * 60 * 1000
87
98 exports.needs = nest({
10- 'sbot.pull.feed': 'first'
9 + 'sbot.pull.stream': 'first'
1110 })
1211
1312 exports.gives = nest('profile.obs.recentlyUpdated')
1413
@@ -22,29 +21,41 @@
2221
2322 function load () {
2423 if (instance) return
2524
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 + })
3152 )
3253
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-
4354 instance = throttle(result, 2000)
44- instance.sync = result.sync
55 + instance.sync = sync
4556
4657 instance.has = function (value) {
47- return computed(instance, x => x.has(value))
58 + return computed(instance, x => x.includes(value))
4859 }
4960 }
5061 }
sbot/index.jsView
@@ -2,8 +2,9 @@
22 var Subscriptions = require('./subscriptions')
33 var Roots = require('./roots')
44 var Progress = require('./progress')
55 var Search = require('./search')
6 +var RecentFeeds = require('./recent-feeds')
67
78 exports.name = 'patchwork'
89 exports.version = require('../package.json').version
910 exports.manifest = {
@@ -12,8 +13,9 @@
1213 roots: 'source',
1314 latest: 'source',
1415 linearSearch: 'source',
1516 progress: 'source',
17 + recentFeeds: 'source',
1618 getSubscriptions: 'async',
1719 getChannels: 'async'
1820 }
1921
@@ -22,15 +24,17 @@
2224 var channels = Channels(ssb, config)
2325 var subscriptions = Subscriptions(ssb, config)
2426 var roots = Roots(ssb, config)
2527 var search = Search(ssb, config)
28 + var recentFeeds = RecentFeeds(ssb, config)
2629
2730 return {
2831 channels: channels.stream,
2932 subscriptions: subscriptions.stream,
3033 roots: roots.read,
3134 latest: roots.latest,
3235 progress: progress.stream,
36 + recentFeeds: recentFeeds.stream,
3337 linearSearch: search.linear,
3438 getSubscriptions: subscriptions.get,
3539 getChannels: channels.get
3640 }
sbot/recent-feeds.jsView
@@ -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