git ssb

10+

Matt McKegg / patchwork



Tree: 02b8eb670ca5703df29be0edc856fb4e91b20511

Files: 02b8eb670ca5703df29be0edc856fb4e91b20511 / plugs / channel / obs / recent.js

1685 bytesRaw
1// uses lib/flumeview-channels
2
3var nest = require('depnest')
4var pull = require('pull-stream')
5
6var { Value, Dict, Struct, computed, throttle } = require('mutant')
7
8exports.needs = nest({
9 'sbot.pull.stream': 'first'
10})
11
12exports.gives = nest({
13 'channel.obs.recent': true
14})
15
16exports.create = function (api) {
17 var recentChannels = null
18 var channelsLookup = null
19
20 return nest({
21 'channel.obs.recent': function () {
22 load()
23 return recentChannels
24 }
25 })
26
27 function load () {
28 if (!recentChannels) {
29 var sync = Value(false)
30 channelsLookup = Dict()
31
32 pull(
33 api.sbot.pull.stream(sbot => sbot.patchwork.channels({live: true})),
34 pull.drain(data => {
35 channelsLookup.transaction(() => {
36 for (var channel in data) {
37 var obs = channelsLookup.get(channel)
38 if (!obs) {
39 obs = ChannelRef(channel)
40 channelsLookup.put(channel, obs)
41 }
42 var count = data[channel].count != null ? data[channel].count : obs.count() + 1
43 var updatedAt = data[channel].timestamp
44 obs.set({ id: channel, updatedAt, count })
45 }
46 })
47 if (!sync()) {
48 sync.set(true)
49 }
50 })
51 )
52
53 recentChannels = computed(throttle(channelsLookup, 1000), (lookup) => {
54 var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.updatedAt - a.updatedAt).map(x => x.id)
55 return values
56 })
57 recentChannels.sync = sync
58 }
59 }
60}
61
62function ChannelRef (id) {
63 return Struct({
64 id,
65 updatedAt: Value(0),
66 count: Value(0)
67 }, {merge: true})
68}
69

Built with git-ssb-web