Files: 9216a0ee5a7da1602222d794ef538ca059e1face / modules / obs-following.js
1223 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_following = 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 === 'contact' |
17 | }) |
18 | ) |
19 | |
20 | var result = MutantPullReduce(stream, (result, msg) => { |
21 | var c = msg.value.content |
22 | if (c.contact) { |
23 | if (typeof c.following === 'boolean') { |
24 | if (c.following) { |
25 | result.add(c.contact) |
26 | } else { |
27 | result.delete(c.contact) |
28 | } |
29 | } |
30 | } |
31 | return result |
32 | }, { |
33 | startValue: new Set(), |
34 | nextTick: true |
35 | }) |
36 | |
37 | var instance = throttle(result, 2000) |
38 | instance.sync = result.sync |
39 | |
40 | instance.has = function (value) { |
41 | return computed(instance, x => x.has(value)) |
42 | } |
43 | |
44 | cache[userId] = instance |
45 | return instance |
46 | } |
47 | } |
48 |
Built with git-ssb-web