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