git ssb

10+

Matt McKegg / patchwork



Tree: a64c5f3b893f894105903efc339ec3209633ef1d

Files: a64c5f3b893f894105903efc339ec3209633ef1d / modules / channel / obs / subscribers.js

810 bytesRaw
1var nest = require('depnest')
2var MutantPullReduce = require('mutant-pull-reduce')
3
4exports.needs = nest({
5 'keys.sync.id': 'first',
6 'sbot.pull.stream': 'first'
7})
8
9exports.gives = nest('channel.obs.subscribers')
10
11exports.create = function (api) {
12 return nest('channel.obs.subscribers', function (channel) {
13 var stream = api.sbot.pull.stream(sbot => sbot.patchwork.subscriptions({live: true, channel}))
14 return MutantPullReduce(stream, (state, msg) => {
15 if (msg.value) {
16 if (!state.includes(msg.from)) {
17 state.push(msg.from)
18 }
19 } else {
20 var index = state.indexOf(msg.from)
21 if (index >= 0) {
22 state.splice(index, 1)
23 }
24 }
25 return state
26 }, {
27 startValue: [],
28 nextTick: true,
29 sync: true
30 })
31 })
32}
33

Built with git-ssb-web