git ssb

1+

Daan Patchwork / patchwork



Tree: 81788a3aabacb716a48ed9cff993466e6eeffb3b

Files: 81788a3aabacb716a48ed9cff993466e6eeffb3b / lib / depject / channel / obs / subscribers.js

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

Built with git-ssb-web