Files: 99aead0a4c4797532b2986ba64d8c30f3d3c5993 / modules / obs-subscribed-channels.js
1350 bytesRaw
1 | var pull = require('pull-stream') |
2 | var computed = require('@mmckegg/mutant/computed') |
3 | var MutantPullReduce = require('../lib/mutant-pull-reduce') |
4 | var plugs = require('patchbay/plugs') |
5 | var sbot_user_feed = plugs.first(exports.sbot_user_feed = []) |
6 | var cache = {} |
7 | var throttle = require('@mmckegg/mutant/throttle') |
8 | |
9 | exports.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