git ssb

10+

Matt McKegg / patchwork



Tree: ed68746e557dd7a29e7bf0327cdef4794c412633

Files: ed68746e557dd7a29e7bf0327cdef4794c412633 / modules / profile / obs / recently-updated.js

1450 bytesRaw
1var pull = require('pull-stream')
2var Value = require('mutant/value')
3var computed = require('mutant/computed')
4var throttle = require('mutant/throttle')
5var nest = require('depnest')
6var hr = 60 * 60 * 1000
7
8exports.needs = nest({
9 'sbot.pull.stream': 'first'
10})
11
12exports.gives = nest('profile.obs.recentlyUpdated')
13
14exports.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