Files: b8dcb5e1ae317132d4e8498b59e5279fcf92490f / modules / profile / obs / recently-updated.js
1450 bytesRaw
1 | var pull = require('pull-stream') |
2 | var Value = require('mutant/value') |
3 | var computed = require('mutant/computed') |
4 | var throttle = require('mutant/throttle') |
5 | var nest = require('depnest') |
6 | var hr = 60 * 60 * 1000 |
7 | |
8 | exports.needs = nest({ |
9 | 'sbot.pull.stream': 'first' |
10 | }) |
11 | |
12 | exports.gives = nest('profile.obs.recentlyUpdated') |
13 | |
14 | exports.create = function (api) { |
15 | var instance = null |
16 | |
17 | return nest('profile.obs.recentlyUpdated', function () { |
18 | load() |
19 | return instance |
20 | }) |
21 | |
22 | function load () { |
23 | if (instance) return |
24 | |
25 | var sync = Value(false) |
26 | var result = Value([]) |
27 | var max = 1000 |
28 | |
29 | pull( |
30 | api.sbot.pull.stream(sbot => sbot.patchwork.recentFeeds({ |
31 | since: Date.now() - (7 * 24 * hr), |
32 | live: true |
33 | })), |
34 | pull.drain((id) => { |
35 | if (id.sync) { |
36 | result.set(result()) |
37 | sync.set(true) |
38 | } else { |
39 | var values = result() |
40 | var index = values.indexOf(id) |
41 | if (sync()) { |
42 | values.length = Math.max(values.length, max) |
43 | if (~index) values.splice(index, 1) |
44 | values.unshift(id) |
45 | result.set(values) |
46 | } else if (values.length < max) { |
47 | values.push(id) |
48 | // don't broadcast until sync |
49 | } |
50 | } |
51 | }) |
52 | ) |
53 | |
54 | instance = throttle(result, 2000) |
55 | instance.sync = sync |
56 | |
57 | instance.has = function (value) { |
58 | return computed(instance, x => x.includes(value)) |
59 | } |
60 | } |
61 | } |
62 |
Built with git-ssb-web