-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathv04.js
71 lines (60 loc) · 1.82 KB
/
v04.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const Connection = require('ssb-client')
const pull = require('pull-stream')
pull.paraMap = require('pull-paramap')
const html = require('yo-yo')
const daysPosts = require('./source/days-posts')
const getName = require('./async/get-name')
const App = html`
<div style="margin: 2rem;">Loading...</div>
`
document.body.appendChild(App)
Connection((err, server) => {
if (err) throw err
const today = new Date(2018, 9, 17)
pull(
daysPosts(server)(today),
pull.paraMap(addName, 50), // run up to 50 asyncrhonous maps in parallel
pull.collect(onDone)
)
function addName (data, cb) {
// getName is a much less opinionated method which just takes a feedId and asynchronously calls back with a name
// addName is then a function which knows about the shape of the data coming through the stream and how to handle the results
getName(server)(data.author, (err, name) => {
if (err) cb(err)
else {
data.authorName = name
cb(null, data)
}
})
}
function onDone (err, data) {
if (err) {
console.error('oh noes', err)
server.close()
return
}
const newView = Messages(data)
html.update(App, newView)
console.log(`${data.length} messages`)
console.timeEnd('get posts')
server.close()
}
})
function Messages (data) {
return html`
<div style="font-family: arial;">
${data.map(Message)}
</div>
`
}
function Message (msgData) {
const { authorName, timestamp, text, root } = msgData
// this is called 'destructuring' and is equivalent to `const authorName = msgData.authorName` etc
return html`
<div style="margin: 2rem;">
<strong>${authorName}</strong> - ${new Date(timestamp).toLocaleString()}
<p style="font-size: .8rem; margin: 0"> ${root ? 'thread:' : ''} ${root}</p>
<p>${text}</p>
</div>
`
}