git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 62ee1b7bfd9fae4bbec8650f48cc6d5494403927

Files: 62ee1b7bfd9fae4bbec8650f48cc6d5494403927 / modules / obs-subscribed-channels.js

1350 bytesRaw
1var pull = require('pull-stream')
2var computed = require('@mmckegg/mutant/computed')
3var MutantPullReduce = require('../lib/mutant-pull-reduce')
4var plugs = require('patchbay/plugs')
5var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
6var cache = {}
7var throttle = require('@mmckegg/mutant/throttle')
8
9exports.obs_subscribed_channels = function (userId) {
10 if (cache[userId]) {
11 return cache[userId]
12 } else {
13 var stream = pull(
14 sbot_user_feed({id: userId, live: true}),
15 pull.filter((msg) => {
16 return !msg.value || msg.value.content.type === 'channel'
17 })
18 )
19
20 var result = MutantPullReduce(stream, (result, msg) => {
21 var c = msg.value.content
22 if (typeof c.channel === 'string' && c.channel) {
23 var channel = c.channel.trim()
24 if (channel) {
25 if (typeof c.subscribed === 'boolean') {
26 if (c.subscribed) {
27 result.add(channel)
28 } else {
29 result.delete(channel)
30 }
31 }
32 }
33 }
34 return result
35 }, {
36 startValue: new Set(),
37 nextTick: true
38 })
39
40 var instance = throttle(result, 2000)
41 instance.sync = result.sync
42
43 instance.has = function (value) {
44 return computed(instance, x => x.has(value))
45 }
46
47 cache[userId] = instance
48 return instance
49 }
50}
51

Built with git-ssb-web