git ssb

10+

Matt McKegg / patchwork



Tree: 856d88afbef69775a9d80aca2652a45f598a0163

Files: 856d88afbef69775a9d80aca2652a45f598a0163 / modules / profile / obs / following.js

1401 bytesRaw
1var pull = require('pull-stream')
2var computed = require('mutant/computed')
3var MutantPullReduce = require('mutant-pull-reduce')
4var throttle = require('mutant/throttle')
5var nest = require('depnest')
6
7exports.needs = nest({
8 'sbot.pull.userFeed': 'first'
9})
10
11exports.gives = nest({
12 'profile.obs': ['following']
13})
14
15exports.create = function (api) {
16 var cache = {}
17
18 return nest({
19 'profile.obs': {following}
20 })
21
22 function following (userId) {
23 if (cache[userId]) {
24 return cache[userId]
25 } else {
26 var stream = pull(
27 api.sbot.pull.userFeed({id: userId, live: true}),
28 pull.filter((msg) => {
29 return !msg.value || msg.value.content.type === 'contact'
30 })
31 )
32
33 var result = MutantPullReduce(stream, (result, msg) => {
34 var c = msg.value.content
35 if (c.contact) {
36 if (typeof c.following === 'boolean') {
37 if (c.following) {
38 result.add(c.contact)
39 } else {
40 result.delete(c.contact)
41 }
42 }
43 }
44 return result
45 }, {
46 startValue: new Set(),
47 nextTick: true
48 })
49
50 var instance = throttle(result, 2000)
51 instance.sync = result.sync
52
53 instance.has = function (value) {
54 return computed(instance, x => x.has(value))
55 }
56
57 cache[userId] = instance
58 return instance
59 }
60 }
61}
62

Built with git-ssb-web