git ssb

10+

Matt McKegg / patchwork



Tree: b6089670d854fcefa6bc5ca05636a801707deb30

Files: b6089670d854fcefa6bc5ca05636a801707deb30 / modules / obs-following.js

1223 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_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