Files: 0860eec08ace9e3fda8b85223b0bf924740870ec / modules / channel / obs / subscribers.js
810 bytesRaw
1 | var nest = require('depnest') |
2 | var MutantPullReduce = require('mutant-pull-reduce') |
3 | |
4 | exports.needs = nest({ |
5 | 'keys.sync.id': 'first', |
6 | 'sbot.pull.stream': 'first' |
7 | }) |
8 | |
9 | exports.gives = nest('channel.obs.subscribers') |
10 | |
11 | exports.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