Files: 202db7a1462af5a1b1dbeb6b5c220468060f96ea / channel / obs.js
813 bytesRaw
1 | const nest = require('depnest') |
2 | const ref = require('ssb-ref') |
3 | const computed = require('mutant/computed') |
4 | |
5 | exports.needs = nest({ |
6 | 'keys.sync.id': 'first', |
7 | 'channel.obs.subscribed': 'first', |
8 | }) |
9 | |
10 | exports.gives = nest('channel.obs.isSubscribedTo') |
11 | |
12 | exports.create = function (api) { |
13 | var subscriptions = {} |
14 | var myId |
15 | |
16 | return nest('channel.obs.isSubscribedTo', isSubscribedTo) |
17 | |
18 | function isSubscribedTo (channel, id) { |
19 | if (!ref.isFeed(id)) { |
20 | id = getMyId() |
21 | } |
22 | |
23 | return computed(getSubscriptions(id), (v) => v.has(channel)) |
24 | } |
25 | |
26 | //cache getters |
27 | |
28 | function getMyId () { |
29 | if (!myId) myId = api.keys.sync.id() |
30 | return myId |
31 | } |
32 | |
33 | function getSubscriptions (id) { |
34 | if (!subscriptions[id]) subscriptions[id] = api.channel.obs.subscribed(id) |
35 | return subscriptions[id] |
36 | } |
37 | } |
38 | |
39 |
Built with git-ssb-web