Files: a6ff6e81fe4c9b6ccd9cc84b5d8739674900b38f / tag / obs / all.js
1455 bytesRaw
1 | var nest = require('depnest') |
2 | var { Dict, Array } = require('mutant') |
3 | var MutantPullReduce = require('mutant-pull-reduce') |
4 | |
5 | exports.needs = nest({ |
6 | 'sbot.pull.stream': 'first' |
7 | }) |
8 | |
9 | exports.gives = nest('tags.obs.all', true) |
10 | |
11 | exports.create = function (api) { |
12 | var cached = null |
13 | return nest('tags.obs.all', () => { |
14 | if (!cached) { |
15 | var stream = api.sbot.pull.stream(s => s.tags.stream({live: true})) |
16 | cached = MutantPullReduce(stream, (result, data) => { |
17 | if (data.tags) { |
18 | // handle initial state |
19 | for (var tag in data.tags) { |
20 | var messages = new Dict() |
21 | for (var message in data.tags[tag]) { |
22 | if (data.tags[tag][message].tagged) { |
23 | messages.put(message, data.tags[tag][message]) |
24 | } |
25 | } |
26 | result.put(tag, messages) |
27 | } |
28 | } else if (data.tag) { |
29 | // handle realtime changes |
30 | var { author, tag, message, tagged, timestamp } = data |
31 | if (!result.get(tag)) result.put(tag, new Dict()) |
32 | if (tagged && (!result.get(tag).get(message) || timestamp > result.get(tag).get(message).timestamp)) { |
33 | result.get(tag).put(message, { timestamp, tagged }) |
34 | } else if (!tagged && result.get(tag).get(message)) { |
35 | result.get(tag).delete(message) |
36 | } |
37 | } |
38 | return result |
39 | }, { |
40 | startValue: new Dict() |
41 | }) |
42 | } |
43 | return cached |
44 | }) |
45 | } |
46 |
Built with git-ssb-web