Files: b54dfb523349bba5720363e6c177039b9762ad5d / about / pull / updates.js
1435 bytesRaw
1 | const nest = require('depnest') |
2 | const pull = require('pull-stream') |
3 | const Notify = require('pull-notify') |
4 | const { isMsg, isFeed } = require('ssb-ref') |
5 | |
6 | exports.gives = nest('about.pull.updates') |
7 | |
8 | exports.needs = nest({ |
9 | 'keys.sync.id': 'first', |
10 | 'message.obs.likes': 'first', |
11 | 'sbot.pull.stream': 'first' |
12 | }) |
13 | |
14 | exports.create = (api) => { |
15 | const cache = {} |
16 | var listening = false |
17 | |
18 | return nest('about.pull.updates', updates) |
19 | |
20 | function updates (key) { |
21 | if (!(isMsg(key) || isFeed(key))) throw new Error(`about.pull.updates expects a valid message/ feed key, got ${key}`) |
22 | startListening() |
23 | |
24 | if (!cache[key]) cache[key] = Notify() |
25 | return cache[key].listen() |
26 | } |
27 | |
28 | function startListening () { |
29 | if (listening) return |
30 | |
31 | const opts = { |
32 | live: true, |
33 | old: false, |
34 | query: [{ |
35 | $filter: { |
36 | value: { |
37 | timestamp: { $gt: 0 }, |
38 | content: { |
39 | type: 'about', |
40 | about: { $truthy: true } |
41 | } |
42 | } |
43 | } |
44 | }, { |
45 | $map: { |
46 | about: ['value', 'content', 'about'] |
47 | } |
48 | }] |
49 | } |
50 | pull( |
51 | api.sbot.pull.stream(server => server.query.read(opts)), |
52 | pull.filter(m => !m.sync), |
53 | pull.drain( |
54 | ({ about }) => { |
55 | if (!cache[about]) return |
56 | |
57 | cache[about](1) // emit a minimal update! |
58 | }, |
59 | (err) => console.error(err) |
60 | ) |
61 | ) |
62 | |
63 | listening = true |
64 | } |
65 | } |
66 |
Built with git-ssb-web